如何允许用户输入数字和运算符?
How to allow user input of numbers and operators?
我将如何添加一个允许用户输入类似 2 + 2 或 10 / 5 的函数,然后简单地 运行 对象来计算它,就像他们使用我的 "Enter first input" 语句。因此,作为作业的一部分,我需要允许用户在控制台中输入类似 10 / 5 + 1 / 2 的内容。我还需要能够允许运算符重载,但我不确定我的程序当前是否允许这样做。任何帮助,将不胜感激。谢谢!
#include <iostream>
#include <conio.h>
using namespace std;
class Rational
{
private:
float numInput;
public:
Rational(): numInput(0)
{}
void getValues()
{
cout << "Enter number: ";
cin >> numInput;
}
void showValues()
{
cout << numInput << endl;
}
Rational operator + (Rational) const;
Rational operator - (Rational) const;
Rational operator * (Rational) const;
Rational operator / (Rational) const;
};
Rational Rational::operator + (Rational arg2) const
{
Rational temp;
temp.numInput = numInput + arg2.numInput;
return temp;
}
Rational Rational::operator - (Rational arg2) const
{
Rational temp;
temp.numInput = numInput - arg2.numInput;
return temp;
}
Rational Rational::operator * (Rational arg2) const
{
Rational temp;
temp.numInput = numInput * arg2.numInput;
return temp;
}
Rational Rational::operator / (Rational arg2) const
{
Rational temp;
temp.numInput = numInput / arg2.numInput;
return temp;
}
int main()
{
Rational mathOb1, mathOb2, outputOb;
int choice;
mathOb1.getValues();
cout << "First number entered: ";
mathOb1.showValues();
cout << endl;
cout << "Enter operator: + = 1, - = 2, * = 3, / = 4 ";
cin >> choice;
cout << endl;
mathOb2.getValues();
cout << "Second number entered: ";
mathOb2.showValues(); cout << endl;
switch (choice)
{
case 1:
outputOb = mathOb1 + mathOb2;
break;
case 2:
outputOb = mathOb1 - mathOb2;
break;
case 3:
outputOb = mathOb1 * mathOb2;
break;
case 4:
outputOb = mathOb1 / mathOb2;
break;
default:
cout << "Invalid choice! " << endl;
}
cout << "Answer: ";
outputOb.showValues();
cout << endl;
return 0;
}
您不能使用 cin >> {int}
,如果您提供 char
,那只会失败,您会被卡在那里。
只需使用 std::getline
并从那里解析出标记:
std::string expression;
std::getline(std::cin, expression);
然后,您可以使用许多方法中的任何一种将 string
拆分为 this question 中表示的标记,并循环遍历标记并根据它们是运算符还是数字。
我将如何添加一个允许用户输入类似 2 + 2 或 10 / 5 的函数,然后简单地 运行 对象来计算它,就像他们使用我的 "Enter first input" 语句。因此,作为作业的一部分,我需要允许用户在控制台中输入类似 10 / 5 + 1 / 2 的内容。我还需要能够允许运算符重载,但我不确定我的程序当前是否允许这样做。任何帮助,将不胜感激。谢谢!
#include <iostream>
#include <conio.h>
using namespace std;
class Rational
{
private:
float numInput;
public:
Rational(): numInput(0)
{}
void getValues()
{
cout << "Enter number: ";
cin >> numInput;
}
void showValues()
{
cout << numInput << endl;
}
Rational operator + (Rational) const;
Rational operator - (Rational) const;
Rational operator * (Rational) const;
Rational operator / (Rational) const;
};
Rational Rational::operator + (Rational arg2) const
{
Rational temp;
temp.numInput = numInput + arg2.numInput;
return temp;
}
Rational Rational::operator - (Rational arg2) const
{
Rational temp;
temp.numInput = numInput - arg2.numInput;
return temp;
}
Rational Rational::operator * (Rational arg2) const
{
Rational temp;
temp.numInput = numInput * arg2.numInput;
return temp;
}
Rational Rational::operator / (Rational arg2) const
{
Rational temp;
temp.numInput = numInput / arg2.numInput;
return temp;
}
int main()
{
Rational mathOb1, mathOb2, outputOb;
int choice;
mathOb1.getValues();
cout << "First number entered: ";
mathOb1.showValues();
cout << endl;
cout << "Enter operator: + = 1, - = 2, * = 3, / = 4 ";
cin >> choice;
cout << endl;
mathOb2.getValues();
cout << "Second number entered: ";
mathOb2.showValues(); cout << endl;
switch (choice)
{
case 1:
outputOb = mathOb1 + mathOb2;
break;
case 2:
outputOb = mathOb1 - mathOb2;
break;
case 3:
outputOb = mathOb1 * mathOb2;
break;
case 4:
outputOb = mathOb1 / mathOb2;
break;
default:
cout << "Invalid choice! " << endl;
}
cout << "Answer: ";
outputOb.showValues();
cout << endl;
return 0;
}
您不能使用 cin >> {int}
,如果您提供 char
,那只会失败,您会被卡在那里。
只需使用 std::getline
并从那里解析出标记:
std::string expression;
std::getline(std::cin, expression);
然后,您可以使用许多方法中的任何一种将 string
拆分为 this question 中表示的标记,并循环遍历标记并根据它们是运算符还是数字。