"C Primer Plus" (sixth edition) answers (4.8)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43219615/article/details/99879352

To use the "C Primer Plus" C beginners learning to prepare.

  1. Write a program that prompts the user to enter the name and last name, then print out the "Name, Last Name" format.
#include <stdio.h>
int main(int argc, char const **argv) {
	char name[20];
	char surname[20];
	
	printf("请输入名:");
	scanf("%s", name);
	
	printf("请输入姓:");
	scanf("%s", surname);
	 
	printf("Hello, %s %s", name, surname);
	return 0;
} 
  1. Write a program that prompts the user to enter the first and last name, and perform the following operations:
    . A printing and last name, the quotation marks;
    . B the width of the field the right end 20 of the printing and last name, the quotation marks;
    C in the width of the field 20. left print name and last name, the quotation marks;
    . D wider than the name of the print field name and surname 3.
#include <stdio.h>
#include <string.h>
int main(int argc, char const **argv) {
	char name[20];
	char surname[20];
	
	printf("请输入名:");
	scanf("%s", name);
	
	printf("请输入姓:");
	scanf("%s", surname);
	 
	char fullName[50];
	//strcpy字符串拷贝函数 
	strcpy(fullName, name);
	//strcat字符串连接函数 (在原字符串后面接上指定字符串) 
	strcat(fullName, " ");
	strcat(fullName, surname);
	
	printf("\"%20s\"\n", fullName);
	//负号表示左对齐 
	printf("\"%-20s\"\n", fullName);
	
	//strlen获取字符串长度 
	int length = strlen(fullName);
	//*用于通过变量指定宽度 
	printf("%*s", length+3, fullName);
	return 0;
} 
  1. Write a program that reads a floating point decimal notation printed first, then printed in exponential notation. With the following output formats (different systems, the output may be different): A
    output 001 + 21.3 or 2.1e;.
    B output +21.290 or 2.129E + 001;.
#include <stdio.h>
int main(int argc, char const **argv) {
	float f;
	
	scanf("%f", &f);
	//%g输出自适应格式  e输出时指数计数那里的E是小写 
	printf("%g %e\n", f, f);
	
	//正号表示输出时带正号 E输出时指数计数那里的E是大写 
	printf("%+g, %E", f, f); 
	
	return 0;
} 
  1. Write a program that prompts the user to enter height (inches) and name, and then display the information entered by the user in the following format.
#include <stdio.h>
int main(int argc, char const **argv) {
	float f;
	char name[20];
	
	printf("输入身高(英寸):");
	scanf("%f", &f); 
	
	printf("输入姓名:");
	scanf("%s", name);//字符数组的名字即字符数组的地址 
	
	printf("%s, you are %g feet tall.", name, f); 
	return 0;
} 
  1. Write a program that prompts the user to megabits per second (Mb / s) download speed units and megabytes (MB) units of file size. Program file download time should be calculated. Note that, where 1 byte is equal to 8 bits. Using the float type, and with / as a division sign. The program to print the values of the three variables in the following format, display two digits after the decimal point.
    Megebits 18.12 per SECOND, AT, A File of 2.20 megabytes
    Downloads in 0.97 seconds The.
#include <stdio.h>
int main(int argc, char const **argv) {
	float speed;
	float fileSize;
	
	printf("请输入下载速度(Mb/s)和文件大小(MB):") ;
	scanf("%f %f", &speed, &fileSize);
	
	/*一字节等于八位*/
	printf("At %.2f megebits per second, a file of %.2f megabytes\ndownloads in %.2f seconds.", speed, fileSize, (fileSize*8)/speed);
	return 0;
} 
  1. Write a program that prompts the user to enter the name first, and then prompts the user to enter the name. Print name and last name in a single line, the next line of the number of letters were printed name and last name. Number of letters to the end of the respective first and last names and aligned, as shown below.
    Picture format
#include <stdio.h>
#include <cstring>
int main(int argc, char const **argv) {
	char name[20];
	char surname[20];
	
	scanf("%s", name);
	scanf("%s", surname);
	
	printf("%s %s\n", name, surname);
	
	/*strlen函数获取字符串长度*/
	int length_name = strlen(name);
	int length_surname = strlen(surname);
	
	printf("%*d %*d", length_name, length_name, length_surname, length_surname);
	
	return 0;
} 
  1. Write a program, a double variable is set to 1.0 / 3.0, a float type variable is set to 1.0 / 3.0 The results are shown twice each three times: a 6-digit after the decimal point display; a display 12 after the decimal point digits; a display 16 digits after the decimal point. Float.h program to include the header file, and displays the value of FLT_DIG and DBL_DIG.
#include <stdio.h>
#include <float.h>
int main(int argc, char const **argv) {
	double d = 1.0 / 3.0;
	float f = 1.0 / 3.0; 
	
	printf("%.6lf %.6f\n", d, f);
	printf("%.12lf %.12f\n", d, f);
	printf("%.16lf %.16f\n", d, f);
	
	printf("FLT_DIG:%d DBL_DIG:%d\n", FLT_DIG, DBL_DIG);
	return 0;
} 
  1. Write a program that prompts the user to enter mileage and the amount of gasoline consumed in travel. And then calculates and displays the consumption miles per gallon, the display one digit after the decimal point. Next, a one gallon of about 3.785 liters, approximately one mile 1.609 kilometers, the units of miles / gallon value is converted to liters / 100 km, and displays the result, the display behind the decimal point decimal.
#include <stdio.h>
#define GALLON_PER_L 3.785
#define MILE_PER_KILOMETER 1.609
int main(int argc, char const **argv) {
	float mile;
	float gasoline;
	
	printf("请输入旅行的里程(英里)和汽油量(加仑):");
	scanf("%f %f", mile, gasoline);
	
	printf("每加仑汽油行驶的公里数:%.1f.\n", mile / gasoline);
	
	printf("每100千米燃烧的汽油量(升):%.1.f.", (mile * MILE_PER_KILOMETER * 100) / (gasoline * GALLON_PER_L));
	
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_43219615/article/details/99879352