我如何編寫一個小型計算器,將四個算術運算中的一個作爲輸入,將這兩個參數作爲這些運算的輸入,然後輸出結果? 就這麼簡單,我不需要一個真正的計算器。如何用C++語言編寫一個小型計算器?
這裏是我試過到目前爲止,但遠沒有奏效:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x,y,result;
string arithmatic;
cout<<"enter the first number:";
cin>>x;
cout<<"enter the second number:";
cin>>y;
cout<<"use one of the four artimatic operations /, *, + or - :";
cin>>arithmatic;
if (arithmatic=="/")
result=x/y;
cout<<"x/y ="<<result;
if (arithmatic == "*")
result=x*y;
cout<<"x * y ="<<result;
if (arithmatic == "+")
result = x + y;
cout<<"x+y ="<<result;
if (arithmatic == "-")
result = x-y;
cout<<"x-y ="<<result;
else
cout<<"what is this? i said use arithmatic operations!";
return 0;
}
我知道有很多不對的節目,我剛開始學習的,這種做法是在一個書。
顯示您迄今爲止編寫的代碼。 –
你想學習如何編寫一個執行'+, - ,*,/'的小型計算器,但你不想要一個真正的計算器...抱歉...只是說... – czchlong
打開文本編輯器並開始編碼... – Jason