函数多文件管理

.h文件

//

//  MyFunction.h

//  C6_函数多文件管理

//

//  Created by dllo on 15/7/7.

//  Copyright (c) 2015年 cml. All rights reserved.

// 

#import <Foundation/Foundation.h>

 // 在.h里,我们写函数的声明

 // 声明相当于说明书里的目录部分,具体的功能是实现和介绍

 // 1-n的求和

//int sumValue(int n);

 

//int dayOfYear(int year,int month,int day);

//void  sum(int a,int b );

 

//// 对整形一位数组进行冒号排序

//void bubblenSort(int arr[],int count);

//// 对整形的一维数组进行打印

//void printArr(int arr[],int count);

 

// 回文串

// 字符串本身有结束标志,所以不需要再传一个长度

//BOOL huiwenchuan(char str[]);

 

  // 找到两个整数中的最大值,并且返回

int maxInTwo(int a,int b);

 

  // 三个整数的最大值,并且返回

int maxInThree(int a,int b,int c);

 // 四个数的最大值

int maxInFour(int a,int b,int c,int d);

// wuge

int maxInFive(int a,int b, int c,int d,int f); 

// 递归函数

void test(); 

// 计算一下阶乘

int  factorialNum(int n); 

   // 1,如果数组中有78这个数字,把这个数字变成100,并且把这个下表进行返回,没有的话返回5

int checkArr(int arr[],int count, int num);

 

 

.m文件

//  MyFunction.m

//  C6_函数多文件管理

//

//  Created by dllo on 15/7/7.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import "MyFunction.h"

 

// 在.m文件里写相应的函数的定义

 

int sumValue(int n){

    int sum =0;

    for (int i =1; i<=n ; i++) {

        sum +=i;

    }

    return sum;

}

 

int dayOfYear(year,month,day ){

    switch (month-1) {

        case 11:

            day +=30;

        case 10:

            day +=31;

        case 9:

            day +=30;

        case 8:

            day +=31;

        case 7:

            day +=31;

        case 6:

            day +=30;

        case 5:

            day +=31;

        case 4:

            day +=30;

        case 3:

            day +=31;

        case 2:

            if ((year % 400 == 0)||((year % 4 == 0)&&(year % 100 != 0)) ){

                day +=29;

            }else{

                day += 28;

            }

        case 1:

            day +=31;

                

                

         

    }

    return day;

}

 

void  sum(int a,int b ){

    printf("%d\n%d\n%d\n%d\n",a+b,a-b,a*b,a/b);

    

}

// 如果复制过来之后有声明的分号,分号一定要删除

void bubblenSort(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 =0;

                temp =arr[j];

                arr[j]=arr[j+1];

                arr[j+1]=temp;

            }

        }

    }

}

void printArr(int arr[],int count){

    for (int i = 0;i< count ;i++){

        printf("%d ",arr[i]);

    }

    printf("\n");

}

BOOL huiwenchuan(char str[]){

for(int i = 0;i<strlen(str )/2;i++){

    if(str[i ]!=str[strlen(str )-1-i]){

        return NO;

    }

}return YES;

}

 

// 两个数中较大的

int maxInTwo(int a,int b){

    a=a>b?a:b;

    return a;

}

 

 

int maxInThree(int a,int b,int c){

    // 函数的嵌套调用

    int max =maxInTwo(a, b);

    max = maxInTwo(max , c);

    return max;

}

 

// 四个数的最大值

int maxInFour(int a,int b,int c,int d){

    int max =maxInThree(a, b, c);

    max = maxInTwo(max , d);

    return max;

}

 

// wuge

int maxInFive(int a,int b, int c,int d,int f){

    int max = maxInFour(a, b, c, d);

    max = maxInTwo(max , f );

    return max;

}

// 如果写递归的话,一定要注意,在自己调用自己的时候一定要给程序留一个结束的出口

 

 

void test(){

    test();

}

 

// 计算一下阶乘

int  factorialNum(int n){

    // 首先给函数找一个结束的出口

    if (n == 1) {

        return 1;

    }

    return factorialNum(n-1)*n;

}

 

 

int checkArr(int arr[],int count, int num){

    for (int i =0; i< count; i++) {

        if (arr[i] == num) {

            arr[i]= 100;

            return i;

        }

    }

    return 5;

}

 

 

主函数

  main.m

//  C6_函数多文件管理

//

//  Created by dllo on 15/7/7.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import <Foundation/Foundation.h>

// 引头文件

#import "MyFunction.h"

 

int g = 10;

// 最大公约数

//int maxNnmber(int a,int b){

//    int min= 0;

//    int maxn=0;

