function multi-file management

Source: Internet
Author: User
Tags greatest common divisor

. h file

//

MyFunction.h

c6_ function Multi-file management

//

Created by Dllo on 15/7/7.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

In. h, we write the declaration of the function

The statement is equivalent to the directory part of the specification, the specific function is to implement and introduce

The summation of 1-n

int sumvalue (int n);

int dayofyear (int year,int month,int day);

void sum (int a,int b);

To sort an array of integers by colon

void Bubblensort (int arr[],int count);

Print a one-dimensional array of shaping

void Printarr (int arr[],int count);

Palindrome string

The string itself has an end flag, so there is no need to pass a length

BOOL Huiwenchuan (char str[]);

Finds the maximum value in two integers, and returns

int maxintwo (int a,int b);

The maximum value of three integers, and returns

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

Four-digit maximum value

int maxinfour (int a,int b,int c,int D);

Wuge

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

Recursive functions

void Test ();

Calculate factorial

int factorialnum (int n);

1, if the array has 78 this number, the number is changed to 100, and the following table is returned, no words returned 5

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

. m file

Myfunction.m

c6_ function Multi-file management

//

Created by Dllo on 15/7/7.

Copyright (c) 2015 CML. All rights reserved.

//

#import "MyFunction.h"

Write the definition of the corresponding function in the. m file

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

}

If a semicolon is declared after it has been copied, the semicolon must be removed.

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;

}

The larger of the two numbers

int maxintwo (int a,int b) {

a=a>b?a:b;

return A;

}

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

Nested calls to functions

int Max =maxintwo (A, b);

max = Maxintwo (max, c);

return Max;

}

Four-digit maximum value

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;

}

If you write recursion, be sure to note that when you call yourself, you must leave the program with an end exit.

void Test () {

Test ();

}

Calculate factorial

int factorialnum (int n) {

First, find an end exit for the function.

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 function

Main.m

c6_ function Multi-file management

//

Created by Dllo on 15/7/7.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

Header file

#import "MyFunction.h"

int g = 10;

Greatest common divisor

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]!= ') {

len++;

//    }

return Len;

//}

The array is passed as a parameter

First part: Passing an array name]

Part II: The effective length of an array that needs to be executed

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

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

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

//    }

//

//}

's really into a single-shaped array to bubble sort by the way of a function

void Bubblesort (int arr[],int count) {//Pass in Address

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. Ordering of string arrays

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] = "";

Use strcpy to manipulate strings.

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); Stop when you encounter a space

Gets (str); How much to enter and how much to print

printf ("%s", str);

Palindrome string

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

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

printf ("Not palindrome string");

Break

//        }

//    }

Write a program with a function, enter two positive integers, ask to return greatest common divisor

int a=0,b = 0;

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

int ruselt = Maxnnmber (A, b);

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

String length

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

When multiple files are used, the function is called by the function name.

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};

Call two functions, sort and print

Bubblensort (arr, 8);

Printarr (arr, 8);

is to save code and improve code reuse.

if (Huiwenchuan ("level")) {

printf ("Yes \ n");

}else{

printf ("No \ n");

int Result=maxintwo (3, 5);

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

int result = Maxinthree (3, 5, 4);

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

If you write recursion, be sure to note that when you call yourself, you must leave the program with an end exit.

Test ();

printf ("%d\n", Factorialnum (5));

g++;

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

Testnum ();

//

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

1, if the array has 78 this number, the number is changed to 100, and the following table is returned, no words returned 5

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

return 0;

}

function multi-file management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.