栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实现后缀表达式求值

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实现后缀表达式求值

输入要求
 *      用栈保存操作数
 *      前缀:Prefix Notation(Polish notation)
 *      中缀:Infix Notation
 *      后缀:Postfix Notation(Reverse Polish notation)
 *      操作数:A-Z, a-z, 0-9
 *      运算符:+, -, *, /, (, )
 *      Postfix:输入字符串
 *      StackOperand:操作数栈
样例输入
	中缀表达式为:((5/(7-(1+1)))*3)-(2+(1+1))
	后缀表达式为:5711+-/3*211++-
	计算结果应为:-1
此程序只能接收10以下数字的运算
暂时没想到怎么简单的处理例如15这样的操作数,被表示为后缀表达式后,判断出它是15,还是1,5
算法代码
ElemType CalculatePostfix(string Postfix)
{
    // 初始化操作数栈
    SqStack StackOperand;
    InitStack(StackOperand);
    // 遍历Postfix
    for (int i = 0; i < Postfix.length(); i++) {
        string OpSign = JudgeOperation(Postfix[i]);
        // 错误直接退出循环
        if (OpSign == "InvalidInput")
        {
            cout << "表达式有误!" << endl;
            break;
        }
        // 遇到操作数,压入栈中,进行下一次扫描,跳过后面的操作
        if (OpSign == "Operand")
        {
            ElemType temp = Postfix[i] - '0'; // 把char转 double数字
            Push(StackOperand, temp);
            continue;
        }
        // 遇到运算符
        if (OpSign == "Operator")
        {
            // 遇到运算符时,栈空,则表达式有误
            if (StackEmpty(StackOperand))
            {
                cout << "表达式有误!" << endl;
                break;
            } else{
                // 弹出栈顶两个元素,进行操作
                ElemType RightOperand;   // 先弹栈为右操作数
                Pop(StackOperand, RightOperand);
                ElemType LeftOperand;   // 左操作数
                Pop(StackOperand, LeftOperand);
                ElemType temp = CalculateTwoOperand(LeftOperand, RightOperand, Postfix[i]);
                Push(StackOperand, temp);
            }
        }
    }
    ElemType outcome;
    // 最后结果在栈顶
    Pop(StackOperand, outcome);
    return outcome;
}
ElemType CalculateTwoOperand(ElemType Left, ElemType Right, char Operator)
{
    ElemType Temp;
    switch (Operator) {
        case '+':
            Temp = Left + Right;
            return Temp;
        case '-':
            Temp = Left - Right;
            return Temp;
        case '*':
            Temp = Left * Right;
            return Temp;
        case '/':
            Temp = Left / Right;
            return Temp;
        default:
            return 0;
    }
}
string JudgeOperation(char ch){
    // 操作数
    if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
    {
        return "Operand";
    }
    // 运算符
    if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
    {
        return "Operator";
    }
    // 括号
    if (ch == '(')
    {
        return "LeftBracket";
    }
    if (ch == ')')
    {
        return "RightBracket";
    }
    return "InvalidInput";
}
C++程序(顺序栈)
#include 
#define MaxSize 50
using namespace std;
// 顺序栈实现
typedef double ElemType;
typedef struct
{
    ElemType data[MaxSize];  // 存放栈中元素
    int top;    // 栈顶指针
}SqStack;
// 栈的基本操作
void InitStack(SqStack &S);
bool StackEmpty(SqStack S);
bool Push(SqStack &S, ElemType x);
bool Pop(SqStack &S, ElemType &x);
bool GetTop(SqStack S, ElemType &x);
// 后缀表达式的计算
ElemType CalculatePostfix(string Postfix);
ElemType CalculateTwoOperand(ElemType Left, ElemType Right, char Operator);
string JudgeOperation(char ch);    // 判断字符类型
int main()
{
    string Postfix = "5711+-/3*211++-";
    ElemType output = CalculatePostfix(Postfix);
    cout << output << endl;
    return 0;
}
// 初始化
void InitStack(SqStack &S)
{
    S.top = -1;
}
// 判栈空
bool StackEmpty(SqStack S)
{
    if (S.top == -1)
        return true;
    else
        return false;
}
// 入栈
bool Push(SqStack &S, ElemType x)
{
    if (S.top == MaxSize - 1)   // 栈满,报错
        return false;
    S.data[++S.top] = x;
    return true;
}
// 出栈
bool Pop(SqStack &S, ElemType &x)
{
    if (S.top == -1)
        return false;
    x = S.data[S.top--];
    return true;
}
// 读栈顶元素
bool GetTop(SqStack S, ElemType &x)
{
    if (S.top == -1)
        return false;
    x = S.data[S.top];
    return true;
}
ElemType CalculatePostfix(string Postfix)
{
    // 初始化操作数栈
    SqStack StackOperand;
    InitStack(StackOperand);
    // 遍历Postfix
    for (int i = 0; i < Postfix.length(); i++) {
        string OpSign = JudgeOperation(Postfix[i]);
        // 错误直接退出循环
        if (OpSign == "InvalidInput")
        {
            cout << "表达式有误!" << endl;
            break;
        }
        // 遇到操作数,压入栈中,进行下一次扫描,跳过后面的操作
        if (OpSign == "Operand")
        {
            ElemType temp = Postfix[i] - '0'; // 把char转 double数字
            Push(StackOperand, temp);
            continue;
        }
        // 遇到运算符
        if (OpSign == "Operator")
        {
            // 遇到运算符时,栈空,则表达式有误
            if (StackEmpty(StackOperand))
            {
                cout << "表达式有误!" << endl;
                break;
            } else{
                // 弹出栈顶两个元素,进行操作
                ElemType RightOperand;   // 先弹栈为右操作数
                Pop(StackOperand, RightOperand);
                ElemType LeftOperand;   // 左操作数
                Pop(StackOperand, LeftOperand);
                ElemType temp = CalculateTwoOperand(LeftOperand, RightOperand, Postfix[i]);
                Push(StackOperand, temp);
            }
        }
    }
    ElemType outcome;
    // 最后结果在栈顶
    Pop(StackOperand, outcome);
    return outcome;
}
ElemType CalculateTwoOperand(ElemType Left, ElemType Right, char Operator)
{
    ElemType Temp;
    switch (Operator) {
        case '+':
            Temp = Left + Right;
            return Temp;
        case '-':
            Temp = Left - Right;
            return Temp;
        case '*':
            Temp = Left * Right;
            return Temp;
        case '/':
            Temp = Left / Right;
            return Temp;
        default:
            return 0;
    }
}
string JudgeOperation(char ch){
    // 操作数
    if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
    {
        return "Operand";
    }
    // 运算符
    if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
    {
        return "Operator";
    }
    // 括号
    if (ch == '(')
    {
        return "LeftBracket";
    }
    if (ch == ')')
    {
        return "RightBracket";
    }
    return "InvalidInput";
}
运行结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号