一本通 1356:计算(calc) (栈,中缀表达式求值)

这篇博客介绍如何解决一个计算密码的问题,其中涉及到将中缀表达式转换为后缀表达式,并使用栈来求解表达式的值。通过解析算术运算符的优先级,实现从左括号到右括号的匹配,以及运算符的正确处理,最终得出密码。示例展示了如何处理包含加减乘除和幂运算的复杂表达式。
摘要由CSDN通过智能技术生成

【题目描述】
小明在你的帮助下,破密了Ferrari设的密码门,正要往前走,突然又出现了一个密码门,门上有一个算式,其中只有“(”,“)”,“0-9”,“+”,“-”,“*”,“/”,“^”,求出的值就是密码。小明数学学得不好,还需你帮他的忙。(“/”用整数除法)

【输入】
共1行,为一个算式。

【输出】
共1行,就是密码。

【输入样例】
1+(3+2)(7^2+69)/(2)
【输出样例】
258

1、先把中缀表达式转为 后缀表达式。后缀表达式求值很简单。
2、中缀转 后缀的算法
(a)如果遇到一个数,就输出这个数
(b)如果遇到左括号,加入栈中
©如果遇到右括号,不断取出栈顶元素,直到栈顶为左括号。左括号也出栈。
(d)如果遇到运算符,栈顶运算符的优先级大于等于 新符号,则不断取出栈顶运算符并输出。
最后把新符号进栈。
3、运算符优先级
左括号 < 加减 < 乘除 < 幂
priority[0 + ‘(’] = 0, priority[0 + ‘+’] = 1, priority[0 + ‘-’] = 1;
priority[0 + ‘*’] = 2, priority[0 + ‘/’] = 2, priority[0 + ‘^’] = 3;

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <cctype>
#include <string>
#include <vector>
using namespace std;
const int MaxN = 100010;
char str[MaxN];
int priority[300];
stack<char> s;
vector<string> vec;

void handle(char ch)
{
   
	if(ch == '(')
	{
   
		s.push('(');	
	}else if(ch == ')'){
   
		while(!s.empty
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用实现复杂表达式求值C++代码: ```c++ #include <iostream> #include <stack> #include <string> #include <cstring> #include <cmath> using namespace std; // 优先级比较函数 int cmp(char op1, char op2) { if (op1 == '(' || op2 == '(') { return -1; } if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) { return -1; } else { return 1; } } // 计算函数 double calc(double num1, double num2, char op) { switch (op) { case '+': return num1 + num2; case '-': return num1 - num2; case '*': return num1 * num2; case '/': return num1 / num2; case '^': return pow(num1, num2); default: return 0; } } // 将中缀表达式转成后缀表达式 string infixToPostfix(string infix) { stack<char> s; string postfix; for (int i = 0; i < infix.length(); i++) { char c = infix[i]; if (isdigit(c) || c == '.') { // 数字和小数点直接输出 postfix += c; continue; } postfix += ' '; // 操作符之间加空格 while (!s.empty() && cmp(s.top(), c) == 1) { // 顶元素优先级大于等于当前操作符 postfix += s.top(); s.pop(); postfix += ' '; } if (c == ')') { // 如果是右括号,弹出直到左括号 while (!s.empty() && s.top() != '(') { postfix += s.top(); s.pop(); postfix += ' '; } s.pop(); } else { // 其他情况直接入 s.push(c); } } while (!s.empty()) { postfix += ' '; postfix += s.top(); s.pop(); } return postfix; } // 计算后缀表达式 double calcPostfix(string postfix) { stack<double> s; char buf[100]; for (int i = 0; i < postfix.length(); i++) { char c = postfix[i]; if (isdigit(c) || c == '.') { // 数字直接入 int j = i, k = 0; while (isdigit(postfix[j]) || postfix[j] == '.') { buf[k++] = postfix[j++]; } buf[k] = '\0'; double num = atof(buf); s.push(num); i = j - 1; } else if (c != ' ') { // 操作符弹出顶元素计算 double num2 = s.top(); s.pop(); double num1 = s.top(); s.pop(); s.push(calc(num1, num2, c)); } } return s.top(); } int main() { string infix; cout << "请输入中缀表达式:"; getline(cin, infix); string postfix = infixToPostfix(infix); cout << "后缀表达式为:" << postfix << endl; double result = calcPostfix(postfix); cout << "计算结果为:" << result << endl; return 0; } ``` 这个代码过将中缀表达式转换为后缀表达式,然后使用计算后缀表达式的值来实现复杂表达式求值。其中,`cmp`函数用于比较两个操作符的优先级,`calc`函数用于计算两个数和一个操作符的结果,`infixToPostfix`函数用于将中缀表达式转换为后缀表达式,`calcPostfix`函数用于计算后缀表达式的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值