僵尸艳谈免费观看影片:关于C语言的问题:while 循环的

来源:百度文库 编辑:高考问答 时间:2024/05/07 15:33:09
关于C语言的问题:在C primer Plus里,有一道练习题是用while 循环的,要求输入非数字值时,循环结束。请问怎样实现?谢谢!

Write a program that requests the user to enter a Fahrenheit temperature. The program should read the temperature as a type double number and pass it as an argument to a user-supplied function called Temperatures(). This function should calculate the Celsius equivalent and the Kelvin equivalent and display all three temperatures with a precision of two places to the right of the decimal. It should identify each value with the temperature scale it represents. Here is the formula for converting Fahrenheit to Celsius:

Celsius = 1.8 * Fahrenheit + 32.0

The Kelvin scale, commonly used in science, is a scale in which 0 represents absolute zero, the lower limit to possible temperatures. Here is the formula for converting Celsius to Kelvin:

Kelvin = Celsius + 273.16

The Temperatures() function should use const to create symbolic representations of the three constants that appear in the conversions. The main() function should use a loop to allow the user to enter temperatures repeatedly, stopping when a q or other nonnumeric value is entered.

楼上的解决方法不好,其实从scanf的返回值就可以看出输入类型是否匹配。下面给出全部代码

#include <stdio.h>

void Temperatures(double Fahrenheit){

double Celsius,Kelvin;

Celsius = 1.8 * Fahrenheit + 32.0;
Kelvin = Celsius + 273.16;
printf("Fahrenheit:%.2f\n",Fahrenheit);
printf("Celsius:%.2f\n",Celsius);
printf("Kelvin:%.2f\n",Kelvin);

}

void main() {

double f;
int i;

while(1){
i=scanf("%lf",&f);
if (i==0 || f==0){
break;
}
else{
Temperatures(f);
}
}
}

#include <stdio.h>

void Temperatures(double);

int main(void)
{
double temperature;

printf("Please enter a temperature in Fahrenheit: ");

while (scanf("%lf", &temperature) == 1)
{
Temperatures(temperature);
printf("\nAgain, enter a temperature in Fahrenheit (or a non-numeric input to quit): ");
}

return 0;
}

void Temperatures(double fahrenheit)

{
const double CELSIUS1 = 32;
const double CELSIUS2 = 1.8;
const double KELVIN1 = 273.15;

double celsius, kelvin;
celsius = (fahrenheit - CELSIUS1) / CELSIUS2;
kelvin = celsius + KELVIN1;

printf("Fahrenheit: %.2lf\n", fahrenheit);
printf("Celsius: %.2lf\n", celsius);
printf("Kelvin: %.2lf\n", kelvin);
}

设个永真循环,里面用 getchar(),赋给一个变量T,判断T>'0' && T<'9'做为循环条件,退出用break;