• 实验3 简单的分支与循环结构


    编写求圆面积的程序,要求当输入的半径r<=0时,提示输入错误,要求r为浮点型,r的数值是动态的由键盘输入

    #include<stdio.h>
    int main(void)
    {
        
        double R,S;                     
        printf("Enter R:");
        scanf("%Lf",&R);
    
        if(R>=0){              //当r大于0时,执行循环    
            S=3.14*R*R;
            printf("f(%.2f)=%.2f
    ",R,S);        //输出半径r和面积s的值    }
        else{                                   //若r<0;则显示输入错误,并退出程序                 
            printf("sorry!您的输入错误
    ");
        }
        
    
        return 0;
    }

    利用循环计算n个圆柱体体积

    #include<stdio.h>
    int main(void)
    {
        int n,i;                      //定义两个整型变量n和i    
        double r,h,volume;            //设置半径r,高h以及体积volume为浮点型变量       
        double cylinder(double r,double h);   //定义一个自定义函数    
        printf("请输入n");
        scanf("%d",&n);
    
         for(i=1;i<=n;i++){                  //执行for语句循环        
             printf("请输入圆柱体的半径和高:");
            scanf("%lf%lf",&r,&h);
    
            if((r<=0)||(h<=0)){               //当r或h小于等于0时,退出程序,并显示输入错误        
                printf("sorry,您的输入有误");
            }
            else{
                volume=cylinder(r,h);           //调用函数,返回值赋给volume            
                printf("volume=%.3f
    ",volume);
            }
         }
        
    return 0;
    }
    /* 定义求圆柱体积的函数 */
    double cylinder(double r,double h)
    {
        double result;
        result = 3.14*r*r*h;   
        return result;
    }

    阅读下面程序、分析说明运行结果

    #include<stdio.h>
    int main(void)
    {
        int c1,c2,c3,c4;            
    scanf("%d%d",&c1,&c2); printf("%d ",c1+c2); getchar( ); c3 = getchar( ); c4 = getchar( ); printf("%d ",c3+c4); return 0; }

    #include<stdio.h>
    int main(void)
    {
        char c1,c2,c3,c4;            
        scanf("%c%c",&c1,&c2);
        printf("%c%c
    ",c1,c2);
        getchar( );
        c3 = getchar( );
        c4 = getchar( );
        printf("%c%c
    ",c3,c4);
        printf("%c%c%c%c
    ", c1,c2,c3,c4);
        return 0;
    }

  • 相关阅读:
    LocalSessionFactoryBean有几个属性查找hibernate映射文件
    关于Spring中配置LocalSessionFactoryBean来生成SessionFactory
    【Spring源码分析】配置文件读取流程
    Java序列化接口的作用总结1
    Java序列化接口的作用总结
    hibernate抓取策略
    170531、FormData 对象的使用
    170530、java 迭代hashmap常用的三种方法
    170529、springMVC 的工作原理和机制
    170526、spring 执行定时任务
  • 原文地址:https://www.cnblogs.com/liyang1995/p/3417277.html
Copyright © 2020-2023  润新知