zl程序教程

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

当前栏目

练习 1-3 修改温度转换程序,使之能在转换表的顶部打印一个标题。// C语言

C语言转换程序 一个 修改 练习 打印 标题
2023-09-14 09:06:57 时间

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));
    }
    */
}