练习 1-4 编写一个程序打印摄氏温度转换为相应华氏温度的转换表。// C语言
2023-09-11 14:22:19 时间
C语言程序设计(第二版) 练习1-4 个人设计
练习 1-4 编写一个程序打印摄氏温度转换为相应华氏温度的转换表。
注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010
代码块:
方法1:(printf函数直接输出)
#include <stdio.h>
#include <stdlib.h>
int main()
{
//定义华氏,摄氏温度
float fahr, celsius;
//定义最低,最高温度,和温度阶梯
float lower, upper, step;
lower = 0.0;
upper = 100.0;
step = 10.0;
celsius = lower;
printf("****************************\n");
printf("Temperature Conversion Table\n");
printf("****************************\n");
while (celsius <= upper) {
fahr = celsius * (9.0/5.0) + 32.0;
printf("%6.1f %18.1f\n", celsius, fahr);
celsius = celsius + step;
}
system("pause");
return 0;
}
方法2:(使用打印函数)
#include <stdio.h>
#include <stdlib.h>
void starOutput(); //定义星号输出函数
void wordOutput(); //定义文字输出函数
void titleOutput(); //定义标题输出函数
int main()
{
//定义华氏,摄氏温度
float fahr, celsius;
//定义最低,最高温度,和温度阶梯
float lower, upper, step;
lower = 0.0;
upper = 100.0;
step = 10.0;
celsius = lower;
titleOutput();
while (celsius <= upper) {
fahr = celsius * (9.0/5.0) + 32.0;
printf("%6.1f %18.1f\n", celsius, fahr);
celsius = celsius + step;
}
system("pause");
return 0;
}
void starOutput() {
printf("****************************\n");
}
void wordOutput() {
printf("Temperature Conversion Table\n");
}
void titleOutput() {
starOutput();
wordOutput();
starOutput();
}
方法3:(在方法2的基础上加入温度转换函数)
#include <stdio.h>
#include <stdlib.h>
void starOutput(); //定义星号输出函数
void wordOutput(); //定义文字输出函数
void titleOutput(); //定义标题输出函数
void temperatureConvert(float lower, float upper, float step); //定义温度转换函数
int main()
{
//定义最低,最高温度,温度阶梯
float lower, upper, step;
lower = 0.0;
upper = 100.0;
step = 10.0;
titleOutput();
temperatureConvert(lower, upper, step);
system("pause");
return 0;
}
void starOutput() {
printf("****************************\n");
}
void wordOutput() {
printf("Temperature Conversion Table\n");
}
void titleOutput() {
starOutput();
wordOutput();
starOutput();
}
void temperatureConvert(float lower, float upper, float step) {
float fahr, celsius;
celsius = lower;
while (celsius <= upper) {
fahr = celsius * (9.0/5.0) + 32.0;
printf("%6.1f %18.1f\n", celsius, fahr);
celsius = celsius + step;
}
}
方法4:(使用常量定义)
#include <stdio.h>
#include <stdlib.h>
#define LOWER 0.0 //定义最低温度
#define UPPER 100.0 //定义最高温度
#define STEP 10.0 //定义温度阶梯
void starOutput(); //定义星号输出函数
void wordOutput(); //定义文字输出函数
void titleOutput(); //定义标题输出函数
void temperatureConvert(float lower, float upper, float step); //定义温度转换函数
int main()
{
titleOutput();
temperatureConvert(LOWER, UPPER, STEP);
system("pause");
return 0;
}
void starOutput() {
printf("****************************\n");
}
void wordOutput() {
printf("Temperature Conversion Table\n");
}
void titleOutput() {
starOutput();
wordOutput();
starOutput();
}
void temperatureConvert(float lower, float upper, float step) {
float fahr, celsius;
celsius = lower;
while (celsius <= upper) {
fahr = celsius * (9.0/5.0) + 32.0;
printf("%6.1f %18.1f\n", celsius, fahr);
celsius = celsius + step;
}
}
相关文章
- 计算机等级考试二级C语言程序设计专项训练题——程序设计题(三)
- C语言程序设计100例之(19):欢乐的跳
- 编程——C语言的问题,比特率
- C语言数据结构---顺序表
- 【C语言基础】编码规范
- AI Studio下C语言编程:一道简单的C语言作业题目
- 2021年最新C语言教程入门,C语言自学教程(最全整理)
- 【指针进阶三】实现C语言快排函数qsort&回调函数
- C语言中的强制转换
- Linux下C语言获取目录中的文件列表
- 【C语言】统计素数并求和
- 为Python编写一个简单的C语言扩展模块
- C语言循环语句进阶练习题
- C语言将NMEA定位数据中的BL转换为标准BLH及XYZ
- PT1000测温仿真_有继电器模拟加热丝,有C语言代码,有电路图,Proteus仿真
- C语言盲点笔记1
- 二叉树C语言
- 1034 有理数四则运算 (20 分)C语言
- 学习C语言的数组