asim.syslock.org
Tue 05 Dec 2006.  
Asim’s blog   /   Syslock archive



Home : C/C++

Name : Expression Evaluator.
Description : This program converts an expression from infix to postfix and evaluates it.One of the smallest programs ever doing this.The file stck.c contains integer based stack routines while stckf.c contains floating point based stack routines .

Code :

#include <stdio.h>
#include "stck.c"
#include "stckf.c"
struct expr {
char val;
struct expr *nxt;
};
struct expr *infix;
struct expr *postfix;
void insertchar(char,struct expr **);
int oprt(char);
int prt (char);
float expeval();
int validpo();

main () {
char a;struct expr *temp,*prev;
int c,terminate;
printf ("Enter the expression : ");
while (a!= '\n') {
scanf ("%c",&a);
if (a!='\n') insertchar (a,&infix);
}
temp=infix;
while (temp!=NULL)
{

if (oprt(temp->val)==0)
{ terminate=0;
while ((temp!=NULL)&&(terminate!=1))
{ if (oprt(temp->val)==0) { insertchar (temp->val,&postfix);
temp=temp->nxt;}
else {terminate=1;
insertchar (' ',&postfix);}
}
}

if (temp!=NULL)
if (temp->val=='(')
{ push(temp->val);
temp=temp->nxt;
}
if (temp!=NULL)
if (temp->val==')')
{ a=pop ();
while (a!='(')
{ insertchar (a,&postfix);
a=pop();
}
temp=temp->nxt;
}
if (temp!=NULL) {
a=temp->val;
if (a=='+'||a=='*'||a=='/'||a=='-'||a=='~')
{ if (top==-1) push (temp->val);
else {
a=pop ();
while(prt(a)>=prt(temp->val))
{ insertchar(a,&postfix);
a=pop(); }
push(a);
push(temp->val); }
temp=temp->nxt; }
}
}
while (top!=-1) {
a=pop();
insertchar(a,&postfix); }

if (validpo()==1) {
printf ("\n The postfix expression is :- \n ");
temp=postfix;
while (temp!=NULL) {
printf ("%c",temp->val);
temp=temp->nxt;
}
printf ("\nThe value of the expression is %f \n ",expeval()); }
else printf (" \n Invalid expression \n");
}

void insertchar (char c,struct expr **p) {
struct expr *temp;
if (*p==NULL)
{ *p=(struct expr *) malloc (sizeof(struct expr ));
temp=*p;}
else
{ temp=*p;
while (temp->nxt!=NULL) temp=temp->nxt;
temp->nxt=(struct expr *) malloc (sizeof(struct expr ));
temp=temp->nxt; }
temp->val=c; }

int oprt (char c) {
int i=0;
if ((c=='*')||(c=='+')||(c=='-')||(c=='/')||(c=='(')||(c==')')||(c=='~')) i=1;
return i;
}
int prt (char a)
{ int pr;
if (a=='~') pr=3; else {
if (a=='*'||a=='/'||a=='%')
pr=2;
else
{if (a=='+'||a=='-') pr=1;
else
pr=0;
}}
return pr;
}

float expeval() {
struct expr*temp;int i,w,dotst;float no,op3,op1,op2;
temp=postfix;
while (temp!=NULL) {
if (oprt(temp->val)==0) {
no=temp->val-'0';
temp=temp->nxt;
dotst=0;
if (temp!=NULL) while ((temp->val!=' ')&&(oprt(temp->val)!=1)) { if (temp->val=='.') { dotst=1;
temp=temp->nxt;
}
if (dotst==1) { i=1;
while ((temp->val!=' ')&&(oprt(temp->val)!=1)) {
w=temp->val-'0';
no=no+w*ex(-i);
i=i+1;
temp=temp->nxt;
}
}
else { i=1;
while ((temp->val!=' ')&&(oprt(temp->val)!=1)&&(temp->val!='.')) {
w=temp->val-'0';
no=no*ex(i)+w;
temp=temp->nxt; }
}
}
pushf(no);
}

if (temp!=NULL)
if (oprt(temp->val)==1)
{
op1=popf();
op2=popf();
switch (temp->val)
{
case '~':op3=-1*op1;
pushf(op2);
break;
case '+':
op3=op2+op1;
break;
case '-':
op3=op2-op1;
break;
case '*':
op3=op2*op1;
break;
case '/':
op3=op2/op1;
break;
}
pushf(op3);

}
if (temp!=NULL) temp=temp->nxt;
}
return popf();
}

int validpo () {
struct expr *temp;
int rank=0,terminate;
char c;
temp=postfix;
while (temp!=NULL) {
terminate=0;
c=temp->val;
if ((c!='~')&&(c!=' ')) {
if (oprt(c)==1) rank=rank-1;
else {
rank=rank+1;
temp=temp->nxt;
while ((temp!=NULL)&&(terminate==0)) {
c=temp->val;
if ((oprt(c)==1)||(c==' ')) { terminate=1;
if ((oprt(c)==1)&&(c!='~')) {rank=rank-1;}
}
else temp=temp->nxt;
}
} }
if (temp!=NULL) temp=temp->nxt;
}
return rank;

}