|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 沐丶心 于 2020-4-25 23:46 编辑
写了半天
结果一输入字母(a,s,m,d,)就选择default的选项
有没有大佬帮忙找找错 #include <stdio.h>
void pr();
float add();//加
float subtract();//减
float multiply();//乘法
float divide();//除
char wdnmd();
float first();
float last();
int main()
{
pr();
char ch;
while((ch = wdnmd()) != 'q')
{
switch (ch)
{
case 'a':add();break;
case 's':subtract();break;
case 'm':multiply();break;
case 'd':divide();break;
default:printf("非法输入,请重新输入:");continue;
}
}
printf("game over");
return 0;
}
void pr()
{
printf("Enter the operation of your choice:\n");
printf("a. add(+) s. subtract(-)\n");
printf("m. mulit(*) d. divide(/)\n");
printf("q. quit\n");
}
char wdnmd()
{
char ch;
ch = getchar();
while(getchar() != '\n')
continue;
return ch;
}
float first()
{
float lbw;
char ch;
printf("Enter first number:");
while(scanf("%f", &lbw) == 0)
{
while((ch=getchar()) != '\n')
putchar(ch);
printf("is not an number\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
}
while(getchar()!= '\n')
continue;
return lbw;
}
float last()
{
float lbw;
char ch;
printf("Enter first number:");
while(scanf("%f", &lbw) == 0)
{
while((ch=getchar()) != '\n')
putchar(ch);
printf("is not an number.\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
}
while(getchar()!= '\n')
continue;
return lbw;
}
float add()
{
float a = first();
float b = last();
printf("%.2f + %.2f = %.2f\n", a, b, a+b);
}
float subtract()
{
float a = first();
float b = last();
printf("%.2f - %.2f = %.2f\n", a, b, a-b);
}
float multiply()
{
float a = first();
float b = last();
printf("%.2f * %.2f = %.2f\n", a, b, a*b);
}
float divide()
{
float a = first();
float b = last();
if(b == 0)
{
printf("除数不能为0,请重新输入:");
scanf("%f", &b);
}
printf("%.2f / %.2f = %.2f\n", a, b, a/b);
}
#include <stdio.h>
void pr();
float add();//加
float subtract();//减
float multiply();//乘法
float divide();//除
char wdnmd();
float first();
float last();
int main()
{
pr();
char ch;
//while(ch = wdnmd() != 'q')
while((ch = wdnmd()) != 'q')
{
switch (ch)
{
case 'a':add();break;
case 's':subtract();break;
case 'm':multiply();break;
case 'd':divide();break;
default:printf("非法输入,请重新输入:");continue;
}
}
printf("game over");
return 0;
}
void pr()
{
printf("Enter the operation of your choice:\n");
printf("a. add(+) s. subtract(-)\n");
printf("m. mulit(*) d. divide(/)\n");
printf("q. quit\n");
}
char wdnmd()
{
char ch;
ch = getchar();
while(getchar() != '\n')
continue;
return ch;
}
float first()
{
float ch;
printf("Enter first number:");
while((ch = getchar()) == 0)
{
while(getchar() != '\n')
putchar(ch);
printf("is not an number\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
}
while(getchar()!= '\n')
continue;
return ch;
}
float last()
{
float ch;
printf("Enter first number:");
while((ch = getchar()) == 0)
{
while(getchar() != '\n')
putchar(ch);
printf("is not an number.\nplease enter a number, such as 2.5, -1.78E8, or 3:1:");
}
while(getchar()!= '\n')
continue;
return ch;
}
float add()
{
float a = first();
float b = last();
printf("%.2f + %.2f = %.2f\n", a, b, a+b);
}
float subtract()
{
float a = first();
float b = last();
printf("%.2f + %.2f = %.2f\n", a, b, a-b);
}
float multiply()
{
float a = first();
float b = last();
printf("%.2f + %.2f = %.2f\n", a, b, a*b);
}
float divide()
{
float a = first();
float b = last();
if(b == 0)
{
printf("除数不能为0,请重新输入:");
scanf("%f", &b);
}
printf("%.2f + %.2f = %.2f\n", a, b, a/b);
}
|
|