0% found this document useful (0 votes)
21 views

Saran Dsa Ass3

Hgfd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Saran Dsa Ass3

Hgfd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

DSA LAB EXERCISE-3

Name: Saran P
Reg No: 23BAI0022
QUESTION-1A

CODE:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function to return precedence of operators


int get_precedence(char op) {
if (op == '^') return 3;
else if (op == '/' || op == '*') return 2;
else if (op == '+' || op == '-') return 1;
else return -1;
}

// Function to return associativity of operators


char get_associativity(char op) {
if (op == '^') return 'R'; // Right associative
return 'L'; // Default to left associative
}

// The main function to convert infix expression to postfix expression


void infix_to_postfix(const char infix_expr[]) {
char postfix_expr[100];
int postfix_index = 0;
int expr_length = strlen(infix_expr);
char operator_stack[100];
int stack_index = -1;

for (int i = 0; i < expr_length; i++) {


char current_char = infix_expr[i];

// If the scanned character is an operand, add it to the output string


if ((current_char >= 'a' && current_char <= 'z') ||
(current_char >= 'A' && current_char <= 'Z') ||
(current_char >= '0' && current_char <= '9')) {
postfix_expr[postfix_index++] = current_char;
}
// If the scanned character is '(', push it to the stack
else if (current_char == '(') {
operator_stack[++stack_index] = current_char;
}
// If the scanned character is ')', pop and add to the output string from the stack until '(' is encountered
else if (current_char == ')') {
while (stack_index >= 0 && operator_stack[stack_index] != '(') {
postfix_expr[postfix_index++] = operator_stack[stack_index--];
}
stack_index--; // Pop '('
}
// If an operator is scanned
else {
while (stack_index >= 0 &&
(get_precedence(current_char) < get_precedence(operator_stack[stack_index]) ||
(get_precedence(current_char) == get_precedence(operator_stack[stack_index]) &&
get_associativity(current_char) == 'L'))) {
postfix_expr[postfix_index++] = operator_stack[stack_index--];
}
operator_stack[++stack_index] = current_char;
}
}

// Pop all the remaining elements from the stack


while (stack_index >= 0) {
postfix_expr[postfix_index++] = operator_stack[stack_index--];
}
postfix_expr[postfix_index] = '\0'; // Null-terminate the postfix expression
printf("Postfix Expression: %s\n", postfix_expr);
}

// Driver code
int main() {
char infix_expression[100];
printf("Enter infix expression: ");
if (fgets(infix_expression, sizeof(infix_expression), stdin) != NULL) {
// Remove trailing newline character if present
size_t length = strlen(infix_expression);
if (length > 0 && infix_expression[length - 1] == '\n') {
infix_expression[length - 1] = '\0';
}

infix_to_postfix(infix_expression);
} else {
printf("Error reading input\n");
}
return 0;
}
OUTPUT:-

QUESTION-1B
CODE:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 100

int stack_top = -1;


char operator_stack[MAX_SIZE];

// Checking if the stack is full


int is_stack_full() {
return stack_top == MAX_SIZE - 1;
}

// Checking if the stack is empty


int is_stack_empty() {
return stack_top == -1;
}

// Push an item onto the stack


void push_to_stack(char item) {
if (is_stack_full()) return;
stack_top++;
operator_stack[stack_top] = item;
}

// Pop an item from the stack


char pop_from_stack() {
if (is_stack_empty()) return -1; // Return -1 if the stack is empty
return operator_stack[stack_top--];
}

// Peek the top item of the stack without removing it


char peek_stack() {
if (is_stack_empty()) return -1; // Return -1 if the stack is empty
return operator_stack[stack_top];
}

// Check if the given character is an operand


int is_operand(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}

// Function to compare operator precedence


int get_precedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return -1;
}
}

// Convert infix expression to postfix expression


