zl程序教程

您现在的位置是:首页 >  后端

当前栏目

练习 1-4 编写一个程序打印摄氏温度转换为相应华氏温度的转换表。// C语言

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;
	}
}