练习 1-3 修改温度转换程序,使之能在转换表的顶部打印一个标题。// 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));
}
*/
}
相关文章
- C语言 整数与字符串的相互转换
- C语言——五子棋人机对战
- 简单选择排序 C语言
- C语言 Char* 和Char 用法[通俗易懂]
- ACM训练【排队买票】 (C语言描述 递归 5ms 过 简单易懂)--------C语言——菜鸟级
- C语言printf()/scanf()的转换说明和转换说明修饰符
- 【C语言进阶】常见数据格式转换处理的代码实现
- 【安全算法之MD5】MD5摘要运算的C语言源码实现
- 约瑟夫环问题 C语言
- [编程] C语言结构体指针作为函数参数详解编程语言
- C语言指针常量和指向常量的指针
- 编程Linux下C语言数据库编程实战(linuxc数据库)
- Linux C语言项目实战:掌握编程技能的新方法(linuxc项目实战)
- C语言快速构建MySQL实体类(c mysql 实体类)
- C语言连接Oracle数据库的简单操作方法(c语言怎么连oracle)
- C语言数与串之间转换的方法
- C语言实现图的遍历之深度优先搜索实例