C语言表达式求值程序和程序注释

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/22 07:07:44
C语言表达式求值程序和程序注释C语言表达式求值程序和程序注释C语言表达式求值程序和程序注释#include#include#definenull0#definemax100typedefstructd

C语言表达式求值程序和程序注释
C语言表达式求值程序和程序注释

C语言表达式求值程序和程序注释
#include
#include
#define null 0
#define max 100typedef struct date /*定义字符栈*/
{ char sk[max];
int top;}
seqstack;typedef struct da /*定义数据栈*/
{ int sk[max];
int top;}
stack;void push(seqstack *s,char x) /*进栈*/
{
if(s->top==max-1)
{ printf("out of space.\n"); }
s->top++;
s->sk[s->top]=x;}
void spush(stack *s,int x) /*进栈*/
{ if(s->top==max-1)
{ printf("sout of space.\n"); }
s->top++;
s->sk[s->top]=x;}
char pop(seqstack *s ,char c) /*出栈*/
{ if(s->top==-1)
{ printf("no element.\n");
return 0; }
else
{ c=s->sk[s->top];
s->top--;
return c; }}
int spop(stack *s ,int c) /*出栈*/
{ if(s->top==-1)
{ printf("sno element.\n");return 0; }
else
{ c=s->sk[s->top];
s->top--;
return c; }}
int pp(char c) /*判断字符*/
{ switch(c)
{
case '#':return 0;break;
case '+':return 2;break;
case '-':return 2;break;
case '*':return 3;break;
case '/':return 3;break;
case '(':return 1;break;
case ')':return 4;break;
default:return 9;break;
}}
int yunsuan(int a,char b,int c) /*运算*/
{ switch(b)
{ case '+':return a+c;break;
case '-':return a-c;break;
case '*':return a*c;break;
default:return a/c;break;
}}
char bijiao(seqstack *f,char c) /*比较优先级*/
{ int m,n;
char w;
m=pp(c);
w=f->sk[f->top];
n=pp(w);
if(ntop=-1;
stack *g;
g=(stack*)malloc(sizeof(stack));
if(!g) printf("no door");
g->top=-1;
push(h,'#');
ch=getchar();
while(ch!='\n')
{ if(pp(ch)==9)
{
if(p>0)
{
x=spop(g,x);

spush(g,10*x+ch-48);
}
else
{
spush(g,ch-48);
p++;
}
}
if(pp(ch)==1)
{
push(h,ch);p=0;
}
if(pp(ch)==4)
{
while(pp(h->sk[h->top])!=1)
{
q=spop(g,q);
w=spop(g,w);
th=pop(h,th);
spush(g,yunsuan(w,th,q));
}
d=pop(h,d);p=0;
}
if(pp(ch)!=9&&pp(ch)!=1&&pp(ch)!=4)
{
switch(bijiao(h,ch))
{ case '>':push(h,ch);p=0;break;
case '