電卓 // c++ の code を公開する。
今回は、前回諸事情で載せられなかったコードを、編集したので、載せます。それだけです。
#include <iostream>
#include <string>
using namespace std;
int main(){
int num, num1, num2, amari, power, ans;
string what = "0";
string yn = "n";
amari = 0;
for (int fin = 10; fin >= 10; fin++){
cout << " 電卓" << endl;
//数字を入力。3桁制限。
for (int i = 10; i >= 10; i++) {
cout << " 数字を入力してください。" << endl;
cin >> num;
if (num >= -999 && num <= 999){
i = 0;
} else {
cout << "エラー:3桁を超えています。" << endl;
}
num1 = num;
num = 1000;
}
//演算方法を選択
cout << " 計算方法を選択してください。" << endl;
cout << " 足し算なら、add" << endl;
cout << " 引き算なら、sub" << endl;
cout << " 掛け算なら、mul" << endl;
cout << " 割り算なら、div" << endl;
cout << " 累乗なら、 pow" << endl;
cout << " を入力してください。" << endl;
//分岐計算方法を確認。
for (int i = 10; i >= 10; i++) {
cin >> what;
if (what == "0") {
cout << "未入力です。" << endl;
cout << " 入力してください。" <<endl;
} else if (what == "add") {
i = 0;
cout << " 足し算が選択されました。" << endl;
} else if (what == "sub") {
i =0;
cout << " 引き算が選択されました。" << endl;
} else if (what == "mul") {
i = 0;
cout << " 掛け算が選択されました。" << endl;
} else if (what == "div") {
i = 0;
cout << " 割り算が選択されました。" << endl;
} else if (what == "pow") {
i = 0;
cout << " 累乗が選択されました。" << endl;
cout << " 何乗しますか。" << endl;
} else {
cout << "エラー:無効な値です。" << endl;
cout << " 入力してください。" <<endl;
}
}
//数字を入力3桁制限
for (int i = 10; i >= 10; i++) {
cout << " 数字を入力してください。" << endl;
cin >> num;
if (num >= -999 && num <= 999) {
i = 0;
} else {
cout << "エラー:3桁を超えています。" << endl;
}
num2 = num;
num = 1000;
}
//実際に計算する。
if (what == "add") {
num = num1 + num2;
} else if (what == "sub") {
num = num1 - num2;
} else if (what == "mul") {
num = num1 * num2;
} else if (what == "div") {
num = num1 / num2;
amari = num1 % num2;
} else if (what == "pow") {
power = num1;
for (int i = num2; i > 0; i--) {
power = power * num1;
}
num = power;
}
//答えの3桁制限
if (num >= -999 && num <= 999) {
ans = num;
cout << " 計算結果:" << ans << "です。" << endl;
if (amari > 0) {
cout << " 余りが出ました。" << endl;
cout << " " << amari - 1 << "以上" << amari
<< "以下です。" << endl;
}
} else {
cout << "答えが3桁を超えました。" << endl;
}
//続けるかの確認。
for (int i = 10; i >= 10; i++) {
cout << " 計算を続けますか。[y/n]" << endl;
cin >> yn;
if (yn == "y") {
i = 0;
cout << " 計算を続けます。" << endl;
} else if (yn == "n") {
i = 0;
fin = 0;
cout << " 計算を終了します。" <<endl;
} else {
cout << "エラー:y か n を入力してください" << endl;
}
}
}
return 0;
}
0コメント