关于C语言的一个程序问题
#include<stdio.h>#include<ctype.h>voidmain(){doublenumber1=0.0;doublenumber2=0.0;char...
#include<stdio.h>
#include<ctype.h>
void main()
{
double number1=0.0;
double number2=0.0;
char operation=0;
char ch=0;
printf("Please input number1,number2 and operation.");
scanf("%lf,%c,%lf",&number1,&operation,&number2);
switch(operation)
{
case '+':
printf("=%.2lf",number1+number2);
printf("\nEnter 'y' or 'Y' and 'n' or 'N' for next calculation");
/*scanf("%c",&ch);*/
/*toupper(ch);
if('ch'=='Y')*/
/* printf("%c",ch);*/
/*goto case ;*/
/*else*/
break;
case '-':
printf("\n=%.2lf",number1-number2);
break;
case '*':
printf("\n%.2lf",(number1*number2));
break;
case '/':
if(number2!=0)
printf("\n%.2lf",number1/number2);
else
printf("\nError!");
break;
case '%':
if(number2!=0)
printf("\n%.1d",(int)number1%(int)number2);
else
printf("\nError!");
break;
default :
printf("\nTry it again!");
break;
}
}
我想只用goto语句实现,给用户两种选择,输入y或Y ,就执行下一个计算,输入n或N,结束程序,我在程序插入了goto语句,但出现问题。 展开
#include<ctype.h>
void main()
{
double number1=0.0;
double number2=0.0;
char operation=0;
char ch=0;
printf("Please input number1,number2 and operation.");
scanf("%lf,%c,%lf",&number1,&operation,&number2);
switch(operation)
{
case '+':
printf("=%.2lf",number1+number2);
printf("\nEnter 'y' or 'Y' and 'n' or 'N' for next calculation");
/*scanf("%c",&ch);*/
/*toupper(ch);
if('ch'=='Y')*/
/* printf("%c",ch);*/
/*goto case ;*/
/*else*/
break;
case '-':
printf("\n=%.2lf",number1-number2);
break;
case '*':
printf("\n%.2lf",(number1*number2));
break;
case '/':
if(number2!=0)
printf("\n%.2lf",number1/number2);
else
printf("\nError!");
break;
case '%':
if(number2!=0)
printf("\n%.1d",(int)number1%(int)number2);
else
printf("\nError!");
break;
default :
printf("\nTry it again!");
break;
}
}
我想只用goto语句实现,给用户两种选择,输入y或Y ,就执行下一个计算,输入n或N,结束程序,我在程序插入了goto语句,但出现问题。 展开
1个回答
2009-02-26
展开全部
给,已经编译运行通过:
#include<stdio.h>
#include<ctype.h>
void main()
{
double number1=0.0;
double number2=0.0;
char operation=0;
char ch=0;
START: //加上这个
printf("Please input number1,number2 and operation.");
scanf("%lf,%c,%lf",&number1,&operation,&number2);
switch(operation)
{
case '+':
printf("=%.2lf",number1+number2);
printf("\nEnter 'y' or 'Y' and 'n' or 'N' for next calculation");
/*scanf("%c",&ch);*/
/*toupper(ch);
if('ch'=='Y')*/
/* printf("%c",ch);*/
/*goto case ;*/
/*else*/
break;
case '-':
printf("\n=%.2lf",number1-number2);
break;
case '*':
printf("\n%.2lf",(number1*number2));
break;
case '/':
if(number2!=0)
printf("\n%.2lf",number1/number2);
else
printf("\nError!");
break;
case '%':
if(number2!=0)
printf("\n%.1d",(int)number1%(int)number2);
else
printf("\nError!");
break;
default :
printf("\nTry it again!");
break;
}
printf("\nDo you want to try again?"); //加上这个
fflush(stdin);//加上这个
ch=getchar();//加上这个
if(ch=='y'||ch=='Y') goto START; //加上这个
}
#include<stdio.h>
#include<ctype.h>
void main()
{
double number1=0.0;
double number2=0.0;
char operation=0;
char ch=0;
START: //加上这个
printf("Please input number1,number2 and operation.");
scanf("%lf,%c,%lf",&number1,&operation,&number2);
switch(operation)
{
case '+':
printf("=%.2lf",number1+number2);
printf("\nEnter 'y' or 'Y' and 'n' or 'N' for next calculation");
/*scanf("%c",&ch);*/
/*toupper(ch);
if('ch'=='Y')*/
/* printf("%c",ch);*/
/*goto case ;*/
/*else*/
break;
case '-':
printf("\n=%.2lf",number1-number2);
break;
case '*':
printf("\n%.2lf",(number1*number2));
break;
case '/':
if(number2!=0)
printf("\n%.2lf",number1/number2);
else
printf("\nError!");
break;
case '%':
if(number2!=0)
printf("\n%.1d",(int)number1%(int)number2);
else
printf("\nError!");
break;
default :
printf("\nTry it again!");
break;
}
printf("\nDo you want to try again?"); //加上这个
fflush(stdin);//加上这个
ch=getchar();//加上这个
if(ch=='y'||ch=='Y') goto START; //加上这个
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询