rjps.net
当前位置:首页 >> 求C语言大神解答!在线等!急!!! 用switCh语句编写程序实现两个数的+%*/四则运算 >>

求C语言大神解答!在线等!急!!! 用switCh语句编写程序实现两个数的+%*/四则运算

#include<stdio.h> int main(void) { int a, b; char c; printf("请输入两个数:"); scanf("%d %d", &a, &b); getchar(); printf("请输入你需要的运算法则:"); scanf("%c",&c); switch (c) { case '+': printf("%d+%d = %d", a, b, a+b); break;

#include <stdio.h>#include <math.h>void main(){ float a,b; char() c; printf("\n input a:"); scanf("%f",&a); printf("\n input b:"); scanf("%f",&b); //这种情况经常遇到.输入完后回车被下一个scanf("%c",&c)捕获了. //因为回车符也

#include<stdio.h> void main() { int a,b; char ch; printf("请输入两个整数和一个四则运算符:"); scanf("%d %d %ch",&a,&b,&ch); switch(ch) { case '+':printf("%d+%d=%d\n",a,b,a+b);break; case '-':printf("%d-%d=%d\n",a,b,a-b);break; case '*':printf("%d*%d=%d\n",a,b,a*b);break; case '/':printf("%d/%d=%lf\n",a,b,(double)(a)/b);break; } }

#include<stdio.h> int main() { double a, b, c; char d; scanf("%lf %c %lf", &a, &d, &b); switch (d) { case'+': c = a + b; break; case'-': c = a - b; break; case'*': c = a*b; break; case'/': if (b > 0) c = a / b; break; } printf("%f", c); return 0; }

#include<stdio.h> void main() { char ch; int x,y,sum=0; scanf("%d %c %d",&x,&ch,&y); switch(ch) { case '+':sum=x+y; break; case '-':sum=x-y; break; case '*':sum=x*y; break; case '/':sum=x/y; break; } printf("%d%c%d=%d",x,ch,y,sum); getch(); }

其实还bai可以加个循环du输入的,不过怕你输入格zhi式dao错误,没加了..回.#include<stdio.h>#include<math.h>int main(){ double a,b; char c; scanf("%lf%c%lf",&a,&c,&b); switch(c) { case'+':printf("=%g",a+b);break; case'-':printf("=%g

switch 是一个开关语句,和case配套使用, 和if else 判断语句差不多, switch 语句是用于多分支语句进行条件判断.下例为用switch语句编写的四则运算:#include <stdio.h> void main() { double N1,N2; char Operation; printf("输入运算的两个

#include int main(int argc, char *argv[]){ int age; printf("input age:"); scanf("%d",&age); switch(age) { case 2: case 3: printf("age:%d,enter Lower class\n",age); break; case 4: printf("age:%d,enter Middle class\n",age); break; case 5: case 6: printf("age:%d,enter Higher class\n",age); break; } return 0;}

c=a/b; c=a+b; c=a-b; c=a*b; switch(n) { case '+': printf("结果为:%d",c);break; case '-': printf("结果为:%d",c);break; case '*': printf("结果为:%d",c);break; case '/': printf("结果为:%d",c);break; default:printf("输入有误,请重新输入!");这里有问题 c的值的问题 也是你在写代码的时候没有考虑好的问题谢谢采纳

1)把scanf("%d%c%d",&a,&op,&b);改成scanf("%d,%c,%d",&a,&op,&b);,输入用,号隔开2)switch括号里面把条件加上,即switch(op)

相关文档
网站首页 | 网站地图
All rights reserved Powered by www.rjps.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com