基于栈的中缀算术表达式求值 用c++语言 题目描述 本关任务:输入一个中缀算术表达式,求解表达式的值。运算符包括+、-、*、/、(、)、=,参加运算的数为double类型且为正数。(要求:直接针对中缀算术表达式进行计算,不能转换为后缀或前缀表达式再进行计算,只考虑二元运算即可。) 输入要求 多组数据,每组数据一行,对应一个算术表达式,每个表达式均以“=”结尾。当表达式只 有一个“=”时,输入结束。参加运算的数为double类型。 输出要求 对于每组数据输出1行,为表达式的运算结果。输出保留两位小数。 输入样例 2+2= 20*(4.5-3)=
以下是使用 C++ 实现基于栈的中缀算术表达式求值的代码: ```cpp #include <iostream> #include <stack> #include <string> #include <cctype> double calculate(double num1, double num2, char op) { switch (op) { case '+': return num1 + num2; case '-': return num1 - num2; case '*': return num1 * num2; case '/': if (num2!= 0) { return num1 / num2; } else { std::cout << "除数不能为 0" << std::endl; return 0; } } return 0; } double evaluateInfixExpression(std::string expression) { std::stack<double> operandStack; std::stack<char> operatorStack; for (int i = 0; i < expression.size(); i++) { if (isdigit(expression[i]) || expression[i] == '.') { std::string numStr; while (i < expression.size() && (isdigit(expression[i]) || expression[i] == '.')) { numStr += expression[i++]; } i--; operandStack.push(atof(numStr.c_str())); } else if (expression[i] == '(') { operatorStack.push(expression[i]); } else if (expression[i] == ')') { while (operatorStack.top()!= '(') { double num2 = operandStack.top(); operandStack.pop(); double num1 = operandStack.top(); operandStack.pop(); char op = operatorStack.top(); operatorStack.pop(); operandStack.push(calculate(num1, num2, op)); } operatorStack.pop(); } else if (expression[i] == '+' || expression[i] == '-') { while (!operatorStack.empty() && operatorStack.top()!= '(') { double num2 = operandStack.top(); operandStack.pop(); double num1 = operandStack.top(); operandStack.pop(); char op = operatorStack.top(); operatorStack.pop(); operandStack.push(calculate(num1, num2, op)); } operatorStack.push(expression[i]); } else if (expression[i] == '*' || expression[i] == '/') { while (!operatorStack.empty() && (operatorStack.top() == '*' || operatorStack.top() == '/')) { double num2 = operandStack.top(); operandStack.pop(); double num1 = operandStack.top(); operandStack.pop(); char op = operatorStack.top(); operatorStack.pop(); operandStack.push(calculate(num1, num2, op)); } operatorStack.push(expression[i]); } } while (!operatorStack.empty()) { double num2 = operandStack.top(); operandStack.pop(); double num1 = operandStack.top(); operandStack.pop(); char op = operatorStack.top(); operatorStack.pop(); operandStack.push(calculate(num1, num2, op)); } return operandStack.top(); } int main() { std::string expression; while (true) { std::getline(std::cin, expression); if (expression == "=") { break; } double result = evaluateInfixExpression(expression.substr(0, expression.size() - 1)); std::printf("%.2f\n", result); } return 0; } ``` 这段代码实现了对中缀表达式的求值,通过两个栈分别存储操作数和操作符,并按照运算符的优先级进行计算。 [2024-10-26 20:10:49 | AI写代码神器 | 872点数解答]
- [字节豆包] dim caizhi1 as string dim caizhi2 as string dim caizhi as string dim dianzulv1 as double dim dianzulv2 as double dim midu1 as double dim midu2 as double dim zhijing as double dim jiejubi as double dim jiemianji as double dim zhouchang as double dim dianzusichangdu as double dim zhongliang as double dim quanshu as double dim jieju as double dim dianzulv as double dim midu as double dim zuzhi as double private sub command1_click() dim xlapp as object dim xlworkbook as object dim xlworkshee(32点数解答 | 2024-10-31 15:19:51)230
- [字节豆包] // rod6.cpp : defines the entry point for the console application. // #include "stdafx.h" // consoleapplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <stdio.h> #include <math.h> #include <iostream> #include <conio.h> #include <cstdlib> using namespace std; double ro[10001]; double th[10001]; double e[10001]; double a[10001]; double b[10001]; double y[10001]; double x[10001]; double d[10001]; double d[10001]; double da[10001]; double db[10001]; double dc[10001]; double z[10001]; d(55点数解答 | 2024-08-16 15:22:27)251
- [字节豆包] 递归下降语法分析程序举例 本例文法: g[<表达式>]: g[e]: <表达式>→<表达式>+<项>|<表达式>-<项>|<项> e→e+t|t <项>→<项>*<因式>|<项>/<因式>|<因式> t→t*f|f <因式>→<标识符>|<无符号整数>|(<表达式>) f→i|(e) #include <stdio.h> #include <dos.h> #include <stdlib.h> #include <string.h> char a[50],b[50],d[200],e[10];/*数组a存输入串,数组d存推导式,*/ char ch; int n1,i1=0,flag=1,n=5; /*flag=1处理非终结符,flag=0处理终结符*/ int total=0; /*步骤计数器*/ int e(); int e1(); int t(); int g();/*e’*/ int s();/*t’*/ int f(); void input(); void input1((1371点数解答 | 2024-12-12 22:46:04)195
- [字节豆包] #define _crt_secure_no_warnings #include <iostream> #include <cstdlib> #include <cmath> #include <vector> #include <cstdio> using namespace std; // 你的代码... const int nx = 784, nb = 500, ny = 10;//输入层x,隐藏层b,输出层y节点数 const double eta = 0.1;//学习率 struct node { double val{}; double bias{}; vector<double> weight; } x[nx], b[nb], y[ny];//输入层x,隐藏层b,输出层y double g[ny], e[nb];//用于反向传播 double trainx[nx], trainy[ny];//训练数据 double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); } double(34点数解答 | 2024-09-06 08:43:52)297
- [GPT] #define _crt_secure_no_warnings #include <iostream> #include <cstdlib> #include <cmath> #include <vector> #include <cstdio> using namespace std; // 你的代码... const int nx = 784, nb = 500, ny = 10;//输入层x,隐藏层b,输出层y节点数 const double eta = 0.1;//学习率 struct node { double val{}; double bias{}; vector<double> weight; } x[nx], b[nb], y[ny];//输入层x,隐藏层b,输出层y double g[ny], e[nb];//用于反向传播 double trainx[nx], trainy[ny];//训练数据 double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); } double(1309点数解答 | 2024-09-06 08:46:09)313
- [阿里通义] #include <stdio.h> #include <math.h> #define pi 3.14159265358979323846 double meridianarclength(double b) { double a = 6378140.0; double e2 = 0.00669438002290; double m = a * (1 - e2) * (1 - e2 * sin(b) * sin(b)) * pow(1 - e2 * sin(b) * sin(b), 1.5); double n = a / sqrt(1 - e2 * sin(b) * sin(b)); return m + n * sin(b) * cos(b) * pow(tan(b),2) /2 + n * sin(b) * pow(cos(b), 3) * (5 - pow(tan(b), 2) + 9 * e2 * pow(cos(b), 2)) * pow(tan(b), 4) / 24; } double calculatebaselatitude(double x, double y)(565点数解答 | 2024-11-21 13:08:38)199
- [字节豆包] #include <stdio.h> #include <math.h> #define pi 3.14159265358979323846 double meridianarclength(double b) { double a = 6378140.0; double e2 = 0.00669438002290; double m = a * (1 - e2) * (1 - e2 * sin(b) * sin(b)) * pow(1 - e2 * sin(b) * sin(b), 1.5); double n = a / sqrt(1 - e2 * sin(b) * sin(b)); return m + n * sin(b) * cos(b) * pow(tan(b),2) /2 + n * sin(b) * pow(cos(b), 3) * (5 - pow(tan(b), 2) + 9 * e2 * pow(cos(b), 2)) * pow(tan(b), 4) / 24; } double calculatebaselatitude(double x,(48点数解答 | 2024-11-21 13:05:21)194
- [字节豆包] 题目描述 现在给出一排共 n 只鹅的身高,李白想知道最高的鹅比其他所有鹅高多少、最矮的鹅 比其他所有鹅矮多少。 请输出这两行信息。 输入格式 输入共两行。 第一行一个整数 n 表示鹅的数目。 第二行共 n 个整数 ai(i=1,2,3...n),表示第 i 只鹅的身高。 输出格式 输出共两行。 第一行共 n 个空格隔开的整数,第 i 个整数表示序列中第 i 只鹅比最高的那只鹅要 矮多少。 第二行共 n 个空格隔开的整数,第 i 个整数表示序列中第 i 只鹅比最矮的那只鹅要 高多少。 输入输出样例 输入 #1 6 4 7 8 6 3 2 输出 #1 4 1 0 2 5 6 2 5 6 4 1 0 说明/提示 李白一共有 6 只鹅,最高的一只身高为 8,最矮的一只身高为 2,然后分别作为被减 数和减数参与身高差计算可得结果。 对于 30% 数据,保证 0≤ai≤30,1≤n≤20。 对于 100% 数据,保证 0≤ai≤1018,1≤n≤106。 用c++语言(549点数解答 | 2025-11-16 20:19:06)25
- [GPT] ```csharp using system; interface igetmoney { double callfee(int minutes); } class acard : igetmoney { private const double initialfee =100; private const double feeperminute = 0.1; private const int freeminutes = 3; private double balance; public acard() { balance = initialfee; } public double callfee(int minutes) { double fee = 0; if (minutes <= freeminutes) { fee = 0; } else { fee = (minutes - freeminutes) * feeperminute; } balance -= fee; return fee; } public double getbalance() { re(291点数解答 | 2024-05-27 16:22:31)288
- [阿里通义] #include<stdio.h> int main() { double r, h; //定义变量r(半径)和h(高度) const double pi = 3.1415926; //定义常量pi // 输入半径r和高度h printf("请输入圆的半径 r 和圆柱的高 h (例如: 5 10): "); scanf("%lf %lf", &r, &h); // 计算各项值 double circumference = 2 * pi * r; //圆的周长 double circle_area = pi * r * r; // 圆的面积 double sphere_surface_area = 4 * pi * r * r; // 圆球的表面积 double sphere_volume = (4.0 / 3) * pi * r * r * r; //圆球的体积 double cylinder_volume = circle_area * h; // 圆柱的体积 // 输出结果,保留两位小数 printf("圆的周长: %.2f\n", circumference); prin(668点数解答 | 2024-10-19 10:58:06)243
- [阿里通义] #include <stdio.h> int main() { double r, h; // 定义变量r(半径)和h(高度) const double pi =3.1415926; //定义常量pi // 输入半径r和高度h printf("请输入圆的半径 r 和圆柱的高 h (例如: 5 10): "); scanf("%lf %lf", &r, &h); // 计算各项值 double circumference = 2 * pi * r; // 圆的周长 double circle_area = pi * r * r; // 圆的面积 double sphere_surface_area =4 * pi * r * r; // 圆球的表面积 double sphere_volume = (4.0 / 3) * pi * r * r * r; //圆球的体积 double cylinder_volume = circle_area * h; // 圆柱的体积 // 输出结果,保留两位小数 printf("圆的周长: %.2f\n", circumference); pri(542点数解答 | 2024-10-19 11:00:12)200
- [字节豆包] 本题要求实现一个函数,用下列公式求cos(x)的近似值,精确到最后一项的绝对值小于e: cos(x)=x 0 /0!−x 2 /2!+x 4 /4!−x 6 /6!+⋯ 函数接口定义: double funcos( double e, double x ); 其中用户传入的参数为误差上限e和自变量x;函数funcos应返回用给定公式计算出来、并且满足误差要求的cos(x)的近似值。输入输出均在双精度范围内。 裁判测试程序样例: #include <stdio.h> #include <math.h> double funcos( double e, double x ); int main() { double e, x; scanf("%lf %lf", &e, &x); printf("cos(%.2f) = %.6f\n", x, funcos(e, x)); return 0; } /* 你的代码将被嵌在这里 */(144点数解答 | 2024-11-18 21:13:12)237