第四章编程练习
*4-2
#include <stdio.h> // #include <string.h> int main(int argc, char const *argv[]) { char name[10]; printf("give me your name:"); scanf("%s", name ); printf("\"%s\"\n", name); printf("\"%18s\"\n", name); printf("\"%-18s\"\n", name); int b = strlen(name) + 3; printf("\"%*s\"\n", b, name); return 0; } /* give me your name:elon "elon" " elon" "elon " " elon" */
*4-3
#include <stdio.h> int main(int argc, char const *argv[]) { float a; printf("give me a float number:"); scanf("%f", &a); printf("The input is %.1f or %.1e.\n", a, a); printf("The input is %+.3f or %.3E.\n", a, a); return 0; } /* give me a float number:21.29 The input is 21.3 or 2.1e+001. The input is +21.290 or 2.129E+001. */
4-4
#include <stdio.h> int main(int argc, char const *argv[]) { float a; char name[20]; printf("give me your height(inches):"); scanf("%f", &a); printf("your name please:"); scanf("%s", name); printf("%s, you are %.3f feet tall\n", name, a/12.0 ); return 0; } /* give me your height(inches):74.496 your name please:Dabney Dabney, you are 6.208 feet tall */
*4-5
#include <stdio.h> int main(int argc, char const *argv[]) { float speed, file_size, dtime; printf("Enter the download speed(Mb/s):"); scanf("%f", &speed); printf("Enter the size of file(MB):"); scanf("%f", &file_size); dtime = file_size * 8 / speed; printf("At %.2f megabits per second,a file of %.2f megabytes \ download in %.2f seconds.\n", speed, file_size, dtime ); return 0; } /* Enter the download speed(Mb/s):18.12 Enter the size of file(MB):2.20 At 18.12 megabits per second,a file of 2.20 megabytes download in 0.97 seconds. */
*4-6
#include <stdio.h> // #include <string.h> int main(int argc, char const *argv[]) { char fname[20]; char lname[20]; printf("Enter your first name:"); scanf("%s", fname); printf("Enter your last name:"); scanf("%s", lname); int fname_len = strlen(fname); int lname_len = strlen(lname); printf("%s %s\n", fname, lname ); printf("%*d %*d\n", fname_len, fname_len, lname_len, lname_len ); printf("%s %s\n", fname, lname ); printf("%-*d %-*d\n", fname_len, fname_len, lname_len, lname_len ); return 0; } /* Enter your first name:Melissa Enter your last name:Honeybee Melissa Honeybee 7 8 Melissa Honeybee 7 8 */
4-7

#include <stdio.h> #include <float.h> int main(int argc, char const *argv[]) { float f = 1.0/3.0; double d = 1.0/3.0; printf("%.6f\n", f ); printf("%.12f\n", f ); printf("%.16f\n", f ); printf("%.6f\n", d ); printf("%.12f\n", d ); printf("%.16f\n", d ); printf("FLT_DIG:%d\n", FLT_DIG); printf("DBL_DIG:%d\n", DBL_DIG); return 0; } /* 0.333333 0.333333343267 0.3333333432674408 0.333333 0.333333333333 0.3333333333333333 FLT_DIG:6 DBL_DIG:15 */

浙公网安备 33010602011771号