简单的表达式求值

表达式求值的实现

C语言解决:输入表达式,转成后缀式后求值

// 4.14 表达式求值
#include<stdio.h>
#include<string.h>
#define MAXSIZE 50
typedef struct{
  char elem[MAXSIZE];
  int top;
}Stack;

void Init(Stack *S);
int NorSyn(char obj);
void push(Stack *S,int e);
void Cpush(Stack *S,char c);
void pop(Stack *S,int *e);
void Cpop(Stack *S,char *c);
void transf(char suffix[],char exp[]);
void Pass(char suffix[],char ch);
int Priority(char ch);
void value(Stack *S,char suffix[]);



int main(){
Stack S;
char exp[MAXSIZE];       //原表达式exp
char suffix[MAXSIZE];    //转换过的后缀式suffix
memset(suffix,'\0',sizeof(suffix));
Init(&S);
transf(suffix,exp);
value(&S,suffix);
return 0;
}

void Init(Stack *S){
S->top = -1;
}

void transf(char suffix[],char exp[]){

Stack Sy;  //暂存符号栈
int step;
char save,ch,*p;
Init(&Sy);
Cpush(&Sy,'#');
printf("请输入表达式(以'#'结束):");
scanf("%s",exp);
p = exp;  //p存exp地址
ch = *p;  //ch为处理对象

while(Sy.top != -1){

    step = NorSyn(ch);
    char topsyntax = Sy.elem[Sy.top];//栈顶运算符
    if(step == 5){Pass(suffix,ch);}   //数字直接发送
    else if(step == 0){

            while(Sy.top != -1){
            Cpop(&Sy,&ch);
            Pass(suffix,ch);
            }
            break;
    }  //#号结束,全部出栈

    else{                            //括号或运算符 判定优先级

       if(ch == '('){Cpush(&Sy,ch);}
       else if(ch == ')'){
            Cpop(&Sy,&save);
            while(save != '('){Pass(suffix,save);Cpop(&Sy,&save);}
       }
       else{                         //最后判断+-*/
            if(topsyntax == '('){Cpush(&Sy,ch);}
            else if (Priority(ch) >= Priority(topsyntax)){Cpush(&Sy,ch);}    //ch比栈顶优先级高,直接入栈
            else {Pass(suffix,topsyntax);                                    //否则先将前一栈顶出栈,然后ch入栈
                  char temp;
                  Cpop(&Sy,&temp);   //保护ch
                  Cpush(&Sy,ch);}
       }
    }
p++;
ch = *p;
}
printf("转化过后的后缀式为:%s\n",suffix);
}


void Pass(char suffix[],char ch){
int n=0;
while(suffix[n]){
    n++;
}
suffix[n] = ch;
}

int Priority(char ch){
switch(ch){
case '+':return 5;
case '-':return 5;
case '*':return 10;
case '/':return 10;
case '#':return 0;

}

}

void value(Stack *S,char suffix[]){
char ch;
int step,i=0,a,b,rel;
ch = suffix[i];          //数字压栈,符号处理
step = NorSyn(ch);
while(step){
    if(step == 5){push(S,ch-48);}
    else{
       pop(S,&b);
       pop(S,&a);
       if(step == 1){push(S,(a)*(b));}
       if(step == 2)push(S,(a)/(b));
       if(step == 3)push(S,(a)+(b));
       if(step == 4)push(S,(a)-(b));
    }
ch = suffix[++i];
step = NorSyn(ch);
}
pop(S,&rel);
printf("结果为:%d\n",rel);
}

int NorSyn(char obj){
    if(obj<=57&&obj>=48)           //'0~9'
    return 5;
    else if(obj == '*')return 1;    //运算符
    else if(obj == '/')return 2;
    else if(obj == '+')return 3;
    else if(obj == '-')return 4;
    else if(obj == '#')return 0;    //结束符
}

void push(Stack *S,int e){
if(S->top == MAXSIZE-1){
    printf("栈满");
    return;}
S->elem[++S->top] = e;
}

void pop(Stack *S,int *e)
{
if(S->top == -1){
    printf("栈空");
    return;}
*e = S->elem[S->top--];
}

void Cpush(Stack *S,char c){
if(S->top == MAXSIZE-1){
    printf("栈满");
    return;}
S->elem[++S->top] = c;
}

void Cpop(Stack *S,char *c)
{
if(S->top == -1){
    printf("栈空");
    return;}
*c = S->elem[S->top--];
}


数据结构刚学到栈和队列,小小记录一下作业的完成情况。
代码很多地方都不完善,如只支持0~9数字,除法直接默认的截尾取整等等…(溜

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值