//    min =a > b?b:a;

//    for (int i = min; i>0; i--) {

//        if (b % i == 0 && a % i ==0) {

//            maxn=i;

//            break;

//        }

//    }

//    return maxn;

//}

 

//int strintLen(char str[]){

//    int len = 0;

//    while(str[len]!='\0'){

//        len++;

//    }

//    return len;

//}

 

 // 数组作为参数进行传递

// 第一部分:传递一个数组名]

// 第二部分:需要执行一个数组的有效长度

//void printArr(int arr[],int count){

//    for(int i = 0; i<count;i++ ){

//        printf("%d ",arr[i]);

//    }

//        

//}

 

// 通过函数的方式,対一位整形数组进行冒泡排序

//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 =0;

//                temp = arr[j];

//                arr[j]=arr[j+1];

//                arr[j+1]=temp;

//            }

//        }

//    }

//}

 

//void addNum(int a, int b){

//    printf("%d\n",a+b );

//}

//int  addNum(int a,int b){

//    return a+b;

//}

 

void testNum(){

    printf("%d\n",g );

}

 

 

int main(int argc, const char * argv[]) {

    // 1.字符串数组的排序

//    char stuName[4][20]={"zhaozhicheng","luochuanxi","zhangbingjian","guohongrui"};

//    for (int i =0; i<4-1; i++) {

//        for (int j =0; j<4-1-i; j++) {

//            if (strcmp(stuName[j], stuName[j+1])>0) {

//                char temp[20] ="";

//                // 字符串的操作都要用strcpy

//                strcpy(temp, stuName[j]);

//                strcpy(stuName[j], stuName[j+1]);

//                strcpy(stuName[j+1], temp);

//            }

//        }

//    }

//    for (int i =0; i<4; i++) {

//        printf("%s\n",stuName[i]);

//    }

//     char str[20] = "";

//    scanf("%s",str); //遇到空格就停

////    gets(str);    // 输入多少,打印多少

//    printf("%s",str);

    // 回文串

//    for (int i =0; i<strlen(str ); i++) {

//        if (str[i]!=str[strlen(str )-1-i]) {

//            printf("不是回文串");

//            break;

//        }

//    }

     // 用函数编写一个程序,输入两个正整数,要求返回最大公约数

//    int a=0,b =0;

//    scanf("%d%d",&a,&b);

//    int ruselt = maxNnmber(a, b);

//    printf("%d\n",ruselt);

     // 字符串长度

//    char str[20] = "yanglin";

//    printf("%d\n",strintLen(str));

//    int arr[5]={1,5,3,4,2};

////    printf("%p\n",arr);

////    printf("%p\n",&arr[0]);

////    printf("%p\n",&arr[1]);

////    printf("%p\n",&arr[2]); 

////    printArr(arr,5);

//    printArr(arr, sizeof(arr)/sizeof(int)); 

//    int arr[8]={123,5435,765,987,578,234,65,47};

//    bubbleSort(arr, sizeof(arr)/sizeof(int));

//    for (int i =0; i<sizeof(arr)/4; i++) {

//        printf("%d ",arr[i]);

//    }printf("\n");

//    int a,b;

//    printf("%d\n",3+5);

//    printf("%d\n",addNum(3 , 5 ));

    // 多文件的时候,函数的调用还是用函数名来进行

//    printf("%d\n",sumValue(100));

//    printf("%d\n",dayOfYear(1993, 2, 29));  

//    int a,b ;

//    scanf("%d%d",&a ,&b);

//      sum(a , b );

    

//    int arr[8]={1,6,78,8,4,3,18,2};

//    // 调用两个函数,排序和打印

//    bubblenSort(arr, 8);

//    printArr(arr, 8);

//    // 就是节省代码,提高代码的重用

    

//    if (huiwenchuan("level")) {

//        printf("是\n");

//    }else{

//        printf("否\n");

 //    int result=maxInTwo(3, 5);

//    printf("%d\n",result);

//    int result = maxInThree(3, 5, 4);

//    printf("%d\n",result );

    // 如果写递归的话,一定要注意,在自己调用自己的时候一定要给程序留一个结束的出口

//        test();

//    printf("%d\n",factorialNum(5));

//    g++;

//    printf("%d\n",g);

//    testNum();

//    

    

    int arr[5] = {14,3,5465,7,56};

   // 1,如果数组中有78这个数字,把这个数字变成100,并且把这个下表进行返回,没有的话返回5

    printf("%d\n",checkArr(arr,5,78));

  return 0;

}

 

posted @ 2015-07-07 20:43  天涯ml  阅读(122)  评论(0编辑  收藏  举报