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

    1、利用循环计算多个圆柱体积。

    /*计算n个圆柱体体积*/
    #include<stdio.h>
    int main(void)
    {
     double r,v,h;
     double cylinder(double r,double h);
    
     printf("Enter r and h:");
     scanf("%lf%lf",&r,&h);
     v=cylinder(r,h);
     printf("v=%.3f
    ",v);
    
     return 0;
    }
    
    double cylinder(double r,double h)
    {
     double result;
    
     result=3.1415926*r*r*h;
    
     return result;
    }
    
    
     
     

    2、输出用电量并计算水费。

    /*输出用电量并计算应付电费*/
    #include<stdio.h>
    int main(void)
    {
        double x,y;
    
        printf("Enter x: ");
        scanf("%.2f",&x);
        if(x<=0)
           printf("输入结果错误,请重新输入");
        else if(0<x<=50)
        y=0.53*x;
        else
            y=0.53*x+(x-50)*0.05;
        printf("y=%.2f
    ",y);
    
        return 0;
    }

    3、计算多个用户的电费。

    /*计算多户人家的电费*/
    #include<stdio.h>
    int main(void)
    {
        int i,n;
        double x,y;
        double fee(double x);
    
        printf("Enter n:");
        scanf("%d",&n);
    
        for(i=1;i<=n;i++)
        {
            printf("Enter x:");
            scanf("%2f",&x);
    
            if(x<=0)
                printf("输入错误,重新输入");
            else{
                y=fee(x);
            }
            printf("y=%.2f
    ",y);
        }
        return 0;
    }
    double fee(double x)
    {
        double y;
        if(x<=50)
        {
            y=0.53*x;
        }
        else
        {
            y=0.53*x+0.05(x-50);
        }
        return y;
    }
  • 相关阅读:
    bzoj4864 [BeiJing 2017 Wc]神秘物质
    HNOI2011 括号修复
    bzoj2402 陶陶的难题II
    ZJOI2008 树的统计
    USACO09JAN 安全出行Safe Travel
    HAOI2015 树上操作
    hdu5126 stars
    BOI2007 Mokia 摩基亚
    SDOI2011 拦截导弹
    国家集训队 排队
  • 原文地址:https://www.cnblogs.com/zhangling213549/p/3373506.html
Copyright © 2011-2022 走看看