void infix_to_postfix(char *expression) {
int i, j;
for (i = 0, j = -1; expression[i]; ++i) {
if (is_operand(expression[i])) {
expression[++j] = expression[i];
} else if (expression[i] == '(') {
push_to_stack(expression[i]);
} else if (expression[i] == ')') {
while (!is_stack_empty() && peek_stack() != '(') {
expression[++j] = pop_from_stack();
}
if (!is_stack_empty() && peek_stack() != '(') {
return; // Invalid expression
} else {
pop_from_stack(); // Remove '('
}
} else { // If an operator is scanned
while (!is_stack_empty() && get_precedence(expression[i]) <=
get_precedence(peek_stack())) {
expression[++j] = pop_from_stack();
}
push_to_stack(expression[i]);
}
}
// Pop all remaining elements from the stack
while (!is_stack_empty()) {
expression[++j] = pop_from_stack();
}
expression[++j] = '\0'; // Null-terminate the postfix expression
}

// Reverse a string
void reverse_string(char *str) {
int length = strlen(str);
char reversed[length + 1];
int j = length;
reversed[j--] = '\0';
for (int i = 0; i < length; i++) {
reversed[j--] = str[i];
}
strcpy(str, reversed);
}

// Swap brackets in an expression


void swap_brackets(char *expr) {
for (int i = 0; expr[i] != '\0'; i++) {
if (expr[i] == '(') {
expr[i] = ')';
} else if (expr[i] == ')') {
expr[i] = '(';
}
}
}

// Convert infix expression to prefix expression


void infix_to_prefix(char *exp) {
int length = strlen(exp);
// Reverse the infix expression
reverse_string(exp);
// Swap brackets
swap_brackets(exp);
// Convert to postfix
infix_to_postfix(exp);
// Reverse the postfix expression to get the prefix
reverse_string(exp);
}

int main() {
char infix_expression[MAX_SIZE];
printf("Enter infix expression: ");
if (fgets(infix_expression, sizeof(infix_expression), stdin) != NULL) {
// Remove trailing newline character if present
size_t len = strlen(infix_expression);
if (len > 0 && infix_expression[len - 1] == '\n') {
infix_expression[len - 1] = '\0';
}

infix_to_prefix(infix_expression);
printf("The prefix expression is: %s\n", infix_expression);
} else {
printf("Error reading input\n");
}
return 0;
}

OUTPUT:-
QUESTION-2

CODE:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 100

// Stack implementation
int stack[MAX_SIZE];
int top = -1;

void push(int item) {


if (top >= MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
top++;
stack[top] = item;
}
int pop() {
if (top < 0) {
printf("Stack Underflow\n");
return -1;
}
int item = stack[top];
top--;
return item;
}

int is_operator(char symbol) {


return (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/');
}

int evaluate(char* expression) {


int i = 0;
char symbol;
int operand1, operand2, result;

while ((symbol = expression[i]) != '\0') {


if (symbol >= '0' && symbol <= '9') {
int num = symbol - '0';
push(num);
} else if (is_operator(symbol)) {
operand2 = pop();
operand1 = pop();

// Check for division by zero


if (symbol == '/' && operand2 == 0) {
printf("Error: Division by zero\n");
return -1;
}

switch(symbol) {
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/': result = operand1 / operand2; break;
default:
printf("Error: Unknown operator %c\n", symbol);
return -1;
}
push(result);
}
i++;
}

// There should be exactly one item in the stack if the expression is valid
if (top != 0) {
printf("Error: Invalid postfix expression\n");
return -1;
}

result = pop();
return result;
}

int main() {
char expression[MAX_SIZE];
printf("Enter postfix expression: ");
if (fgets(expression, MAX_SIZE, stdin) != NULL) {
// Remove trailing newline character if present
size_t len = strlen(expression);
if (len > 0 && expression[len - 1] == '\n') {
expression[len - 1] = '\0';
}

int result = evaluate(expression);


if (result != -1) {
printf("Result = %d\n", result);
}
} else {
printf("Error reading input\n");
}
return 0;
}

OUTPUT:-

You might also like