c语言数据结构实现后缀表达式求值

来源:互联网 发布:微信开发 php java 编辑:程序博客网 时间:2024/05/01 19:43

通常人在书写的时候习惯是书写中缀表达式也叫逆波兰式,然而在计算机处理的时候中缀表达式的效率远小于后缀表达式,即操作数在前面,运算符在后面例如:

中缀表达式 A+B     后缀表达式AB+

                 A+B*C                     ABC*+

               A*B+C*D                AB*CD*+

             D+A/(B_C)              DABC-/+

后缀表达式计算时,所有运算按照运算符出现的顺序,严格从左到右,每个操作符取前两个操作数进行运算,运算后的结果仍然作为下次的操作数,这样做与中缀表达式完全等价,即计算次序和运算结果完全相同,并且不再使用括号,逆波兰式的主要特点在于运算对象(操作数)不变,运算符号反应运算顺序,采用逆波兰式很好的表达简单运算符,其优点在于已与计算机处理表达式,因此算术表达式计算问题进行分解,先将中缀转换为后缀表达式在进行计算。

这个过程一共分为两步,第一步,把输入的表达式转换为后缀表达式,第二步,计算后缀表达式


转化后缀表达式的过程:

1.当读到操作数后存入数组中,

2.当读到运算符的时候,把优先级比这个运算符小或者等于的所有运算符出栈,再把这个运算符入栈

3.当读左括号的时候,入栈

4.当读到右括号得时候,将靠近栈顶的左括号之前的所有运算符出栈,存入数组中然后再将左括号出栈


计算后缀表达式的过程:

对于计算后缀表达式而言,只是按照自然地从左到右的逻辑顺序,当遇到操作数的时候就入栈,当遇到运算符的时候,就取栈顶的前两个元素进行操作,然后操作完的数,再入栈,然后继续扫描,一直直到表达式遍历完,

实现算法如下:

#include<stdio.h>#include<string.h>#include<stdlib.h>#include <string>#include <stack>#include<algorithm>#define MAXN 1000using namespace std;stack<char> s;  //定义了一个栈char *tranfExp(char* exp){    char tempStr[1000];//保存后缀表达式的字符串    int i=0,j=0;    while(exp[i] !='\0')    {        if(exp[i]>='0' &&exp[i]<='9')  //如果是数字字符串就保存到后缀表达式字符串中        {            tempStr[j++] = exp[i];        }        else if(exp[i] == '(' )  //如果是左括号及入栈        {            s.push(exp[i]);        }        else if(exp[i] == ')' )  //如果是右括号就把接近栈顶的左括号上面所有的运算符出栈存进字符串中  左括号出栈        {            while(s.empty() == false)            {                if(s.top() == '(' )                {                    s.pop();                    break;                }                else                {                   tempStr[j++] = s.top();                   s.pop();                }            }        }        else if(exp[i] == '+' || exp[i] == '-')   //如果的事+-|操作符就把比他优先级高或者等于的所有运算符出栈进入字符串        {            while(s.empty() == false)            {                char ch = s.top();                if(ch == '+'||ch == '-'||ch == '/'||ch == '*')                {                   tempStr[j++] = s.top();                   s.pop();                }                else                    break;            }            s.push(exp[i]);        }        else if(exp[i] == '*' || exp[i] == '/')  //类似于扫描到+- 只是如果栈中有=-运算符就不用出栈  因为运算符优先级比较小        {            while(s.empty() == false)            {                char ch = s.top();                if(ch == '/' || ch=='*')                {                    tempStr[j++] = s.top();                   s.pop();                }                else                    break;            }            s.push(exp[i]);        }        i++;    }    while(s.empty() == false)   //把栈中剩余的所有运算符出栈    {        tempStr[j++] = s.top();        s.pop();    }    tempStr[j] = 0;   //最后一个赋值为0  也就是字符串结束的标志    return tempStr;   //返回已经得到的后缀表达式}int calcExp(char* exp)// 计算后缀表达式{    puts(exp);   //展示已经得到的后缀    while( !s.empty() )      s.pop();    int i=0;    while(exp[i] != '\0')    {        if(exp[i]>='0' && exp[i]<='9')        {            s.push(exp[i]-'0');        }        else if(exp[i] == '-')        {            int m = s.top();            s.pop();            int n = s.top();            s.pop();            s.push(n-m);        }        else if(exp[i] == '+')        {            int m = s.top();            s.pop();            int n = s.top();            s.pop();            s.push(n+m);        }        else if(exp[i] == '/')        {            int m = s.top();            s.pop();            int n = s.top();            s.pop();            s.push(n/m);        }        else if(exp[i] == '*')        {            int m = s.top();            s.pop();            int n = s.top();            s.pop();            s.push(n*m);        }        i++;    }  /* while(s.empty() == false)    {         printf("%d",s.top());         s.pop();    }*/    printf("\n\n\n");    return s.top();}int main(){   char str[1000];   char* tranStr;   tranStr = (char *)malloc(100*sizeof(char));   printf("please input expression with kuohao:\n");   scanf("%s",str);   tranStr =  tranfExp(str);//中缀表达式转换为后缀表达式函数   //puts(tranStr); //输出转换后的后缀表达式   printf("%d",calcExp(tranStr));   return 0;}


1 1
原创粉丝点击