Cprimerplus第五版完整.docx
《Cprimerplus第五版完整.docx》由会员分享,可在线阅读,更多相关《Cprimerplus第五版完整.docx(223页珍藏版)》请在冰豆网上搜索。
![Cprimerplus第五版完整.docx](https://file1.bdocx.com/fileroot1/2022-10/12/fe88a099-b239-4e94-bf19-6add8b0f0101/fe88a099-b239-4e94-bf19-6add8b0f01011.gif)
Cprimerplus第五版完整
C-primer-plus(第五版)(完整)
C_primer_plus(第五版)(完整)
@第一章概览
编程练习
1.您刚刚被MacroMuscle有限公司(SoftwareforHardBodies)聘用。
该公司要进入欧洲市场,需要一个将英寸转换为厘米(1英寸=2.54cm)的程序。
他们希望建立的该程序可提示用户输入英寸值。
您的工作是定义程序目标并设计该程序(编程过程的第1步和第2步)。
1.将英寸值转化为厘米值
2.显示“输入英寸值”->得到该值->转换为厘米值->存储->告知用户已结束
第二章C语言概述
编程练习
1.编写一个程序,调用printf()函数在一行上输出您的名和姓,再调用一次printf()函数在两个单独的行上输出您的名和姓,然后调用一对printf()函数在一行上输出您的名和姓。
输出应如下所示(当然里面要换成您的姓名):
AntonBruckner
Anton
Bruckner
AntonBruckner
第一个输出语句
第二个输出语句
仍然是第二个输出语句
第三个和第四个输出语句
#include
intmain(void)
{
printf("HeJin\n");
printf("He\n");
Forhe'sajollygoodfellow!
Forhe'sajollygoodfellow!
Whichnobodycandeny!
程序中除了main()函数之外,要使用两个用户定义的函数:
一个用于把上面的夸奖消息输出一次:
另一个用于把最后一行输出一次。
#include
voidprintf1(void);
voidprintf2(void);
intmain(void)
{
printf1();
printf1();
printf1();
printf2();
return(0);
}
voidprintf1(void)
{
printf("Forhe'sajollygoodfellow!
\n");
}
voidprintf2(void)
{
printf("Whichnobodycandeny!
\n");
}
5.编写一个程序,创建一个名为toes的整数变量。
让程序把toes设置为10。
再让程序计算两个toes的和以及toes的平方。
程序应该输出所有的3个值,并分别标识它们。
#include
intmain(void)
{
inttoes=10;
inttoes_add;
inttoes_square;
toes_add=toes+toes;
toes_square=toes*toes;
printf("toes=%d\ntoes_add=%d\ntoes_square=%d\n",toes,toes_add,toes_square);
return(0);
}
6.编写一个能够产生下列输出的程序:
Smile!
Smile!
Smile
Smile!
Smile!
Smile!
在程序中定义一个能显示字符串smile卜一次的函数,并在需要时使用该函数。
#include
voiddisplay(void);
intmain(void)
{
display();
display();
display();
printf("\n");
display();
display();
printf("\n");
display();
printf("\n");
return(0);
}
voiddisplay(void)
{
printf("Smile!
");
}
7.编写一个程序,程序中要调用名为one_three()的函数。
该函数要在一行中显示单词"one",再调用two()函数,然后再在另一行中显示单词"three"。
函数two()应该能在一行中显示单词"two"。
main()函数应该在调用one_three()函数之前显示短语"startingnow:
",函数调用之后要显示"done!
"o这样,最后的输出结果应如下所示:
startingnow
one
two
three
done!
#include
voidone_three(void);
voidtwo(void);
intmain(void)
{
printf("startingnow:
\n");
one_three();
printf("done!
\n");
return(0);
}
voidone_three(void)
{
printf("One\n");
two();
printf("Three\n");
}
voidtwo(void)
{
printf("Two\n");
}
第三章数据和C
编程练习
1.通过试验的方法(即编写带有此类问题的程序)观察系统如何处理整数上溢、浮点数上溢和浮点数下溢的情况。
#include
intmain(void)
{
unsignedinta=4294967295;
floatb=3.4E38;
floatc=b*10;
floatd=0.1234E-2;
printf("%u+1=%u\n",a,a+1);
printf("%e*10=%e\n",b,c);
printf("%f/10=%f\n",d,d/10);
return(0);
}
2.编写一个程序,要求输入一个ASCII码值(如66),然后输出相应的字符。
#include
intmain(void)
{
chara;
scanf("%d",&a);
printf("%c\n",a);
return(0);
}
3.编写一个程序,发出警报声,并打印下列文字:
Startledbythesuddensound,Sallyshouted,"BytheGreatPumpkin,whatwasthat!
"
#include
intmain(void)
{
printf("\aStartledbythesuddensound,Sallyshouted,\"BytheGreatpumpkin,whatwasthat!
\"\n");
return(0);
}
4.编写一个程序,读入一个浮点数,并分别以小数形式和指数形式打印。
输出应如同下面格式(实际显示的指数位数也许因系统而不同):
Theinputis21.290000or2.129000e+001.
#include
intmain(void)
{
floata;
scanf("%f",&a);
printf("Theinputis%for%e\n",a,a);
return(0);
}
5.一年约有3.156×l07S。
编写一个程序,要求输入您的年龄,然后显示该年龄合多少秒。
#include
intmain(void)
{
floata;
printf("Pleaseinputyourage:
");
scanf("%f",&a);
printf("Yourageis%eseconds\n",a*3.156E7);
return(0);
}
6.1个水分子的质量约为3.0×10^-23g,l夸脱水大约有950g。
编写一个程序,要求输入水的夸脱数,
然后显示这么多水中包含多少个水分子。
#include
intmain(void)
{
floata;
printf("Pleaseinputhowmuchquartsthewateris:
");
scanf("%f",&a);
printf("%fquartswaterhas%emolecules.\n",a,a*950/3E-23);
return(0);
}
7.1英寸等于2.54cm。
编写一个程序,要求输入您的身高(以英寸为单位),然后显示该身高值等于
多少厘米。
如果您愿意,也可以要求以厘米为单位输入身高,然后以英寸为单位进行显示。
#include
intmain(void)
{
floata;
printf("Pleaseinputyourheightbyinches:
");
scanf("%f",&a);
printf("Yourheightis%fcm.\n",a*2.54);
return(0);
}
第4章字符串和格式化输入/输出
编程练习
1.编写一个程序,要求输入名字和姓氏,然后以“名字,姓氏”的格式打印。
#include
intmain(void)
{
charsurname[20],firstname[20];
printf("Pleaseinputyourfirstnameandsurname:
");
scanf("%s",firstname);
scanf("%s",surname);
printf("Youare%s,%s.\n",firstname,surname);
return(0);
}
2.编写一个程序,要求输入名字,并执行以下操作:
a.把名字引在双引号中打印出来。
b.在宽度为20个字符的字段内打印名字,并且整个字段引在引号内。
c.在宽度为20个字符的字段的左端打印名字,并且整个字段引在引号内。
d.在比名字宽3个字符的字段内打印它。
#include
#include
intmain(void)
{
charname[20];
intwidth;
printf("Pleaseinputyourname:
");
scanf("%s",name);
printf("A:
\"%s\"\n",name);
printf("B:
\"%20s\"\n",name);
printf("C:
\"%-20s\"\n",name);
width=strlen(name)+3;
printf("D:
\"%*s\"\n",width,name);//*对应width
return(0);
}
3.编写一个程序,读取一个浮点数,并且首先以小数点记数法,然后以指数记数法打印之。
输出使用下列形式(在指数位置显示的数字的位数可能会随系统而不同):
a.Theinputis21.3or2.1e+001.
b.Theinputis+21.290or2.129E+001.
#include
intmain(void)
{
floata;
printf("Pleaseinput