实现算术表达式求值程序(栈的运用)
答案:1 悬赏:20 手机版
解决时间 2021-03-30 21:20
- 提问者网友:爱唱彩虹
- 2021-03-30 03:04
实现算术表达式求值程序(栈的运用)
最佳答案
- 五星知识达人网友:动情书生
- 2021-03-30 03:52
#include
#include
#include
#include
typedef struct node
{ char data;
struct node *next;
}snode,*slink;
int Emptystack(slink S)
{ if(S==NULL)return(1);
else return(0);
}
char Pop(slink* top)
{ char e;
slink p;
if(Emptystack(*top))
return(-1);
else
{ e=(*top)->data;
p=*top;
*top=(*top)->next;
free(p);
return(e);
}
}
void Push(slink* top,char e)
{ slink p;
p=(slink)malloc(sizeof(snode));
p->data=e;
p->next=*top;
*top=p;
}
void Clearstack(slink* top)
{ slink p;
while(*top!=NULL)
{ p=(*top)->next;
Pop(top);
*top=p;
}
*top=NULL;
}
char Getstop(slink S)
{ if(S!=NULL)
return(S->data);
return(0);
}
int Precedence(char x,char y)
{switch(x)
{case '(':x=0;break;
case '+':
case '-':x=1;break;
case '*':
case '/':x=2;break;
}
switch(y)
{case '+':
case '-':y=1;break;
case '*':
case '/':y=2;break;
case ')':x=3;break;
}
if(x>=y)
return(1);
else return(0);
}
void Mid_post(char E[],char B[])
{
slink S=NULL;
//给栈底放入'@'字符,它具有最低优先 级0
//Push(&S,'#');
//定义i,j分别用于扫描S1和指示S2串中待存字符的位置
int i=0,j=0;
//定义ch保存S1串中扫描到的字符,初值为第1个字符
//Push(&S,'#');
char ch;
//依次处理中缀表达式中的每个字符
do
{
//对于空格字符不做任何处理,顺序读取下一个字符
if(ch==' ') ch=E[++i];
ch=E[i++];
switch(ch)
{ case '#':
{ while(!Emptystack(S))
B[j++]=Pop(&S);
}break;
case ')':
{while(Getstop(S)!='(')
B[j++]=Pop(&S);
Pop(&S);
}break;
case '+':
case '-':
case '*':
case '/':
case '(':
{ while(Precedence(Getstop(S),ch))
B[j++]=Pop(&S);
Push(&S,ch);
}break;
default:B[j++]=ch;
}
}while(ch!='#');
B[j++]='#';
B[j]='\0';
}
int Postcount(char B[])
{int i=0;
char x;
float z,a,b;
slink S=NULL;
while(B[i]!='#')
{x=B[i];
switch(x)
{case '+':b=Pop(&S);a=Pop(&S);z=a+b;Push(&S,z);break;
case '-':b=Pop(&S);a=Pop(&S);z=a-b;Push(&S,z);break;
case '*':b=Pop(&S);a=Pop(&S);z=a*b;Push(&S,z);break;
case '/':b=Pop(&S);a=Pop(&S);z=a/b;Push(&S,z);break;
default : x=B[i]-'0';Push(&S,x);
}
i++;
}
if(!Emptystack(S)) return(Getstop(S));
}
void main()
{ char B[255],E[255]=" ";
char check[2];
int i=1;
int round = 1;
while(i<2)
{printf("please input 中缀表达式:");
//scanf("%s",E);
if(round > 1){getchar();}
gets(E);
//puts(E);
//strcpy(B,E); //将B填充为与E相同的空间(其实是忘了加'#')
//gets(B);
//puts(B);
printf("后缀表达式为:");
Mid_post(E,B);
puts(B);
printf("the result is %d\n",Postcount(B));
printf("\ncontine?(Y/N)");
scanf("%s",check);
round ++;
if(check[0]=='N')
break;
if(check[0]!='Y'&&check[0]!='N')
{printf("您输入了规定以外的字符\n");
break;
}
}
printf("------------FIN------------");
getch();
}
就是这样
#include
#include
#include
typedef struct node
{ char data;
struct node *next;
}snode,*slink;
int Emptystack(slink S)
{ if(S==NULL)return(1);
else return(0);
}
char Pop(slink* top)
{ char e;
slink p;
if(Emptystack(*top))
return(-1);
else
{ e=(*top)->data;
p=*top;
*top=(*top)->next;
free(p);
return(e);
}
}
void Push(slink* top,char e)
{ slink p;
p=(slink)malloc(sizeof(snode));
p->data=e;
p->next=*top;
*top=p;
}
void Clearstack(slink* top)
{ slink p;
while(*top!=NULL)
{ p=(*top)->next;
Pop(top);
*top=p;
}
*top=NULL;
}
char Getstop(slink S)
{ if(S!=NULL)
return(S->data);
return(0);
}
int Precedence(char x,char y)
{switch(x)
{case '(':x=0;break;
case '+':
case '-':x=1;break;
case '*':
case '/':x=2;break;
}
switch(y)
{case '+':
case '-':y=1;break;
case '*':
case '/':y=2;break;
case ')':x=3;break;
}
if(x>=y)
return(1);
else return(0);
}
void Mid_post(char E[],char B[])
{
slink S=NULL;
//给栈底放入'@'字符,它具有最低优先 级0
//Push(&S,'#');
//定义i,j分别用于扫描S1和指示S2串中待存字符的位置
int i=0,j=0;
//定义ch保存S1串中扫描到的字符,初值为第1个字符
//Push(&S,'#');
char ch;
//依次处理中缀表达式中的每个字符
do
{
//对于空格字符不做任何处理,顺序读取下一个字符
if(ch==' ') ch=E[++i];
ch=E[i++];
switch(ch)
{ case '#':
{ while(!Emptystack(S))
B[j++]=Pop(&S);
}break;
case ')':
{while(Getstop(S)!='(')
B[j++]=Pop(&S);
Pop(&S);
}break;
case '+':
case '-':
case '*':
case '/':
case '(':
{ while(Precedence(Getstop(S),ch))
B[j++]=Pop(&S);
Push(&S,ch);
}break;
default:B[j++]=ch;
}
}while(ch!='#');
B[j++]='#';
B[j]='\0';
}
int Postcount(char B[])
{int i=0;
char x;
float z,a,b;
slink S=NULL;
while(B[i]!='#')
{x=B[i];
switch(x)
{case '+':b=Pop(&S);a=Pop(&S);z=a+b;Push(&S,z);break;
case '-':b=Pop(&S);a=Pop(&S);z=a-b;Push(&S,z);break;
case '*':b=Pop(&S);a=Pop(&S);z=a*b;Push(&S,z);break;
case '/':b=Pop(&S);a=Pop(&S);z=a/b;Push(&S,z);break;
default : x=B[i]-'0';Push(&S,x);
}
i++;
}
if(!Emptystack(S)) return(Getstop(S));
}
void main()
{ char B[255],E[255]=" ";
char check[2];
int i=1;
int round = 1;
while(i<2)
{printf("please input 中缀表达式:");
//scanf("%s",E);
if(round > 1){getchar();}
gets(E);
//puts(E);
//strcpy(B,E); //将B填充为与E相同的空间(其实是忘了加'#')
//gets(B);
//puts(B);
printf("后缀表达式为:");
Mid_post(E,B);
puts(B);
printf("the result is %d\n",Postcount(B));
printf("\ncontine?(Y/N)");
scanf("%s",check);
round ++;
if(check[0]=='N')
break;
if(check[0]!='Y'&&check[0]!='N')
{printf("您输入了规定以外的字符\n");
break;
}
}
printf("------------FIN------------");
getch();
}
就是这样
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