如果你想用scanf("%c",&a);也可以,不过你必须调整一下它的位置:把它往前放~~
#include<stdio.h>
main()
{
int num1, demon1,num2,demon2,result_num,result_demon;
char a;
printf("Enter a character(+,-,*,/): ");
scanf("%c",&a);
printf("Enter first fraction: ");
scanf("%d/%d",&num1,&demon1);
printf("Enter second fraction: ");
scanf("%d/%d",&num2,&demon2);
switch(a)
{
case '+': result_num=num1*demon2+num2*demon1;
result_demon=demon1*demon2;
printf("The sum is %d/%d\n",result_num,result_demon);
break;
case '-': result_num=num1*demon2-num2*demon1;
result_demon=demon1*demon2;
printf("The sum is %d/%d\n",result_num,result_demon);
break;
case '*': result_num=num1*num2;
result_demon=demon1*demon2;
printf("The sum is %d/%d\n",result_num,result_demon);
break;
case '/': result_num=num1*demon2;
result_demon=num2*demon1;
printf("The sum is %d/%d\n",result_num,result_demon);
break;
default: printf("ERROR!\n");
break;
}
}
自己试试吧!
#include<stdio.h>
main()
{
int num1, demon1,num2,demon2,result_num,result_demon;
char a;
printf("Enter a character(+,-,*,/): ");
scanf("%c",&a);
printf("Enter first fraction: ");
scanf("%d/%d",&num1,&demon1);
printf("Enter second fraction: ");
scanf("%d/%d",&num2,&demon2);
switch(a)
{
case '+': result_num=num1*demon2+num2*demon1;
result_demon=demon1*demon2;
printf("The sum is %d/%d\n",result_num,result_demon);
break;
case '-': result_num=num1*demon2-num2*demon1;
result_demon=demon1*demon2;
printf("The sum is %d/%d\n",result_num,result_demon);
break;
case '*': result_num=num1*num2;
result_demon=demon1*demon2;
printf("The sum is %d/%d\n",result_num,result_demon);
break;
case '/': result_num=num1*demon2;
result_demon=num2*demon1;
printf("The sum is %d/%d\n",result_num,result_demon);
break;
default: printf("ERROR!\n");
break;
}
}
自己试试吧!