C Primer Plus Test4
试题来源 :C Primer Plus(第6版) 中文版
本章主要为字符串的处理,包括printf函数的使用。

4.1 编写一个程序,提示用户输入名和姓,然后以“名,姓”的格式打印出来。
#include<stdio.h>
#include<stdlib.h>
int main(){
char first_name[20], last_name[20];
printf("Please enter your first name and last name");
scanf("%s %s", first_name, last_name); //由于两个变量为数组,所以不需要加&
printf("%s, %s\n", first_name, last_name);
system("pause");//避免闪退问题
return 0;
}


4.2 编写一个程序,提示用户输入名和姓,并执行一下操作:
a.打印名和姓,包括双引号
b.在宽度为20的字段右端打印名和姓,包括双引号
c.在宽度为20的字段左端打印名和姓,包括双引号
d.在比姓名宽度宽3的字段中打印名和姓
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char name[20];
printf("Please enter your name:\n");
scanf("%s", name);
printf("\"%s\"\n", name);
printf("\"%20s\"\n", name);
printf("\"%-20s\"\n", name);
printf("%*s", strlen(name)+3, name);
system("pause");
return 0;
}


4.3 编写一个程序,读取一个浮点数,首先以小数点计数法打印,然后以指数计数法打印。用下面的格式进行输出(系统不同,指数计数法显示的位数可能不同):
a.输入21.3或2.1e+001;
b.输入+21.290或2.129E+001;
#include<stdio.h>
#include<stdlib.h>
int main(){
float number;
printf("Please enter a float number:");
scanf("%f", &number);
printf("The input is %.1f or %.1e\n", number, number);
printf("The input is %.3f or %.3E\n", number, number);
system("pause");
return 0;
}


4.4 编写一个程序,提示用户输入身高(单位:英寸)和姓名,然后以下面的格式显示用户刚输入的信息:(1英尺=12英寸)
Dabney, you are 6.208 feet tall
使用float类型,并用/作为除号。如果你愿意,可以要求用户以厘米为单位显示身高,并以米为单位显示出来。
#include<stdio.h>
#include<stdlib.h>
int main(){
float height;
char name[20];
printf("Please enter your name and height:\n");
scanf("%s %f", name, &height);
printf("%s, you are %.3f feet tall.\n", name, height/12);
system("pause");
return 0;
}


4.5编写一个程序,提示用户输入以兆位每秒(Mb/s)为单位的下载速度和以兆字节(MB)为单位的文件大小。程序中应计算文件的下载时间。注意,这里1字节等于8位。使用float类型,并用/作为除号。该程序要以下面的格式打印3个变量的值(下载速度、文件大小和下载时间),显示小数点后面两位数字:
At 18.12 megabits per second, a file of 2.20 megabtyes downloads in 0.97 seconds.
#include<stdio.h>
#include<stdlib.h>
int main(){
float speed, size, time;
printf("Enter the download sppeds(Mb/s) and file size(MB)\n");
scanf("%f %f", $speed, $size);
time = (size * 8) / speed;
printf("At %.2f megabits per second, a file of %.2f megabtytes download in %.2f seconds\n",
speed, size, time);
system("pause");
return 0;
}


4.6 编写一个程序,先提示用户输入名,然后提示用户输入姓。在一行打印用户输入的名和姓,下一行分别打印名和姓的字母数。字母数要与相应名和姓的结尾对齐,如下所示:
Melissa Honeybee
7 8
接下来,再打印相同的信息,但是字母个数与相应名和姓的开头对齐,如下所示:
Melissa Honeybee
7 8
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char first_name[20], last_name[20];
printf("Pleas enter your first name:");
scanf("%s", first_name);
printf("Pleas enter your last name:");
scanf("%s", last_name);
printf("%s %s\n", first_name, last_name);
printf("%*d %*d\n", strlen(first_name), strlen(first_name), strlen(last_name), strlen(last_name));
printf("%s %s\n", first_name, last_name);
printf("%d %*d\n", strlen(first_name), strlen(last_name), strlen(last_name));
system("pause");
return 0;
}


4.7 编写一个程序,将一个double类型的变量设置位1.0/3.0,一个float类型的变量设置位1.0/3.0。分别显示两次计算的结果各3次:一次显示小数点后面6位数字;一次显示小数点后面12位数字;一次显示小数点后面16位数字。程序中要包含float.h头文件,并显示FLT_DIG和DBL_DIG的值。
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
int main(){
double d_num;
float f_num;
d_num = 1.0 / 3.0;
f_num = 1.0 / 3.0;
printf("double:%.6lf, float:%.6f\n", d_num, f_num);
printf("double:%.12lf, float:%.12f\n", d_num, f_num);
printf("double:%.16lf, float:%.16f\n", d_num, f_num);
printf("FLT_DIG:%d, DBL_DIG:%d\n", FLT_DIG, DBL_DIG);
system("pause");
return 0;
}


4.8编写一个程序,提示用户输入旅行的里程和消耗的汽油量。然后计算并显示消耗每加仑汽油行驶的英里数,显示小数点后面一位数字。接下来,每使用1加仑大约3.785升,1英里大约为1.609千米,把单位是英里/加仑的值转换为升/公里(欧洲通用的燃料消耗表示法),并显示结果,显示小数点后面1位数字。注意:美国采用的方案测量消耗单位燃料的行程(值越大越好),而欧洲则采用单位距离消耗的燃料测量方案(越小越好)。使用#define创建符号常量或使用const限定符创建变量来表示两个转换系数。
#include<stdio.h>
#include<stdib.h>
#define J_S 3.785 //转换系数
#define Y_Q 1.609 //距离转换系数
int main(){
float f_juli, f_qiyou;
printf("请输入距离(英里)和汽油消耗量(加仑)\n");
scanf("%f %f", &f_juli, &f_qiyou);
printf("%.1f 英里/加仑\n", f_juli / f_qiyou);
printf("%.1f 升/公里\n", (f_qiyou * J_S) / (f_juli * Y_Q ));
system("pause");
return 0;
}
