以下提示输入半径和高度,并使用这些值来计算圆柱体的体积。如何编写此程序,以便在用户为任一高度半径输入负值时它不会终止?范围必须是 1-10(含),并且不允许其他提示。只有在输入非数字内容时才必须终止循环。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float areaCirc(float r){
return (M_PI*r*r);
}
float volCyl(float r, float h){
return (areaCirc(r)*h);
}
int main(void) {
float r, h;
int k = 0;
float volume;
float avgh = 0;
float toth = 0;
do{
float exit = scanf("%f%f", &r, &h);
if (exit == 0)
{break;}
if(r<=0 || r>10){
printf("Invalid radius: %.2f\n",r);
}
if(h<=0 || h>10){
printf("Invalid height: %.2f\n",h);
}
if(r>=0 && r<=10 && h>=0 && h <= 10){
volume = volCyl(r,h);
k = k++;
printf(" Cylinder %d radius %.2f height %.2f volume %.2f\n",k,r,h,volume);
toth = toth + h;
} }while(r>0 && h>0);
avgh = toth/k;
printf("Total Height: %.2f\n",toth);
printf("Average Height: %.2f\n",avgh);
return EXIT_SUCCESS;
}