2015-06-27 187 views
1

我一直在一个项目上工作,我几乎完成了我最后一个问题是创建一个while循环,不断询问用户是否要转换表达式。到目前为止它只做了一次,然后不再继续询问。我知道这是一个简单的问题,我认为我有逻辑,但由于某种原因,它不起作用。虽然循环不回到循环

这里是我的主:

int main(){ 
string answer=" "; 

string expression; 

while(answer!="no"){ 
    cout<<"Would you like to do a conversion,type yes or no:"; 
getline(cin,answer); 

    cout<<" Enter a Post Fix expression:"; 


getline(cin,expression); 
convert(expression); 

} 



return 0; 

} 

虽然不是真的有必要在这里我的问题是上面我主要的代码的情况下,它是有用的:

/* 
* PLEASE DO NOT PLACE A SPACE BEFORE YOU INPUT THE FIRST OPERAND 
* 
* 
*/ 
#include "stack.h" 

void convert(string expression){ 
stack k; //Stores raw input string 
stack c; //stores input string without spaces 
stack s;//stores the string values 

string post =" "; 
string rightop=""; 
string leftop=""; 
string op =""; 
int countop=0;// counts the number of operators 
int countoper=0;// counts the number of operands 
for (int i =0; i<=expression.length()-1;i++){ 
k.push(expression[i]); 

if(expression[i] == '*' || 
    expression[i] == '+' || 
    expression[i] == '-' || 
    expression[i] == '/') 
    { 

    countop++; 

    } 

} 
c.push(expression[0]); 
    int count=expression.length()/2; 

    countoper=(count-countop)+1; 

    if (countop==countoper){ //tells when there are too many opertors and not enough operands 
     cout<<"too many operators and not enough operand"<<endl; 
     exit(1); 
    } 
    if(countop==countoper*2){ //tells when there are too many opertors and not enough operands 

     cout<<"too many operands and not enough operators"<<endl; 
     exit(1); 
    } 
for(int i=1;i<=expression.length()/2;i++){ 

    c.push(expression[2*i]); 

} 




for(int i=0; i<2;i++){ 

    leftop=c.top(); 
    c.pop(); 

    rightop=c.top(); 
    c.pop(); 
    op=c.top(); 
    c.pop(); 

    post="(" + leftop + " " + op + " " + rightop + ")"; 

    s.push(post); 
    if(count<6){ 
     cout<<s.top()<<endl; 
    } 

} 

if (count>=6){ 

    cout<<"("; 
    cout<<s.top(); 

    cout<<c.top(); 

    s.pop(); 

    cout<<s.top(); 

    cout<<")"; 
    } 
} 

回答

0

我尝试这个代码,这是我从你的复制,它运作良好。这使我得出结论,我不明白问题是什么或你想要什么。

#include <iostream> 
#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 

using namespace std; 
int main(){ 
    string answer=" "; 
    string expression; 

    while(answer!="no"){ 
     cout<<"Would you like to do a conversion,type yes or no:"; 
     getline(cin,answer); 

     cout<<" Enter a Post Fix expression:"; 

     getline(cin,expression); 
     // do something 
    } 

    return 0; 
} 

这是什么做的:

1)用户回答“否”的第一个问题,然后用户写在第二输入一些东西。退出程序。

2)用户回答与“否”不同的东西,然后用户写东西。回到第一个问题。

你想要什么行为?更好地解释。

+0

我又试了一遍,它没有回到第一个问题。我认为这可能是我的转换函数关闭它不确定。 –

+0

当你按照你写的程序运行程序时,你说它只运行一次循环。接下来发生什么?程序是否终止或卡住了? 我想要得到的是你的convert()函数卡在无限循环或阻塞,或者你的convert()函数抛出一些导致程序终止的错误 – jose

0

我也跑你的代码和while循环为我工作。也许你可以从注释掉你的convert(表达式)开始;从那里调用和调试!

#include <iostream> 
using namespace std; 


int main(){ 
string answer=" "; 

string expression; 

while(answer!="no"){ 
cout<<"Would you like to do a conversion,type yes or no:"; 
getline(cin,answer); 

cout<<" Enter a Post Fix expression:"; 


getline(cin,expression); 
/*convert(expression);*/ 

} 



return 0; 

} 
+0

我非常确信它是我的转换方法,因为它评论它出来后工作。我不知道我的转换方法有什么问题。 –