作者: selfimpr
博客: http://blog.csdn.net/lgg201
<C语言程序设计-现代方法>----示例及练习题----第二章--C语言基本概念
声明:
部分示例代码为书中源代码.
其他原创代码多数对原题要求有所改动.
page9: pun.c
int main() {
printf("To C, or not to C: that is the question./n%.3f", 3.234666666);
return 0;
}
page 18: dweight.c
int main() {
int height = 0;
int length = 0;
int width = 0;
int volume = 0;
int weight = 0;
/*
* height = 8;
* length = 12;
* width = 10;
*/
int i = 0;
while(i < 5) {
printf("please input height:/n");
scanf("%d", &height);
printf("please input width:/n");
scanf("%d", &width);
printf("please input length:/n");
scanf("%d", &length);
volume = height * length * width;
weight = (volume + CUBIC_IN_PER_LB - 1) / CUBIC_IN_PER_LB;
printf("Dimensions: %dx%dx%d/n", length, width, height);
printf("Volume (cubic inches): %d/n", volume);
printf("Dimensional weight (pounds): %d/n", weight);
}
return 0;
}
page 22: celsius.c
#define PERZING_PT 32.0
#define SCALE_FACTOR (5.0 / 9.0)
int main() {
float fahrenheit, celsius;
printf("Enter Fahrenheit temperature: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - PERZING_PT) * SCALE_FACTOR;
printf("Celsius equivalent: %.1f/n", celsius);
return 0;
}
练习3: 编写一个程序, 程序要使用printf在屏幕上显示出下面的图形:
*
*
*
* *
* *
*
int main() {
int size = 1;
printf("please enter the size of this fit: ");
scanf("%d", &size);
int times = size * 3;
int up_space_by_down = times * 2 - 1;
int i = 0;
for(; i < times; i ++) {
int space_amount = times - i - 1 + up_space_by_down;
int j = 0;
for(; j < space_amount; j ++) {
printf(" ");
}
printf("*/n");
}
i = 0;
for(; i < times; i ++) {
int left_space_amount = i;
int center_space_amount = 2 * ( times - i - 2) + 1;
int j = 0;
for(; j < left_space_amount; j ++) {
printf(" ");
}
if(center_space_amount >=1) {
printf("*");
}
j = 0;
for(; j < center_space_amount; j ++) {
printf(" ");
}
printf("*/n");
}
return 0;
}
练习5, 7, 9: 编写一个计算球体体积的程序, 其中球体半径为10m, 参考公式v=4/3派r^3. 注意, 分数4/3应写成4.0/3.0
int main() {
printf("please enter the ball's radii: ");
int radii = 0;
scanf("%d", &radii);
float cubage = 4.0 / 3.0 * PAI * radii * radii * radii;
printf("The ball that radii is %d, its cubage is %f", radii, cubage);
return 0;
}