CCF认证-201903-2 二十四点提示运行错误。。。求大佬解决

写bug战神 2019-08-28 04:06:23
#include<iostream>
#include<stack>
using namespace std;
bool cmp(char top, char out)
{
if ((top == '+' || top == '-') && (out == '+' || out == '-'))
return true;
else if ((top == 'x' || top == '/') && (out == 'x' || out == '/'))
return true;
else if ((top == 'x' || top == '/') && (out == '+' || out == '-'))
return true;
else
return false;
}
int calculate(int nx, int nh, char sign)
{
switch (sign)
{
case '+': return nx + nh;break;
case '-': return nx - nh;break;
case 'x': return nx * nh;break;
case '/': return nx / nh;break;
}
}
char s[8];
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
stack<int> num;
stack<char> sym;
while (!num.empty())
num.pop();
while (!sym.empty())
sym.pop();
for (int j = 0; j < 8; j++)
{
scanf("%c", &s[j]);
if (s[j] != '\n')
{
if (s[j] >= '0' && s[j] <= '9')
num.push(s[j] - '0');
else if (sym.empty())
{
sym.push(s[j]);
}
else
{
if (cmp(sym.top(), s[j]))
{
while (cmp(sym.top(), s[j]))
{
int nh = num.top();
num.pop();
int nx = num.top();
num.pop();
num.push(calculate(nx, nh, sym.top()));
sym.pop();
}
sym.push(s[j]);
}
else
sym.push(s[j]);
}
}
}
while (!sym.empty())
{
int nh = num.top();
num.pop();
int nx = num.top();
num.pop();
num.push(calculate(nx, nh, sym.top()));
sym.pop();
}
if (num.top() == 24)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}

...全文
294 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
写bug战神 2019-08-28
  • 打赏
  • 举报
回复
谢谢,我感觉我的问题应该是栈溢出了
  • 打赏
  • 举报
回复
我估计这是史上最简洁的二十四点求值(没时间仔细测试,不保证正确):

#include <stdio.h>

int main()
{
int n, x[4], p, q, s;
char o[3];

scanf("%d", &n);
while (n--)
{
scanf("%1d%c%1d%c%1d%c%1d", &x[0], &o[0], &x[1], &o[1], &x[2], &o[2], &x[3]);
for (p = q = s = 0; p < 3; p++)
o[p] == 'x' ? x[p + 1] = s = x[p] *= x[p + 1], o[p] = 0, q++ :
o[p] == '/' ? x[p + 1] = s = x[p] /= x[p + 1], o[p] = 0, q++ : 0;
for (p = 0; q < 3; p++)
o[p] == '+' ? x[p + 1] = s = x[p] += x[p + 1], o[p] = 0, q++ :
o[p] == '-' ? x[p + 1] = s = x[p] -= x[p + 1], o[p] = 0, q++ : 0;
if (s == 24) puts("Yes"); else puts("No");
}

return 0;
}

赵4老师 2019-08-28
  • 打赏
  • 举报
回复
引用 2 楼 KhanCS 的回复:
引用 1 楼 早打大打打核战争 的回复:
预测老赵正在迅速赶来~~~
赵4老师 2019-08-28
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
写bug战神 2019-08-28
  • 打赏
  • 举报
回复
引用 3 楼 赵4老师 的回复:
https://bbs.csdn.net/topics/380157851
什么意思呀老师,这个代码输入到第八个算式的时候就崩了……不知道为什么
赵4老师 2019-08-28
  • 打赏
  • 举报
回复
写bug战神 2019-08-28
  • 打赏
  • 举报
回复
引用 1 楼 早打大打打核战争 的回复:
预测老赵正在迅速赶来~~~
  • 打赏
  • 举报
回复
预测老赵正在迅速赶来~~~

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