C Primer Plus 编程练习

21-12-2更新代码块格式兼容导出至notion

一、初识C语言
#include <stdio.h>

/*
1.你刚被MacroMuscle有限公司聘用。该公司准备进入欧洲市场,需要一个把英寸单位转换为厘米单位(1英寸=2.54厘米)的程序。
该程序要提示用户输入英寸值。你的任务是定义程序目标和设计程序(编程过程的第1步和第2步)。
*/
int main(int argc, char *argv[])
{
	int size;
	printf("please input your size, i can gave your a new number\n");
	scanf("%d",&size);
	printf("new number is %.4f", (size * (2.53)));

	// 读入回车键
	getchar();
	// 等待输入新的字符
	getchar();
	return 0;
}
二、C语言概述

1

#include <stdio.h>

/*
1.编写一个程序,调用一次 printf()函数,把你的姓名打印在一行。
再调 用一次 printf()函数,把你的姓名分别打印在两行。
然后,再调用两次printf() 函数,把你的姓名打印在一行。
输出应如下所示(当然要把示例的内容换成 你的姓名):

Gustav Mahler      ←第1次打印的内容
Gustav             ←第2次打印的内容
Mahler             ←仍是第2次打印的内容
Gustav Mahler      ←第3次和第4次打印的内容

*/
int main(int agrc, char *argv[]){
	
	printf("Gustav Mahler\n");
	printf("Gustav\nMahler\n");
	printf("Gustav ");
	printf("Mahler");

	getchar();
	return 0;
}

2

#include <stdio.h>

/*
2.编写一个程序,打印你的姓名和地址。
*/

int main(int argc, char * argv[]){

	printf("H HH");

	getchar();
	return 0;
}

3

#include <stdio.h>

/*
3.编写一个程序把你的年龄转换成天数,并显示这两个值。这里不用考虑闰年的问题
*/
int main(int argc, char *argv[]){

	int age = 18;
	printf("my age is %d, including %d days", age, age *365);

	getchar();
	return 0;
}

4

#include <stdio.h>

/*
4.编写一个程序,生成以下输出:
For he's a jolly good fellow!
For he's a jolly good fellow!
For he's a jolly good fellow!
Which nobody can deny!

除了main()函数以外,该程序还要调用两个自定义函数:一个名为jolly(),用于打印前3条消息,调用一次打印一条;另一个函数名为deny(),打印最后一条消息。
*/

void jolly();
void deny();

int main(int argc, char *argv[]){

	/*
	jolly();
	jolly();
	jolly();
	*/
	// 需要注意的是,C要求这里必须先声明i,在使用
	int i = 0;
	for(i; i < 3 ; i++){
		jolly();
	}

	deny();

	getchar();
	return 0;
}

void jolly(){
	printf("For he's a jolly good fellow!\n");
}

void deny(){
	printf("Which nobody can deny!\n");

}

5

#include <stdio.h>

/*
5.编写一个程序,生成以下输出:
------------------------------
Brazil, Russia, India, China
India, China
Brazil, Russia
------------------------------

除了main()以外,该程序还要调用两个自定义函数:一个名为br(),调用一次打印一次“Brazil, Russia”;另一个名为ic(),调用一次打印一次“India, China”。其他内容在main()函数中完成。
*/

void br();
void ic();
int main(int argc, char *argv[])
{
	br();
	printf(", ");
	ic();
	printf("\n");

	ic();
	printf("\n");
	br();
	getchar();
	return 0;
}

void br(){
	printf("Brazil, Russia");
}

void ic(){
	printf("India, China");
}

6

#include <stdio.h>

/*
6.编写一个程序,创建一个整型变量toes,并将toes设置为10。程序中还要计算toes的两倍和toes的平方。该程序应打印3个值,并分别描述以示区分。
*/
int main(int argc, char *argv[]){

	int toes = 10;
	printf("toes is %d, toes*2 is %d, toes^2 is %d", toes, toes * 2, toes * toes);

	getchar();
	return 0;
}

7

#include <stdio.h>

/*
7.许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输出:
----------------
Smile!Smile!Smile!
Smile!Smile!
Smile!
----------------
该程序要定义一个函数,该函数被调用一次打印一次“Smile!”,根据程序的需要使用该函数。
*/
void smile();
int main(int argc, char *argv[]){

	/*
	smile();
	smile();
	smile();
	printf("\n");

	smile();
	smile();
	printf("\n");

	smile();
	*/

	int i = 3;
	while(i > 0)
	{
		int j = i;
		for(j; j>0; j--)
		{
			smile();
		}
		printf("\n");
		i--;
	}

	getchar();
	return 0;
}

void smile(){
	printf("Smile!");
}

8

#include <stdio.h>

/*
8.在C语言中,函数可以调用另一个函数。
编写一个程序,调用一个名为one_three()的函数。
该函数在一行打印单词“one”,再调用第2个函数two(),然后在另一行打印单词“three”。
two()函数在一行显示单词“two”。
main()函数在调用one_three()函数前要打印短语“starting now:”,并在调用完毕后显示短语“done!”。因此,该程序的输出应如下所示:
--------------------------
starting now:
one
two
three
done!
--------------------------
*/

