一道C语言编程题 简单计算器 哪里有错
#include<stdio.h>intmain(void){intnum1,denom1,num2,denom2,result_num,result_denom;cha...
#include <stdio.h>
int main(void)
{
int num1, denom1, num2, denom2, result_num, result_denom;
char ch;
scanf("&d/&d %c %d/%d",&num1,&denom1,&ch,&num2,&denom2);
switch (ch){
case '+':
printf("The sum is %d/%d\n", result_num = num1 * denom2 + num2 * denom1, result_denom = denom1 * denom2);
case '-':
printf("The difference is %d/%d\n", result_num = num1 * denom2 - num2 * denom1, result_denom = denom1 * denom2);
case '*':
printf("The product is %d/%d\n", result_num = num1 * num2, result_denom = denom1 * denom2);
case '/':
printf("The quotient is %d/%d\n", result_num = num1 * denom2, result_denom = denom1 * num2);
}
} 展开
int main(void)
{
int num1, denom1, num2, denom2, result_num, result_denom;
char ch;
scanf("&d/&d %c %d/%d",&num1,&denom1,&ch,&num2,&denom2);
switch (ch){
case '+':
printf("The sum is %d/%d\n", result_num = num1 * denom2 + num2 * denom1, result_denom = denom1 * denom2);
case '-':
printf("The difference is %d/%d\n", result_num = num1 * denom2 - num2 * denom1, result_denom = denom1 * denom2);
case '*':
printf("The product is %d/%d\n", result_num = num1 * num2, result_denom = denom1 * denom2);
case '/':
printf("The quotient is %d/%d\n", result_num = num1 * denom2, result_denom = denom1 * num2);
}
} 展开
展开全部
scanf("&d/&d %c %d/%d",&num1,&denom1,&ch,&num2,&denom2);
改为
scanf("%d/%d %c %d/%d",&num1,&denom1,&ch,&num2,&denom2);
另外switch语句中应该加上break
#include <iostream>
using namespace std;
int main(void)
{
int num1, denom1, num2, denom2, result_num, result_denom;
char ch;
//scanf("&d/&d %c %d/%d",&num1,&denom1,&ch,&num2,&denom2);
scanf("%d/%d %c %d/%d",&num1,&denom1,&ch,&num2,&denom2);
switch (ch){
case '+':
printf("The sum is %d/%d\n", result_num = num1 * denom2 + num2 * denom1, result_denom = denom1 * denom2);
break;
case '-':
printf("The difference is %d/%d\n", result_num = num1 * denom2 - num2 * denom1, result_denom = denom1 * denom2);
break;
case '*':
printf("The product is %d/%d\n", result_num = num1 * num2, result_denom = denom1 * denom2);
break;
case '/':
printf("The quotient is %d/%d\n", result_num = num1 * denom2, result_denom = denom1 * num2);
break;
}
}
展开全部
switch语句中,每一个case语句结束的地方都应该有break,否则他会顺序执行下去
#include <stdio.h>
int main(void)
{
int num1, denom1, num2, denom2, result_num, result_denom;
char ch;
scanf("&d/&d %c %d/%d",&num1,&denom1,&ch,&num2,&denom2);
switch (ch){
case '+':
printf("The sum is %d/%d\n", result_num = num1 * denom2 + num2 * denom1, result_denom = denom1 * denom2);
break;
case '-':
printf("The difference is %d/%d\n", result_num = num1 * denom2 - num2 * denom1, result_denom = denom1 * denom2);
break;
case '*':
printf("The product is %d/%d\n", result_num = num1 * num2, result_denom = denom1 * denom2);
break;
case '/':
printf("The quotient is %d/%d\n", result_num = num1 * denom2, result_denom = denom1 * num2);
break;
default: break;
}
return 0;
}
追问
我加上break了 可是完全没有输出啊怎么回事
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
缺少返回值,
就是return 0掉了;
希望可以帮到你,祝你生活愉快。
就是return 0掉了;
希望可以帮到你,祝你生活愉快。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
出现什么问题了?你怎么输入的。输入格式要按照scanf的格式输入
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的scanf中有两个&d
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询