c++输入字符串计算出算式结果
时间: 2023-02-07 17:50:29 浏览: 15
你可以使用 C 语言的 eval() 函数来计算输入的字符串表达式的结果。 eval() 函数定义在头文件 stdlib.h 中,使用时需要包含该头文件。
例如,如果要计算输入字符串 "2 + 3" 的结果,可以使用如下代码:
```
#include <stdlib.h>
#include <stdio.h>
int main() {
char input[] = "2 + 3";
int result = eval(input);
printf("%d\n", result); // 输出 5
return 0;
}
```
你也可以使用其他 C 语言函数,如 atoi() 或者 sscanf(),来手动解析字符串并计算结果。不过这需要自己写代码来解析字符串并计算结果。
相关问题
cpp输入字符串计算出算式结果
你可以使用 C++ 的字符串输入函数来获取输入的字符串,然后将其转换为适当的数据类型,再使用 C++ 的数学运算函数来计算结果。
下面是一个例子,假设输入的字符串是 "3+4*5":
```
#include <iostream>
#include <string>
int main() {
std::string input_str;
std::cin >> input_str;
// 使用字符串流来解析输入的字符串
std::stringstream input_ss(input_str);
int result = 0;
char op;
int num;
input_ss >> result; // 读取第一个数字
while (input_ss >> op >> num) { // 读取运算符和数字
if (op == '+') {
result += num;
} else if (op == '-') {
result -= num;
} else if (op == '*') {
result *= num;
} else if (op == '/') {
result /= num;
}
}
std::cout << "结果是: " << result << std::endl;
return 0;
}
```
这段代码会使用字符串流来解析输入的字符串,并逐个读取运算符和数字,最终将结果输出到控制台。
如果你想要支持更复杂的算式,你可以使用递归下降解析器或者可编程计算器库,比如 Boost.Spirit。
c++输入算式计算结果
以下是一个简单的 C++ 程序,可以读入一个算式并计算结果:
```cpp
#include <iostream>
#include <stack>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string expression;
cout << "Enter an expression: ";
getline(cin, expression);
stack<int> operands;
stack<char> operators;
for (int i = 0; i < expression.length(); i++) {
if (expression[i] == ' ') {
continue;
} else if (expression[i] >= '0' && expression[i] <= '9') {
string operand = "";
while (i < expression.length() && expression[i] >= '0' && expression[i] <= '9') {
operand += expression[i];
i++;
}
i--;
operands.push(stoi(operand));
} else if (expression[i] == '(') {
operators.push(expression[i]);
} else if (expression[i] == ')') {
while (!operators.empty() && operators.top() != '(') {
int op2 = operands.top();
operands.pop();
int op1 = operands.top();
operands.pop();
char op = operators.top();
operators.pop();
int result;
if (op == '+') {
result = op1 + op2;
} else if (op == '-') {
result = op1 - op2;
} else if (op == '*') {
result = op1 * op2;
} else if (op == '/') {
result = op1 / op2;
}
operands.push(result);
}
operators.pop();
} else {
while (!operators.empty() && ((expression[i] == '*' || expression[i] == '/') ||
(operators.top() == '+' || operators.top() == '-'))) {
int op2 = operands.top();
operands.pop();
int op1 = operands.top();
operands.pop();
char op = operators.top();
operators.pop();
int result;
if (op == '+') {
result = op1 + op2;
} else if (op == '-') {
result = op1 - op2;
} else if (op == '*') {
result = op1 * op2;
} else if (op == '/') {
result = op1 / op2;
}
operands.push(result);
}
operators.push(expression[i]);
}
}
while (!operators.empty()) {
int op2 = operands.top();
operands.pop();
int op1 = operands.top();
operands.pop();
char op = operators.top();
operators.pop();
int result;
if (op == '+') {
result = op1 + op2;
} else if (op == '-') {
result = op1 - op2;
} else if (op == '*') {
result = op1 * op2;
} else if (op == '/') {
result = op1 / op2;
}
operands.push(result);
}
cout << "Result: " << operands.top() << endl;
return 0;
}
```
这个程序使用了两个栈,一个用于存储操作数,另一个用于存储操作符。遍历输入的算式字符串,如果是数字,则将其转换为整数并压入操作数栈;如果是左括号,则将其压入操作符栈;如果是右括号,则弹出操作符栈中的操作符和操作数栈中的操作数进行计算,直到遇到左括号;如果是操作符,则弹出操作符栈中的操作符和操作数栈中的操作数进行计算,直到操作符栈为空或者栈顶操作符的优先级小于等于当前操作符的优先级。最终,操作数栈中剩下的一个元素就是算式的结果。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)