使用VC++, 输入程序不报错,但是结果很奇怪,想不明白,所以求助。
程序#include <stdio.h>
#include <math.h>
void main()
{
double x, s;
printf("input number:\n");
scanf("%1f", &x);
s = sin(x);
printf("sin of %1f is %1f\n", x, s);
}
启动后的结果
input number:
30
sin of -92559604425286806000000000000000000000000000000000000000000000.000000 is -0.048960
Press any key to continue
ps. 这个程序似乎是求未知数的sin用的,我输入的未知数是30, 但是结果却根30 没有任何关系。作为初学者,我无法凭借自己解决问题,所以诚信求助。 诚心求助,我打错别字了QAQ 本帖最后由 jackz007 于 2020-12-26 14:49 编辑
scanf("%1f", &x); /*红色字符应该是字母 'L' 的小写,而不是数字 '1' */
. . . . . .
printf("sin of %1f is %1f\n", x, s);/*红色字符应该是字母 'L' 的小写,而不是数字 '1' */
30 度应该输入0.523599
60 度应该输入1.047198
进行测试。
30 度是 pi / 6 = 3.1415926 / 6
60 度是 pi / 3 = 3.1415926 / 3 本帖最后由 昨非 于 2020-12-26 14:45 编辑
#include <stdio.h>
#include <math.h>
void main()
{
double x, s;
printf("input number:\n");
scanf("%lf", &x);//%lf,l是L的小写,不是数字1
s = sin(x);
printf("sin of %lf is %lf\n", x, s);
}
如果要限制位数,可以
#include <stdio.h>
#include <math.h>
void main()
{
double x, s;
printf("input number:\n");
scanf("%lf", &x);
s = sin(x);
printf("sin of %.3lf is %.3lf\n", x, s);//显示三位小数
}
测试结果:
input number:
30
sin of 30.000 is -0.988
jackz007 发表于 2020-12-26 14:35
scanf("%1f", &x); /*红色字符应该是字母 'L' 的小写,而不是数字 ' ...
谢谢你,容我称你一声老师 昨非 发表于 2020-12-26 14:43
如果要限制位数,可以
测试结果:
谢谢,原来是这样,感激不尽
页:
[1]