// Majdal AL Hindi //1071842 //project #2 // this project read equations from file and check if there are //errors in the equations,convert the equation from infix to postfix expression //and evaluate the value , finally its save the equation and // the conversion form and the value in output file #include #include #include #define MAX 20 // strucuture to bulid the stack typedef struct { int a[100]; int top; }STACK; // push function to put the value in the stack void push(STACK *s,int x) { if(s->top==99) printf("STACK OVERFLOW\n"); else s->a[++s->top]=x; } // pop function to remove value from the stack int pop(STACK *s) { int x; if(s->top<0) printf("STACK UNDERFLOW\n"); else { x=s->a[s->top--]; return x; } } // operation function to evaluate the last two value in the stack int operation(int operand1,int operand2,char operatr) { switch(operatr) { case '+':return operand1+operand2; case '*':return operand1*operand2; case '-':return operand1-operand2; case '/':return operand1/operand2; } } // function to calculate the value of the postfix expression int evaluate(char postfix[]) { STACK s1; int p1,p2,result,i; s1.top=-1; for(i=0;postfix[i]!='\0';i++) if(isdigit(postfix[i])) push(&s1,postfix[i]-'0'); else { p2=pop(&s1); p1=pop(&s1); result=operation(p1,p2,postfix[i]); push(&s1,result); } return pop(&s1); } char stack[MAX]; int top1=-1; char pop1(); void push1(char item); // function to check which operator has a high precedence to evaluate int precdence (char symbol) { switch(symbol) { case '+': case '-':return 2; break; case '*': case '/':return 4; break; case '^':return 6; break; case '(': case ')': case '#':return 1; break; } } // function to check if the current operator is one of these operators int isoperator(char symbol) { switch(symbol) { case '+': case '-': case '*': case '/': case '^': case '(': case ')':return 1; break; default:return 0; } } // function to convert from infix to postfix expression void convert(char infix[],char postfix[]) { int i,symbol,j=0; stack[++top1]='#'; for(i=0;i precdence (stack[top1])) push1(symbol); else { while( precdence (symbol)<= precdence (stack[top1])) { postfix[j]=pop1(); j++; } push1(symbol); } } } } while(stack[top1]!='#') { postfix[j]=pop1(); j++; } postfix[j]='\0'; printf("%s\n",postfix); } void push1(char item) { top1++; stack[top1]=item; } char pop1() { char a; a=stack[top1]; top1--; return a; } int menu() { int i ; do{ printf("\t\t\t\t *********"); printf("\n\t\t\t **************************"); printf("\n\t*************************** MENU ***************************\n\n\n"); printf("\t\t\t 1 - Read Data from file\n \n"); printf("\t\t\t 2- Compile\n\n"); printf("\t\t\t 3 - Exit\n"); printf("\nSelect number :"); scanf("%d",&i); }while((i != 1)&&(i != 2)&&(i != 3)); return (i); } // function to read data from the file void read_data(FILE *in,char infix[][80]) { int i = 0; printf(" \n\n\tThe data in the file: \n\n"); char temp[200]; while(fgets(temp,200,in)!= NULL) { strcpy(infix[i],temp); printf("%s\n",infix[i]); i++; } } //function to check if there is an error in the equations int compile(char infix[]) { int i ; for(i=0;ij)) flag = 0; } if(flag){ printf("open parntheses\n"); return 1 ; } i = 0 ; for(i=0;i