谁能帮我编个程序啊
来源:百度知道 编辑:UC知道 时间:2024/06/06 17:03:23
Write a program that requests two real numbers and a character to return the result
corresponding to one of the four arithmetic operators (+, -, *, and /) applied to the
given two real numbers. The character + corresponds to the usual arithmetic addition,
- for the subtraction, * for the multiplication, and / for the division. Any other character
will be considered as representing the arithmetic addition. We will not take into
account the risk of dividing by zero.
corresponding to one of the four arithmetic operators (+, -, *, and /) applied to the
given two real numbers. The character + corresponds to the usual arithmetic addition,
- for the subtraction, * for the multiplication, and / for the division. Any other character
will be considered as representing the arithmetic addition. We will not take into
account the risk of dividing by zero.
#include<iostream>
using namespace std;
int main(void)
{
double a,b;
char c;
while(cin>>a>>b>>c)
{
if(c=='-')
cout<<a-b;
if(c=='*')
cout<<a*b;
if(c=='/')
{
if(b!=0)
cout<<a/b;
else
cout<<"Number cannot be divided by zero";
}
if(c!='-'&&c!='*'&&c!='/')
cout<<a+b;
cout<<endl;
}
return 0;
}
测试结果:
输入:1 2 /
输出:0.5
输入:3 4 *
输出:12
输入:1 0 /
输出:Number cannot be divided by zero
。。。。