1.可多位的整数运算
2.进行加减乘除四则运算
3.可进行多次计算
#include
#include
#include
#include
#define Stack_Size 100
#define maxn 110
int color(int num){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),num);
return 0;
}
char priority[7][7]={
{
'>','>','<','<','<','>','>'},
{
'>','>','<','<','<','>','>'},
{
'>','>','>','>','<','>','>'},
{
'>','>','>','>','<','>','>'},
{
'<','<','<','<','<','=','0'}, // 此行"("=")"表示左右括号相遇,括号内运算已完成
{
'>','>','>','>','0','>','>'},
{
'<','<','<','<','<','0','='} // "=" 表示整个表达式求值完毕
}; // "0"表示不可能出现这种情况 ( 语法错误 )
struct OPTR{
char elem[Stack_Size];//用来存放运算符
int top;//栈顶
};
struct OPND{
int elem[Stack_Size];//用来存放操作数
int top;//栈顶
};
void InitStack1(struct OPTR *S)//构造运算符栈(空栈)
{
S->top=-1;
}
void InitStack2(struct OPND *S)//构造操作数栈(空栈)
{
S->top=-1;
}
void Push1(struct OPTR *S,char ch)// 运算符栈插入ch为新的栈顶元素
{
if(S->top==Stack_Size-1){
printf("栈已满,入栈失败!");
return;
}
S->top++;
S->elem[S->top]=ch;
return;
}
void Push2(struct OPND *S,int ch)// 操作数栈插入ch为新的栈顶元素
{
if(S->top==Stack_Size-1){
printf("栈已满,入栈失败!");
return;
}
S->top++;
S->elem[S->top]=ch;
return;
}
void Pop1(struct OPTR *S)//删除运算符栈S的栈顶元素 ,用p返回其值
{
if(S->top==-1){
printf("栈空,出栈失败!");
return;
}
// *p=S->elem[S->top];//栈顶元素赋给p
S->top--;//修改栈顶指针
return;
}
void Pop2(struct OPND *S)//删除操作数栈S的栈顶元素 ,用p返回其值
{
if(S->top==-1){
printf("栈空,出栈失败!");
return;
}
// *p=S->elem[S->top];//栈顶元素赋给p
S->top--;//修改栈顶指针
return;
}
char GetTop1(struct OPTR *S)//用p返回运算符栈S的栈顶元素
{
if(S->top==-1){
printf("栈空,取值失败!");
return -1;
}
char p=S->elem[S->top];//栈顶元素赋值给p
return p;
}
int GetTop2(struct OPND *S)//用p返回操作数栈S的栈顶元素
{
if(S->top==-1){
printf("栈空,取值失败!");
return -1;
}
int p=S->elem[S->top];//栈顶元素赋值给p
return p;
}
int In(char ch)//判断ch是否为运算符
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='#'||ch=='='){
return 0;
}
return -1;
}
char Precede(char t1,char t2)//判断运算符t1和t2的优先级
{
int i,j;
switch(t1){
case'+':i=0;break;
case'-':i=1;break;
case'*':i=2;break;
case'/':i=3;break;
case'(':i=4;break;
case')':i=5;break;
case'#':i=6;break; // #是表达式的结束符
}
switch(t2){
case'+':j=0;break;
case'-':j=1;break;
case'*':j=2;break;
case'/':j=3;break;
case'(':j=4;break;
case')':j=5;break;
case'#':j=6;break;
}
return priority[i][j];
}
int Operate(int a,char theta,int b)//对a和b进行二元运算theta
{
if(theta=='+') return a+b;
if(theta=='-') return b-a;
if(theta=='*') return a*b;
if(theta=='/') return b/a;
}
int main(){
system("mode 100,30");
int flag;
do{
struct OPTR *S1;
S1=(struct OPTR *)malloc(sizeof(struct OPTR));
InitStack1(S1);
struct OPND *S2;
S2=(struct OPND *)malloc(sizeof(struct OPND));
InitStack2(S2);
Push1(S1,'#');
char s[maxn];
color(11);
printf("请输入算数表达式(正整数),并以=结束\n");
scanf("%s",&s);
char c=s[0];
int k=1;
while(c!='='||GetTop1(S1)!='#'){
//表达式未读完或者运算符未完
int y=0;
if(c>='0'&&c<='9'){
while(c>='0'&&c<='9'){
//读入连续的数字
y=y*10+(c-'0');
c=s[k++];
}
Push2(S2,y);
}
else{
if(In(c)==-1){
printf("运算符或表达式不合法!计算失败!");
return 0;
}
char x;
int m,n;
switch(Precede(GetTop1(S1),c)){
//不是操作数则说明是运算符 ,和运算符栈的顶元素进行优先级比较
case'<'://栈顶元素优先级低
Push1(S1,c);
c=s[k++];
break;
case'=':
Pop1(S1);//脱括号
c=s[k++];//读取下一个字符
break;
case'>':
x=GetTop1(S1);Pop1(S1);
m=GetTop2(S2);Pop2(S2);
n=GetTop2(S2);Pop2(S2);
Push2(S2,Operate(m,x,n));
break;
default:
printf("语法错误"); break;
}
}
}
int result=GetTop2(S2);
printf("%d",result);
color(3);
printf("\n是否继续计算1/0:");
scanf("%d",&flag);
if(flag==0){
printf("☆☆感谢您的使用 ☆☆");
}
}while(flag!=0);
return 0;
}