求解决该问题
该问题出现在我自己实现stack
的代码中
```c++
#include <iostream>
#include <string>
using namespace std;
class Stack{
private:
int stack[1000];
int index_to_push;
int length;
public:
Stack(){
index_to_push = 0;
};
void push(int element){
stack[index_to_push] = element;
index_to_push++;
};
int pop() {
index_to_push--;
return stack[index_to_push];
};
int isEmpty() {
if ( index_to_push == 0 ) {
return 1;
}
return 0;
};
};
void SolveA() {
string s;
Stack st;
for ( int i = 0; i < 1000; i++ ) {
getline(cin, s);
//cout << s << endl;
if ( i < 10 ) cout << "read in s: " << s << "!" << endl;
if ( s == "pop" ) {
cout << "poping" << endl;
if ( st.isEmpty() ) {
cout << "false";
return;
}
//cout << st.pop() << " ";
}
else if ( s.substr(0,4) == "push" ) {
cout << "pushing" << endl;
int num = atoi(s.substr(5, s.length()-5).c_str());
st.push(num);
}
fflush(stdin);
}
}
int main() {
SolveA();
return 0;
}
在console里的输出
push 6
read in s: push 6!
pushing
push 9
read in s: push 9!
pushing
pop
read in s: pop!
poping
pop
read in s: pop!
poping
push 12
read in s: push 12!
pushing
push 6
read in s: push 6!
pushing
push 1
read in s: push 1!
pushing
pop
read in s: pop!
poping
pop
read in s: pop!
poping
read in s: !
直接把输入作为文件导入的输出
push 6
push 9
pop
pop
push 12
push 6
push 1
pop
pop
!ead in s: push 6
pushing
!ead in s: push 9
pushing
!ead in s: pop
!ead in s: pop
!ead in s: push 12
pushing
!ead in s: push 6
pushing
!ead in s: push 1
pushing
!ead in s: pop
read in s: pop!
poping
read in s: pop!
poping
poping
poping
poping
poping
poping
poping
poping
poping
poping
...
在此,可以发现两个问题