C primer plus sixth edition chapter 4 programming exercises answers

C primer plus sixth edition 6th edition 004 chapter 4 programming practice answers Chinese

4.1

 /*编写一个程序,提示用户输入名和姓,然后以“名,姓”的格式打印出出来*/
    #include <stdio.h>
    int main(void) 
    {
    	char first_name[40], last_name[40];
    	printf("Please enter your first name and last name: ");
    	scanf("%s %s", &first_name, &last_name);
    	printf("%s,%s", last_name, first_name);
    
    	return 0;
    }

4.2

/*编写程序,提示用户输入名和姓,并执行以下操作*/

#include <stdio.h>
#include <string.h>

int main(void)
{
	char first_name[40], last_name[40];
	int first_width;
	int last_width;
	printf("Please enter your first name and last name: ");
	scanf("%s %s", &first_name, &last_name);
	
	//打印名和姓,包括双引号
	printf("\"%s\",\"%s\"\n", last_name, first_name);

	
	//在宽度为20的字段右端打印名和姓
	printf("\"%20s\",\"%20s\"\n", last_name, first_name);
	
	//在宽度为20的字段左端打印名和姓
	printf("\"%-20s\",\"%-20s\"\n", last_name, first_name);
	
	//在比姓名宽度3的字段中打印名和姓
	first_width = strlen(first_name) + 3;
	last_width = strlen(last_name) + 3;
	printf("\"%*s\",\"%*s\"", last_width, last_name, first_width, first_name);
	
	return 0;
}

4.3

/*编写一个程序,读取一个浮点数,首先以小数点记数法打印,
然后以指数记数法打印。用下面的格式进行输出*/

#include <stdio.h>

int main(void)
{
	float num;
	printf("Please enter a float number: ");
	scanf("%f", &num);
	printf("小数点计数法:%f\n", num);
	printf("指数计数发: %e", num);
}

4.4

/*编写一个程序,提示用户输入身高(单位:英寸)和姓名
然后以下面的格式显示用户刚输入的信息*/

#include <stdio.h>

int main(void)
{
	float height;
	printf("Please enter your height(inch):");
	scanf("%f", &height);
	printf("Dabney, you are %1.3f feet tall.", height);

}

4.5

/*编写一个程序,提示用户输入以兆位每秒(Mb/s)为单位的下载速度
和以兆字节(MB)为单位的文件大小。计算文件下载的时间*/

#include <stdio.h>

int main(void)
{
	float volume, speed, time;

	printf("Please enter your file size(MB): ");
	scanf("%f", &volume);
	printf("Please enter your net speed(Mb/s): ");
	scanf("%f", &speed);

	time = volume * 8 / speed; //一字节=8位
	printf("\n");
	printf("At %.2f megabits per second, a file of %.2f megabytes downloads in %.2f second",
		speed, volume, time);
	return 0;
}

4.6

/*编写一个程序,先提示用户输入名,然后提示用户输入姓
在一行打印用户输入的名和姓,下一行分别打印名和姓的字母数。
字母名和姓的结尾对其*/

#include <stdio.h>
#include <string.h>

int main(void)
{
	char first_name[40], last_name[40];
	int first_width, last_width; //定义名和姓的长度变量

	printf("Please enter your first name: ");
	scanf("%s", &first_name);
	printf("Please enter your last name: ");
	scanf("%s", &last_name);

	first_width = strlen(first_name);
	last_width = strlen(last_name);

	//字母个数与姓名结尾对其
	printf("%s %s\n", first_name, last_name);
	printf("%*d %*d\n", first_width, first_width, last_width, last_width);

	//字母个数与姓名首部对其
	printf("%s %s\n", first_name, last_name);
	printf("%-*d %-*d\n", first_width, first_width, last_width, last_width);
}

4.7

/*题目太长,不抄*/

#include <stdio.h>
#include <float.h>

int main(void)
{
	double a = 1.0 / 3.0;
	float b = 1.0 / 3.0;

	//显示小数点后6为数字
	printf("%.6f %.6f\n", a, b);
	//显示小数点后12位数字
	printf("%.12f %.12f\n", a, b);
	//显示小数点后16位数字
	printf("%.16f %.16f\n", a, b);

	printf("%d %d", FLT_DIG, DBL_DIG);

	return 0;
}

4.8

#include <stdio.h>
#include <float.h>
int main(){
    
    const double statuteMileToKilometer = 1.609;
    const double gallonToLitre = 3.785;
    
    
    double statuteMile;
    double gallon;
    double consumption;
    
    printf("里程英里\n");
    
    scanf("%lf",&statuteMile);
    
    printf("加仑耗油量\n");
    scanf("%lf",&gallon);
    
    
    
    double kilometer = statuteMile/statuteMileToKilometer;
    
    //一公里的耗油量
    double litre = gallon/kilometer/gallonToLitre;
    consumption = litre * 100;
    
    printf("100公里耗油量是:  %.1f/100",consumption);

    return 0;
}

Welcome to Markdown Editor

