您的位置:首页 > 其它

C6_函数多文件管理

2015-07-14 19:33 323 查看
//

// MyFunction.h

// C6_函数多文件管理

//

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

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

//

#import <Foundation/Foundation.h>

// 在 .h
里,我们需要先写函数声明

// 声明相当于说明书的目录部分,具体的功能实现和介绍在 .m
里对应的位置

// 1 - n 的求和

// 1、编写函数int sumValue(int n):计算1到n的和。
int sumValue(int n);

// 2、编写函数dayOfYear(year, mouth,day),使得函数返回由这三个参
数确定的那一天是一年中的第几天。
int dayOfYear(int year,
int month, int day);

// 3、编写函数,返回三个整数的中间数。
int middleNum(int a,
int b, int c);

// 4、编写函数,返回正整数n中的数字的个数。
int sum(int n);

// 对整形的一维数组进行冒泡排序
void bubbleSort(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);

// 五个数的最大值
int maxInFive(int a,
int b, int c,
int d, int e);

// 递归调用
void test();

// 计算:s = 5!;
int factorialNum(int n);

void testNum();

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

//

// MyFunction.m

// C6_函数多文件管理

//

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

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

//

#import "MyFunction.h"

// 在 .m
文件里写相应的函数定义注意声明部分要与 .h 里的完全一样

// 1、编写函数int sumValue(int n):计算1到n的和。
int sumValue(int n){
// 此处与 .h
完全一致

int sum = 0;

for (int i =
0; i <= n ; i++) {
sum += i;
}

return sum;
}

// 2、编写函数dayOfYear(year, mouth,day),使得函数返回由这三个参
数确定的那⼀一天是⼀一年中的第⼏几天。
int dayOfYear(int year,
int month, int 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:
day +=
28;

case 1:
day +=
31;

if (year % 400 ==0 || (year %
4 == 0 && year %
10 != 0)) {
day = day;
}else{
day = day +
1;
}
}

return day;
}

// 3、编写函数,返回三个整数的中间数。
int middleNum(int a,
int b, int c){

int max = 0, min =
0;
max = a > b ? a : b;
max = max > c ? max : c;
min = a < b ? a : b;
min = min < c ? min : c;

return a + b + c - max - min;
}

// 4、编写函数,返回正整数n中的数字的个数。
int sum(int n){

int a = 0;

while (n != 0) {
n /=
10;
a++;
}

return a;
}

// 对整形的一维数组进行冒泡排序

// 如果复制过来之后有声明的分号,一定要删除分号
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;

}
}
}

}

// 对整形的一维数组进行打印操作
void printArr(int arr[],
int count){

for (int i =
0; i < count ; i++) {
printf("%d ", arr[i]);
}
}

BOOL huiWenChuan(char str[]){

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

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

return NO;
}
}

return
YES;
}

// 两个数中较大的数
int maxInTwo(int a,
int b){

int max = 0;
max = a > b ? a : b;

return max;
}

// 三个数的最大值
int maxInThree(int a,
int b, int c){

//
函数的嵌套调用

// int max = a > b ? a : b;

int max = maxInTwo(a , b);

// max = max < c ? max : c;
max = maxInTwo(max, c);

return max;
}

// 四个数的最大值
int maxInFour(int a,
int b, int c,
int d){

int max = maxInThree(a, b, b);
max = maxInTwo(max, d);

return max;
}

// 五个数的最大值
int maxInFive(int a,
int b, int c,
int d, int e){

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

return max;
}

//递归调用

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

int factorialNum(int n){

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

if (n == 1) {

return 1;
}

return factorialNum(n -
1) * n;
}

void testNum(){

int g = 0;
printf("%d\n",g);
}

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年 Clare. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "MyFunction.h"
// 引头文件

//// 最大公约数

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

// int min = a < b ? a : b;

// int maxNum = 0;

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

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

// maxNum = i;

// break;

// }

// }

// return maxNum;

//}

//

//// 字符串长度

//int strintLen(char str[20]){

// int len = 0;

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

// len++;

// }

// return len;

//}

//

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

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

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

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

// for (int i = 0; i < count ; i++) { // count
有效长度防止越界

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

// }

// printf("\n");

//}

//// 通过函数的方式,对一维整型数组进行冒泡排序

//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);

// }

// 是否返回看题目下面是否需要该值需要则要有返回值否则有没有都可以

int g =
0;

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

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

// char stuName[4][20] = {"zhangsan", "lisi", "wangwu", "liushanshan"};

// 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 ",stuName[i]);

// }

// printf("\n");

// char str[20] = "";

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

// // gets(str);

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

//

//

// // 回文串

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

// if (str[i] != str[strlen(str) - i - 1]) { //实际长度比下标的最大值小1

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

// break;

// }

// }

//

//

//

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

//

// int a = 0, b = 0;

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

// int result = maxDivisor(a, b);

//

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

//

//

// // 字符串长度

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

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

// // 用数组作为参数传递时,要注意越界问题

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

// printf("%p\n", &arr[0]); // 下标为0的数在栈区内的保存地址

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

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

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

//

// printf("%ld\n",sizeof(arr));

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

//

// int arr[8] = { 98, 1, 92, 66, 12, 21, 5, 7};

// bubbleSort(arr, 8);

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

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

// }

// printf("\n");

//

// // 1、编写函数int sumValue(int n):计算1到n的和。

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

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

//

//

// //2、编写函数dayOfYear(year, mouth,day),使得函数返回由这三个参
数确定的那⼀一天是⼀一年中的第⼏几天。

// int year = 0, month = 0, day = 0;

// scanf("%d%d%d", &year, &month, &day);

// printf("这是第%d天\n",dayOfYear(year, month, day));

//

// // 3、编写函数,返回三个整数的中间数。

// int a = 0, b = 0, c = 0;

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

// printf("中间数是:%d\n",middleNum(a, b, c));

//

// // 4、编写函数,返回正整数n中的数字的个数。

// int n = 0;

// scanf("%d", &n);

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

// int arr[8] = { 32, 25, 54, 87, 3, 21, 12, 11};

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

// bubbleSort(arr, 8);

// printArr(arr, 8);

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

//

//

// if (huiWenChuan("level")) {

// printf("是\n");

// }else{

// printf("否\n");

// }

//

//

// // 两个数中的大数

// int a = 0, b = 0;

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

// printf("%d",maxInTwo(a, b));

// test();

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

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

// g++;

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

// testNum();

int arr[5] = {15,
78, 20,
6, 7};

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

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

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: