/* Note:Your choice is C IDE */
#include "stdio.h"
#define NULL 0
#define MAXSIZE 15
struct stack1
{
float data[MAXSIZE];
int top1;
int move1;
};
struct stack2
{
char symbol[15];
int top2;
int move2;
};
struct stack1*initstack1()
{
struct stack1*s1;
s1=(struct stack1*)malloc(sizeof(struct stack1));
s1->top1=-1;
s1->move1=-1;
return(s1);
}
struct stack2*initstack2()
{
struct stack2*s2;
s2=(struct stack2*)malloc(sizeof(struct stack2));
s2->top2=-1;
s2->move2=-1;
return(s2);
}
int Emptystack1(struct stack1*s1)
{
if(s1->top1==-1)
return(1);
else
return(0);
}
int Emptystack2(struct stack2*s2)
{
if(s2->top2==-1)
return(1);
else
return(0);
}
int pushstack1(struct stack1*s1,float a)
{
if(s1->top1==MAXSIZE-1)
return(0);
else
{
s1->top1++;
s1->data[s1->top1]=a;
/*printf("\n%g",s1->data[s1->top1]);*/
return(1);
}
}
int pushstack2(struct stack2*s2,char h)
{
s2->top2++;
s2->symbol[s2->top2]=h;
/*printf("\n%c",s2->symbol[s2->top2]);*/
return(1);
}
float popstack1(struct stack1*s1)
{
float a;
if(Emptystack1(s1))
return(0);
else
a=s1->data[s1->top1];
/*printf("\n%g",a);*/
s1->top1--;
return(a);
}
char popstack2(struct stack2*s2)
{
char b;
if(Emptystack2(s2))
return(0);
else
b=s2->symbol[s2->top2];
/*printf("\n%c",b);*/
s2->top2--;
return(b);
}
char GetTop2(struct stack2*s2)
{
char b;
b=s2->symbol[s2->top2];
/*printf("\n%c",a);*/
return(b);
}
char Precede(char a,char b)
{
int i,j;
char Table[9][9]={' ','+','-','*','/','(',')','#','^',
'+','>','>','<','<','<','>','>','<',
'-','>','>','<','<','<','>','>','<',
'*','>','>','>','>','<','>','>','<',
'/','>','>','>','>','<','>','>','<',
'(','<','<','<','<','<','=',' ','<',
')','>','>','>','>',' ','>','>','>',
'#','<','<','<','<','<',' ','=','<',
'^','>','>','>','>','<','>','>','>',};
for(i=0;i<9;i++)
if(Table[0][i]==b)
break;
for(j=0;j<9;j++)
if(Table[j][0]==a)
break;
return Table[j][i];
}
int count=1;
float MainWork(char*k)
{
char r,t;
int j,i=0;
int s,setp=1;
float e,m,n,p,num;
struct stack1*p1;
struct stack2*p2;
p1=initstack1();
p2=initstack2();
pushstack2(p2,'#');
for(;k[i]!='\0';setp++,count+=1)
{
float weight=0.1;
num=0;
gotoxy(1,count+5);
printf("%d",setp);
if(in(k[i]))
{
gotoxy(48,count+5);
printf("%c",k[i]);
switch(Precede(GetTop2(p2),k[i]))
{
case'<':
pushstack2(p2,k[i]);
gotoxy(56,count+5);
printf("pushstack2:%c",k[i]);
i++;
break;
case'>':
m=popstack1(p1);
t=popstack2(p2);
n=popstack1(p1);
gotoxy(56,count+5);
printf("operate:(%g,%c,%g)",n,t,m);
switch(t)
{
case'*':
p=n*m;
pushstack1(p1,p);
break;
case'/':
p=n/m;
pushstack1(p1,p);
break;
case'+':
p=n+m;
pushstack1(p1,p);
break;
case'-':
p=n-m;
pushstack1(p1,p);
break;
case'^':
e=n*n;
for(s=2;s<m;s++)
e*=n;p=e;
pushstack1(p1,p);
break;
}
break;
case'=':
{gotoxy(56,count+5);
printf("popstack2:%c",popstack2(p2));i++;}
}
}
else
{
for(;!in(k[i])&&k[i]!='.';i++)
{num=10*num+k[i]-'0';}
if(k[i]=='.')
{
i++;
while(!in(k[i]))
{
num=num+(k[i]-'0')*weight;
weight*=0.1;
i++;
}
}
gotoxy(56,count+5);
printf("pushstack1:%g",num);
gotoxy(48,count+5);
printf("%g",num);
pushstack1(p1,num);
}
for(p1->move1=0,j=0;p1->move1<=p1->top1;p1->move1++,j=j+8)
{gotoxy(8+j,count+5);
printf("%g",p1->data[p1->move1]);}
for(p2->move2=0,j=0;p2->move2<=p2->top2;p2->move2++,j=j+2)
{gotoxy(35+j,count+5);
printf("%c",p2->symbol[p2->move2]);}
}
gotoxy(1,count+5);
printf("The value of expression is:%g",popstack1(p1));
}
int in(char c)
{
if(c=='+'||c=='-'||c=='('||c==')'||c=='*'||c=='/'||c=='#'||c=='%'||c=='^')
return(1);
else
return(0);
}
char expression[100];
void accept()
{
gotoxy(1,count);
printf("Please input the correct expression:");
scanf("%s",&expression);
gotoxy(1,count+2);
printf("You input the expression is:%s",expression);
}
void output()
{
float num;
gotoxy(1,count+4);
printf("STEP");
gotoxy(8,count+4);
printf("DATASTACK1");
gotoxy(35,count+4);
printf("SYMBOLSTACK2");
gotoxy(48,count+4);
printf("READ");
gotoxy(56,count+4);
printf("OPERATE\n");
MainWork(expression);
}
main()
{
accept();
output();
}
/*输入表达式必须以‘#’结束。目前表达式没有判错功能,本人将进一步完善*/
用C编的算术表达式求值(支持浮点数)