C语言题目——温度转换

C语言题目——温度转换,第1张

#include

main()

{

float

x,y

int

z

printf("请选择:\n")

printf("

(1)将温度摄氏度转换为华氏度。(2)将温度从华氏度转换为摄氏度")

scanf("%d",&z)

if(z==1)

{

printf("请答弯输入要陵举友转换的温尺槐度:")

scanf("%f",&x)

y=x*1.8+32

printf("

转换后的温度为

%f

华氏度",y)}

else

{

printf("请输入要转换的温度:")

scanf("%f",&x)

y=(x-32)*5/9

printf("

转换后的温度为

%f

摄氏度",y)}

}

第一段代码,首先计算知升5/9这个值你可以试一试,在c语言中他的值为0,然后0*后面的自然是0.第二个由于你写的是5.0/9.0所以可以往后精确,算出来的数就不会差粗是0,*后面的式子也就不会得0

.。。。。哦,5.0/9.0这一段会被认为是单精度的数。然后和后面的式子相乘这时也是单精度浮点搭庆老,知道最后强制转换为整数

#include

<stdio.h>

/主头文件/

#include<ctype.h>

/字符测试与映射头文件/

void

main()

{

double

celsius=0.0

/摄氏/

double

fahrenheit=0.0

/华氏/

double

celsius_to_fahrenheit=0.0

/摄氏转华氏/

double

fahrenheit_to_celsius=0.0

/华氏转摄氏/

char

choose=0

/选择/

printf("请选择你要查询的温度计量单位

摄氏度(C)和

华氏度(F):")

scanf("%c",&choose)

switch(toupper(choose))

/switch将输入的字符转换为大写/

{

case

'C':

printf("\n请输宏盯入摄氏温度:

")

scanf("%lf",&celsius)

if(celsius)

celsius_to_fahrenheit=celsius*1.8+32

/摄氏转华氏运算方法/

printf("\n摄氏

%.2lf

转换成

华氏

%.2lf

\n\n",celsius,celsius_to_fahrenheit)

break

/跳出switch/

case

'F':

printf("\n请输入华氏温度:

")

scanf("%lf",&fahrenheit)

if(fahrenheit)

fahrenheit_to_celsius=(fahrenheit-32)*5/9

/华氏转摄氏运算方法/

printf("\n华氏

%.2lf

度庆困

转换成

摄氏

%.2lf

\n\n",fahrenheit,fahrenheit_to_celsius)

break

/跳出switch/

default:

/如誉绝念果与case不匹配/

printf("\n\a输入错误

请重新尝试!\n\n")

/输出这个语句/

}

}

这个基本上很具体了

你应该能看得懂。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12430960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存