虽然循环不会回到循环中

时间:2015-06-27 01:25:22

标签: loops menu while-loop stack main

我一直致力于一个项目,我几乎完成了我的最后一个问题,即创建一个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<<")";
    }
}

2 个答案:

答案 0 :(得分: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)用户回答&#34; no&#34;第一个问题,然后用户在第二个输入上写东西。退出计划。

2)用户回答与&#34; no&#34;不同的内容,然后用户写一些东西。回到第一个问题。

您想要的行为是什么?解释得更好。

答案 1 :(得分:0)

我也运行了你的代码,而while循环对我有效。 也许你可以先评论你的转换(表达式);从那里打电话和调试!

#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;

}