zoukankan      html  css  js  c++  java
  • C语言--函数

    #import <Foundation/Foundation.h>
    #import "MyFunction.h"
    #import "Operator.h"
    #define PI 3.1415926
    int mediumValue(int o , int p ,int q)
    {
        
    #pragma mark-------------总结几种求中间数的方法
        //三个数求和,减去最大的,最小的
        //数组排序
        //第一种方法
         //先求最大,再求最小,最后就是中间的
        int max = 0,min =0,med= 0;
        if (o>p&&o>q) {
            max = o;
        }else if (p>o&&p>q){
            max = p;
        }else{
            max =q;
        }
        
        if (o<p&&o<q) {
            min = o;
        }else if (p<o&&p<q){
            min = p;
        }else{
            min =q;
        }
        
        if ((o>p&&o<q)||(o<p&&o>q)) {
            med = o;
        }else if ((p>o&&p<q)||(p<o&&p>q)){
            med = p;
        }else{
            med =q;
        }
        return med;
    }
    
    //没有返回值,没有參数
    void printfLanou(void);
    void printfLanou(void)
    {
        printf("蓝鸥 ");
    }
    
    //没有返回值,有參数
    void printfNum(int number);
    void printfNum(int number)
    {
        printf(" %d ",number);
    }
    
    //有返回值,无參数
    //float PI()
    //{
    //    return 3.1415;
    //}
    
    //既有返回值,又有參数
    
    //int square(int x)
    //{
    //    return x*x;
    //}
    
    //求和
    int sumsValue( int m,int n);
    int sumsValue( int m,int n)
    {
        int sums = 0 ;
        for (int i = m; i<=n; i++) {
            sums = sums +i;
        }
        return sums;
    
    }
    
    int main(int argc, const char * argv[])
    {
    //    int  b[10]={0};
    //    for (int i = 0; i<10; i++) {
    //        b[i]=arc4random()%(20-10+1)+10;
    //    }
    //    for (int j = 0; j<10; j++) {
    //        printf("%d ",b[j]);
    //    }
    //    
    //    int c[5]={0};
    //    for (int i  =0; i<5; i++) {
    //        c[i]=arc4random()%(60-20+1)+20;
    //    }
    //    printf("
    ");
    //    for (int i = 0; i<5; i++) {
    //        printf("%d ",c[i]);
    //    }
    //    
    //    printfLanou();
    //    printfNum(5);
    //    printf("%f ",PI()+3);
    //    printf(" %d",square(5));
    //    printf(" %d",sumsValue(1,9));
    //    
    //    int a[5]={3,5,1,2,9};
    //    bubbleSort(a, 5);
        
    #pragma mark --------总结数组函数
        //数组作为參数,把数组名传入,即数组的首地址
        //数组一旦创建,就有固定地址,不能操作整个数组,仅仅能操作数组中某个元素
        //函数能够嵌套调用,可是不能够嵌套定义
        
    
        
        
        
        //1.编写函数int sumValeu(int n);计算1到n的和
    //    int a = 0;
    //    a = sumValue(101);
    //    printf("%d",a);
        
        //2.编写函数dayOfYear(Year,month,day)
    //    printf("
    ");
    //    dayOfYear(2014, 1, 13);
        
        //3.编写函数,返回三个整数的中间数
    //    int mediu = mediumValue(2, 3,1);
    //    printf("mediu = %d",mediu);
        //4,编写函数,返回正整数n中数字的个数
        //方法-
    //    numbers(345);
        //方法二(while循环)
    //    5.创建⼀对⽂件:operator.h operator.m
    //       实现函数。对两个整型数的加、减、乘、除。

    //加 // add(1, 2); //减 //乘 //除 //6.计算 s = (2*2)! + (3*3)! +(4*4)! //1、整型数的平⽅ //2、⼀个整型数的阶乘 //3、三个整形的平⽅的阶乘的和 //平方 // square(2); // printf("平方%d", square(2)); //阶乘 // factorial(square(2)); // printf("阶乘%d",factorial(square(2))); //求和 // int s = 0; //第一种 // s = sum(factorial(square(2)), factorial(square(3)), factorial(square(4))); //另外一种 // for (int i = 2; i<=4; i++) { // s=s+factorial(square(i)); // } // s = factorial(5); // printf(" "); // printf("s=%d",s); #pragma mark--------总结static //同样函数类型,同样的返回值,同样个数的參数 //(后执行期,先编译期) //凡是函数内部定义的变量都是局部变量 //没有static修饰,执行期放在栈区,用完销毁 //有static修饰,编译期已经放在静态区,非常占内存,仅仅能初始化一次 //用static修饰的函数,仅仅能在本文件里使用 //NTFS插件 // for (int i = 0; i<10; i++) { // test(); // } return 0; }


    "Operator.h"
    //加
    void add(int a,int b);
    //减
    void reduce(int a,int b);
    //乘
    void multiply(int a,int b);
    //除
    void divide(int a,int b);
    
    
    void test();
    
    <pre name="code" class="objc">"Operator.m"

    
    

    //加
    void add(int a,int b)
    {
        printf("%d",a+b);
    }
    //减
    void reduce(int a,int b)
    {
        printf("%d",a-b);
    }
    //乘
    void multiply(int a,int b)
    {
        printf("%d",a*b);
    }
    //除
    void divide(int a,int b)
    {
        printf("%d",a/b);
    }
    
    #pragma mark-------statickeyword
    void test()
    {
    //    static int i = 10;
        int i = 10;
        printf("%d ",++i);
    }
    MyFunction.h

    int  sumValue(int n);
    void dayOfYear(int year,int month,int day);
    int mediumValue(int o , int p ,int q);
    
    //冒泡排序,arr是要排序的数组,count是数组的个数
    void bubbleSort(int arr[],int count);
    //帮我写一个随机数的函数
    
    //正整数n中数字的个数
    void numbers(int n);
    
    //平方
    int square(int x);
    //阶乘
    int factorial(int n);
    //求和
    int sum(int a, int b, int c);
    MyFunction.m

    int  sumValue(int n)
    {
        int sum = 0;
        for (int i = 1; i<n; i++) {
            sum = sum + i;//不要把n写进去
        }
        return sum;
    };
    void dayOfYear(int year,int month,int day)
    {
        //1,3,5,7,8,10,12 31天
        //4,6,9,11 30
        //2 28
        int days = 0;
        int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        for (int i = 1; i<month; i++) {
            days = days + a[i];
        }
        if (year % 400 ==0 || (year %4 == 0 && year /100 !=0)) {
            if (month >2) {
                days = days + 1;
            }
        }
        days = days + day;
        printf("第%d天",days);
    }
    
    //冒泡排序,arr是要排序的数组,count是数组的个数
    void bubbleSort(int arr[],int count)
    {
        for (int i = 0; i<count-1; i++) {
            for (int j = 0; j<count-1-i; j++) {
                if (arr[j]>arr[j+1]) {
                    int temp =arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
        printf("
    ");
        for (int i = 0; i<count; i++) {
            printf("%d ",arr[i]);
        }
    }
    
    //正整数n中数字的个数
    void numbers(int n)
    {
        int number =n;
        int num[5]={1,10,100,1000,10000};
        printf("
    ");
        int temp = 0;
        for (int i = 0; i<5; i++) {
            if (number/num[i] != 0) {
                temp = i;
            }
        }
        printf("正整数n中数字的个数是%d",temp+1);
    }
    
    //平方
    int square(int x)
    {
        return x*x;
    }
    
    //阶乘
    int factorial(int n)
    {
    //    int factor = 1;//错过一次,乘法for循环累乘从0開始
    //    for (int i = 1; i<=n; i++) {
    //        factor = factor * i;
    //    }
    //    return  factor;
        
    #pragma mark-----------递归求阶乘
        //递归
        if (n == 1) {
            return 1 ;
        }
        return n * factorial(n-1);
        
    }
    //求和
    int sum(int a, int b, int c)
    {
        return a+b+c;
    }






  • 相关阅读:
    牛客(14)链表中倒数第k个结点
    牛客(13)调整数组顺序使奇数位于偶数前面
    牛客(12)数值的整数次方
    牛客(11)二进制中1的个数
    牛客(10)矩形覆盖
    牛客(9)变态跳台阶
    牛客(8)跳台阶
    牛客(7)斐波那契数列
    Docker数据卷
    Docker镜像
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5269986.html
Copyright © 2011-2022 走看看