算术表达式(可以算多位数)
#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -1//申请空间失败
#include<conio.h>
#include<dos.h>
typedef int ElemType;
typedef int Status;
typedef struct Stack_Node{
ElemType data;
struct Stack_Node *next;
}Stack_Node;
typedef struct {
Stack_Node *top;
Stack_Node *bottom;
int length;
}StackList;
StackList Establish()//建栈
{
StackList S;
S.top=(Stack_Node*)malloc(sizeof(Stack_Node));
if(!S.top)
{
printf("Failed to open up space!\n");
exit(1);//建站失败
}
S.bottom=S.top;
S.bottom->next=NULL;
return S;
}
void Push(StackList* S,ElemType e)
{
Stack_Node *newnode=(Stack_Node*)malloc(sizeof(Stack_Node));
if(!newnode)
{
printf("Failed to open up space!");
exit(1);
}
S->top->data=e;
newnode->next=S->top;
S->top=newnode;
}
ElemType Pop(StackList* S)//出栈 未判断是否为空
{
Stack_Node *p;//暂时指向top
ElemType e;
p=S->top->next;
e=p->data;
S->top->next=p->next;
free(p);
return e;
}
ElemType GetTop(StackList* S)//出栈 未判断是否为空
{
Stack_Node *p;//暂时指向top
ElemType e;
p=S->top->next;
e=p->data;
return e;
}
Status Precede(ElemType e1,ElemType e2)
{
int optr[7][7]={//1表示大于,2表示<,-1表示=
1, 1, 2, 2, 2, 1, 1,
1, 1, 2, 2, 2, 1, 1,
1, 1, 1, 1, 2, 1, 1,
1, 1, 1, 1, 2, 1, 1,
2, 2, 2, 2, 2, 0, -1,
1, 1, 1, 1, -1, 1, 1,
2, 2, 2, 2, 2, -1, 0
};
//printf("ee%d\n",optr[1][1]);
int op[7]={'+','-','*','/','(',')','#'};//用于查找运算符的序号
int i=0;
while(op[i]!=e1)
{
i++;
}
int j=0;
while(op[j]!=e2)
{
j++;
}
// printf("##%d %d %d\n",i,j,optr[i][j]);
return optr[i][j];
}
/*
Status In(ElemType e)
{
if()
{
return 0;
}
else{
return 1;
}
}*/
ElemType Operate(ElemType a,char theta,ElemType b)
{
if(theta=='+')
{
return a+b;
}
else if(theta=='-')
{
return a-b;
}
else if(theta=='*')
{
return a*b;
}
else if(theta=='/')
{
return a/b;
}
}
int Getin(int *f,char ch[],int *k)
{
int p=0;
if(ch[*k]>='0'&&ch[*k]<='9')
{
p=p*10+ch[*k]-'0';
*k=*k+1;
while(ch[*k]>='0'&&ch[*k]<='9')
{
//printf(" a %c\n",a);
p=p*10+ch[*k]-'0';
*k=*k+1;
}
*f=0;
// *k=*k-1;//再退回去
}
else{
*f=1;
p=(int)ch[*k];
*k=*k+1;
}
//printf("%d",p);
return p;
}
int EvaluateExpression(char* ch)
{
ElemType c;
int b, a;
int flag=0;
char theta;
int k=0;//位置
StackList S1;//操作符栈
StackList S2;//操作数栈
S1= Establish();
S2= Establish();
//c='#'
Push(&S1,'#');
c=Getin(&flag,ch,&k);
while(c!='#'||GetTop(&S1)!='#')
{
if(flag==0) //如果是运算数
{
Push(&S2,c);
c=Getin(&flag,ch,&k);
}
else{
//printf("%c %c\n",c,GetTop(&S1));
// printf("%d\n",Precede(GetTop(&S1),c));
switch(Precede(GetTop(&S1),c))
{
case 2:
Push(&S1,c);
c=Getin(&flag,ch,&k);
break;
case 0:
c=Pop(&S1);
c=Getin(&flag,ch,&k);
break;
case 1:
theta=Pop(&S1);
a=Pop(&S2);
b=Pop(&S2);
Push(&S2,Operate(b,theta,a));
break;
}
}
}
return GetTop(&S2);
}
int main()
{
int value;
char ch[1000];
printf("请输入算数式(最后请以“#”结束)\n");
scanf("%s",ch);
value=EvaluateExpression(ch);
printf("%d\n",value);
return 0;
}
只能输入个位数
#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -1//申请空间失败
#include<conio.h>
#include<dos.h>
typedef int ElemType;
typedef int Status;
typedef struct Stack_Node{
ElemType data;
struct Stack_Node *next;
}Stack_Node;
typedef struct {
Stack_Node *top;
Stack_Node *bottom;
int length;
}StackList;
StackList Establish()//建栈
{
StackList S;
S.top=(Stack_Node*)malloc(sizeof(Stack_Node));
if(!S.top)
{
printf("Failed to open up space!\n");
exit(1);//建站失败
}
S.bottom=S.top;
S.bottom->next=NULL;
return S;
}
void Push(StackList* S,ElemType e)
{
Stack_Node *newnode=(Stack_Node*)malloc(sizeof(Stack_Node));
if(!newnode)
{
printf("Failed to open up space!");
exit(1);
}
S->top->data=e;
newnode->next=S->top;
S->top=newnode;
}
ElemType Pop(StackList* S)//出栈 未判断是否为空
{
Stack_Node *p;//暂时指向top
ElemType e;
p=S->top->next;
e=p->data;
S->top->next=p->next;
free(p);
return e;
}
ElemType GetTop(StackList* S)//出栈 未判断是否为空
{
Stack_Node *p;//暂时指向top
ElemType e;
p=S->top->next;
e=p->data;
return e;
}
Status Precede(ElemType e1,ElemType e2)
{
int optr[7][7]={//1表示大于,2表示<,-1表示=
1, 1, 2, 2, 2, 1, 1,
1, 1, 2, 2, 2, 1, 1,
1, 1, 1, 1, 2, 1, 1,
1, 1, 1, 1, 2, 1, 1,
2, 2, 2, 2, 2, 0, -1,
1, 1, 1, 1, -1, 1, 1,
2, 2, 2, 2, 2, -1, 0
};
//printf("ee%d\n",optr[1][1]);
int op[7]={'+','-','*','/','(',')','#'};//用于查找运算符的序号
int i=0;
while(op[i]!=e1)
{
i++;
}
int j=0;
while(op[j]!=e2)
{
j++;
}
// printf("##%d %d %d\n",i,j,optr[i][j]);
return optr[i][j];
}
Status In(ElemType e)
{
if(e>'0'&&e<'9')
{
return 0;
}
else{
return 1;
}
}
ElemType Operate(ElemType a,char theta,ElemType b)
{
if(theta=='+')
{
return a+b;
}
else if(theta=='-')
{
return a-b;
}
else if(theta=='*')
{
return a*b;
}
else if(theta=='/')
{
return a/b;
}
}
int EvaluateExpression()
{
ElemType c;
int b, a;
char theta;
StackList S1;//操作符栈
StackList S2;//操作数栈
S1= Establish();
S2= Establish();
//c='#'
Push(&S1,'#');
c=getchar();
while(c!='#'||GetTop(&S1)!='#')
{
if(!(c=='#'||c=='('||c==')'||c=='+'||c=='-'||c=='*'||c=='/'||c>='0'&&c<='9')){
printf("你的输入不符合计算器规则!");
exit(1);
}
if(!In(c))
{
Push(&S2,c-'0');
c=getchar();
}
else{
//printf("%c %c\n",c,GetTop(&S1));
// printf("%d\n",Precede(GetTop(&S1),c));
switch(Precede(GetTop(&S1),c))
{
case 2:
Push(&S1,c);
c=getchar();
break;
case 0:
c=Pop(&S1);
c=getchar();
break;
case 1:
theta=Pop(&S1);
a=Pop(&S2);
b=Pop(&S2);
Push(&S2,Operate(b,theta,a));
break;
}
}
}
return GetTop(&S2);
}
int main()
{
int value;
printf("请输入算数式(最后请以“#”结束)\n");
value=EvaluateExpression();
printf("%d\n",value);
return 0;
}