为什么我的"while"条件没有得到满足?
Why is my "while" condition not being met?
我一直在运行这个并输入"12+"作为表达式。当它到达while循环时,它就卡住了,好像条件从未满足过一样。我不明白为什么会这样,因为在while循环之前"it"等于2,所以循环甚至不应该被使用。
//array based stack implementation
class Stack
{
private:
int capacity; //max size of stack
int top; //index for top element
char *listArray; //array holding stack elements
public:
Stack (int size = 50){ //constructor
capacity = size;
top = 0;
listArray = new char[size];
}
~Stack() { delete [] listArray; } //destructor
void push(char it) { //Put "it" on stack
listArray[top++] = it;
}
char pop() { //pop top element
return listArray [--top];
}
char& topValue() const { //return top element
return listArray[top-1];
}
char& nextValue() const {//return second to top element
return listArray[top-2];
}
int length() const { return top; } //return length
};
int main()
{
string exp;
char it = ' ';
int count;
int push_length;
cout << "Enter an expression in postfix notation:n";
cin >> exp;
cout << "The number of characters in your expression is " << exp.length() << ".n";
Stack STK;
for(count= 0; count < exp.length() ;count++)
{
if (exp[count] == '+')
{
it = exp[count - 1];
cout << it << "n";
while (it != 1 || it != 2 || it != 3 || it != 4 || it != 5 || it != 6 || it != 7 || it != 8 || it != 9 || it != 0)
{
cout << it << "n";
it = exp[count--];
}
STK.push(it);
//cout << STK.topValue() << "n";
it = exp[count --];
if (it == 1 || it == 2 || it == 3 || it == 4 || it == 5 || it == 6 || it == 7 || it == 8 || it == 9 || it == 0){
STK.push(it);
cout << it;
}
cout << STK.topValue() << "n";
it = STK.topValue() + STK.nextValue();
STK.pop();
STK.pop();
STK.push(it);
cout << STK.topValue() << "n";
}
}
cout << "The number of characters pushed into the stack is " << STK.length() << ".n";
push_length = STK.length();
return(0);
}
如您所述,您的while
条款始终是true
。
while (it != 1 || it != 2 || it != 3 || it != 4 || it != 5 || it != 6 || it != 7 || it != 8 || it != 9 || it != 0)
it
将总是不是其中的一些数字。
您可以将该语句中的每个||
更改为&&
,因为这可能是您的意思。并将1
更改为'1'
, 2
更改为'2'
,依此类推…
更清晰的方法是:
while ( !std::isdigit(it) )
http://en.cppreference.com/w/cpp/string/byte/isdigit 相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 虽然第三次循环循环,尽管在我眼中没有满足它的条件,因此打印了一个空字符串
- C++概念是否允许我的类在声明/定义中指定它满足某些概念?
- C 为什么在满足布尔条件后,我的DO-while循环就无法结束
- 模板专业化不能满足我的需要
- 我的代码有什么问题..而满足条件时循环未关闭
- 如何正确声明我的 if 或 while 语句以满足所需的输入
- 如何确保我的代码是ansi C,不涉及C++.像gcc这样的编译器是否可以设置为在满足C++中的功能时提示错误
- 为什么我的"while"条件没有得到满足?
- 我的循环条件未得到满足