-2
只是一個簡單的計算器,我是用C++編寫的第一個編碼,我很樂意接受有關如何改進的建設性批評!
我只使用整數的操作,希望現在就簡化它!我的第一個獨立的C++程序。我該如何改進它?
#include <iostream>
using namespace std;
//Simple calculator that handles +,-,*,/ with whole numbers
int Add (int x, int y){
return (x+y);
}
int Sub (int x, int y){
return (x-y);
}
int Mult (int x, int y){
return (x*y);
}
int Div (int x, int y){
return (x/y);
}
int main(){
enum operation {sum, subtract, multiply, divide};
operation operationSelect;
int sel;
int a,b,c;
cout << "Which 2 numbers do you want to perform an operation on?\n";
cin >> a;
cin >> b;
cout << "Which operation do you want to perform? sum, subtract, multiply, divide (0-3)\n";
cin >> sel;
operationSelect = operation(sel);
if (operationSelect == sum){
c = Add (a, b);
cout << "The result is: " << c << endl;
}
if (operationSelect == subtract){
c = Sub (a, b);
cout << "The result is: " << c << endl;
}
if (operationSelect == multiply){
c = Mult (a, b);
cout << "The result is: " << c << endl;
}
if (operationSelect == divide){
c = Div (a, b);
cout << "The result is: " << c << endl;
}
return 0;
}
通過縮進代碼來改進它。 –
codereview.stackexchange.com將是一個更好的地方發佈此 – JVApen
不用於stackoverflows格式,對於可憐的縮進感到抱歉! – vuskovic09