最近在研读c primer plus,小白一枚,把自己做的答案写出来,望指正。
编程环境 visual studio 2010.第四章。
1 /* Programming Exercise 4-1 */
2 #include<stdio.h>
3 int main(void)
4 {
5 char fname[40];
6 char lname[40];
7 printf("please enter your first name
");
8 scanf("%s",&fname);
9 printf("please enter your last name
");
10 scanf("%s",&lname);
11 printf("%s,%s",fname,lname);
12 return 0;
13 }
1 /*programming exercise 4-2 */
2 #include <stdio.h>
3 #include<iostream>
4
5 int main(void)
6 {
7 char first_name[40];
8 char last_name[40];
9 int first_name_length = 0;
10 int last_name_lenght = 0;
11 printf("What's your first name:");
12 scanf("%s", first_name);
13
14 printf("What's your last name:");
15 scanf("%s", last_name);
16
17 getchar();
18
19 first_name_length = strlen(first_name);
20 last_name_lenght = strlen(last_name);
21
22 printf("a."%s,%s"
", first_name, last_name);
23 printf("b."%20s,%20s"
", first_name, last_name);
24 printf("c."%-20s,%-20s"
", first_name, last_name);
25 printf("d.%*s,%*s
", first_name_length+3, first_name, last_name_lenght+3, last_name);
26
27 return 0;
28 }
1 /*programming exercise 4-3*/
2 #include <stdio.h>
3 int main(void)
4 {
5 float n;
6 printf("enter a float number:
");
7 scanf("%f",&n);
8 printf("a. The input is%.1f or%.1e.
",n,n);
9 printf("b.The input is +%.3f or %.3E.
",n,n);
10 return 0;
11 }
1 /*programming exercise 4-4*/
2 #include <stdio.h>
3 #define CM_INCH 2.54
4 int main(void)
5 {
6 float height_cm=0;
7 float height_inch=0;
8 printf("enter your height:
");
9 scanf("%f",&height_inch);
10 getchar();
11 printf("Dabney,you are %f feet tall
",height_inch);
12 height_cm=height_inch*CM_INCH;
13 printf("Dabney,you are %f cm tall
",height_cm);
14 return 0;
15 }
1 #include<stdio.h>
2 int main(void)
3 {
4 float speed=0;
5 float file_size=0;
6 float download_time;
7 printf("How much your network speed:");
8 scanf_s("%f", &speed);
9 printf("How much is your download file:");
10 scanf_s("%f", &file_size);
11 download_time = file_size / (speed / 8);
12 printf("At %f megabits per secnod, a file of %f magebytes
download in %f seconds.
", speed, file_size, download_time);
13 }
1 /*programming exercise 4-7*/
2 #include<stdio.h>
3 #include<float.h>
4 int main(void)
5 {
6 double d_value = 1.0 / 3.0;
7 float f_value = 1.0 / 3.0;
8
9 printf("the value of FLT_DIG:%d, the value of DBL_DIG:%d
", FLT_DIG, DBL_DIG);
10 printf("the value of double:%.6lf, the value of float:%.6lf
", d_value, f_value);
11 printf("the value of double:%.12lf, the value of float:%.12lf
", d_value, f_value);
12 printf("the value of double:%.18lf, the value of float:%.18lf
", d_value, f_value);
13
14 }
1 /*programming exercise 4-6*/
2 #include <stdio.h>
3 #include<iostream>
4 /*programming exercise 4-6*/
5 int main(void)
6 {
7 char first_name[40] ;
8 char last_name[40] ;
9 int first_name_length = 0;
10 int last_name_length = 0;
11
12 printf("What's your first name:");
13 scanf("%s", first_name);
14 printf("What's your last name:");
15 scanf("%s", last_name);
16 first_name_length = strlen(first_name);
17 last_name_length = strlen(last_name);
18 printf("%s %s
", first_name, last_name);
19 printf("%*d %*d
", first_name_length, first_name_length, last_name_length, last_name_length);
20 printf("%s %s
", first_name, last_name);
21 printf("%-*d %-*d
", first_name_length, first_name_length, last_name_length, last_name_length);
22 }
1 /*programming exercise 4-8*/
2 #include <stdio.h>
3 #define KM_PER_MILE 1.609
4 #define PINT_PER_GALLON 3.785
5 int main(void)
6 {
7 float driven_distance = 0.0;
8 float gas_consumption = 0.0;
9 float pint_per_hundred_km = 0.0;
10 float mile_per_gallon = 0.0;
11 printf("How much distance have you traveled in kilometer:");
12 scanf_s("%f", &driven_distance);
13 getchar();
14
15 printf("How much gas have you used in pint:");
16 scanf_s("%f", &gas_consumption);
17 getchar();
18
19 pint_per_hundred_km = gas_consumption / driven_distance * 100;
20 mile_per_gallon = (driven_distance / KM_PER_MILE) / (gas_consumption / PINT_PER_GALLON);
21
22 printf("Fuel consumptions:%f pint/100km or %f mile/gallon
", pint_per_hundred_km, mile_per_gallon);
23 }