当前位置:网站首页>【无标题】
【无标题】
2022-08-02 14:12:00 【nanocode】
C++实现中缀表达式转后缀表达式并计算结果
思路:
1.中缀转后缀
创建一个字符串存储后缀表达式,一个栈存储运算符
遍历中缀表达式,遇到运算符进栈,遇到数字直接添加到字符串中
进栈时,若当前运算符优先级比栈顶高,直接进栈,否则将栈顶元素添加到字符串中并退栈,直到遇见优先级低的运算符,若栈顶为‘(’,直接进栈,若当前运算符为‘)’,将栈顶元素添加到字符串中并退栈至‘(’【注意:此时‘(’ 不进入字符串但依旧需要将‘(’ 退栈】。
最后将栈中所有运算符依次添加到字符串中,清空栈,此时字符串即为后缀表达式
2.后缀计算结果
创建一个栈存储数字
遍历后缀表达式,遇到数字进栈,遇到运算符出栈两次,将这两个数字根据响应的运算符进行计算,将计算结果进栈。
最后栈中存下的数字即为结果。
源代码:
#include<bits/stdc++.h>
#include<stack>
using namespace std;
int cuo(char[]);
void _cuo(int a);
void systems(void);
int level(char ch) //计算各个运算符优先级
{
switch (ch)
{
case '+':
return 1;
case '-':
return 1;
case '*':
return 2;
case '/':
return 2;
case '^':
return 2;
default:
return 0;
}
}
string InToPost(string str) //中缀转后缀
{
stack<char> temp; //用于存储运算符的临时栈
string back = ""; //用于存储后缀表达式
for (int i = 0; i < str.length(); i++)
{
switch (str[i])
{
case '(': //若为'(',直接进栈
temp.push('(');
break;
case ')': //若为')',出栈至'('
while (!temp.empty() && temp.top() != '(')
{
back += temp.top();
temp.pop();
}
temp.pop();
break;
default:
if (str[i] >= '0' && str[i] <= '9') //若为数字,直接进字符串
{
if (str[i + 1] < '0' || str[i + 1] > '9')
{
back += str[i];
back += '#'; //数字结束后加#用以区分
}
else
{
back += str[i];
}
}
else if (temp.empty() || temp.top() == '(' || (temp.top() != '(' && level(str[i]) > level(temp.top()))) //若为运算符,且比栈顶运算符优先级高,则直接进栈
{
temp.push(str[i]);
}
else //否则出栈至遇到低优先级的运算符
{
while (!temp.empty() && level(str[i]) <= level(temp.top()))
{
back += temp.top();
temp.pop();
}
temp.push(str[i]);
}
break;
}
}
while (!temp.empty()) //将剩余的运算符出栈
{
back += temp.top();
temp.pop();
}
return back;
}
double Result(string back) //利用后缀表达式计算结果
{
double tempNum, num1, num2;
stack<double> tempStack; //用于存储数字的临时栈
for (int i = 0; i < back.length(); i++)
{
string tempStr = ""; //用于string转double的临时字符串
if (back[i] >= '0' && back[i] <= '9')
{
while (back[i] != '#')
{
tempStr += back[i];
i++;
}
tempNum = stod(tempStr);//string转double
tempStack.push(tempNum);//转换后入栈
}
else if (back[i] != '#')//遇到运算符后出栈两次,用num1和num2分别暂时存储
{
num1 = tempStack.top();
tempStack.pop();
num2 = tempStack.top();
tempStack.pop();
}
switch (back[i]) //遇到运算符就对num1和num2进行相应运算,用tempNum存储计算结果,并入栈
{
case '+':
tempNum = num2 + num1;
tempStack.push(tempNum);
break;
case '-':
tempNum = num2 - num1;
tempStack.push(tempNum);
break;
case '*':
tempNum = num2 * num1;
tempStack.push(tempNum);
break;
case '/':
tempNum = num2 / num1;
tempStack.push(tempNum);
break;
case '^':
tempNum = pow(num2,num1);
tempStack.push(tempNum);
break;
}
}
return tempStack.top();
}
int main()
{
XunHuan:
char str[100];
gets(str);
string back = InToPost(str);
cout<<back<< endl;
double r = Result(back);
cout<< "<< r;
goto XunHuan;
return 0;
}
拜拜
边栏推荐
猜你喜欢

Unity-Post Processing

剑指offer:反转链表

Redis常见面试题

Open the door of electricity "Circuit" (1): voltage, current, reference direction

VirtualLab Fusion中的可视化设置

倍增和稀疏表

Exotic curiosity-a solution looking - bit operations

泰伯效应.
![[System Design and Implementation] Flink-based distracted driving prediction and data analysis system](/img/f0/23ac631b6eb9b794224d8ae78e6523.png)
[System Design and Implementation] Flink-based distracted driving prediction and data analysis system

How to simulate 1/3 probability with coins, and arbitrary probability?
随机推荐
Use libcurl to upload the image of Opencv Mat to the file server, based on two methods of post request and ftp protocol
如何编辑VirtualLab Fusion结果的格式
光栅区域衍射级数和效率的规范
系统性能和TCP/UDP网络优化-学习大杂烩
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
2021-06-06
Happy, 9/28 scene collection
[System Design and Implementation] Flink-based distracted driving prediction and data analysis system
光学好书推荐
第二十九章:树的基本概念和性质
Unity-PlayMaker
第二十八章:解题技巧
Codeforces Round #605 (Div. 3)
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
Lightweight AlphaPose
队列与栈
Qt | 定时器的使用 QTimer
cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
Article pygame drag the implementation of the method
剑指offer:合并两个排序的链表