我在做一个c++的二十四点游戏,现在只是在做1~9的运算,代码如下:
#include
#include
#include
#include
using namespace std;
unsigned int i;
int main()
{
stack n1;
stack n2;
stack n3;
int m;
void houzhui(string,stack &, stack &);
int calculate(stack &, stack &);
srand((unsigned)time(NULL));
cout << rand() % 9 + 1 << " " << rand() % 9 + 1 << " " << rand() % 9 + 1 << " " << rand() % 9 + 1 << endl;
string shizi;
cin >> shizi;
houzhui(shizi, n1, n2);
m = calculate(n1, n3);
if (m != -1)
{
if (n3.top() == 24)
cout << "结果正确" << endl;
else
cout << "结果错误" << endl;
}
else
cout << "计算过程中除数出现0!" << endl;
system("pause");
}
void houzhui(string shizi, stack &n1, stack &n2)
{
int check(char);
for (i = 0; i < shizi.size(); i++)
{
if (shizi[i] >= '1' && shizi[i] <= '9')
n1.push(shizi[i]);
else
{
if (n2.empty())
n2.push(shizi[i]);
else
{
if (shizi[i] == ')')
{
while (n2.top() != '(')
{
n1.push(n2.top());
n2.pop();
}
n2.pop();
}
if (check(shizi[i]) < check(n2.top()))
{
if (n2.top() != '(')
{
do
{
n1.push(n2.top());
n2.pop();
if (n2.empty())
break;
} while (check(shizi[i]) <= check(n2.top()));
n2.push(shizi[i]);
}
n2.push(shizi[i]);
}
else
n2.push(shizi[i]);
}
}
}
while (!n2.empty())
{
n1.push(n2.top());
n2.pop();
}
}
int calculate(stack &n1, stack &n3)
{
char c[100];
double s1, s2, s;
c[n1.size()] = '\0';
for (i = n1.size() - 1; i >= 0; i--)
{
c[i] = n1.top();
n1.pop();
}
for (i = 0; c[i]!='\0'; i++)
{
if (c[i] >= '1'&&c[i] <= '9')
n3.push(c[i]-'0');
else
{
if (c[i] != '/')
{
switch (c[i])
{
case '+':s2 = n3.top(); n3.pop(); s1 = n3.top(); n3.pop(); s = s1 + s2; n3.push(s); break;
case '-':s2 = n3.top(); n3.pop(); s1 = n3.top(); n3.pop(); s = s1 - s2; n3.push(s); break;
case '*':s2 = n3.top(); n3.pop(); s1 = n3.top(); n3.pop(); s = s1*s2; n3.push(s); break;
}
}
else
{
s2 = n3.top();
if (s2 != 0)
{
n3.pop();
s1 = n3.top();
n3.pop();
s = s1 / s2;
n3.push(s);
}
else
return -1;
}
}
}
return 0;
}
int check(char c)
{
if (c == '(' || c == ')')
return 1;
if (c == '*' || c == '/')
return 0;
if (c == '+' || c == '-')
return -1;
return -1;
}
为何运行时出现错误deque iterator not dereferencable的?纠结了好多天,还有如果要添加上10~13的运算,要怎么做?多位数的后缀表达式怎么运算?
c++程序运行时出现deque iterator not dereferencable
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答
- dsbanana 2015-05-30 13:55关注
#include
#include
#include
#include
明明是这个,发出来却是这样:
#include
#include
#include
#include
搞什么鬼啊解决 无用评论 打赏 举报
悬赏问题
- ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
- ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
- ¥20 腾讯企业邮箱邮件可以恢复么
- ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
- ¥15 错误 LNK2001 无法解析的外部符号
- ¥50 安装pyaudiokits失败
- ¥15 计组这些题应该咋做呀
- ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
- ¥15 让node服务器有自动加载文件的功能
- ¥15 jmeter脚本回放有的是对的有的是错的