Hello! This is the welcome page displayed for the first time you use the Markdown editor . If you want to learn how to use the Markdown editor, you can read this article carefully to understand the basic syntax of Markdown.

new changes

We have made some functional extensions and syntax support for the Markdown editor. In addition to the standard Markdown editor functions, we have added the following new features to help you write blogs with it:

  1. The new interface design will bring a new writing experience;
  2. Set your favorite code highlighting style in the authoring center, and Markdown will display the selected highlighting style in the code snippet ;
  3. Added image dragging function, you can directly drag and drop local images to the editing area for direct display;
  4. Brand new KaTeX math formula syntax;
  5. Added the mermaid syntax 1 function that supports Gantt charts ;
  6. Added the function of editing Markdown articles on multiple screens;
  7. Added functions such as focus writing mode, preview mode, concise writing mode, and left and right area synchronization wheel settings . The function button is located in the middle of the editing area and the preview area;
  8. Added checklist functionality.

Function shortcut key

Ctrl/Command+ Undo: + Z
Redo: Ctrl/Command+ Y
Bold: Ctrl/Command+ B
Italic: Ctrl/Command+ I
Title: Ctrl/Command+ Shift+ H
Unordered List: Ctrl/Command+ Shift+ U
Ordered List: Ctrl/Command+ Shift+ O
Check List: + +Insert Code : + Ctrl/Command+ Insert Link: + + Insert Image: + +ShiftC
Ctrl/CommandShiftK
Ctrl/CommandShiftL
Ctrl/CommandShiftG

Reasonable creation of titles is helpful for the generation of catalogs

Enter 1 time directly #and press it space, and a level 1 title will be generated.
After entering 2 times #and pressing it space, a 2-level title will be generated.
By analogy, we support 6 levels of headings. TOCHelps to generate a perfect table of contents after using syntax.

How to change the style of text

emphasized text emphasized text

bold text bold text

markup text

delete text

quote text

H2O is a liquid .

2 10 operation result is 1024.

Insert links and images

link: link .

picture:Alt

Picture with dimensions:Alt

Centered image:Alt

Centered and sized images:Alt

Of course, in order to make it more convenient for users, we have added the function of dragging and dropping pictures.

How to insert a nice piece of code

Go to the blog settings page, choose a code snippet highlighting style you like, and the same highlighting is shown below 代码片.

// An highlighted block
var foo = 'bar';

Generate a list that works for you

  • project
    • project
      • project
  1. item 1
  2. item 2
  3. item 3
  • Scheduled Tasks
  • mission accomplished

create a form

A simple table is created like this:

project Value
computer $1600
cell phone $12
catheter $1

Set the content to be centered, left, and right

Use :---------:Center
Use :----------Left
Use ----------:Right

first row The second column third column
Center the first column of text The second column of text is right-aligned Third column text left

SmartyPants

SmartyPants converts ASCII punctuation characters into "smart" typographical punctuation HTML entities. For example:

TYPE ASCII HTML
Single backticks 'Isn't this fun?' ‘Isn’t this fun?’
Quotes "Isn't this fun?" “Isn’t this fun?”
Dashes -- is en-dash, --- is em-dash – is en-dash, — is em-dash

create a custom list

Markdown
Text-to- HTML conversion tool
Authors
John
Luke

How to create a footnote

A text with footnotes. 2

Annotations are also essential

Markdown converts text to HTML .

KaTeX math formula

You can render LaTeX math expressions using KaTeX :

Gamma infrastructure ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb NC ( n )=(n1)!nN is the integral by Euler

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t &ThinSpace; . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. C ( z )=0tz 1 etdt.

You can find more information about LaTeX mathematical expressions here .

New Gantt chart feature to enrich your articles

Mon 06 Mon 13 Mon 20 已完成 进行中 计划一 计划二 现有任务 Adding GANTT diagram functionality to mermaid
  • For Gantt chart syntax, refer to here ,

UML diagrams

Can be rendered using UML diagrams. Mermaid . For example, a sequence diagram generated below:

张三 李四 王五 你好!李四, 最近怎么样? 你最近怎么样,王五? 我很好,谢谢! 我很好,谢谢! 李四想了很长时间, 文字太长了 不适合放在一行. 打量着王五... 很好... 王五, 你怎么样? 张三 李四 王五

This will generate a flowchart. :

链接
长方形
圆角长方形
菱形
  • For Mermaid syntax, refer to here ,

FLowchart flow chart

We will still support the flowchart of flowchart:

Created with Raphaël 2.2.0 开始 我的操作 确认? 结束 yes no
  • For Flowchart flowchart syntax, refer to here .

Export and Import

export

If you want to try using this editor, you can edit it freely in this article. When you finish writing an article, find the article export on the upper toolbar , and generate a .md file or .html file for local storage.

import

If you want to load an .md file or .html file that you have written, you can select the import function on the upper toolbar to import the file with the corresponding extension and
continue your creation.


  1. mermaid syntax description ↩︎

  2. Explanation of footnotes↩︎

Guess you like

Origin blog.csdn.net/Double____C/article/details/87564477