4.8 编程练习
1.编写一个程序,提示用户输入名和姓,然后以“名,姓”的格式打印出来。
#include<stdio.h>
#include<string.h>
int main(){
char name[40];
printf("Please enter your first and last name:");
scanf("%s,%s",name);
printf("%s",name);
return 0;
}
2.编写一个程序,提示用户输入名和姓,并执行一下操作:
a.打印名和姓,包括双引号;
b.在宽度为20的字段右端打印名和姓,包括双引号;
c.在宽度为20的字段左端打印名和姓,包括双引号;
d.在比姓名宽度宽3的字段中打印名和姓。
#include<stdio.h>
#include<string.h>
int main(){
char first_name[40];
char last_name[40];
int width;
printf("Please enter your first name:");
scanf("%s",first_name);
printf("Please enter your last name:");
scanf("%s",last_name);
printf("\"%s,%s\"\n",first_name,last_name);//a
printf("\"%20s,%20s\"\n",first_name,last_name);//b
printf("\"%-20s,%-20s\"\n",first_name,last_name);//c
width=strlen(first_name)+3;
printf("%*s,%s\n",width,first_name,last_name);//d 不会啊,没搞懂
return 0;
}
3.编写一个程序,读取一个浮点数,首先以小数点记数法打印,然后以指数记数法打印。用下面的格式进行输出(系统不同,指数记数法显示的位数可能不同):
a.输入21.3或2.1e+001;
b.输入+21.290或2.129E+001;
#include<stdio.h>
#include<string.h>
int main()
{
float a;
// a=21.290;
scanf("%f",&a);
printf("%.1f或%.1e\n",a,a);
printf("%+.3f或%.3e\n",a,a);
return 0;
}
4.编写一个程序,提示用户输入身高(单位:英寸)和姓名,然后以下面的格式显示用户刚输入的信息:
Dabney, you are 6.208 feet tall
使用float类型,并用/作为除号。如果你愿意,可以要求用户以厘米为单位输入身高,并以米为单位显示出来。
#include<stdio.h>
#include<string.h>
int main()
{
float inch;
char name[30];
printf("Please enter your height:\n");
scanf("%f",&inch);
printf("Please enter your name:\n");
scanf("%s",name);
printf("%s,you are %.3f feet tall\n",name,inch);
return 0;
}
5.编写一个程序,提示用户输入以兆位每秒(Mb/s)为单位的下载速度和以兆字节(MB)为单位的文件大小。程序中应计算文件的下载时间。注意,这里1字节等于8位。使用float类型,并用/作为除号。该程序要以下面的格式打印 3 个变量的值(下载速度、文件大小和下载时间),显示小数点后面两位数字:
At 18.12 megabits per second, a file of 2.20 megabytes
downloads in 0.97 seconds.
#include<stdio.h>
#include<string.h>
int main()
{
float speed;
float file;
float time;
printf("please enter the speed of your download by Mb/s:\n");
scanf("%f", &speed);
printf("please enter the size of your download file:\n");
scanf("%f",&file);
time=file*8/ speed;
printf("At %4.2f megabits per second, a file of %3.2f megabytes\n",speed,file);
printf("downloads in %3.2f seconds\n",time);
return 0;
}
6.编写一个程序,先提示用户输入名,然后提示用户输入姓。在一行打印用户输入的名和姓,下一行分别打印名和姓的字母数。字母数要与相应名和姓的结尾对齐,如下所示:
Melissa Honeybee
7 8
接下来,再打印相同的信息,但是字母个数与相应名和姓的开头对齐,如下所示:
Melissa Honeybee
7 8
#include<stdio.h>
#include<string.h>
int main()
{
char frist_name[30];
char last_name[30];
printf("please enter your frist name:\n");
scanf("%s", frist_name);
printf("please enter last name:\n");
scanf("%s",last_name);
printf("%s %s\n",frist_name,last_name);
printf("%*d%*d\n",strlen(frist_name),strlen(frist_name),strlen(last_name)+1,strlen(last_name));
return 0;
}
7.编写一个程序,将一个double类型的变量设置为1.0/3.0,一个float类型的变量设置为1.0/3.0。分别显示两次计算的结果各3次:一次显示小数点后面6位数字;一次显示小数点后面12位数字;一次显示小数点后面16位数字。程序中要包含float.h头文件,并显示FLT_DIG和DBL_DIG的值。1.0/3.0的值与这些值一致吗?
#include<stdio.h>
#include<float.h>
int main()
{
float ot_f = 1.0 / 3.0;
double ot_d = 1.0 / 3.0;
printf(" float values: ");
printf("%.6f %.12f %.16f\n", ot_f, ot_f, ot_f);
printf("double values: ");
printf("%.6f %.12f %.16f\n", ot_d, ot_d, ot_d);
printf("FLT_DIG: %d\n", FLT_DIG);
printf("DBL_DIG: %d\n", DBL_DIG);
return 0;
}
float 和 double 1.0/3.0 输出的3个数不同,因为它们的有效数字不同,6和15 。
8.编写一个程序,提示用户输入旅行的里程和消耗的汽油量。然后计算并显示消耗每加仑汽油行驶的英里数,显示小数点后面一位数字。接下来,使用1加仑大约3.785升,1英里大约为1.609千米,把单位是英里/加仑的值转换为升/100公里(欧洲通用的燃料消耗表示法),并显示结果,显示小数点后面 1 位数字。注意,美国采用的方案测量消耗单位燃料的行程(值越大越好),而欧洲则采用单位距离消耗的燃料测量方案(值越低越好)。使用#define 创建符号常量或使用 const 限定符创建变量来表示两个转换系数。
#include<stdio.h>
#include<float.h>
#define galon 3.785/1.609
int main()
{
const float mile =3.785/1.609;
float dis;
float gas;
float per;
printf("please enter the mile of your tarvel :\n");
scanf("%f",&dis);
printf("please enter the galon of your tarvel:\n");
scanf("%f",&gas);
per = dis/gas;
printf("your asume number is %.1f\n",per);
printf("europe number:%.1f",mile/per);
return 0;
}