void one_three();
void two();
int main(int argc, char *argv[]){
	one_three();

	getchar();
	return 0;
}

void one_three()
{
	printf("Starting now:\n");
	printf("one\n");
	two();
	printf("three\n");
	printf("done!");
}

void two(){
	printf("two\n");
}
三、数据和C

1

#include <stdio.h>
/*
1.通过试验(即编写带有此类问题的程序)观察系统如何处理整数上溢、浮点数上溢和浮点数下溢的情况。
*/

int main(int argc, char *argv[])
{
	int data_i = 2147483647;
	float data_fu = 3.4E38 * 10;
	float data_fd = 3.4E-38 / (1.E10);

	// =====整数上溢=====
	printf("整数上溢:%d\n", data_i+1);

	// =====浮点数上溢=====
	printf("浮点数数上溢:%e\n", data_fu);

	// =====浮点数下溢=====
	printf("浮点数数下溢:%e\n", data_fd);

	getchar();
	return 0;
}

2

#include <stdio.h>

/*
2.编写一个程序,要求提示输入一个ASCII码值(如,66),然后打印输入的字符。
*/

int main(int argc, char *argv[])
{
	int acii;
	printf("please input a int num;");
	scanf("%d", &acii);
	printf("your input num is %d, which means %c" ,acii, acii);

	getchar();
	getchar();
	return 0;
}

3

#include<stdio.h>

/*
3.编写一个程序,发出一声警报,然后打印下面的文本:
Startled by the sudden sound, Sally shouted,
"By the Great Pumpkin, what was that!"
*/

int main(int argc, char *argv[])
{
	printf("\aStartled by the sudden sound, Sally shouted,\n\"By the Great Pumpkin, what was that!\"");
	/*
	拆成两行写,结构清晰点
	printf("\aStartled by the sudden sound, Sally shouted,\n");
	printf("\"By the Great Pumpkin, what was that!\"");
	*/

	getchar();
	return 0;
}

4

#include<stdio.h>

/*
4.编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p记数法(即十六进制记数法)。
按以下格式输出(实际显示的指数位数因系统而异):
Enter a floating-point value: 64.25

fixed-point notation: 64.250000
exponential notation: 6.425000e+01
p notation: 0x1.01p+6
*/

int main(int argc, char *argv[])
{
	float num;
	printf("Enter a floating-point value:");
	scanf("%f", &num);

	printf("fixed-point notation:%f\n",num);
	printf("exponential notation:%e\n",num);
	printf("p notation:%a\n",num);

	getchar();
	getchar();
	return 0;
}

5

#include<stdio.h>

/*
5.一年大约有3.156×10^7秒。编写一个程序,提示用户输入年龄,然后显示该年龄对应的秒数。
*/
int main(int argc, char *agrv[])
{
	int age;
	float year_second = 3.156E7;
	printf("please input your age:");
	scanf("%d", &age);
	printf("your age is %d, including %e second", age, age * year_second);

	getchar();
	getchar();

	return 0;
}

6

#include<stdio.h>

/*
6.1个水分子的质量约为3.0×10^−23克。1夸脱水大约是950克。编写一个程序,提示用户输入水的夸脱数,并显示水分子的数量。
*/

int main(int argc, char *argv[])
{
	float quart_n;
	float water_g = 3.E-23;
	int water_qt = 950;
	printf("please input Quart number of water:");
	scanf("%f", &quart_n);
	printf("%f quart of water has %e water molecule", quart_n, quart_n * water_qt / water_g);

	getchar();
	getchar();
	return 0;
}

7

#include <stdio.h>

/*
7.1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显示身高。
*/

int main(int agrc, char *argv[])
{
	float height;
	float ft_ratio_cm = 2.54;
	printf("please input your height(ft):");
	scanf("%f", &height);
	printf("your height is %f ft, it's %f cm", height, height * ft_ratio_cm);

	getchar();
	getchar();
	return 0;
}

8

#include <stdio.h>

/*
8.在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于3茶勺。
编写一个程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。
思考对于该程序,为何使用浮点类型比整数类型更合适?

因为计量体系中不完全是整数,贸然使用整型,会损失数据精度
*/

int main(int argc, char *argv[])
{
	// 注意这里先声明全部变量是为了兼容老旧编译器肯能不支持新的c标准

	float cups;
	// 注意这里计算比例必须显示声明这是浮点运算,要不然默然按整型运行,第一个变成0了都
	float cups_ratio_pint = 1. / 2;
	float cups_ratio_ounce = 1. * 8;
	float ounce_ratio_spoon = 1. * 2;
	float spoon_ratio_teaspoon = 1. * 3;

	float pint;
	float ounce;
	float spoon;
	float teaspoon;

	printf("please input the number of cups:");
	scanf("%f", &cups);

	pint = cups * cups_ratio_pint;
	ounce = cups * cups_ratio_ounce;
	spoon = ounce * ounce_ratio_spoon;
	teaspoon = spoon * spoon_ratio_teaspoon;

	printf("%f cups = %f pint = %f ounce = %f spoon = %f teaspoon", cups, pint, ounce, spoon, teaspoon);

	getchar();
	getchar();

	return 0;
}
四、字符串