Upload files to "DS/C/Lab/Week5"

This commit is contained in:
Aadit Agrawal 2024-09-03 11:27:09 +05:30
parent 44cced8454
commit 5b8ed261ff
3 changed files with 96 additions and 56 deletions

View File

@ -1,70 +1,91 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#define MAX_SIZE 100 #define MAX_SIZE 100
char stack[MAX_SIZE]; char stack[MAX_SIZE];
int top = -1; int top = -1;
void push(char item) { void push(char item)
if (top >= MAX_SIZE - 1) { {
if (top >= MAX_SIZE - 1)
{
printf("Stack Overflow\n"); printf("Stack Overflow\n");
return; return;
} }
stack[++top] = item; stack[++top] = item;
} }
char pop() { char pop()
if (top < 0) { {
if (top < 0)
{
printf("Stack Underflow\n"); printf("Stack Underflow\n");
return -1; return -1;
} }
return stack[top--]; return stack[top--];
} }
int precedence(char symbol) { int precedence(char symbol)
switch (symbol) { {
case '^': return 3; switch (symbol)
case '*': {
case '/': return 2; case '^':
case '+': return 3;
case '-': return 1; case '*':
default: return 0; case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
} }
} }
void infixToPostfix(char infix[], char postfix[]) { void infixToPostfix(char infix[], char postfix[])
{
int i, j = 0; int i, j = 0;
char symbol, next; char symbol, next;
for (i = 0; i < strlen(infix); i++) { for (i = 0; i < strlen(infix); i++)
{
symbol = infix[i]; symbol = infix[i];
if (isalnum(symbol)) { if ((symbol >= 'a' && symbol <= 'z') || (symbol >= 'A' && symbol <= 'Z') || (symbol >= '0' && symbol <= '9'))
{
postfix[j++] = symbol; postfix[j++] = symbol;
} else if (symbol == '(') { }
else if (symbol == '(')
{
push(symbol); push(symbol);
} else if (symbol == ')') { }
while ((next = pop()) != '(') { else if (symbol == ')')
{
while ((next = pop()) != '(')
{
postfix[j++] = next; postfix[j++] = next;
} }
} else { }
while (top > -1 && precedence(stack[top]) >= precedence(symbol)) { else
{
while (top > -1 && precedence(stack[top]) >= precedence(symbol))
{
postfix[j++] = pop(); postfix[j++] = pop();
} }
push(symbol); push(symbol);
} }
} }
while (top > -1) { while (top > -1)
{
postfix[j++] = pop(); postfix[j++] = pop();
} }
postfix[j] = '\0'; postfix[j] = '\0';
} }
int main() { int main()
{
char infix[MAX_SIZE], postfix[MAX_SIZE]; char infix[MAX_SIZE], postfix[MAX_SIZE];
printf("Enter infix expression: "); printf("Enter infix expression: ");
fgets(infix, MAX_SIZE, stdin); gets(infix);
infix[strcspn(infix, "\n")] = 0; // Remove newline if present
infixToPostfix(infix, postfix); infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix); printf("Postfix expression: %s\n", postfix);
return 0; return 0;

View File

@ -6,61 +6,80 @@
int stack[MAX_SIZE]; int stack[MAX_SIZE];
int top = -1; int top = -1;
void push(int item) { void push(int item)
if (top >= MAX_SIZE - 1) { {
if (top >= MAX_SIZE - 1)
{
printf("Stack Overflow\n"); printf("Stack Overflow\n");
return; return;
} }
stack[++top] = item; stack[++top] = item;
} }
int pop() { int pop()
if (top < 0) { {
if (top < 0)
{
printf("Stack Underflow\n"); printf("Stack Underflow\n");
return -1; // Return a sentinel value instead of exiting return -1;
} }
return stack[top--]; return stack[top--];
} }
int isDigit(char c) { int isDigit(char c)
{
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
int evaluatePostfix(char* exp) { int evaluatePostfix(char *exp)
{
int i, operand1, operand2, result; int i, operand1, operand2, result;
for (i = 0; exp[i] != '\0'; i++) { for (i = 0; exp[i] != '\0'; i++)
if (isDigit(exp[i])) { {
if (isDigit(exp[i]))
{
push(exp[i] - '0'); push(exp[i] - '0');
} else { }
else
{
operand2 = pop(); operand2 = pop();
operand1 = pop(); operand1 = pop();
switch (exp[i]) { switch (exp[i])
case '+': push(operand1 + operand2); break; {
case '-': push(operand1 - operand2); break; case '+':
case '*': push(operand1 * operand2); break; push(operand1 + operand2);
case '/': break;
if (operand2 == 0) { case '-':
printf("Error: Division by zero\n"); push(operand1 - operand2);
return -1; // Return a sentinel value instead of exiting break;
} case '*':
push(operand1 / operand2); push(operand1 * operand2);
break; break;
case '/':
if (operand2 == 0)
{
printf("Error: Division by zero\n");
return -1;
}
push(operand1 / operand2);
break;
} }
} }
} }
return pop(); return pop();
} }
int main() { int main()
{
char exp[MAX_SIZE]; char exp[MAX_SIZE];
printf("Enter a postfix expression: "); printf("Enter a postfix expression: ");
fgets(exp, MAX_SIZE, stdin); gets(exp);
exp[strcspn(exp, "\n")] = 0; // Remove newline if present
int result = evaluatePostfix(exp); int result = evaluatePostfix(exp);
if (result != -1) { if (result != -1)
{
printf("Result: %d\n", result); printf("Result: %d\n", result);
} }
return 0; return 0;
} }

View File

@ -30,7 +30,7 @@ int isOperator(char c) {
void postfixToInfix(const char* postfix) { void postfixToInfix(const char* postfix) {
char op1[MAX_EXPR_LEN], op2[MAX_EXPR_LEN], temp[MAX_EXPR_LEN]; char op1[MAX_EXPR_LEN], op2[MAX_EXPR_LEN], temp[MAX_EXPR_LEN];
for (int i = 0; postfix[i] != '\0'; i++) { for (int i = 0; postfix[i] != '\0'; i++) {
if (!isOperator(postfix[i])) { if (!isOperator(postfix[i])) {
temp[0] = postfix[i]; temp[0] = postfix[i];
@ -43,7 +43,7 @@ void postfixToInfix(const char* postfix) {
push(temp); push(temp);
} }
} }
printf("Infix expression: %s\n", stack[top]); printf("Infix expression: %s\n", stack[top]);
} }
@ -53,4 +53,4 @@ int main() {
gets(postfix); gets(postfix);
postfixToInfix(postfix); postfixToInfix(postfix);
return 0; return 0;
} }