如何测试输入是字符串还是int
How to test whether input is a string or int
我正在构建一个计算器,用户可以为不同的操作输入不同的字符串cout << endl << endl << "+,-,*,/,^,pi,sqrt,clear,quit,help" << endl;
程序需要知道它是否是字符串,这样才能运行pi,sqrt,clear,quit,help
。如果它是一个浮点,它需要知道这一点,这样它就可以运行简单的算术运算,还需要为运算保存浮点。
main.cpp
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include "PageFormat.h"
#include "Help.h"
#include "MathFunctions.h"
int main()
{
//VARIABLES
bool mathMode;
string operatorType;
//OBJECTS
Help ho; //object for the help class
MathFunctions mfo;
PageFormat pfo;
//INTRO
cout << "****** CalcPal ******" ;
mathMode = true;
while (mathMode == true){
cout << endl << endl << "+,-,*,/,^,pi,sqrt,clear,quit,help" << endl;
cin >> operatorType;
if (operatorType == "+"){
mfo.add();
}
if (operatorType == "-"){
mfo.subtract();
}
if (operatorType == "/"){
mfo.divide();
}
if (operatorType == "*"){
mfo.multiply();
}
if (operatorType == "^"){
mfo.power();
}
if (operatorType == "pi"){
mfo.pi();
}
if (operatorType == "sqrt"){
mfo.squareRoot();
}
if (operatorType == "clear" || operatorType == "Clear"){
pfo.clearPage();
}
if (operatorType == "quit" || operatorType == "Quit"){
mathMode = false;
}
if (operatorType == "help" || operatorType == "Help" ){ //Triggers the help menu
ho.helpMenu();
}
}
}
这是MathFunctions cpp文件
#include "Help.h"
#include "PageFormat.h"
#include "MathFunctions.h"
#include <iostream>
#include <cmath>
using namespace std;
MathFunctions::MathFunctions()
{
}
int MathFunctions::add(){ // Addition Function
float num1;
float num2;
cin >> num1;
cout << "+" << endl;
cin >> num2;
float answer = num1 + num2;
cout << "= " << answer;
}
int MathFunctions::subtract(){ //Subtraction Function
float num1;
float num2;
cin >> num1;
cout << "-" << endl;
cin >> num2;
float answer = num1 - num2;
cout << "= " << answer;
}
int MathFunctions::divide(){ //Division function
float num1;
float num2;
cin >> num1;
cout << "/" << endl;
cin >> num2;
float answer = num1 / num2;
cout << "= " << answer;
}
int MathFunctions::multiply(){ //Multiplication function
float num1;
float num2;
cin >> num1;
cout << "*" << endl;
cin >> num2;
float answer = num1 * num2;
cout << "= " << answer;
}
int MathFunctions::power(){ //Power function
float num1;
int num2;
cin >> num1;
cout << "^" << endl;
cin >> num2;
float num1Holder = num1;
for (int powerTower = 1; powerTower < num2; powerTower ++){
num1 = num1 * num1Holder;
}
cout << "= " << num1;
}
int MathFunctions::pi(){
float num1;
double pii;
pii = 3.14159;
cin >> num1;
cout << "*" << endl << "pi" << endl;
float answer = num1 * pii;
cout << answer;
}
int MathFunctions::squareRoot(){
float num1;
cin >> num1;
float answer = sqrt(num1);
cout << answer;
}
这是MathFunctions头文件
#ifndef MATHFUNCTIONS_H
#define MATHFUNCTIONS_H
class MathFunctions
{
public:
MathFunctions();
int add();
int subtract();
int multiply();
int divide();
int power();
int pi();
int squareRoot();
private:
float num1();
float num2();
float answer();
double pii();
int x;
int y;
};
#endif // MATHFUNCTIONS_H
还有另外两个文件,但它们在这里没有区别,所以我宁愿不要过多地讨论这个问题。
正如您可能看到的,现在用户必须输入他们想要使用的操作类型,然后输入两个数字。输入数字比输入操作更方便,但也可以选择输入help
或clear
等命令。如果有某种方法可以判断字符串是否为浮点值,那么将其转换为浮点值将非常有用。
将所有输入作为字符串。您可以使用atof
转换为双精度。如果您使用的是C++11,那么您也可以使用stof
。
相关文章:
- 我如何获取从 C++ 到 C 的映射<字符串、int>#
- 构造<int>具有 2 个字符串文字的向量
- 字符串中int的加法运算
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- cin >> int 给定一个字符串将 int 赋值为 0
- 将字符串中的 int 转换为字母;
- 使用 switch,但我想用字符串而不是 int 进行选择
- 两个数组,int 和 string,如何显示带有对应 int 的字符串?
- 从字符串流中读取具有特定字段宽度的 int
- win 表单应用程序字符串^ 到 int
- C++程序在将 int 与 cin 一起使用时有效,但不能使用字符串
- 将子字符串字符从字符串值转换为 int,然后将其分配给 int 变量
- 将字符串转换为 int 后的随机值
- 如何检查字符串是否正确以将其转换为 int?
- 在 c++ 中逐个字符从字符串传递到 int 时不精确
- <int> 使用 fmt 库将向量转换为字符串
- 我正在尝试将 int 值插入字符串,而不是 1,我得到 10
- 为什么编译:字符串 = int
- C++ 隐式类型转换字符串 -> int?
- 如何在C++中连接字符串+int+字符串