因为转专业的原因,算是半路出家吧。所以开这个博客的想法是想记录自己的学习过程,也许还能提高文字输出能力(逃)
第二章 C语言概述----2.12练习
1.编写一个程序,调用printf()函数在一行上输出姓名,再调用一次printf()函数在两个单独的行上输出名和姓,然后调用一对printf()函数在一行上输出名和姓。输出应如下所示:
Anton Bruckner 第一个输出语句
Anton 第二个输出语句
Bruckner 仍然是第二个输出语句
Anton Bruckner 第三个和第四个输出语句
1 #include <stdio.h> 2 int main(void) 3 { 4 printf("Anton Bruckner\n"); 5 printf("Anton\nBruckner\n"); 6 printf("Anton "); 7 printf("Bruckner"); 8 return 0; 9 }
2.编写一个程序输出您的姓名及地址。(感觉和上题超类似,就略了)
3.编写一个程序,把您的年龄转换成天数并显示二者的值。不用考虑平年(fractional year)和闰年(leap year)的问题。
1 #include <stdio.h> 2 int main(void) 3 { 4 int age, days; 5 age = 21; 6 days = age*365; 7 printf("I am %d years old and It's %d days.", age, days); 8 return 0; 9 }
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()函数之外,要使用两个用户定义的函数:一个用于把上面的夸奖消息输出一次;另一个用于把最后一行输出一次。
1 #include <stdio.h> 2 void repeat(void); 3 void image(void); 4 5 int main(void) 6 { 7 repeat(); 8 repeat(); 9 repeat(); 10 image(); 11 12 return 0; 13 } 14 15 void repeat(void) 16 { 17 printf("For he's a jolly good fellow!\n"); 18 } 19 20 void image(void) 21 { 22 printf("Which nobody can deny!\n"); 23 }
5.编写一个程序,创建一个名为toes的整数变量。让程序把toes设置为10。再让程序计算两个toes的和以及toes的平方。程序应该能输出所有的3个值,并分别标识它们。
1 #include <stdio.h> 2 int main(void) 3 { 4 int toes, toes_sum, toes_squared; 5 toes = 10; 6 toes_sum = toes+toes; 7 toes_squared = toes*toes; 8 printf("toes = %d, toes_sum = %d, toes_squared = %d\n", 9 toes, toes_sum, toes_squared); 10 11 return 0; 12 }
6.编写一个能够产生下列输出的程序:
Smile!Smile!Smile!
Smile!Smile!
Smile!
在程序中定义一个能显示字符串 smile! 一次的函数,并在需要时使用该函数。
1 #include <stdio.h> 2 void smile(void); 3 4 int main(void) 5 { 6 smile(); 7 smile(); 8 smile(); 9 printf("\n"); 10 11 smile(); 12 smile(); 13 printf("\n"); 14 15 smile(); 16 17 return 0; 18 } 19 20 void smile(void) 21 { 22 printf("Smile!"); 23 }
7.编写一个程序,程序中要调用名为one_three()的函数。该函数要在一行中显示单词"one",再调用two()函数,然后再在另一行中显示单词"three"。函数two()应该能在一行中显示单词"two"。main()函数应该在调用one_three()函数之前显示短语"starting now: ",函数调用之后要显示"done!"。最后的输出结果应如下所示:
starting now:
one
two
three
done!
1 #include <stdio.h> 2 void one_three(void); 3 void two(void); 4 5 int main(void) 6 { 7 printf("starting now: \n"); 8 one_three(); 9 printf("done!"); 10 11 return 0; 12 } 13 14 void two(void) 15 { 16 printf("two\n"); 17 } 18 19 void one_three(void) 20 { 21 printf("one\n"); 22 two(); 23 printf("three\n"); 24 }