C ++从cin读取输入,直到读取整行

时间:2014-05-30 05:43:23

标签: c++ input eof cin

我在char'c'中有一个while循环读取,我检查一下char是否是一个运算符; '+',' - ','/'或'*'。 while循环逐个字符地读取,但它不会停止...如果输入的最后一个char是'+',例如。它将保持循环,char'c'永远设置为'+'。

stack<int> num;
char c;
int n,count=0,a,b;
while (cin>>c)
{
    if (c != '+' && c != '-' && c != '/' && c != '*')
     {
         cout << c << endl;
         n = (c - 48);
         num.push(n);
         cin >> c;
         count++;
     }
     else if (c == '+' || c == '-' || c == '/' || c == '*')
     {
         cout << "count is " << count << endl;
         if (count>1)
         {
            b =  num.top();
            a = num.top();
            num.pop();
            num.pop();

            if (c == '+')
            {
                num.push(a+b);
                count--;
            }
            else if (c == '-')
            {
                num.push(a+b);
                count--;
            }
            else if (c == '/')
            {
                if (b != 0)
                {
                    num.push(a/b);
                    count--;
                }
                else
                {
                    cout << "division by zero" << endl;
                    return(0);
                }
            }
            else if (c == '*')
            {
                num.push(a*b);
                count--;
            }
            else
            {
                cout << "invalid input" << endl;
                return(0);          
            }
         }
         else
         {
             cout << "stack underflow" << c << endl;
             return(0);
         }
     }
     cout << c << endl;
 }
}

4 个答案:

答案 0 :(得分:3)

既然你澄清了你的问题,那就更清楚了。而@TeoZec答案应该是正确的。我只想在上面的代码中注意两件似乎有问题的东西:

else if (c == '-')
{
    num.push(a+b);
    count--;
}

这里你可能想要a-b

if (count>1)
{
    b =  num.top();
    a = num.top();
    num.pop();
    num.pop();

ba在这里是相同的数字,您应该在获得第二个号码之前拨打pop(),例如:

if (count>1)
{
    b =  num.top();
    num.pop();
    a = num.top();
    num.pop();

答案 1 :(得分:1)

因为你没有条件终止你的while循环,你需要为你添加更多条件,如下所示:

while(cin>>c && c != '+' && c != '-' && c != '*' && c != '/')
{
    //do stuff here
}

或者在执行检查以查看输入的字符后,在while循环内部添加break

答案 2 :(得分:1)

这应该有效:

#include <cstdio>
...

int c; // As pointed out by James Kanze in his comment, c should be an int to detect EOF
do {
    c = getchar();
    // Do stuff
} while ((c != '\n') && c != EOF))

您不需要仅检查EOF,也可以检查行终止符。

答案 3 :(得分:1)

您的代码存在一些问题。对于初学者, 我不明白cin >> cif的目的 科;你输入一个角色,但你似乎永远不会使用它; 它刚刚丢失了。当然,if不需要else if 部分在if,因为它是。switch的补充 if/else if的条件。当然,'0'更多 比较单个时你自然48链 反对许多不同常数的字符。并且 当然,'0'并非总是48(即使是c, 比+更具可读性。

至于将while( std::cin >> c )设置为std::getline而停留在循环中,我不会 看见。我看到的唯一循环是std::string line; while ( std::getline( std::cin, line ) ) { // Set up your stack here... for ( std::string::const_iterator it = line.begin(); it != line.end(); ++ it ) { if ( isdigit( static_cast<unsigned char>( *it ) ) ) { // process digit... } else if ( ispunct( static_cast<unsigned char>( *it ) ) ) { // process punctuation, with eventually switch ( *it ) { case '+': // addition... break; case '-': // substraction... break; // ... default: // illegal operator... break; } } else { // process anything else... } } } ,和 这将读取一个新字符,或终止。

如果你的目标只是阅读一行,那可能会更好 使用isOperator,然后遍历您输入的字符串:

bool
isOperator( char ch )
{
    static std::string const legalOps( "+-*/" );
    return std::find( legalOps.begin(), legalOps.end(), ch) != legalOps.end();
}

或者,您可以定义{{1}}函数 以下几行:

{{1}}
相关问题