能不能问一下这种情况怎么解决?
我想做四则运算的那个课后作业,我的代码是这样的#include<stdio.h>
int main()
{
float a, b;
char c;
printf("please enter the number you want to calculate:");
scanf("%.2f %.2f", &a, &b);
getchar();
printf("please enter the algorithm you want to apply:");
scanf("%c", &c);
switch (c)
{
case '+' : printf("The result is a + b!\n"); break;
case '-' : printf("The result is a - b!\n"); break;
case '*' : printf("The result is a * b!\n"); break;
case '/' : printf("The result is a / b!\n"); break;
default : printf("Are you looking for trouble?\n");
}
return 0;
}
gcc运行后,是这样显示的(见图片)请问怎么解决 scanf和printf的用法是不一样的
这是scanf的用法
https://cplusplus.com/reference/cstdio/scanf/
人造人 发表于 2022-8-22 21:30
scanf和printf的用法是不一样的
这是scanf的用法
https://cplusplus.com/reference/cstdio/scanf/
{:10_266:} switch (c)
{
case '+' : printf(“The result is a + b!\n”); break;
case '-' : printf(“The result is a - b!\n”); break;
case '*' : printf(“The result is a * b!\n”); break;
case '/' : printf(“the result is a / b!\n”); break;
default : printf(“你在寻找麻烦吗?\n”);
}
printf(“The result is %g!\n”, a + b);//要格式输出
输出错了
#include<stdio.h>
int main()
{
float a , b ;
char c ;
printf("please enter your expression : ") ;
scanf("%f%c%f", & a , & c , & b) ;
switch (c) {
case '+' : printf("The result is %.2f\n" , a + b) ; break ;
case '-' : printf("The result is %.2f\n" , a - b) ; break ;
case '*' : printf("The result is %.2f\n" , a * b) ; break ;
case '/' : if(b != 0) printf("The result is %.2f\n" , a / b) ; else printf("error : divide by zero !\n") ; break ;
default: printf("Are you looking for trouble ?\n") ;
}
}
编译、运行实况:
D:\\C>g++ -o x x.c
D:\\C>x
please enter your expression : 8+5
The result is 13.00
D:\\C>x
please enter your expression : 8/5
The result is 1.60
D:\\C>x
please enter your expression : 8/0
error : divide by zero !
D:\\C>x
please enter your expression : 3^3
Are you looking for trouble ?
D:\\C> aaron0919 发表于 2022-8-22 21:36
输出错了
哥,这么改好像不成功 jackz007 发表于 2022-8-22 21:45
编译、运行实况:
您的意思是我这样的代码是无法正常运行的,无法更正对吗,谢谢{:10_282:} 折折星 发表于 2022-8-22 21:45
哥,这么改好像不成功
是输出有误 #include<stdio.h>
int main()
{
float a, b;
char c;
printf("please enter the number you want to calculate:");
scanf("%f", &a);
scanf("%f", &b);
getchar();
printf("please enter the algorithm you want to apply:");
scanf("%c", &c);
switch (c)
{
case '+' : printf("The result is %.2f!\n",a + b); break;
case '-' : printf("The result is %.2f!\n",a - b); break;
case '*' : printf("The result is %.2f!\n",a * b); break;
case '/' : printf("The result is %.2f!\n",a / b); break;
default : printf("Are you looking for trouble?\n");
}
return 0;
} 折折星 发表于 2022-8-22 21:47
您的意思是我这样的代码是无法正常运行的,无法更正对吗,谢谢
我的意思是,给你一个成功的实例代码,是尽量比照你的代码改写的,你可以自己对比寻找差异,通过实验自己找出问题,这是一种重要的学习机会。 jackz007 发表于 2022-8-22 21:52
我的意思是,给你一个成功的实例代码,是尽量比照你的代码改写的,你可以自己对比寻找差异,通过 ...
谢谢谢谢我憨了{:10_250:} 临时号 发表于 2022-8-22 21:49
非常感谢! jackz007 发表于 2022-8-22 21:45
编译、运行实况:
大佬,我悟了!谢谢!
页:
[1]