如何用C语言完成这个作业?第二部分

如何用C语言完成这个作业?第二部分,c,terminate,C,Terminate,以下提示输入半径和高度,并使用这些值计算圆柱体的体积。如何编写此程序,以便在用户输入高度半径的负值时不会终止?范围必须为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

以下提示输入半径和高度,并使用这些值计算圆柱体的体积。如何编写此程序,以便在用户输入高度半径的负值时不会终止?范围必须为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;
}
#包括
#包括
#包括
浮动区域循环(浮动r){
返回值(M_PI*r*r);
}
浮动油缸(浮动r、浮动h){
返回(区域循环(r)*h);
}
内部主(空){
浮动r,h;
int k=0;
浮子体积;
浮动avgh=0;
浮点数=0;
做{
float exit=scanf(“%f%f”,&r,&h);
如果(退出==0)
{break;}
如果(r10){
printf(“无效半径:%.2f\n”,r);
}
如果(h10){
printf(“无效高度:%.2f\n”,h);
}
如果(r>=0&&r=0&&h0&&h>0);
avgh=toth/k;
printf(“总高度:%.2f\n”,总高度);
printf(“平均高度:%.2f\n”,平均高度);
返回退出成功;
}

查看while()中的语句。请注意,当且仅当这些条件导致true时,这将保持循环。

是否执行{
do {
    printf("Enter radius: ")
    scanf("%d", &r);
    printf("Enter height: ")
    scanf("%d", &h);
} while(r<=0 || h<=0);
printf(“输入半径:”) scanf(“%d”、&r); printf(“输入高度:”) scanf(“%d”、&h); }while(r
我指定的范围必须是1到10(含),没有终止程序的负值,并且不允许有其他提示

修改你的主要功能

do{
    int ex = scanf("%f%f", &r, &h); //scanf returns int

   if (ex == 0)
    {break;}
    if(r<=0 || r>10){
        printf("Invalid radius: %.2f\n",r);
       continue;
    }
    if(h<=0 || h>10){
        printf("Invalid height: %.2f\n",h);
       continue;
    }
   // hence above conditions failed means you have given desired input
   // need not to check any conditions 
    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);

  if(k>0) // check this other wise divide by zero will occur
    {
    avgh = toth/k;
    printf("Total Height: %.2f\n",toth);
    printf("Average Height: %.2f\n",avgh);
    }
do{
int-ex=scanf(“%f%f”,&r,&h);//scanf返回int
如果(ex==0)
{break;}
如果(r10){
printf(“无效半径:%.2f\n”,r);
继续;
}
如果(h10){
printf(“无效高度:%.2f\n”,h);
继续;
}
//因此,上述条件失败意味着您已给出所需的输入
//不需要检查任何条件
体积=体积(r,h);
k=k++;
printf(“圆柱体%d半径%.2f高度%.2f体积%.2f\n”,k、r、h、体积);
toth=toth+h;
}而(r>0&&h>0);
如果(k>0)//检查是否会出现另一个被零除的情况
{
avgh=toth/k;
printf(“总高度:%.2f\n”,总高度);
printf(“平均高度:%.2f\n”,平均高度);
}

我知道这一点,但我不知道如何使其包括范围内的所有值,即负值和零包容性。我指定的范围必须是1到10,没有负值终止程序,并且不允许其他提示。当用户输入11或-1作为输入时,是否要打印错误。我的意思是e超出1-10如果半径或高度超出范围,我想打印无效的半径或高度,并在没有文本引导的情况下继续提示输入,循环仅在输入非数字内容时终止。@user2805620相应地修改了答案。程序在输入负值后仍然终止。这是while中的条件当然,我不知道如何改变