나는 이것을 실행하고 표현식으로 "12+"를 입력했습니다. while 루프가되면 조건이 충족되지 않는 것처럼 멈추게됩니다. while 루프가 "it"이 2와 같기 때문에 루프가 사용되어서는 안되기 때문에 이것이 왜 있는지 보지 않습니다. 당신이 언급으로"while"조건이 충족되지 않는 이유는 무엇입니까?
//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);
}
당신은 아닌 char 값 1에 대해 비교하는 char 값 '1'과 같습니다. – Joe
루프가 무한히 실행되거나 예상대로 실행되지 않는 문제는 디버거를 사용하는 법을 배우기에 좋은 시간입니다. 사용중인 컴파일러 및 IDE와 함께 디버거를 사용하는 방법을 검색하십시오. 기본적으로 프로그램을 실행 한 다음 루프 바로 전에 일시 중지하고 변수의 모든 값을 검사 할 수 있습니다. 특정 변수와 표현식을 볼 수도 있습니다. 즉, 실행 중 어느 시점에서! = 1이 true 또는 false인지 확인할 수 있습니다. 나는 오랜 시간 동안 프로그래밍을 한 후에 디버거를 사용하기 시작했다. – Joshua
질문을 뒤로 굴 렸습니다. 귀하의 편집과 함께, 질문은 이해가되지 않습니다. 고정 코드에 대해 더 궁금한 점이 있으면 새로운 질문을하십시오. – avakar