MATH XPRSN EVALUATOR 1.0
(C program)

This program evaluates mathematical expression which may contain addition, subtraction, multiplication, division, power or root. Interactive inputting with syntax checking facility is available.
/* ##################################### # MATH XPRSN EVALUATOR 1.0 # # tested in Turbo C++,version 3.0 # ##############18/2/2008############## */ #include<math.h> #include<conio.h> #include<ctype.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #define FALSE 0 #define TRUE 1 #define COL_LEN 80 #define XPRSN_LINE_LEN 2 #define MSG_START_LINE 2 #define XPRSN_START_LINE (MSG_START_LINE+2) #define XPRSN_LEN (XPRSN_LINE_LEN*COL_LEN-1) int curXY,j; long double operands[26]; char xprsn[XPRSN_LEN+1]; char *msg[]={ "_enter a math expression_ ", "_operator not found_ ", "_operand not found_ ", "_extra closing bracket_ ", "_extra openning bracket_ ", "_more than 25 operators found_", }; void PutXprsn() { int i=0; gotoxy(1,XPRSN_START_LINE); do { switch(xprsn[i]) { case '+': case '-': { if(i==0|| !isdigit(xprsn[i-1])&&xprsn[i-1]!='.'&&xprsn[i-1]!=')') { textcolor(WHITE); break; } } case '/': case '*': case '^': case'\\': textcolor(BROWN); break; case '(': case ')': textcolor(RED); break; default : textcolor(WHITE); break; } cprintf("%c",xprsn[i]); }while(xprsn[i++]!=NULL); } void SetCurser(int NoOfCell) { curXY+=NoOfCell; if(curXY>strlen(xprsn)||curXY<0) curXY-=NoOfCell; } void AddDel() { int i; if(curXY==strlen(xprsn)) return; for(i=curXY; i<strlen(xprsn); i++) xprsn[i]=xprsn[i+1]; PutXprsn(); } void AddBksp() { if(curXY==0) return; curXY--; AddDel(); } void AddChar(char ch) { int i; if(strlen(xprsn)==XPRSN_LEN) return; for(i=strlen(xprsn); i>=curXY; i--) xprsn[i+1]=xprsn[i]; xprsn[curXY]=ch; curXY++; PutXprsn(); } void GetXprsn() { char ch; PutXprsn(); while(1) { gotoxy(curXY%(COL_LEN)+1,XPRSN_START_LINE+curXY/COL_LEN); ch=getch(); switch(ch) { case 27: exit(0); /* Escape */ case '=': case 13: return; /* Enter */ case 8: AddBksp(); continue; /* Backspace */ case 71: SetCurser(-curXY); continue; /* Home */ case 72: SetCurser(-COL_LEN); continue; /* up */ case 75: SetCurser(-1); continue; /* left */ case 77: SetCurser(1); continue; /* right */ case 79: SetCurser(strlen(xprsn)-curXY); continue; /* End */ case 80: SetCurser(COL_LEN); continue; /* down */ case 83: AddDel(); continue; /* Delete */ case '+': case '-': case '*': case '/': case '^': case'\\': case '(': case ')': case '.': AddChar(ch); continue; default : if(isdigit(ch)) AddChar(ch); } } } int SyntexError() { int totalOperators=0, operandNotFound; int openningBracket=0, closingBracket=0; curXY=0; while(1) { while(xprsn[curXY]=='(') openningBracket++, curXY++; if(xprsn[curXY]=='-' || xprsn[curXY]=='+') curXY++; operandNotFound=TRUE; while(isdigit(xprsn[curXY])) operandNotFound=FALSE, curXY++; if(xprsn[curXY]=='.') curXY++; while(isdigit(xprsn[curXY])) operandNotFound=FALSE, curXY++; if(operandNotFound) return 2; while(xprsn[curXY]==')') { if(++closingBracket>openningBracket) return 3; else curXY++; } switch(xprsn[curXY]) { case'\0': if(openningBracket>closingBracket) return 4; else return 0; case '+': case '-': case '*': case '/': case '^': case'\\': { if(++totalOperators>25) return 5; curXY++; continue; } default : return 1; } } } void StoreOperands() { int k,i, index=0; char OS[XPRSN_LEN], *whiteElephant; curXY=i=0; while(1) { while(xprsn[curXY]=='(') xprsn[i++]=xprsn[curXY++]; for(k=0; isdigit(xprsn[curXY])|| xprsn[curXY]=='.' || (xprsn[curXY]=='-'||xprsn[curXY]=='+')&&k==0; k++) OS[k]=xprsn[curXY++]; OS[k]=NULL; operands[index]=_strtold(OS,&whiteElephant); xprsn[i++]='A'+index++; while(xprsn[curXY]==')') xprsn[i++]=xprsn[curXY++]; if((xprsn[i++]=xprsn[curXY++])==NULL) return; } } void postfix(); int HighPriority(char char1, char char2, char char3) { if(char2=='(') { curXY+=2; postfix(); return HighPriority(char1,'?',xprsn[curXY]); } if(char2!='?') curXY+=2, xprsn[j++]=char2; if(char3==NULL || char3==')' || char1=='^' || char1=='\\') return TRUE; if(char3=='^' || char3=='\\') return FALSE; if(char1=='*' || char1=='/') return TRUE; if(char3=='*' || char3=='/') return FALSE; return TRUE; } void postfix() { int index=0; char operators[26]; while(1) { switch(xprsn[curXY]) { case '(': curXY++; postfix(); continue; case ')': curXY++; return; case'\0': xprsn[j]=NULL; return; default : { if(isupper(xprsn[curXY])) xprsn[j++]=xprsn[curXY++]; else { operators[index]=xprsn[curXY]; if(HighPriority(xprsn[curXY],xprsn[curXY+1],xprsn[curXY+2])) do xprsn[j++]=operators[index--]; while(index>=0&&HighPriority(operators[index],'?',xprsn[curXY])); index++; } } } } } /******************** operations ********************/ long double Add(long double leftOperand, long double rightOperand) { return (leftOperand+rightOperand); } long double Sub(long double leftOperand, long double rightOperand) { return (leftOperand-rightOperand); } long double Mul(long double leftOperand, long double rightOperand) { return (leftOperand*rightOperand); } long double Div(long double leftOperand, long double rightOperand) { return (leftOperand/rightOperand); } long double Pow(long double leftOperand , long double rightOperand) { return pow(leftOperand,rightOperand); } long double Root(long double leftOperand , long double rightOperand) { return pow(rightOperand,1/leftOperand); } void instructions() { clrscr(); textcolor(WHITE); cputs("__INSTRUCTIONS__\n\n\r"); cputs(" OPERATORS: + - * / ^ \\\n\r"); cputs(" * fifth power of seven is 7^5\n\r"); cputs(" * fifth root of 32 is 5\\32\n\n\r"); cputs(" BRACKETS: We can use paranthesis.\n\n\r"); cputs(" ERROR: There is no error handling\n\n\r"); cputs(" KEYS: Arrow-keys, Home, End, Del, Bksp, Enter, '=', Esc\n\n\r"); cputs("...press any key..."); getch(); } long double evaluate() { switch(xprsn[--j]) { case '+': return Add (evaluate(),evaluate()); case '-': return Sub (evaluate(),evaluate()); case '*': return Mul (evaluate(),evaluate()); case '/': return Div (evaluate(),evaluate()); case'\\': return Root(evaluate(),evaluate()); case '^': return Pow (evaluate(),evaluate()); default : return operands[xprsn[j]-'A']; } } void main() { int msgNo=0; char xprsnCopy[XPRSN_LEN+1], ch; textmode(C80); instructions(); while(1) { clrscr(); curXY=0; edit: gotoxy(1,1), puts(msg[msgNo]); GetXprsn(); strcpy(xprsnCopy,xprsn); msgNo=SyntexError(); if(msgNo) goto edit; StoreOperands(); curXY=j=0, postfix(); gotoxy(1,XPRSN_START_LINE+XPRSN_LINE_LEN+1); j=strlen(xprsn); printf("= %Lg\n\n..Press any key..",evaluate()); ch=getch(); if(ch==27) exit(0); strcpy(xprsn,xprsnCopy); } } /******************************************************* * More programs: http://programs.on.googlepages.com * * Please report suggestions: programs.on@gmail.com * ********************************************372*lines**/