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;
            }

  • 相关阅读:
    02.ZooKeeper的Java客户端使用
    01.ZooKeeper安装和介绍
    02.Elasticsearch入门
    01.Elasticsearch安装
    01.ActiveMQ安装部署
    springboot项目打包时提示“程序包xxx不存在,找不到符号”
    Eclipse提交git代码 报错authentication not supported
    Eclipse提交git代码 报错authentication not supported
    utf8mb4_general_ci报错解决方案
    mysql开启远程访问
  • 原文地址:https://www.cnblogs.com/liyang1995/p/3417328.html
Copyright © 2011-2022 走看看