码迷,mamicode.com
首页 > 编程语言 > 详细

C语言--函数

时间:2014-06-16 19:58:37      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   http   ext   使用   

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

#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("\n");
//    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("\n");
//    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("\n");
//    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-------static关键字
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("\n");
    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("\n");
    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;
}






C语言--函数,布布扣,bubuko.com

C语言--函数

标签:class   blog   code   http   ext   使用   

原文地址:http://blog.csdn.net/zuoyou1314/article/details/31049839

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!