C Primer Plus 6 第四章编程练习

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Alice_12/article/details/79587708

要点:

(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()函数读取多个单词。


猜你喜欢

转载自blog.csdn.net/Alice_12/article/details/79587708
今日推荐