我如何编写一个小型计算器,将四个算术运算之一作为输入,这些运算的两个参数,然后打印出结果?就这么简单,我不需要真正的计算器。
这是我到目前为止尝试过的,但是它没有用:
#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;
}
我知道这个程序有很多问题,我刚开始学习,而且这种做法在一本书中。