2 dayugenhao4 dayugenhao4 于 2015.05.30 21:52 提问

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个回答

dayugenhao4
dayugenhao4   2015.05.30 21:55

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

dayugenhao4
dayugenhao4   2015.05.30 21:57

怎么include后面的东西发不出的

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!