如何测试输入是字符串还是int

How to test whether input is a string or int

本文关键字:字符串 int 输入 何测试 测试      更新时间:2023-10-16

我正在构建一个计算器,用户可以为不同的操作输入不同的字符串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

还有另外两个文件,但它们在这里没有区别,所以我宁愿不要过多地讨论这个问题。

正如您可能看到的,现在用户必须输入他们想要使用的操作类型,然后输入两个数字。输入数字比输入操作更方便,但也可以选择输入helpclear等命令。如果有某种方法可以判断字符串是否为浮点值,那么将其转换为浮点值将非常有用。

将所有输入作为字符串。您可以使用atof转换为双精度。如果您使用的是C++11,那么您也可以使用stof