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

时间:2016-05-01 17:40:55

标签: c++

我正在构建一个计算器,用户可以为不同的操作输入不同的字符串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等命令。如果有某种方法可以告诉字符串是否为浮点数,那么将其转换为非常有用的浮点数。

1 个答案:

答案 0 :(得分:0)

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