dsbanana 2015-05-30 13:52 采纳率: 0%
浏览 4096

c++程序运行时出现deque iterator not dereferencable

我在做一个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的运算,要怎么做?多位数的后缀表达式怎么运算?

  • 写回答

2条回答 默认 最新

  • dsbanana 2015-05-30 13:55
    关注

    #include
    #include
    #include
    #include
    明明是这个,发出来却是这样:
    #include
    #include
    #include
    #include
    搞什么鬼啊

    评论

报告相同问题?

悬赏问题

  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问