c语言的问题
编写一程序,从键盘上输入任意两个数和一运算符(+,-,*,/),计算其运算的结果并输出。(用if语句编写)
如,假如两个数为,a,b,输入的运算符为‘+’,则计算并输出a+b的结果
参考:
#include <stdio.h>
int main()
{
int a,b,c;
char p;
scanf("%d%d %c",&a,&b,&p);
if (p=='+')
c=a+b;
else
if (p=='-')
c=a-b;
else
if (p=='*')
c=a*b;
else
if (p=='/')
c=a/b;
printf("%d %c %d = %d
",a,p,b,c);
return 0;
}
程序测试结果
12 4 +
12 + 4 = 16
12345678910111213141516171819202122232425262728293031323334#include <iostream> using namespace std;int a,b;char str;int main(){ while(cin>>a>>str>>b) { //cin>>a>>str>>b; switch(str) { case '+': cout<<a+b<<endl; break; case '-': cout<<a-b<<endl; break; case '*': cout<<a*b<<endl; break; case '/': { if(b==0) cout<<"wrong!"<<endl; else cout<<a/b<<endl; break; } default: cout<<"重新输入:"<<endl; break; } } return 0;}
123456789101112131415161718192021222324252627#include <iostream> using namespace std;int a,b;char str;int main(){ while(cin>>a>>str>>b) { if(str=='+') { cout<<a+b<<endl; }else if(str=='-') { cout<<a-b<<endl; }else if(str=='*') { cout<<a*b<<endl; }else if(str=='/') { if(b==0) cout<<"wrong!"<<endl; else cout<<a/b<<endl; }else cout<<"重新输入:"<<endl; } return 0;}
下一篇:定义6个名为print的重载函数
上一篇:请大佬帮忙检查一下下面的代码