天天看點

C Primer Plus 第六版程式設計練習第四章答案和記錄一些學習曆程程式設計題部分:

程式設計題部分:

1,編寫一個程式,提示使用者輸入名和姓,然後以“名,姓”的格式列印出來。

#include <stdio.h> 
int main(void) {     
	char fname[40];     
	char lname[40];          
	printf("Enter your first name: ");     
	scanf("%s", fname);     
	printf("Enter your last name: ");     
	scanf("%s", lname);     
	printf("%s, %s\n", lname, fname);     
	return 0; 
}
           

運作結果:

C Primer Plus 第六版程式設計練習第四章答案和記錄一些學習曆程程式設計題部分:

2,編寫一個程式,提示使用者輸入名字,并執行以下操作:

a.列印名字,包括雙引号:

b.在寬度為20的字段右端列印名字,包括雙引号;

c.在寬度為20的字段左端列印名字,包括雙引号;

d.在比姓名寬度寬3的字段中列印名字

#include <stdio.h>
#include <string.h>
int main(void) 
{	
	char name[40];
	int x;
	printf("please enter your name:");
	scanf("%s",name);
	printf("\"%s\"\n",name);
	printf("\"%20s\"\n",name);
	printf("\"%-20s\"\n",name);
	x=strlen(name)+3;
	printf("%*s",x,name);
	
	return 0; 
 } 
           

運作結果:

C Primer Plus 第六版程式設計練習第四章答案和記錄一些學習曆程程式設計題部分:

3,編寫一個程式,讀取一個浮點數,首先以小數點計數法列印,然後以指數計數法列印。用下面的格式進行輸出:

a. The input is 21.3 or 2.1e+001.

b. The input is +21.290 or 2.129E+001

#include <stdio.h>
#include <string.h>
int main(void) 
{	
	float num;
	printf("please enter your number:");
	scanf("%f",&num);
	printf("the input is %.1f or %.1e\n",num,num);
	printf("the input is %+5.3f or %4.3E\n",num,num);
	return 0; 
 } 
           

運作結果:

C Primer Plus 第六版程式設計練習第四章答案和記錄一些學習曆程程式設計題部分:

4,編寫一個程式,提示使用者輸入身高(機關:英寸)和姓名,然後以下面的格式顯示使用者剛才輸入的資訊:

Dabney, you are 6.208 feet tall

使用float類型,并用/作為除号。如果你願意,可以要求使用者以厘米為機關輸入身高,并以米為機關顯示出來。

#include <stdio.h>
#include <string.h>
int main(void) 
{	
	float height;
	char name[40];
	printf("please enter your height:\n");
	scanf("%f", &height);
	printf("please enter your name:\n");
	scanf("%s",&name);
	height /=12;
	printf("%s, you are %4.3f feet tall.\n",name,height);
	return 0; 
 } 
           

運作結果:

C Primer Plus 第六版程式設計練習第四章答案和記錄一些學習曆程程式設計題部分:

5,編寫一個程式,提示使用者輸入以兆位每秒為機關的下載下傳速度和以兆位元組為機關的檔案大小,程式應計算檔案的下載下傳時間。注意,這裡一位元組等于八位。使用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(void) 
{	
	float speed;
	float file;
	float time;
	
	printf("please enter the speed of your download by Mb/s:\n");
	scanf("%f", &speed);
	fflush(stdin);
	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; 
 } 

           

運作結果:

C Primer Plus 第六版程式設計練習第四章答案和記錄一些學習曆程程式設計題部分:

6,編寫一個程式,先提示使用者輸入名,然後提示使用者輸入姓。在一行列印使用者輸入的名和姓,下一行分别列印名和姓的字母數。字母數要與相應名和姓的結尾對齊,如下所示:

Melissa Honeybee

7 8

接下來,再列印相同的資訊,但是字母個數與相應名和姓的開頭對齊,如下所示:

Melissa Honeybee

7 8

#include <stdio.h>
#include <string.h>
int main(void) 
{	
	char fname[40];
	char lname[40];
	
	printf("please enter your firstname:\n");
	scanf("%s", fname);
	printf("please enter your lastname:\n");
	scanf("%s", lname);
	printf("%s %s\n",fname,lname);
	printf("%*d%*d\n",strlen(fname),strlen(fname),strlen(lname)+1,strlen(lname));
	printf("%s %s\n",fname,lname);
	printf("%-d%*d\n",strlen(fname),strlen(fname)+1,strlen(lname));
	return 0; 
 } 
           

運作結果:

C Primer Plus 第六版程式設計練習第四章答案和記錄一些學習曆程程式設計題部分:

7,編寫一個程式,将一個double類型的變量設定為1.0/3.0,一個float類型的變量設定為1.0/3.0.分别顯示兩次計算的結果各三次:一次顯示小數點後面6位數字:一次顯示小數點後面12位數字:一次顯示小數點後面16位數字。程式要包括float.h頭檔案,并顯示FLT_DIG和DBL_DIG的值。1.0/3.0的值與這些值一緻嗎?

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

int main(void) 
{	
	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; 
 } 
           

運作結果:

C Primer Plus 第六版程式設計練習第四章答案和記錄一些學習曆程程式設計題部分:

8,編寫一個程式,提示使用者輸入旅行的裡程和消耗的汽油量。然後計算并顯示消耗每加侖汽油行駛的英裡數,顯示小數點後面一位數字。接下來,使用一加侖大約3.785升,一英裡大約為1.609千米,把機關是英裡/加侖的值轉換為升/100公裡,并顯示結果,顯示小數點後面一位數字。注意,美國采用的方案測量消耗機關燃料的行程(值越大越好),而歐洲則采用機關距離消耗的燃料測量方案(值越低越好)。使用#define建立符号常量或使用const限定符建立變量來表示兩個轉換系數。

#include <stdio.h>
#include <string.h>
#include <float.h>
#define galon 3.785/1.609
int main(void) 
{	
	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; 
 } 
           

運作結果:

C Primer Plus 第六版程式設計練習第四章答案和記錄一些學習曆程程式設計題部分:

繼續閱讀