#include #include #include #include #include #ifndef __WINAPI_CONSOLE_WRAPPER_H #define __WINAPI_CONSOLE_WRAPPER_H //#include #include int menue(); void gotoxy (int x, int y); void clrscr (void); #endif // struct used to convert from infix to postfix typedef struct stck* ptr1; struct stck { char element; ptr1 next; }; typedef ptr1 stack; typedef ptr1 pstion; void push(stack s,char x); pstion pop(stack s); int isEmptyS(stack s); char top(stack s); int isoperator(char symbol); double converttodouble(char a[]); void convert(char infix[],char postfix[]); double operation(double operand1,double operand2,char operatr); void readfromfile(); int findError(char infix[] ); typedef struct stck2* ptr2; // struct used to find the value of postfix expression struct stck2 { double element; ptr2 next; }; typedef ptr2 stack2; typedef ptr2 pstion2; void push2(stack2 s,double x); pstion2 pop2(stack2 s); int isEmptyS2(stack2 s); double top2(stack2 s); int precdence (char symbol); int isoperation(char c); int findError(char Eq[] ); double evaluate(char postfix[]); void printonfile(); // arrays used to save equations char postfix[100][35]; char equations[100][17]; double value[100]; int j=0; int main() { int select; char ch; for(;;) {clrscr(); char name[17]; select=menue(); if(select==2){ // read words from keyboard printf("\n enter your equation and enter number 1 when finished \n "); scanf("%s",name); // this is a condition to stop reading from screen while(strcmp(name,"1")!=0){ strcpy(equations[j],name); j++; // scanf("%s",name);}} else if(select==1){ // read from file readfromfile(); } else if(select==3){ // check equation int s,flag=0; for(s=0;selement=x; temp->next=s->next; s->next=temp; } } pstion pop(stack s) { pstion p; p=s->next; s->next=p->next; return p; } int isEmptyS(stack s) { return(s->next==NULL); } char top(stack s) { return s->next->element; } int isoperation(char c){ if(c=='+') return 1; if(c=='-') return 1; if(c=='*') return 1; if(c=='/') return 1; if(c=='^') return 1; return 0; } int isoperator(char symbol) { switch(symbol) { case '+': case '-': case '*': case '/': case '^':return 1; break; default:return 0; }} double operation(double operand1,double operand2,char operatr) { switch(operatr) { case '+':return operand1+operand2; case '*':return operand1*operand2; case '-':return operand1-operand2; case '/':return operand1/operand2; } return -1; } // function to calculate the value of the postfix expression double evaluate(char postfix[]) { stack2 s2; s2=(stack2)malloc(sizeof(struct stck2)); s2->next=NULL; double p1,p2,result; int i; char number[15]; int j=0; for(i=0;postfix[i]!='\0';i++){ if(isdigit(postfix[i])){ while(postfix[i]!=' '){ number[j]=postfix[i]; j++; i++; } number[j]='\0'; j=0; i--; p1=converttodouble(number); push2(s2,p1); } else if(isoperation(postfix[i])) { p2=top2(s2); pop2(s2); p1=top2(s2); pop2(s2); result=operation(p1,p2,postfix[i]); push2(s2,result); } } return result;} void push2(stack2 s,double x) { pstion2 temp; temp=(pstion2)malloc(sizeof(struct stck2)); if(temp!=NULL) { temp->element=x; temp->next=s->next; s->next=temp; } } double top2(stack2 s) { return s->next->element; } pstion2 pop2(stack2 s) { pstion2 p; p=s->next; s->next=p->next; return p; } // return a double value to an of string double converttodouble(char a[]){ int i; double result=0; for(i=strlen(a)-1;i>=0;i--) result=result + (double)((a[i]-'0')*pow(10,strlen(a)-1-i)); return result; } void convert(char infix[],char postfix[]){ stack s; s=(pstion)malloc(sizeof(struct stck)); s->next=NULL; int i,j=0; push(s,'#'); for(i=0;inext->element!='(') { postfix[j]=top(s); j++; postfix[j]=' '; pop(s);j++; } pop(s); } else { if( precdence (postfix[i])> precdence (top(s))){ push(s,infix[i]); } else { while( precdence (infix[i])<= precdence (top(s))) { postfix[j]=top(s); pop(s); j++; postfix[j]=' '; j++; } push(s,infix[i]); } } }} while(s->next->element!='#'){ postfix[j]=top(s); j++; postfix[j]=' '; j++; pop(s); } postfix[j]='\0'; } //check weather an equation has a priority more than the other 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; } } int findError(char infix[] ){ int i ; for(i=0;ij)) flag = 0; } if(flag){ return 1 ; } if(isoperator(infix[0])) return 1; if(infix[0]==')') return 1; if(infix[strlen(infix)-1]=='(') return 1; if(isoperator(infix[strlen(infix)-1])) return 1; for(i=0;i> \n "); printf(" 6-write to file \n "); printf(" 7-write on screen \n "); printf(" 8-Exit \n "); printf(" select a number "); gotoxy(60,20); ch=getch(); } while(!strchr("12345678",ch)); return (ch-48); } void readfromfile(){ int MAX_LEN=200; FILE*fp ; char line[MAX_LEN], *result,*myword; char filename[15]; printf("\n please enter your file name e.g (me.txt) "); scanf("%s",filename); fp = fopen( filename ,"r" ) ; if( fp == NULL ) { // this when the file isn't exist printf("cannot open file" ) ; exit(0) ; } else{ // read a full line result = fgets(line,MAX_LEN,fp); while(result!=NULL){ // get the words word by word myword=strtok(result," "); while(myword!=NULL) { // this is done for the word at the end of the line if( myword[strlen(myword)-1]=='\n') myword[strlen(myword)-1]='\0'; { //insert the word to the array strcpy(equations[j],myword); j++; } myword=strtok(NULL," "); } result = fgets(line,MAX_LEN,fp); }} fclose(fp); } void printonfile(){ FILE *fp; char filename[15]; printf("\n please enter your file name to write in e.g (me.txt) "); scanf("%s",filename); fp=fopen(filename, "w"); fprintf(fp," equation postfix value "); int s=0; for(s=0;s