如何把下面C语言的代码改成含有goto语句,让用户选择输入Y或y,以执行计算;输入N或者n,就结束程序!

/* 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;}

/* 加“//”是我加上的代码,程序可以运行,我试过了 */
/* 另外多嘴的说一下,一般大家不用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;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-02-03
你这代码密密麻麻一片,看得都眼花,代码尽量一句占一行,有良好的缩进,别人才好给你看啊
相似回答