简体   繁体   English

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

[英]Splitting a getline() string into multiple int and char variables?

I'm having some trouble understanding how to properly manipulate strings. 我在理解如何正确操作字符串时遇到了一些麻烦。 The program below is a simple calculator. 下面的程序是一个简单的计算器。

When I put the input directly into variables through multiple cin statements, everything worked fine. 当我通过多个cin语句将输入直接放入变量中时,一切正常。 Now, I want to take the input as a string with getline() and store the numbers/operators in existing variables from the getline(). 现在,我想使用getline()将输入作为字符串并将数字/运算符存储在getline()的现有变量中。

My main issue is that I want the program to recognize both 2+2 and 2 + 2 . 我的主要问题是我希望程序能够识别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;
}

Any help would be appreciated, but I'd much prefer an explanation to working code. 任何帮助将不胜感激,但我更喜欢解释而不是工作代码。

I'm not interested in the math logic or using namespace std aspects of the program. 我对程序的数学逻辑或using namespace std方面不感兴趣。

First of all, you should ask yourself whether it is necessary to store the entire string, instead of just using cin on the variables directly, as you said you did previously. 首先,您应该问自己是否有必要存储整个字符串,而不是像之前所说的那样直接在变量上使用cin

If you really do want to store the entire string (as you do now for echoing it), you could consider using a stringstream (you're already including sstream in your file, for some reason): 如果您确实确实想存储整个字符串(就像现在为回显它所做的那样),则可以考虑使用字符串流(由于某种原因,您已经在文件中包含了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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM