编写一个程序实现华氏温度转换成摄氏温度,并将0°~100°的转换值(步长为20)输出的程序。请将其中温度变量fahr和celsius用指针变量来实现。
时间: 2024-03-10 14:50:01 浏览: 43
好的,以下是实现华氏温度转换成摄氏温度并输出的程序,其中使用指针变量来实现温度变量fahr和celsius:
```c
#include <stdio.h>
void convertTemp(float *fahr, float *celsius) {
*celsius = (*fahr - 32) * 5 / 9; // 华氏转摄氏公式
}
int main() {
float fahr, celsius;
float lower, upper, step;
lower = 0; // 温度下限
upper = 100; // 温度上限
step = 20; // 步长
fahr = lower;
while (fahr <= upper) {
convertTemp(&fahr, &celsius); // 调用函数,使用指针变量传递参数
printf("%3.0f\t%6.1f\n", fahr, celsius);
fahr += step;
}
return 0;
}
```
程序运行时,会输出0°~100°的温度值及其对应的摄氏温度值,步长为20。其中,convertTemp函数用于将华氏温度转换成摄氏温度,使用指针变量来实现温度变量fahr和celsius的传递。
相关问题
c语言编程要求:华氏0℉~300℉,每隔20℉输出一个华氏温度对应的摄氏温度值。
好的,这个问题是关于C语言编程的,可以回答。根据华氏与摄氏温度转换公式C=(5/9)*(F-32),我们可以使用循环语句输出0℉~300℉,每隔20℉输出一个对应的摄氏温度值。具体代码如下:
```c
#include <stdio.h>
int main() {
float fahr, celsius;
int lower, upper, step;
lower = 0; // 温度表的下限
upper = 300; // 温度表的上限
step = 20; // 步长
fahr = lower;
while (fahr <= upper) {
celsius = (5.0 / 9.0) * (fahr - 32.0);
printf("%.0f\t%.1f\n", fahr, celsius);
fahr += step;
}
return 0;
}
```
输出结果如下:
```
0 -17.8
20 -6.7
40 4.4
60 15.6
80 26.7
100 37.8
120 48.9
140 60.0
160 71.1
180 82.2
200 93.3
220 104.4
240 115.6
260 126.7
280 137.8
300 148.9
```
编写一个脚本程序,功能为:调用摄氏温度到华氏温度的转换函数生成一个摄氏温度和华氏温度的对照表,其中摄氏温度的范围是从-100℃到100℃并以1℃为步长
以下是Python的代码实现:
```python
def celsius_to_fahrenheit(celsius):
"""
将摄氏温度转换为华氏温度
"""
return celsius * 1.8 + 32
# 生成摄氏温度和华氏温度的对照表
for celsius in range(-100, 101):
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}℃ = {fahrenheit}℉")
```
输出结果如下:
```
-100℃ = -148.0℉
-99℃ = -146.2℉
-98℃ = -144.4℉
...
98℃ = 208.4℉
99℃ = 210.2℉
100℃ = 212.0℉
```
注意,在这里我们使用了 `range(-100, 101)` 生成了从-100℃到100℃的整数序列,然后对于每个摄氏温度,调用 `celsius_to_fahrenheit` 函数将其转换为华氏温度,并输出对照表。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)