C语言五函数温度转换程序for与while循环编写方法
C语言温度转换程序循环实现方案
问题说明
正在开发菜单交互的C语言温度转换工具,共包含5个功能函数,支持华氏度-摄氏度双向转换,可根据用户输入的温度起止范围、转换步长输出格式化的温度对照表。
目前已完成主菜单交互逻辑、两个单点温度换算函数的编写,存在待实现点:
display_FtoC华氏度转摄氏度对照表输出函数,要求使用for循环实现遍历display_CtoF摄氏度转华氏度对照表输出函数,要求使用while循环实现遍历
现有代码框架
#include <stdio.h> #include <conio.h> #include <ctype.h> #pragma warning(disable:4996) float calc_FtoC(float Fahrenheit); void display_FtoC(int start, int stop, int step); float calc_CtoF(float Celsius); void display_CtoF(int start, int stop, int step); void main(void) { char select; int start, stop, step; do { printf("Enter A to Convert Fahrenheit to Celsius.\n"); printf("Enter B to Convert Celsius to Fahrenheit.\n"); printf("Enter Q to Quit.\n"); printf("You entered -> "); scanf("%c", &select); while (getchar() != '\n'); select = toupper(select); printf("Enter the highest temp to convert: \n"); scanf("%d", &start); while (getchar() != '\n'); printf("Enter lowest temp to convert: \n"); scanf("%d", &stop); while (getchar() != '\n'); printf("Enter increments: \n"); scanf("%d", &step); while (getchar() != '\n'); switch (select) { case 'A': display_FtoC(start, stop, step); break; case 'B': display_CtoF(start, stop, step); break; case 'Q': break; default: printf("You did not enter a correct selection!\n"); break; } } while (select != 'Q'); } float calc_FtoC(float Fahrenheit) { float calc_f2c; calc_f2c = 0.556 * Fahrenheit - 17.78; return calc_f2c; } void display_FtoC(int start, int stop, int step) { // 待补全for循环逻辑 } float calc_CtoF(float Celsius) { float calc_c2f; calc_c2f = 1.8 * Celsius + 32; return calc_c2f; } void display_CtoF(int start, int stop, int step) { // 待补全while循环逻辑 }
实现思路
- 先明确遍历方向:主函数交互逻辑是先输入最高温度、再输入最低温度,因此循环需要从最高温起始,每次递减步长,直到温度值小于等于最低温时终止,方向写反会导致循环一次都不执行。
for循环适合已知遍历起点、终点、步长的场景,三个循环控制要素(初始化、终止条件、步长更新)直接写在for的括号结构里即可。while循环只需要写终止条件,循环变量的初始化要放在循环外,步长更新要写在循环体内部,漏写步长更新会导致死循环。- 输出前先打印表头,打印数值时固定小数位数、用制表符对齐,生成的对照表可读性更好。
- 可选优化:函数开头增加起止值判断,如果用户误将最低温输得比最高温高,自动交换两个值,同时把步长转为正数,避免程序无输出。
补全后的函数实现
display_FtoC(for循环实现)
void display_FtoC(int start, int stop, int step) { // 处理用户输反起止值的异常情况 if (start < stop) { int temp = start; start = stop; stop = temp; } if (step <= 0) step = 1; // 避免步长为0或负数导致死循环 // 打印表头 printf("\n===== 华氏度转摄氏度对照表 =====\n"); printf("华氏度\t\t摄氏度\n"); printf("------------------------\n"); // for循环遍历:初始化当前温度为最高温,温度不低于最低温时继续循环,每次减步长 for (int current_f = start; current_f >= stop; current_f -= step) { float c_temp = calc_FtoC(current_f); printf("%d\t\t%.1f\n", current_f, c_temp); } printf("\n"); }
display_CtoF(while循环实现)
void display_CtoF(int start, int stop, int step) { // 处理用户输反起止值的异常情况 if (start < stop) { int temp = start; start = stop; stop = temp; } if (step <= 0) step = 1; // 避免步长为0或负数导致死循环 // 打印表头 printf("\n===== 摄氏度转华氏度对照表 =====\n"); printf("摄氏度\t\t华氏度\n"); printf("------------------------\n"); // while循环需要先在外部初始化循环变量 int current_c = start; while (current_c >= stop) { float f_temp = calc_CtoF(current_c); printf("%d\t\t%.1f\n", current_c, f_temp); // 循环内部必须更新循环变量,否则会死循环 current_c -= step; } printf("\n"); }
内容的提问来源于stack exchange,提问作者Nevada
相关产品推荐
相关产品推荐

