//**************************
//姓名:王洋
//学号:040840110
//内容:表达式
//**************************
#include<string.h>
#include"stack.h"
void main()
{
Sq1Stack S1;
Sq2Stack S2;
Init1Stack (S1); //数字栈
Init2Stack (S2); //字符栈
char sentence[81]; //把表达式输入字幅串中
int i,j,a,b,m,k,x1,t1; //i:为实现循环 j:字幅串长度 a,b:实现运算操作时,所附予的变量m:实现字符向数的转换时,把数附给m,k:m位数的计位
char c,t[11],x,ch,x2;
while(1)
{
t1=1;
system("cls");
cout<<" ***********************************";
cout<<endl;
cout<<" *************表达试计算************";
cout<<endl;
cout<<" ***********************************";
cout<<endl;
cout<<"按任意键开始。。。"<<endl;
cout<<"请输入表达式:(输入完毕后,请输入#并按回车)"<<endl;
cin>>sentence;
Push2(S2,'#');
j=strlen(sentence);
for(i=0;i<j; )
{
k=0;
while('0'<=sentence[i]&&sentence[i]<='9')
{
t[k]=sentence[i];
k++; i++;
}
if(k!=0){
t[k]='\0';
m=atoi(t);
Push1(S1,m);
}
if((sentence[i]>'9'||sentence[i]<'0')&&(sentence[i+1]>'9'||sentence[i+1]<'0'))
{
if((sentence[i]=='('||sentence[i]=='+'||sentence[i]=='-'||sentence[i]=='*'||sentence[i]=='/')&&sentence[i+1]=='(')
;
else if(sentence[i]==')'&&(sentence[i+1]=='('||sentence[i+1]=='+'||sentence[i+1]=='-'||sentence[i+1]=='*'||sentence[i+1]=='/'||sentence[i+1]=='#'))
;
else if(sentence[i]=='#')
;
else
{
t1=0;
cout<<"输入不正确!"<<endl;
break;
}
}
if (sentence[i]>'9'||sentence[i]<'0')
{
switch(Precede(GetTop2(S2),sentence[i],t1))
{
case '<':
Push2(S2,sentence[i]);
break;
case '=':
if(sentence[i]!='#')
Pop2(S2,x);
break;
case '>':
Stack1Traverse( S1);
Stack2Traverse( S2);
if(x2!='(')
{
Pop2(S2,c);
Pop1(S1,b);
Pop1(S1,a);
if(c!='#')
{
Push1(S1,Operate(a,c,b));
i--;
}
}
else
Push2(S2,sentence[i]);
break;
case '@':
if(sentence[i]=='\n')
Push2(S2,'#');
break;
}
i++;
}
}
Stack2Traverse( S2);
Stack1Traverse( S1);
GetTop2(S2,x2);
GetTop1(S1,x1);
if(x2=='#'&&t1==1)
{
cout<<"&&&&&&&&&&&&&&&&&&"<<endl;
cout<<"计算结果是:"<<endl;
cout<<GetTop1(S1)<<endl;
cout<<"&&&&&&&&&&&&&&&&&&"<<endl;
}
else
{
cout<<"您输入的表达式不合规范!"<<endl;
}
cout<<"是否继续?(y/n):"<<endl;
cin>>ch;
if(ch=='n')
break;
cin.ignore();
Clear2Stack(S2);
Clear1Stack(S1);
system("pause");
}
}