前缀表达式、后缀表达式和中缀表达式的计算(double型)




有关中缀表达式的计算以及中缀表达式与前缀表达式、后缀表达式之间的转换   后续文章会继续给出

这里只讲前缀表达式与后缀表达式计算的实现方法




前缀表达式

    

计算方法:  

将得到的字符串处理为只含有数字和运算符    

将处理后的字符串从前到后压如栈S1中

将栈S1中的元素逐个弹出

若弹出元素判断为数字   压入栈S2中

若弹出元素判断为运算符   从栈S2中弹出两个元素   与该运算符进行运算    将运算结果重新压入栈S2中

处理完S1中所有元素后    S2栈顶元素即为计算结果



实现代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stack>
using namespace std;
char *Sep=" ";
char *p;
char str[1005];
double calculate(double a,double b,char c){//   元素的运算顺序和后缀表达式相反
    if(c=='+')  return a+b;
    if(c=='-')  return a-b;
    if(c=='*')  return a*b;
    if(c=='/')  return a/b;
}
int main (){
    while (gets(str)){
        stack<char*> S1;
        stack<double> S2;
        p=strtok(str,Sep);//      字符串处理  以空格为分隔符将字符串分隔  压入栈中
        do{
            S1.push(p);
        }while ((p=strtok(NULL,Sep)));
        while (!S1.empty()){
            p=S1.top();
            S1.pop();
            if (isdigit(p[0]))//      判断是否为数字
                S2.push(atof(p));
            else{
                double a=S2.top();
                S2.pop();
                double b=S2.top();
                S2.pop();
                double temp=calculate(a,b,p[0]);
                S2.push(temp);//      计算结果重新压入栈
            }
        }
        printf ("%g\n",S2.top());//     格式控制 随便啦  根据题意就好
    }
    return 0;
}







后缀表达式


计算方法:

将得到的字符串处理为只含有数字和运算符

从前到后判断分隔后的字符

若判断为数字压入栈中

若判断为运算符    从栈中弹出两个元素    与该运算符进行运算    将运算结果重新压入栈中

处理完所有分隔后的字符     栈顶元素即为计算结果



实现代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stack>
using namespace std;
char *Sep=" ";
char *p;
char str[1005];
double calculate(double a,double b,char c){
    if(c=='+')  return b+a;
    if(c=='-')  return b-a;
    if(c=='*')  return b*a;
    if(c=='/')  return b/a;
}
int main (){
    while (gets(str)){
        stack<double> S;
        p=strtok(str,Sep);
        do{
            if (isdigit(p[0]))
                S.push(atof(p));
            else{
                double a=S.top();
                S.pop();
                double b=S.top();
                S.pop();
                double temp=calculate(a,b,p[0]);
                S.push(temp);
            }
        }while ((p=strtok(NULL,Sep)));
        printf ("%g\n",S.top());
    }
    return 0;
}



中缀表达式现在写了

中缀表达式的计算 :   http://blog.csdn.net/mm__1997/article/details/78115962




附一前缀表达式计算的练习题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=128

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是C++求解中缀表达式后缀表达式前缀表达式的代码,其包括了表达式求值的算法实现: 1. 求解中缀表达式: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; int priority(char c) { // 定义运算符优先级 if (c == '*' || c == '/') { return 2; } else if (c == '+' || c == '-') { return 1; } else { return 0; } } double calculate(double a, double b, char c) { // 定义运算符操作 switch (c) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } double evaluateInfix(string expression) { // 中缀表达式求值 stack<double> nums; // 存放数字 stack<char> ops; // 存放运算符 int len = expression.length(); for (int i = 0; i < len; i++) { char c = expression[i]; if (isdigit(c)) { // 如果是数字 double num = 0; while (i < len && isdigit(expression[i])) { num = num * 10 + (expression[i] - '0'); i++; } nums.push(num); i--; } else if (c == '(') { // 如果是左括号 ops.push(c); } else if (c == ')') { // 如果是右括号 while (!ops.empty() && ops.top() != '(') { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); nums.push(calculate(a, b, op)); } ops.pop(); } else { // 如果是运算符 while (!ops.empty() && priority(ops.top()) >= priority(c)) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); nums.push(calculate(a, b, op)); } ops.push(c); } } while (!ops.empty()) { // 处理剩余的运算符 double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); nums.push(calculate(a, b, op)); } return nums.top(); } int main() { string expression = "1+2*3-4/2"; double result = evaluateInfix(expression); cout << expression << " = " << result << endl; return 0; } ``` 2. 求解后缀表达式: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; double calculate(double a, double b, char c) { // 定义运算符操作 switch (c) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } double evaluatePostfix(string expression) { // 后缀表达式求值 stack<double> nums; // 存放数字 int len = expression.length(); for (int i = 0; i < len; i++) { char c = expression[i]; if (isdigit(c)) { // 如果是数字 double num = 0; while (i < len && isdigit(expression[i])) { num = num * 10 + (expression[i] - '0'); i++; } nums.push(num); i--; } else { // 如果是运算符 double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); nums.push(calculate(a, b, c)); } } return nums.top(); } int main() { string expression = "123*+4-2/"; double result = evaluatePostfix(expression); cout << expression << " = " << result << endl; return 0; } ``` 3. 求解前缀表达式: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; double calculate(double a, double b, char c) { // 定义运算符操作 switch (c) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } double evaluatePrefix(string expression) { // 前缀表达式求值 stack<double> nums; // 存放数字 int len = expression.length(); for (int i = len - 1; i >= 0; i--) { char c = expression[i]; if (isdigit(c)) { // 如果是数字 double num = 0; int j = i; while (j >= 0 && isdigit(expression[j])) { num = num + (expression[j] - '0') * pow(10, i - j); j--; } nums.push(num); i = j + 1; } else { // 如果是运算符 double a = nums.top(); nums.pop(); double b = nums.top(); nums.pop(); nums.push(calculate(a, b, c)); } } return nums.top(); } int main() { string expression = "-+*123/84"; double result = evaluatePrefix(expression); cout << expression << " = " << result << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值