下面的是C Primer Plus 第六版所有章节的课后编程练习的答案,是博主在学习过程中自己编写的,希望能和大家共同讨论,一起进步
PS:小伙伴们可以直接从左边的目录看自己需要的章节哟
2.1
#include
int main(void)
{
printf("Gustav Mahler\n");
printf("Gustav\nMahler\n");
printf("Gustav ");printf("Mahler\n");
return 0;
}
2.2
#include
int main(void)
{
printf("姓名\n地址\n");
return 0;
}
2.3
#include
int main(void)
{
int oldyears;
printf("请输入您的年龄(如19)\n");
scanf("%d",&oldyears);
printf("嘻嘻嘻(●'_'●)\n");
printf("所以您%d岁惹,并且至少已经活了%d天\n",oldyears,oldyears*365);
return 0;
}
2.4
#include
void jolly(void);
void deny(void);
int main(void)
{
jolly();
jolly();
jolly();
deny();
return 0;
}
void jolly(void)
{
printf("For he's a jolly good fellow!\n");
}
void deny(void)
{
printf("Which nobody can deny!\n");
}
2.5
#include
void br(void);
void ic(void);
int main(void)
{
br();
ic();
ic();
printf("Brazil,Russia\n");
return 0;
}
void br(void)
{
printf("Brazil,Russia,");
}
void ic(void)
{
printf("India,China\n");
}
2.6
#include .h>
void smile();
int main(void)
{
smile();
smile();
smile();
printf("\n");
smile();
smile();
printf("\n");
smile();
printf("\n");
return 0;
}
void smile(void)
{
printf("smile!");
}
2.7
#include
void one_three(void);
void two(void);
int main(void)
{
printf("starting now:\n");
one_three();
printf("done!\n");
return 0;
}
void one_three(void)
{
printf("one\n");
two();
printf("three\n");
}
void two(void)
{
printf("two\n");
}
3.2
#include
int main(void)
{
char ascii66;
printf("请输入一个ASCII码值(0-127):");
printf("___\b\b\b") ;
scanf("%d",&ascii66);
printf("您刚刚输入的ASCII值是%d,对应的字符是%c\n",ascii66,ascii66);
return 0;
}
3.3
#include
int main(void)
{
printf("Startled by the sudden sound,Sally shouted,\n");
printf("\"By the Great Pumpkin,what was that!\"\n");
return 0;
}
3.4
#include
int main(void)
{
float f_scan;
printf("Enter a floating-point value:");
scanf("%f",&f_scan);
printf("fixed-point notation:%f\n",f_scan);
printf("exponential notation:%e\n",f_scan);
printf("p notation:%a\n",f_scan);
return 0;
}
3.5
#include
int main(void)
{
short ages;
float life_seconds;
printf("Please enter your ages:");
scanf("%d",&ages);
life_seconds=ages*3.156e7;
printf("%d years old means you have lived %f seconds\n",ages,life_seconds);
return 0;
}
3.6
#include
int main(void)
{
short water_weight;
float water_molecule;
printf("Please enter the water's weight in quart:");
scanf("%d",&water_weight);
water_molecule=water_weight*950/3.0e-23;
printf("%d quarts water contains %e molecules\n",water_weight,water_molecule);
return 0;
}
3.7
#include
int main(void)
{
float hight_inch;
float hight_cm;
printf("Please enter your hight in inch:");
scanf("%f",&hight_inch);
printf("%.2f inch means you're %.2f centimeters tall\n",hight_inch,hight_inch*2.54);
return 0;
}
3.8
#include
int main(void)
{
float cups;
float ounce;
float tablespoon;
float teaspoon ;
printf("Please enter the number of cups:");
scanf("%f",&cups);
ounce=cups*8.;
tablespoon=ounce*2.;
teaspoon=tablespoon*3.;
printf("%f cups means\n",cups);
printf("%f ounce\n",ounce);
printf("%f tablespoon\n",tablespoon);
printf("%f teaspoon\n",teaspoon);
return 0;
}
4.1
#include
int main(void)
{
char name[10],prename[10];
printf("Please enteryour name(such as Joseph Stalin):\n");
scanf("%s %s",name,prename);
printf("Now we know your name is %s,%s\n",name,prename);
return 0;
}
4.2
#include
int main(void)
{
char name[20];
short x;
printf("Please enteryour name(such as Joseph Stalin):\n");
scanf("%s",name);
x=strlen(name)+3;
printf("\"%20s\"\n",name);
printf("\"%-20s\"\n",name);
printf("\"%*s\"\n",x,name);
return 0;
}
4.3
#include
int main(void)
{
float a;
float b;
a=21.3;
b=21.290;
printf("The input is %.1f or %.1e\n",a,a);
printf("The input is %.3f or %.3e\n",b,b);
return 0;
}
4.4
#include
int main(void)
{
float hight;
char name[10];
printf("please enter your name:\n");
scanf("%s",name);
printf("please enter your hight in centimeter:\n");
scanf("%f",&hight);
printf("%s,you are %.2f miters tall\n",name,hight/100);
return 0;
}
4.5
#include
int main(void)
{
float speed;
float size;
printf("please enter your internet speed(in Mb/s):\n");
scanf("%f",&speed);
printf("please enter your file size(in MB):\n");
scanf("%f",&size);
printf("At %.2f megabites per second,a file of %.2f megabytes\n",speed,size);
printf("downloads in %.2f seconds\n",size/speed*8);
return 0;
}
4.6
#include
int main(void)
{
int a,b;
char name[10],surname[10];
printf("please enter you name:\n");
scanf("%s",name);
printf("please enter you surname:\n");
scanf("%s",surname);
a=strlen(name);
b=strlen(surname);
printf("%s %s\n",name,surname);
printf("%*d %*d\n",a,a,b,b);
printf("%s %s\n",name,surname);
printf("%-*d %-*d\n",a,a,b,b);
return 0;
}
4.7
#include
#include
int main(void)
{
double a=1.0/3.0;
float b=1.0/3.0;
printf("double\n");
printf("%.6f\n",a);
printf("%.12f\n",a);
printf("%d\n",DBL_DIG);
printf("float\n");
printf("%.6f\n",b);
printf("%.12f\n",b);
printf("%d\n",FLT_DIG);
return 0;
}
4.8
#include
#define turn1 3.785
int main(void)
{
const float turn2=1.609;
float distance;
float consume;
printf("请以英里为单位输入旅行的里程:\n");
scanf("%f",&distance);
printf("请以加仑为单位输入消耗的燃料:\n");
scanf("%f",&consume);
printf("所以旅行中的油耗是%.1f升/100公里",distance*turn2*100/consume/turn1);
return 0;
}
5.1
#include
#define sec_per_min 60
#define min_per_hour 60;
int main(void)
{
int minute;
float hour;
long second;
printf("Please enter the minutes\n");
scanf("%d",&minute);
while(minute>0)
{
hour=(float)minute/min_per_hour;
second=minute*sec_per_min;
printf("%d minutes equal %d seconds or %f hours\n",
minute,second,hour);
scanf("%d",&minute);
}
}
5.2
#include
int main(void)
{
int i=1;
int num;
int num_pre;
printf("please enter a num:\n");
scanf("%d",&num);
num_pre=num;
while(i++<12)
{
printf("%d ",num);
num=num+1;
}
printf("\n");
i=1;
num=num_pre;
while(i++<12)
{
printf("%d\t",num);
num=num+1;
}
printf("\n");
i=1;
num=num_pre;
while(i++<12)
{
printf("%d\n",num);
num=num+1;
}
return 0;
}
5.3
#include
#define days_per_week 7
int main(void)
{
int days;
int day;
int weeks;
printf("please enter days:");
scanf("%d",&days);
weeks=days/days_per_week;
day=days%days_per_week;
printf("%d days are %d weeks,%d days.\n",days,weeks,day);
return 0;
}
5.4
#include
const float inch_per_cm=0.3937;
const float cm_per_feet=30.48;
int main(void)
{
float height,height_1,inch;
int feet;
printf("enter a height in centimeters:");
scanf("%f",&height);
while(height>0)
{
feet=height/cm_per_feet;
height_1=height-feet*cm_per_feet;
inch=height_1*inch_per_cm;
printf("%.1f cm = %d feet,%.1f inches\n",height,feet,inch);
printf("enter a height in centimeters (<=0 quit):");
scanf("%f",&height);
}
printf("bye\n");
return 0;
}
5.5
#include
int main(void)
{
int count,sum;
int days;
printf("Please enter a num for sum:");
scanf("%d",&days);
count=0;
sum=0;
while(count++sum=sum+count;
printf("sum=%d\n",sum);
return 0;
}
5.6
#include
int main(void)
{
int count,sum;
int days;
printf("Please enter a num for sum:");
scanf("%d",&days);
count=0;
sum=0;
while(count++sum=sum+count*count;
printf("sum=%d\n",sum);
return 0;
}
5.7
#include
float cube(float num);
int main(void)
{
float num;
printf("Please enter a number:");
scanf("%f",&num);
printf("The cube of this num is %f\n",cube(num));
return 0;
}
float cube(float num)
{
return(num*num*num);
}
5.8
#include
int main(void)
{
int num1,num2;
printf("This program computes moduli.\n");
printf("Enter an integer to serve as the second operand:");
scanf("%d",&num1);
printf("Now enter the first operand:");
scanf("%d",&num2);
while(num2>0)
{
printf("%d %% %d is %d\n",num2,num1,num2%num1);
printf("Enter next number for first operand(<=0 to quit):");
scanf("%d",&num2);
}
printf("Done\n");
return 0;
}
5.9
#include
float Temperatures(float temp);
int main(void)
{
float fah;
int status;
printf("Please enter the Fahrenheit:");
status=scanf("%f",&fah);
while(status==1)
{
Temperatures(fah);
printf("Please enter the Fahrenheit:(q to quit):");
scanf("%f",&fah);
status=scanf("%f",&fah);
}
return 0;
}
float Temperatures(float temp)
{
const float fah_need=32.0;
const float kel_need=273.16;
float kel,cent;
cent=5.0/9.0*(temp-fah_need);
kel=cent+kel_need;
printf("digree centigrade is %f\n",cent);
printf("digree kelvin is %f\n",kel);
}