C语言程序设计-现代方法----示例及练习题----第二章--C语言基本概念

本文提供了几个C语言程序设计的基本示例,包括打印格式化字符串、计算包裹重量、转换华氏温度到摄氏温度、输出特定图案以及计算球体体积等。这些示例有助于初学者理解C语言的基本语法和编程技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者: selfimpr

博客: http://blog.youkuaiyun.com/lgg201

邮箱: lgg860911@yahoo.com.cn

<C语言程序设计-现代方法>----示例及练习题----第二章--C语言基本概念

声明:

部分示例代码为书中源代码.

其他原创代码多数对原题要求有所改动.

page9: pun.c

#include <stdio.h> int main() { printf("To C, or not to C: that is the question./n%.3f", 3.234666666); return 0; }

page 18: dweight.c

#include <stdio.h> #define CUBIC_IN_PER_LB 166 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

#include <stdio.h> #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在屏幕上显示出下面的图形:

*
*
*
* *
* *
*

#include <stdio.h> 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

#include <stdio.h> #define PAI 3.14159262453897 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; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值