1. 程式人生 > >C Primer Plus 6 第四章程式設計練習

C Primer Plus 6 第四章程式設計練習

要點:

(1)scanf()函式的讀入特點

(2)掌握scanf()與printf()函式中欄位寬度和精度的用法

(3)strlen()函式的作用

1.關於scanf()讀入字串時遇到空格的結果測試:

//talkback.c——演示與使用者互動
#include<stdio.h>
#include<string.h>	//strlen()函式的原則
#define DENSITY 62.4	//人體密度(立方英尺)
int main(void)
{
	float weight, volume;
	int size, letters;
	char name[40];	//name是一個可容納40個字元的陣列

	printf("Hi! What's your name?\n");
	scanf("%s", name);	//從鍵盤讀入姓名
	printf("%s, what's your weight in pounds?\n", name);
	scanf("%f", &weight);
	size = sizeof(name);	//儲存姓名字串的陣列的長度
	letters = strlen(name);	//姓名的字串長度
	volume = weight / DENSITY;
	printf("Well, %s, your volume is %2.2f cubic feet.\n",
		name, volume);	//2個欄位寬度,小數點後保留兩位
	printf("Also, your first name has %d letters,\n",
		letters);
	printf("and we have %d bytes to store it.\n",size);

	return 0;
}

(1)輸入的名字為一個連續的字串(alice)時,程式正常執行:


(2)輸入漢字的名字時,正常執行:


(3)按照英文習慣輸入名和姓(alice young)時,讀入發生錯誤:


解析:在使用%s轉換說明時,scanf會讀取讀取除空白以外的所有字元——跳過空白讀取第一個非空字元,儲存非空字元直到再次遇到空白。所以程式中的第一個scanf在轉換說明%s的對應之下讀取了字串“alice”,在遇到空白的時候停下了。在第二個scanf讀入時從空白開始,跳過了空白遇到的第一個是“y”,由於此時使用的是%f,無法獲取浮點數,所以讀入失敗。(失敗細節現在不會,以後再探究)。

2.(1)欄位寬度與精度

#include<stdio.h>
int main(void)
{
	printf("He sold the painting for $%2.2f.\n", 2.345e2);
	return 0;
}

(2)關於字元的列印輸出

#include<stdio.h>
int main(void)
{
	printf("%c%c%c\n", 'H', 105, '\41');
	return 0;
}

解析:%c表明輸出的是字元。105是十進位制數,對應的字元是“i”。\041是轉義序列,表示八進位制值,轉換為十進位制為33,十進位制數33對應的字元是“!”。

ASCII字元對照表:

點選開啟連結

(3)字串長度

#include<stdio.h>
#include<string.h>	//提供strlen()函式的原型
#define Q "His Hamlet was funny without being vulgar."

int main(void)
{
	printf("%s\nhas %d characters.\n", Q, strlen(Q));	//列印字串並輸出它的長度.
	return 0;
}

(4)指數記數法

#include<stdio.h>
int main(void)
{
	printf("Is %2.2e the same as %2.2f?\n", 1201.0, 1201.0);	//十進位制指數記數法、浮點數,寬度和精度都為2
	return 0;
}

3.輸出含雙引號的字串

#include<stdio.h>
#include<string.h>
#define Q "His Hamlet was funny without being vuglar."	//定義字串

int main(void)
{
	printf("\"%s\"\nhas %d characters.\n", Q, strlen(Q));	//	\"代表輸出一個雙引號
	return 0;
}

4.

#include<stdio.h>
#define BOOK "War and Pleace"

int main(void)
{
	float cost = 12.99;
	float percent = 80.0;
	printf("This copy of \"BOOK\" sells for $%2.2f.\n", cost);
	printf("That is %0.0f%% of list.\n", percent);	//列印百分號要使用%%

	return 0;
}

5.

#include<stdio.h>

int main(void)
{
	char a[10];
	char b[10];
	printf("please enter your name?\n");
	scanf("%s%s", a, b);
	printf("hello, %s,%s. nice to meet you!\n", a, b);

	return 0;
}

