私は電卓を構築しており、ユーザーは異なる操作のために異なる文字列を入力することができますcout << endl << endl << "+,-,*,/,^,pi,sqrt,clear,quit,help" << endl;
プログラムは文字列かどうかを知る必要があるのでpi,sqrt,clear,quit,help
を実行できます。それが浮動小数点の場合、それはそれを知る必要があるので、単純な算術を実行することができ、浮動小数点演算を保存する必要もあります。入力が文字列かintかどうかをテストする方法
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
の2つのがあり、他のファイルもありますが、それらが作りますここでの違いはないので、私はこの質問よりもむしろ重すぎないだろう。
実際には、使用する操作を入力してから2つの番号を入力する必要があります。数字を入力して操作を入力できる方がはるかに便利ですが、help
またはclear
のようなコマンドを入力するオプションもあります。文字列が浮動小数点であるかどうかを知る方法があれば、それを浮動小数点に変換すると非常に便利です。
通常、入力を数値に変換しようとします。失敗した場合は数字ではありません。 –