练习 1-3 修改温度转换程序,使之能在转换表的顶部打印一个标题。// C语言
2023-09-11 14:22:18 时间
C语言程序设计(第二版) 练习1-3 个人设计
练习1-3 修改温度转换程序,使之能在转换表的顶部打印一个标题。
注意:代码在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 = 300.0;
step = 20.0;
fahr = lower;
//以下3行是打印一个标题
printf("****************************\n");
printf("Temperature Conversion Table\n");
printf("****************************\n");
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%6.0f %18.1f\n", fahr, celsius);
fahr = fahr + 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 = 300.0;
step = 20.0;
fahr = lower;
//由3行printf函数变为一个标题输出函数
titleOutput();
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%6.0f %18.1f\n", fahr, celsius);
fahr = fahr + 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 = 300.0;
step = 20.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;
fahr = lower;
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%6.0f %18.1f\n", fahr, celsius);
fahr = fahr + step;
}
}
方法4:(采用常量定义)
#include <stdio.h>
#include <stdlib.h>
#define LOWER 0.0
#define UPPER 300.0
#define STEP 20.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;
fahr = lower;
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%6.0f %18.1f\n", fahr, celsius);
fahr = fahr + step;
}
/*以上代码while循环可以改为for循环
for(float fahr = lower; fahr <= upper; fahr += step) {
printf("%6.0f %18.1f\n", fahr, (5.0/9.0)*(fahr-32.0));
}
*/
}
相关文章
- C语言中的关于“位域”的说明(转载,有修改)
- C语言指针转换为intptr_t类型
- ZZNUOJ_C语言1084:计算两点间的距离(多实例测试)(完整代码)
- ZZNUOJ_C语言1132:数字字符统计(附完整源码)
- ZZNUOJ_用C语言编写程序实现1237:童鞋你多大了?(附完整源码)
- ZZNUOJ_用C语言编写程序实现1271:数塔(附完整源码)
- 转换字符串的最少操作次数-C语言
- 练习 1-3 修改温度转换程序,使之能在转换表的顶部打印一个标题。// C语言
- 练习 1-9 编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替。// C语言
- C语言中HEX转ASCII函数,ASCII转HEX函数,ASCII转DEC函数,四字节大小端转换函数整理
- C语言每日面试之合并字符串
- AI模型设计:C语言版 TensorFlow2.x安装与使用
- C语言使用技巧(十六):完整解析函数返回二维数组的常用技巧(END)
- C语言---整型字符串转换
- Python重写C语言程序100例--Part6
- C语言 windows下Ansi和UTF-8编码格式的转换
- 【高级C】手把手教你分解C语言中复杂的变量声明
- C语言中利用三条边求得三角形的面积公式 —【海伦公式】
- simulink中调用C语言进行仿真
- 【维生素C语言】第十一章 - 字符串函数与内存函数