如何把下面C语言的代码改成含有goto语句,让用户选择输入Y或y,以执行计算;输入N或者n,就结束程序!
/*Note:YourchoiceisCIDE*/#include"stdio.h"voidmain(void){ doublenumb...
/* Note:Your choice is C IDE */#include "stdio.h"void main(void){ double number1=0.0; double number2=0.0; char letter=0; char operation=0; printf("\nEnter the calculation\n"); scanf("%lf %c %lf",&number1,&operation,&number2); switch(operation) { case'+': printf("= %.2lf\n",number1+number2); break; case'-': printf("= %.2lf\n",number1-number2); break; case'*': printf("= %.2lf\n",number1*number2); break; case'/': if(number2==0) printf("\n\n\aDivision by zero error!\n"); else printf("= %.2lf\n",number1/number2); break; case'%': if((long)number2==0) printf("\n\n\aDivision by zero error!\n"); else printf("= %ld\n",(long)number1%(long)number2); break; default: printf("\n\n\aIllegal operation!\n"); break; } return 0;}
展开
2个回答
展开全部
/* 加“//”是我加上的代码,程序可以运行,我试过了 */
/* 另外多嘴的说一下,一般大家不用goto,你要求的这个功能用if-else也可以实现的,而且代码的可靠性高 */
int main(void)
{
double number1=0.0;
double number2=0.0;
char letter=0;
char operation=0;
char choose='n'; //
printf("\nEnter the calculation\n");
scanf("%lf %c %lf",&number1,&operation,&number2);
fflush(stdin); //
printf("\nY or N ?\n"); //
scanf("%c",&choose); //
if((choose=='n')||(choose=='N'))//
goto finish;//
switch(operation)
{
case'+': printf("= %.2lf\n",number1+number2); break;
case'-': printf("= %.2lf\n",number1-number2); break;
case'*': printf("= %.2lf\n",number1*number2); break;
case'/': if(number2==0) printf("\n\n\aDivision by zero error!\n");
else printf("= %.2lf\n",number1/number2); break;
case'%': if((long)number2==0) printf("\n\n\aDivision by zero error!\n");
else printf("= %ld\n",(long)number1%(long)number2); break;
default: printf("\n\n\aIllegal operation!\n"); break;
}
finish: //
return 0;
}
/* 另外多嘴的说一下,一般大家不用goto,你要求的这个功能用if-else也可以实现的,而且代码的可靠性高 */
int main(void)
{
double number1=0.0;
double number2=0.0;
char letter=0;
char operation=0;
char choose='n'; //
printf("\nEnter the calculation\n");
scanf("%lf %c %lf",&number1,&operation,&number2);
fflush(stdin); //
printf("\nY or N ?\n"); //
scanf("%c",&choose); //
if((choose=='n')||(choose=='N'))//
goto finish;//
switch(operation)
{
case'+': printf("= %.2lf\n",number1+number2); break;
case'-': printf("= %.2lf\n",number1-number2); break;
case'*': printf("= %.2lf\n",number1*number2); break;
case'/': if(number2==0) printf("\n\n\aDivision by zero error!\n");
else printf("= %.2lf\n",number1/number2); break;
case'%': if((long)number2==0) printf("\n\n\aDivision by zero error!\n");
else printf("= %ld\n",(long)number1%(long)number2); break;
default: printf("\n\n\aIllegal operation!\n"); break;
}
finish: //
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询