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

问题描述:

我在理解如何正确操作字符串时遇到了一些麻烦。下面的程序是一个简单的计算器。将getline()字符串拆分为多个int和char变量?

当我通过多个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方面不感兴趣。

+0

此外,请忽略cout DoubleLlama

+0

[Here](http://*.com/questions/23047052/why-does-reading-a-record-struct-fields-from-stdistream-fail-and-how-can-i-fi)是一些提示让你的生活更轻松。使用'std :: istringstream'。 –

+0

操作将始终是单个字符吗? – NathanOliver

首先,您应该问自己,是否有必要存储整个字符串,而不是直接对变量使用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; 
+0

太棒了! Stringstream似乎正是我所需要的。谢谢! – DoubleLlama

+2

@DoubleLlama只要'std :: istringstream'就足够了,'std :: stringstream'有它的怪异之处。 –