zoukankan      html  css  js  c++  java
  • 函数入门

    1.用函数实现pow功能(不含形参)

    #include <stdio.h>
    #include <stdlib.h>
    
    double power();
    
    int main()
    {
        double result = power();
        printf("结果是:%.2lf\n", result);
        return 0;
    }
    
    double power()
    {
        double num1;
        int num2;
        int i;
        double result;
        printf("请输入底数:");
        scanf("%lf", &num1);
        printf("\n请输入指数:");
        scanf("%d", &num2);
        result = 1;
        for (i = 0; i < num2; i++)
        {
            result = result * num1;
        }
        return result;
    }

    2.简单面积的计算(含形参)

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    double Area(double,double,double,int);
    int validata();
    int main()
    {
        double radius,length,highth;
        int i;
        printf("请选择要计算的图形面积:\n1.圆形\n2.长方形\n3.三角形\n");
        scanf("%d",&i);
        switch(i)
        {
        case 1:
            printf("请输入圆的半径:");
             do
            {
                scanf("%lf",&radius);
                if(!validata(radius))
                {
                    printf("您输入的数字不规范,请重新输入:");
                }
            }while(!validata(radius));
            printf("\n圆的面积为:%.2lf",Area(radius,0,0,1));
            break;
        case 2:
            printf("请输入长方形的长、宽:");
            do
            {
                scanf("%lf%lf",&length,&highth);
                if(!validata(length)||!validata(highth))
                {
                    printf("您输入的数字不规范,请重新输入:");
                }
            }while(!validata(length)||!validata(highth));
            printf("\n长方形的面积为:%.2lf",Area(0,length,highth,2));
            break;
        case 3:
            printf("请输入圆的半径:");
             do
            {
                scanf("%lf%lf",&length,&highth);
                if(!validata(length)||!validata(highth))
                {
                    printf("您输入的数字不规范,请重新输入:");
                }
            }while(!validata(length)||!validata(highth));
            printf("\n三角形的面积为:%.2lf",Area(0,length,highth,3));
            break;
        default:
            printf("请输入正确的数字形式:");
        }
    
        return 0;
    }
    double Area(double radius,double length,double highth,int i)
    {
        double s;
        switch(i)
        {
        case 1:
            s=3.14*pow(radius,2);
            return s;
            break;
        case 2:
            s=length*highth;
            return s;
            break;
        case 3:
            s=length*highth/2;
            return s;
            break;
        }
    
        return 0;
    }
    int validata(double num)
    {
        return num>0?1:0;
    }
  • 相关阅读:
    Python——学习笔记
    Ubuntu开发相关环境搭建
    解决联想R720双系统Ubuntu16.04的无线网卡开启问题及信号不稳定
    Win10环境下多JDK切换以及could not find java.dll异常解决
    Java——this关键字总结
    Java——多线程编程学习/01
    Python日常Bug集
    《集体智慧编程学习笔记》——Chapter2:提供推荐
    解决Pycharm无法显示matplotlib绘图问题
    Kettle启动时报错Cannot create java virtual machine & A java exception has occurred
  • 原文地址:https://www.cnblogs.com/hw1988/p/10561664.html
Copyright © 2011-2022 走看看