将getline()字符串拆分为多个int和char变量?

时间:2015-11-02 18:44:34

标签: c++ string getline

我在理解如何正确操作字符串方面遇到了一些麻烦。下面的程序是一个简单的计算器。

当我通过多个cin语句将输入直接放入变量时,一切正常。现在,我想将输入作为带有getline()的字符串,并将数字/运算符存储在getline()的现有变量中。

我的主要问题是我希望程序同时识别2+22 + 2

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

using namespace std;

int main()
{
    int Num1, Num2, Num3 = 0, result;
    char Operator1, Operator2 = 0;
    string input, in1;

    cout << "Enter your equation on one line.\n";
    getline(cin, input);


    //this is where getline needs to be manipulated into Num1/2/3 and Operator1/2

    cout << input;


    if (Operator2 != 0)
    {
        if (Operator1 == '+')
        {
            if (Operator2 == '+')
            {
                result = Num1 + Num2 + Num3;
                cout << "I made it to A!";
            }
            else if (Operator2 == '-')
            {
                result = Num1 + Num2 - Num3;
            }
            else if (Operator2 == '/')
            {
                result = Num1 + Num2 / Num3;
            }
            else if (Operator2 == '*')
            {
                result = Num1 + Num2 * Num3;
            }
        }
        else if (Operator1 == '-')
        {
            if (Operator2 == '+')
            {
                result = Num1 - Num2 + Num3;
            }
            else if (Operator2 == '-')
            {
                result = Num1 - Num2 - Num3;
            }
            else if (Operator2 == '/')
            {
                result = Num1 - Num2 / Num3;
            }
            else if (Operator2 == '*')
            {
                result = Num1 - Num2 * Num3;
            }
        }
        else if (Operator1 == '/')
        {
            if (Operator2 == '+')
            {
                result = Num1 / Num2 + Num3;
            }
            else if (Operator2 == '-')
            {
                result = Num1 / Num2 - Num3;
            }
            else if (Operator2 == '/')
            {
                result = Num1 / Num2 / Num3;
            }
            else if (Operator2 == '*')
            {
                result = Num1 / Num2 * Num3;
            }
        }
        else if (Operator1 == '*')
        {
            if (Operator2 == '+')
            {
                result = Num1 * Num2 + Num3;
            }
            else if (Operator2 == '-')
            {
                result = Num1 * Num2 - Num3;
            }
            else if (Operator2 == '/')
            {
                result = Num1 * Num2 / Num3;
            }
            else if (Operator2 == '*')
            {
                result = Num1 * Num2 * Num3;
            }
        }
        else
        {
            cout << "I don't recognize that operator. Did you type in one of these?: + - * /";
        }
    }
    else if (Operator2 == 0)
    {
        if (Operator1 == '+')
    {
            result = Num1 + Num2;
    }
        else if (Operator1 == '-')
    {
            result = Num1 - Num2;
    }
        else if (Operator1 == '*')
    {
            result = Num1 * Num2;
    }
        else if (Operator1 == '/')
    {
            result = Num1 / Num2;
    }
        else
    {
        cout << "I don't recognize that operator. Did you type in one of these?: + - * /";
    }

        result = Num1 + Num2;
        cout << "I made it to B!";
    }


    cout << "Your result is: " << result << endl << endl;

    return 0;
}

任何帮助都会受到赞赏,但我更喜欢对工作代码的解释。

我对程序的数学逻辑或using namespace std方面不感兴趣。

1 个答案:

答案 0 :(得分:0)

首先,您应该问自己是否有必要存储整个字符串,而不是直接在变量上使用cin,正如您之前所说的那样。

如果您确实想要存储整个字符串(就像现在用来回显它一样),您可以考虑使用字符串流(由于某些原因,您已经在文件中包含sstream) :

getline(cin, input);

std::istringstream iss(input);
// you can now use iss just as cin.
// I'm not sure exactly what you want to do, but it would look something like this:
iss >> num1;
iss >> Operator1;
iss >> num2;

cout << input;