C Primer Plus
第4章 字符串和格式化输入输出
4.8 编程练习
- 编写一个程序,提示用户输入名和姓,然后以“名,姓”的格式打印出来。
//
// Created by DingJiaxiong on 2023/2/5.
// 【第4章的编程练习1】
#include <stdio.h>
int main(void) {
char name[40];
char surname[40];
printf("Please input your first name: ");
scanf("%s", name);
printf("Please input your last name: ");
scanf("%s", surname);
printf("Hello, %s, %s", name, surname);
return 0;
}
运行结果
- 编写一个程序,提示用户输入名字,并执行以下操作:
a.打印名字,包括双引号;
b.在宽度为20的字段右端打印名字,并包括双引号;
c.在宽度为20的字段左端打印名字,并包括双引号:
d.在比姓名宽度长3的字段中打印名字。
//
// Created by DingJiaxiong on 2023/2/5.
// 【第4章的编程练习2】
#include <stdio.h>
int main(void) {
char name[40];
int width;
printf("Please input your name: ");
scanf("%s", name);
width = printf("\"%s\".\n", name);
width -= 4;
printf("\"%20s\".\n", name);
printf("\"%-20s\".\n", name);
printf("\"%*s\".", (width + 3), name);
return 0;
}
运行结果
- 编写一个程序,读取一个浮点数,首先以小数点计数法打印,然后以指数计数法打印,用下面的格式输出(系统不同,指数计数法显示的位数可能不同):
a. The input is 21.3 or 2.1e+001;
b. The input is +21.290 or 2.129E+001.
//
// Created by DingJiaxiong on 2023/2/5.
// 【第4章的编程练习3】
#include <stdio.h>
int main(void) {
float input;
printf("Enter a float number: ");
scanf("%f", &input);
printf("The input is %.1f or %.1e \n", input, input);
return 0;
}
运行结果
- 编写一个程序,提示用户输入身高(以英寸为单位)和姓名,然后以下面的格式显示用户刚输入的信息。
Dabney,you are 6.208 feet tall.
使用float类型,并用“/”作为除号。如果你愿意,可以要求用户以厘米为单位输入身高,并以米为单位显示出来。
//
// Created by DingJiaxiong on 2023/2/5.
// 【第4章的编程练习4】
#include <stdio.h>
int main() {
float heigh;
char name[40];
printf("Enter your name: ");
scanf("%s", name);
printf("Hi %s, how tall you are(inch): ", name);
scanf("%f", &heigh);
printf("%s, you are %.3f feet tall \n", name, heigh / 12.0);
return 0;
}
运行结果
- 编写一个程序,提示用户输入以兆位每秒(Mbit/s)为单位的下载速度和以兆字节(MB)为单位的文件大小。程序中应计算文件下载时间。注意,这里的1字节等于8位。使用float类型,并用“/”作为除号。该程序要以下面的格式打印3个变量(下载速度、文件大小和下载时间)的值,显示小数点后两位数字。
At 18.12 megabits per second,a file of 2.20 megabytes download in 0.97 seconds.
//
// Created by DingJiaxiong on 2023/2/5.
// 【第4章的编程练习5】
#include <stdio.h>
int main(void) {
float speed, size, time;
printf("Please input the net speed(megabits per second): ");
scanf("%f", &speed);
printf("Please input the file size(megabytes): ");
scanf("%f", &size);
time = size * 8 / speed;
printf("At %.2f megabits per second,a file of %.2f megabytes download in %.2f seconds.", speed, size, time);
return 0;
}
运行结果
- 编写一个程序,先提示用户输入名,然后提示用户输入姓。在一行打印用户输入的名和姓,下一行分别打印名和姓的字母数。字母数要与相应的名和姓结尾对齐,如下所示。
Melissa Honeybee
7 8
接下来,再打印相同的信息,但是字母数与相应名和姓的开头对齐,如下所示。
Melissa Honeybee
7 8
//
// Created by DingJiaxiong on 2023/2/5.
// 【第4章的编程练习6】
#include <stdio.h>
int main(void) {
char name[40], surname[40];
int wname, wsurname;
printf("Please input your first name: ");
scanf("%s", name);
printf("Please input your last name: ");
scanf("%s", surname);
printf("\n");
wname = printf("%s", name);
printf(" ");
wsurname = printf("%s", surname);
printf("\n%*d %*d", wname, wname, wsurname, wsurname);
printf("\n");
wname = printf("%s", name);
printf(" ");
wsurname = printf("%s", surname);
printf("\n%-*d %-*d", wname, wname, wsurname, wsurname);
return 0;
}
运行结果
- 编写一个程序,将一个double类型的变量设置为1.0/3.0,一个float类型的变量设置为1.0/3.0。分别计算两个表达式各3次:一次显示小数点后面的6位数字;一次显示小数点后面的12位数字;一次显示小数点后面的16位数字。程序中要包含float.h头文件,并显示FLT_DIG和DBL_DIG的值。1.0/3.0的值与这些值一致吗?
//
// Created by DingJiaxiong on 2023/2/5.
// 【第4章的编程练习7】
#include <stdio.h>
#include <float.h>
int main(void) {
double d_third = 1.0 / 3.0;
float f_third = 1.0 / 3.0;
printf("float of one third(6) = %.6f\n", f_third);
printf("float of one third(12) = %.12f\n", f_third);
printf("float of one third(16) = %.16f\n", f_third);
printf("double of one third(6) = %.6lf\n", d_third);
printf("double of one third(12) = %.12lf\n", d_third);
printf("double of one third(16) = %.16lf\n", d_third);
printf("FLT_DIG in float.h is %d\n", FLT_DIG);
printf("DBL_DIG in float.h is %d\n", DBL_DIG);
return 0;
}
运行结果
- 编写一个程序,首先要求用户输入旅行里程和消耗的汽油量,然后计算并显示消耗每加仑汽油行驶的英里数,显示小数点后的一位数字。接下来,根据1加仑大约等于3.785升,1英里大约等于1.609千米,把单位是英里/加仑的值转换为升/100千米(欧洲通用的燃料消耗表示法),并显示结果,显示小数点后面的1位数字。注意,美国测量消耗单位燃料的行程(值越大越好),而欧洲测量单位距离消耗的燃料(值越小越好)。使用#define创建符号常量或者使用const限定符创建变量来表示两个转换系数。
//
// Created by DingJiaxiong on 2023/2/5.
// 【第4章的编程练习8】
#include <stdio.h>
#define GALLON_TO_LITRE 3.785
#define MILE_TO_KM 1.609
int main(void) {
float range, oil;
printf("Please input the range you traveled(in mile):");
scanf("%f", &range);
printf("Please input the oil you traveled(in gallon):");
scanf("%f", &oil);
printf("In UAS, your oil wear is %.1f M/G\n", range / oil);
printf("In Europe, your oil wear is %.1fL/100KM", (oil * GALLON_TO_LITRE) / (range * MILE_TO_KM));
return 0;
}
运行结果