6.

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

int main(void)
{
	char name[40];
	printf("Enter your name:\n");
	scanf("%s", name);
	printf("OK! %s, We can write down your name as:\n", name);
	printf("\"%s\"\n", name);	//列印名字,包括雙引號
	printf("\"%20s\"\n", name);	//在寬度為20 的欄位右端列印名字,包括雙引號
	printf("\"%-20s\"\n", name);	//在寬度為20的欄位左邊列印名字,包括雙引號
	printf("\"%*s\"\n", strlen(name)+3, name);	//strlen(name)+3意為在比姓名寬3的欄位中列印姓名.原先我寫成了name+3出錯了

	return 0;
}

7.

#include<stdio.h>

int main(void)
{
	float a;
	printf("Enter a numer(float):\n");
	scanf("%f", &a);
	printf("The input is %2.1f or %1.1e.\n", a, a);

	return 0;
}

#include<stdio.h>

int main(void)
{
	float a;
	printf("Enter a numer(float):\n");
	scanf("%f", &a);
	printf("The input is %2.3f or %1.3E.\n", a, a);

	return 0;
}

8.

#include<stdio.h>
#define Q 0.083	//一英寸=0.083英尺

int main(void)
{
	float a;
	float b;
	float c;
	float d;
	char name[20];

	printf("Enter your name and your height(inch):\n");
	scanf("%s%f", name, &a);
	b = a / Q;
	printf("%s, you are %1.3f feet tall.\n", name, b);

	printf("Let's see another form:\n");
	printf("Enter your height in cm:\n");
	scanf("%f", &c);
	d = c / 100;
	printf("%s, you are %1.3f meter tall.\n", name, d);

	return 0;
}

9.

#include<stdio.h>

int main(void)
{
	float a;	//下載速度
	float b;	//檔案大小
	float c;	//下載時間

	printf("Enter download speed(Mb/s) and the size of file(Mb):\n");
	scanf("%f%f", &a, &b);
	c = b / a;
	printf("At %2.2f mebagits per second, a file of %.2f megabits downloads in %.2f seconds.\n", 
		a, b, c*8);	//1位元組=8位

	return 0;
}

10.排版練習

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

int main(void)
{
	char a[10];	//名
	char b[10];	//姓

	printf("Enter your first name:\n");
	scanf("%s", a);
	printf("Enter your last name:\n");
	scanf("%s", b);
	printf("%12s %12s\n", a, b);
	printf("%12d %12d\n", strlen(a), strlen(b));
	printf("%-12s %-12s\n", a, b);
	printf("%-12d %-12d\n", strlen(a), strlen(b));

	return 0;
}

11.

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

int main(void)
{
	double a = 1.0 / 3.0;
	float b = 1.0 / 3.0;
	printf("a:%.6f %.12f %.16f\n", a, a, a);
	printf("b:%.6f %.12f %.16f\n", b, b, b);
	printf("FLT_DIG:%d\n", FLT_DIG);
	printf("DBL_DIG:%d\n", DBL_DIG);

	return 0;
}

12.

#include<stdio.h>
#define M 3.785	//1加侖約等於3.785升
#define N 1.609	//1英里約等於1.609千米

int main(void)
{
	float a;	//里程(英里)
	float b;	//耗油量(加侖)
	float c;	//100公里數
	float d;	//升

	printf("輸入里程和耗油量:\n");
	scanf("%f%f", &a, &b);
	printf("消耗每加侖汽油行駛的里程數:%.1f\n", a/b);
	c = (a * N) / 100;
	d = b * M;
	printf("每行駛100公里消耗的升數:%.1f\n", d/c);

	return 0;
}

小結:

(1)strlen()函式在計算字串長度時不會計入末尾的空字元。

(2)標頭檔案string.h包含了strlen()函式的原型。

(3)陣列是一系列同類型的項或元素。

(4)scanf()函式格式字串中的空白意味著跳過下一個輸入項前面的所有空白。

(5)無法利用欄位寬度讓只有一個%s的scanf()函式讀取多個單詞。