maheswar01.c (4171B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <ctype.h> 4 #include <string.h> 5 6 #define MAX 1000 7 8 /* ================= STACK FOR OPERATORS ================= */ 9 10 typedef struct { 11 char data[MAX]; 12 int top; 13 } CharStack; 14 15 void initCharStack(CharStack *s) { 16 s->top = -1; 17 } 18 19 void pushChar(CharStack *s, char c) { 20 s->data[++s->top] = c; 21 } 22 23 char popChar(CharStack *s) { 24 return s->data[s->top--]; 25 } 26 27 char peekChar(CharStack *s) { 28 return s->data[s->top]; 29 } 30 31 int isEmptyChar(CharStack *s) { 32 return s->top == -1; 33 } 34 35 /* ================= STACK FOR NUMBERS ================= */ 36 37 typedef struct { 38 double data[MAX]; 39 int top; 40 } DoubleStack; 41 42 void initDoubleStack(DoubleStack *s) { 43 s->top = -1; 44 } 45 46 void pushDouble(DoubleStack *s, double x) { 47 s->data[++s->top] = x; 48 } 49 50 double popDouble(DoubleStack *s) { 51 return s->data[s->top--]; 52 } 53 54 /* ================= HELPER FUNCTIONS ================= */ 55 56 int precedence(char op) { 57 if (op == '+' || op == '-') return 1; 58 if (op == '*' || op == '/') return 2; 59 return 0; 60 } 61 62 int isOperator(char c) { 63 return c == '+' || c == '-' || c == '*' || c == '/'; 64 } 65 66 /* ================= INFIX TO POSTFIX ================= */ 67 68 void infixToPostfix(const char *infix, char *postfix) { 69 CharStack stack; 70 initCharStack(&stack); 71 72 int i = 0, k = 0; 73 74 while (infix[i]) { 75 if (isspace(infix[i])) { 76 i++; 77 continue; 78 } 79 80 if (isdigit(infix[i]) || infix[i] == '.') { 81 while (isdigit(infix[i]) || infix[i] == '.') { 82 postfix[k++] = infix[i++]; 83 } 84 postfix[k++] = ' '; 85 } 86 else if (infix[i] == '(') { 87 pushChar(&stack, infix[i]); 88 i++; 89 } 90 else if (infix[i] == ')') { 91 while (!isEmptyChar(&stack) && peekChar(&stack) != '(') { 92 postfix[k++] = popChar(&stack); 93 postfix[k++] = ' '; 94 } 95 popChar(&stack); // remove '(' 96 i++; 97 } 98 else if (isOperator(infix[i])) { 99 while (!isEmptyChar(&stack) && 100 precedence(peekChar(&stack)) >= precedence(infix[i])) { 101 postfix[k++] = popChar(&stack); 102 postfix[k++] = ' '; 103 } 104 pushChar(&stack, infix[i]); 105 i++; 106 } 107 else { 108 printf("Invalid character: %c\n", infix[i]); 109 exit(1); 110 } 111 } 112 113 while (!isEmptyChar(&stack)) { 114 postfix[k++] = popChar(&stack); 115 postfix[k++] = ' '; 116 } 117 118 postfix[k] = '\0'; 119 } 120 121 /* ================= POSTFIX EVALUATION ================= */ 122 123 double evaluatePostfix(const char *postfix) { 124 DoubleStack stack; 125 initDoubleStack(&stack); 126 127 int i = 0; 128 129 while (postfix[i]) { 130 if (isspace(postfix[i])) { 131 i++; 132 continue; 133 } 134 135 if (isdigit(postfix[i]) || postfix[i] == '.') { 136 char number[50]; 137 int k = 0; 138 139 while (isdigit(postfix[i]) || postfix[i] == '.') { 140 number[k++] = postfix[i++]; 141 } 142 number[k] = '\0'; 143 144 pushDouble(&stack, atof(number)); 145 } 146 else if (isOperator(postfix[i])) { 147 double b = popDouble(&stack); 148 double a = popDouble(&stack); 149 150 switch (postfix[i]) { 151 case '+': pushDouble(&stack, a + b); break; 152 case '-': pushDouble(&stack, a - b); break; 153 case '*': pushDouble(&stack, a * b); break; 154 case '/': 155 if (b == 0) { 156 printf("Error: Division by zero\n"); 157 exit(1); 158 } 159 pushDouble(&stack, a / b); 160 break; 161 } 162 i++; 163 } 164 } 165 166 return popDouble(&stack); 167 } 168 169 /* ================= MAIN ================= */ 170 171 int main() { 172 char infix[MAX]; 173 char postfix[MAX]; 174 175 printf("Enter expression: "); 176 fgets(infix, MAX, stdin); 177 178 infixToPostfix(infix, postfix); 179 180 double result = evaluatePostfix(postfix); 181 182 printf("Result = %.10g\n", result); 183 184 return 0; 185 }