程序中的无限循环?程序中没有输出返回?

时间:2013-12-01 11:30:46

标签: c++

我有一些工作代码。它不像我想要的那样工作。当我运行程序时,我没有得到任何输出。我做了一些调试,发现了一些无限循环。 关于我正在做的程序:我正在做一个后缀程序的中缀。我正在使用sstream,从用户那里获取输入并使用peek并检查下一个字符。

我遇到问题的函数标题是:string infixconvert (istream& input)

我认为我已经确定了无限循环:

     if (isspace(ch))
     {
         ch = input.get();
         ch = input.peek();
         continue;
     } 

在这里:

     if (isdigit(ch))
     {
         ch = (int)ch;
         postfixstack.push_back(ch);
         ch = input.peek();
         continue;
     }

当我进行自己的调试时,这些点有无限循环,我不确定无限循环是否是没有输出的问题。如果有人可以在他们的编译器中运行此代码(完整程序)并查看错误,我将非常感激。我需要一双新的眼睛才能看到它。

这是完整的程序:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

// This function converts a vector<int> to string.
string convertInt(vector<int> vec)
{
stringstream ss;//create a stringstream
for (unsigned int i = 0; i < vec.size(); i++)
{
    ss  << " " << vec[i];//add number to the stream
}
return ss.str();//return a string with the contents of the stream
  }

 string infixconvert (istream& input)
 {
 // Have two Stacks made fro vectors one tempstack one, postfix
 vector<int> tempstack;
 vector<int> postfixstack; 
 // used to read in characters
 char ch;
 // Used to read in numbers, although, not used yet.
 int num = 0;
 ch = input.peek();
 // While not End of file
 while (ch != EOF)
 {
     // If white space skip it and continue
     if (isspace(ch))
     {
         ch = input.get();
         ch = input.peek();
         continue;
     }

     if (isdigit(ch))
     {
         // If ch is a digit, push it on to the postfix stack
         ch = (int)ch;
         postfixstack.push_back(ch);
         ch = input.peek();
         continue;
     }

     if (ch == '(' || ch == ')')
     {
         // if a open or closed paraenthiesis push on to tempstack
         // and pop it off
         tempstack.push_back(ch);
         tempstack.pop_back();
         ch = input.peek();
         continue;
     }

     // If operator, push on to tempstack
     if ( ch == '-' || ch == '+' || ch == '*' || ch == '/' || ch == '%')
     {
         ch = (int)ch;
         tempstack.push_back(ch);
         ch = input.peek();

         // Check stack postfix, to see if operator needs to be pushed on
         // if there are two digits next by each other in postfix stack
         // push on to postfix stack, else continue loop.
         for (unsigned int n = 0; n < postfixstack.size(); n++)
         {
        for (unsigned int i = 1; i < postfixstack.size(); i++)
             {
                 if (isdigit(n) && isdigit(i) ||     postfixstack.size() <= 1)
                 {
                     postfixstack.push_back(ch);
                 }
                 else
                 {
                     continue;
                 }
             }
             continue;
         }
     }

 }
 // convert to string
 return convertInt(postfixstack);
}

 int main()
 {
 string input;
 cout << "Enter a infix  expression to convert to postfix, "
     << " \nor a blank line to exit the program : ";
 getline(cin,input);
 while(input.size() != 0)
 {
     istringstream inputexpr(input);
     cout << "The postfix equivalent is "
          << infixconvert(inputexpr) << endl;
     cout << "Enter a infix expression to evaluate: ";
 }
 return 0;
 }

0 个答案:

没有答案