# 数据结构c++表达式计算

0

Time Limit: 1000 MS Memory Limit: 1000 KB

Description

Input

Output

Sample Input

2
1-2#
3*(1+2)#

Sample Output

-1
9

``````#include <iostream>
#include <string>
#include <stack>

using namespace std;

//操作符的栈内、外优先级
int isp(char x)//栈内
{
if (x == '*' || x == '/') return 5;
if (x == '+' || x == '-') return 3;
if (x == '(') return 1;
else return -1;
}
int icp(char x)//栈外
{
if (x == '*' || x == '/') return 4;
if (x == '+' || x == '-') return 2;
if (x == '(') return 6;
else return -1;
}

int main()
{
string infix, postfix;
stack<char> operators;
int T;
cin >> T;
char ch;
for (int q = 0; q < T; ++q)
{
cin >> infix;
for (int i = 0; infix[i] != '#'; i++)
{
ch = infix[i];
if (ch >= '0' && ch <= '9')
{//是操作数，输出
postfix.push_back(ch);
}
else if (operators.empty())
{//若是空的，让该操作符进栈
operators.push(ch);
}
else if (ch == ')')
{
while (operators.top() != '(' && !operators.empty())
{
postfix.push_back(operators.top());
operators.pop();
}
operators.pop();
}
else if (ch == '(')
{
operators.push(ch);
}
else if (ch == '+' || ch == '-'
|| ch == '*' || ch == '/')
{//是加减乘除
while (isp(operators.top()) >= icp(ch))
{
postfix.push_back(operators.top());
operators.pop();
if (operators.empty())
{
break;
}
}
operators.push(ch);
}
}
while (!operators.empty())
{
postfix.push_back(operators.top());
operators.pop();
}
//储存数字
stack<long long int> re;
//开始进行计算
char op;
for (int y = 0; y < postfix.size(); ++y)
{
op = postfix[y];
if (op >= '0' && op <= '9')
{
int l = op - '0';
re.push(l);
}
else
{
//出栈顶两个数字
long long int a = re.top();
re.pop();
long long int b = re.top();
re.pop();
if (op == '+')
{//加
re.push(a + b);
}
else if (op == '-')
{//减
re.push(b - a);
}
else if (op == '*')
{//乘
re.push(a * b);
}
else if (op == '/')
{//除
re.push(b / a);
}
}
}
long long int result = re.top();
cout << result << endl;
infix.clear();
postfix.clear();
}
return 0;
}``````

0

``````#include <iostream>
#include <string>
#include <stack>

using namespace std;

//操作符的栈内、外优先级
int isp(char x)//栈内
{
if (x == '*' || x == '/') return 5;
if (x == '+' || x == '-') return 3;
if (x == '(') return 1;
else return -1;
}
int icp(char x)//栈外
{
if (x == '*' || x == '/') return 4;
if (x == '+' || x == '-') return 2;
if (x == '(') return 6;
else return -1;
}

int main()
{
//postfix的存储中，一个数字结束 用.来表示
string infix, postfix;
stack<char> operators;
int T;
cin >> T;
char ch;
for (int q = 0; q < T; ++q)
{
cin >> infix;
for (int i = 0; infix[i] != '#'; i++)
{
ch = infix[i];
if (ch >= '0' && ch <= '9')
{//是操作数，输出
postfix.push_back(ch);
}
else if (operators.empty())
{//若是空的，让该操作符进栈
operators.push(ch);
postfix.push_back('.');
}
else if (ch == ')')
{
while (operators.top() != '(' && !operators.empty())
{
postfix.push_back('.');
postfix.push_back(operators.top());
operators.pop();
}
operators.pop();
}
else if (ch == '(')
{
postfix.push_back('.');
operators.push(ch);
}
else if (ch == '+' || ch == '-'
|| ch == '*' || ch == '/')
{//是加减乘除
postfix.push_back('.');
while (isp(operators.top()) >= icp(ch))
{
postfix.push_back(operators.top());
operators.pop();
if (operators.empty())
{
break;
}
}
operators.push(ch);
}
}
while (!operators.empty())
{
postfix.push_back(operators.top());
operators.pop();
}
//储存数字
stack<long long int> re;
//re顶的数字与正在读的数字是否属于一个数字
bool iN = true;
//开始进行计算
char op;
for (int y = 0; y < postfix.size(); ++y)
{
op = postfix[y];
if ((op >= '0' && op <= '9'))
{
int l = op - '0';
if (iN == true)
{//是新的数字，直接入栈
re.push(l);
iN = false;
}
else
{//不是新的数字
l += re.top() * 10;
re.pop();
re.push(l);
}
}
else if (op == '.')
{//前一个数字结束
iN = true;
}
else
{
//出栈顶两个数字
long long int a = re.top();
re.pop();
long long int b = re.top();
re.pop();
if (op == '+')
{//加
re.push(a + b);
iN = true;
}
else if (op == '-')
{//减
re.push(b - a);
iN = true;
}
else if (op == '*')
{//乘
re.push(a * b);
iN = true;
}
else if (op == '/')
{//除
re.push(b / a);
iN = true;
}
}
}
long long int result = re.top();
cout << result << endl;
infix.clear();
postfix.clear();
}
return 0;
}
``````

Good work!

blogger2020 | 园豆：237 (菜鸟二级) | 2021-11-25 09:48

1

2.warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
81 | for (int y = 0; y < postfix.size(); ++y)
|
I changed to: for (int y = 0; y < (int)postfix.size(); ++y) - if that is OK with you.
3. After above fixing, compiler worked:
g++ expression.cpp -o exp -Wall
4. running your code on ubuntu20
Test1: worked:
ora01@ubuntu20:~/projects/test\$ ./exp
2
1-2#
-1
3*(1+2)#
9

Test2: failed:
ora01@ubuntu20:~/projects/test\$ ./exp
2
12+(3+9)-3#
11
5+4
3-220/20
Segmentation fault (core dumped)
Remarks: You might want to add check on each line with '#'.
If '#' is not found, you might want to consider the input line is not valid.

Test3: failed
ora01@ubuntu20:~/projects/test\$ ./exp
2
12+37-3#
20
5+3*2-220/20#
Floating point exception (core dumped)
Remarks:
It may have issues on calculation.

Hope this helps.
And let us know if you have any further issue please, if possible.
Thanks,

blogger2020 | 园豆：237 (菜鸟二级) | 2021-10-19 20:00

Sorry about the first one, I missed a line when I copied. It has now been corrected in the question.
On the second issue, although I don't understand what you're trying to do, it didn't work.

1

runtime error是指运行时错误吧，应该是数组越界或者类似的错误

您需要登录以后才能回答，未注册用户请先注册