zoukankan      html  css  js  c++  java
  • 实验 4 在分支循环结构中调用自定义函数

    /* 利用循环计算多个圆柱体体积 */
    #include<stdio.h>
    int main(void)
    {
        int n,i;
        double r,h,volume;   //定义浮点型变量,r=半径,h=高,volume=体积    
        double cylinder(double r,double h);  //定义函数      
        printf("请输入n");     //循环求n个圆柱体的体积    
        scanf("%d",&n);
         for(i=1;i<=n;i++){
            printf("请输入圆柱体的半径和高:");
            scanf("%lf%lf",&r,&h);
    
            if((r<=0)||(h<=0)){   //当半径或高小于等于0是,提示错误        
                printf("sorry,您的输入有误");
            }
            else{
                volume=cylinder(r,h);  //否则,调用自定义函数结果            
                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 i;
        double x,y;                 // 定义x为用电量,y为电费    
        printf("请输入电量:
    ");
        scanf("%Lf",&x);
        if(x<0){
               printf("sorry,您的输入有误");
            }
            else  if(x<=50){    //若电量小于50,则y=0.53*x                
                y=0.53*x;
            }
            else{
                 y=50*0.53+(x-50)*0.58;     //电量大于50后,超出部分0.58元每瓦计算   
               printf("y=%.3f
    ",y);
            }
            return 0;
         }

    // 计算并输出多个用户应支付的电费 
    #include<stdio.h>
    int main(void)
    {
        int m,i;    //m为用户的个数    
        double x,y;    // 定义x为用电量,y为电费 
         double cylinder(double x);  //设置自定义函数     
         printf("请输入m");
        scanf("%d",&m);
         for(i=1;i<=m;i++){
        printf("请输入电量:
    ");
        scanf("%Lf",&x);
        if(x<0){                  //当x小于0时,提示输入错误           
            printf("sorry,您的输入有误");
            }
        else { 
            y=cylinder(x);         //定义函数            
            printf("y=%.3f
    ",y);
            }
         
         }
    return 0;
    }
         double cylinder(double x)   //调用函数     
         {
             double y;
        
              if(x<=50){          //将函数结果返回上面函数中                
                  y=0.53*x;
            }
              
            else{
                y=50*0.53+(x-50)*0.58;
            }
            
                return y;
            }

  • 相关阅读:
    Redis
    Linux 软件安装
    Linux系统的目录和启动过程,Linux命令,权限控制
    虚拟机,Linux,VMware三种网络连接模式区别
    Spring Boot
    shiro和spring和springmvc的集成
    shiro
    Mybatis的逆向工程,MySQL8的数据库,8.0.11驱动的逆向工程的坑的解决方法
    jdk分析工具:jps和jstack
    如何使用jconsole(英文)
  • 原文地址:https://www.cnblogs.com/liyang1995/p/3417328.html
Copyright © 2011-2022 走看看