70,005
社区成员




1 // P95: 2.编程题 (6)调用库函数编程求表达式cos(39.5度)+(e的2.916次幂)+(3.16的算数方根)
2
3 // The begining of C program: test03-06.c.
4
5 // Header files included.
6 #include <stdio.h>
7 #include <math.h>
8
9 // Macro definition.
10 #define PI 3.1415926
11
12 // Main function's declaration.
13 int main ( void )
14 {
15 long int temp=0;
16 float result=0.0;
17 result = cos(39.5*PI/180) + exp(2.916) + sqrt(3.16);
18 temp = (long int) (result * 100);
19 temp = (long int) ((result*100 - temp)*2 + temp);
20 result = temp / 100.0;
21 printf ( "Result: cos(39.5*PI/180) + exp(2.916) + sqrt(3.16) = %.2lf\n",
22 result );
23
24 return (0);
25 }
26
27 // The end of C program: test03-06.c.
28
1 // P120: 3.编程题 (3)
2
3 // The begining of C program: test04-03.c.
4
5 // Header files included.
6 #include <stdio.h>
7 #include <math.h>
8
9 // Main function's declaration.
10 int main ( void )
11 {
12 long int i_tag=0;
13 float f_num1, f_num2, f_num3;
14 double area=0.0, temp=0.0;
15 printf ( "Input the three side's length of triangle: " );
16 scanf ( "%f %f %f", &f_num1, &f_num2, &f_num3 );
17 if ( (f_num1<=0) || (f_num2<=0) || (f_num3<=0) )
18 {
19 printf ( "Input error, data is illegal!\n" );
20 return (1);
21 }
22 if ( ((fabs(f_num2-f_num3) < f_num1) && (f_num1 < (f_num2+f_num3))) ||
23 ((fabs(f_num1-f_num3) < f_num2) && (f_num2 < (f_num1+f_num3))) ||
24 ((fabs(f_num1-f_num2) < f_num3) && (f_num3 < (f_num1+f_num2))) )
25 {
26 printf ( "The three sides can form a triangle.\n" );
27 temp = ( f_num1 + f_num2 + f_num3 ) / 2;
28 area = temp*fabs(f_num1-temp)*fabs(f_num2-temp)*fabs(f_num3-temp);
29 area = sqrt(area);
30
31 // 对所求结果四舍五入并保留到小数点后两位之后输出
32 i_tag = (long int) (area * 100);
33 i_tag = (long int) ((area*100 - i_tag)*2 + i_tag);
34 area = i_tag / 100.0;
35 printf ( "The triangle's area is: %-.2lf\n", area );
36
37 return (0);
38 }
39 else
40 printf ( "The three sides can't form a triangle.\n" );
41
42 return (0);
43 }
44
45 // The end of C program: eg04-03.c.
46