C Primer Plus编程练习(更新中)

2021/11/17 14:10:44

本文主要是介绍C Primer Plus编程练习(更新中),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C Primer Plus 编程练习

一、初识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 Primer Plus编程练习(更新中)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程