C语言基础篇(三)

来源:互联网 发布:中国当前网络环境 编辑:程序博客网 时间:2024/05/22 06:23
//
//  main.c
//  C_03
//
//  Created by 8011 on 15/11/27.
//  Copyright © 2015年 yong. All rights reserved.
//


#include <stdio.h>
//全局变量
int ee = 0;




//在main函数的前面,我们需要将程序需要用到的函数,声明或定义出来
//函数的声明
int getSumm(int a,int b);    //函数的声明+函数的实现=函数的定义








         // 定义
//有返回值有参数类型
int  getMax(int a,int b,int c)
{
    //auto
    int max = a;
    if (max<b)
    {
        max = b;
    }
    if (max<c)
    {
        max = c;
    }
    return max;
}


//无返回值无参数类型
void print()
{
    printf("我是无返回值无参数类型的\n");
}


//有返回值无参数类型
int getSum()
{
    int sum = 0;
    for (int i = 1; i<=100; i++)
    {
        sum+=i;
    }
    return sum;
}


//无返回值有参数类型
void printChar(char alphabet)
{
    printf("alphabet = %c\n",alphabet);
}


//形参实参例子
void changValue(int x)
{
    printf("x 改变之前的值 = %d\n",x);
    x = 999;
    printf("x 改变之后的值 = %d\n",x);
}


//static例子
void test()
{
    static int e = 0;        //对于static类型的局部变量,能再程序运行期间一直保持其最后一次被改变的值
    printf("e = %d\n",e);
    e++;
}



  //求3位数最大一个
int main(int argc, const char * argv[]) {
//    
    int a = 1;
    int b = 2;
    int c = 3;
    int max = a;
    if (a<b)
    {
        max = b;
    }
    if (max<c)
    {
        max = c;
    }
    printf("max = %d\n",max);
    
    /*
     函数 将一些功能代码隐藏起来,给外部提供接口,我们需要知道函数可以干什么就可以了
     使用函数可以实现代码的重用
     */
    
    /*
     C语言函数定义:
     语法:
        返回值类型  函数名(参数类型 参数名1,参数类型 参数名2,...)
     {
        实现函数的代码
         
        return 返回值;
     
     }
     
     
     */
    
    //有返回值有参数类型
    int getmaxa = getMax(1,2,3);  //函数调用
    printf("getmaxa = %d\n",getmaxa);
    
    //无返回值无参数类型
    print();   //函数的调用
    
    //有返还值无参数类型
    int getSumNumber = getSum();
    printf("getSumNumber = %d\n",getSumNumber);
    
    //无返回值无参数类型
    printChar('a');
    
    /*
     每个函数都有返回值类型,即类型标识符
     void类型表示此函数返回值类型为空
     参数列表可以为空
     如果一个函数没有参数,它依然要写()
     
     */
    
    /*
     函数的声明
        函数的声明与函数头一样,以分号结束
    */
    
    int theSum = getSumm(2,3);
     printf("theSum = %d\n",theSum);
    
    /*
     函数的调用
     语法:
        方法名(实参列表)
     
     
     */
    
    /*
     形参(形式参数):在定义函数的时候使用,并且在整个函数内部可以使用,出了函数之后不能使用
     形参:变量,数组
     实参(实际参数):在调用函数时使用,通过实际参数,将数据传递给形式参数,然后再经由形式参数传递给函数
     实参:同类型的常量,变量,数组元素,表达式,数组
     
     */
    
    int x = 10;
    changValue(x);
    
    printf("x = %d\n",x);
    /*
     注意:
     1.形参和实参在数量,类型,顺序上一改严格一致,否则会发生类型不匹配的错误.
     2.在函数中,对形参做出改变,那么在函数执行完毕后,实参的值是不会发生改变的,因为函数内部所操作的都是实参的副本.
     */
    
    /*
     变量的作用域   全局变量   局部变量
     全局变量:定义在我们函数的外部的变量称为全局变量,任何函数都可以使用
     局部变量:定义在我们的函数里面的表量称为局部变量
      如 auto static...
     对于auto类型的局部变量出了函数以后不能使用
     对于static类型的局部变量,能再程序运行期间一直保持其最后一次被改变的值
     如 static i = 0;
     
     */
//    for (int i = 0; i<10; i++)
//    {
//        test();
//    }
    
    /*
     函数的分类
     1.库函数  例如:printf scanf
     2.数学库函数  #include<math.h>
     3.自定义函数  用户在程序中需要而编写的函数
     */
    
    //函数的嵌套调用
    int value = getMax(getSumm(5, 3),2,3);
    printf("value = %d\n",value);
    
    /*
     C程序源代码格式的一般情况如下:
     预编译语句
     宏定义常量
     函数的声明(放这里)
     main函数
     函数的实现(放这里)
     */
    
    
    
    
    
    return 0;
}




int getSumm(int a,int b)
{
   //函数的实现
    int sum = a+b;
    return sum;

 }


三——2

//
//  main.c
//  C_03_02
//
//  Created by 8011 on 15/11/27.
//  Copyright © 2015年 yong. All rights reserved.
//


#include <stdio.h>


#define Square(x)  x+x  //宏的定义




//函数的声明
//函数的迭代

long factorial(int x);

//函数的递归
long factorialSecond(int x);

//斐波那契数列
long factoria1(int x);


//迭代斐波
long fabonacci(int x);




//
//  函数的递归
int main(int argc, const char * argv[])
{
    int result1 = Square(5)*5;  //代表x+x*5
    printf("result1 = %d\n",result1);

    
    
    
      int x;
      printf("请输入一个整数\n");
      scanf("%d",&x);
    //函数的调用
    long xx = factorial(x);
    printf("%ld\n",xx);
    
    //函数的递归
   long result = factorialSecond(x);
   printf("result = %ld\n",result);


    int x,a;
    printf("请输入一个整数:\n");
    scanf("%d",&a);
    for (x = 1; x<=a; x++)
    {
        printf("%ld\t",factoria1(x));
    }

    //函数的调用
    for (int i = 1; i<x; i++)
    {
        printf("%ld\t",fabonacci(i));
    }
  return 0;
}


//
long factorial(int x)
{
     //函数的实现
    long sum = 1;
    for (int i = 1; i<=x; i++)
    {
        sum *= i;
    }
    return sum;
}

   //函数递归
long factorialSecond(int x)
{
    if (x == 1)
    {
        return 1;
    }
    else
    {
        return x*factorialSecond(x-1);
    
    }
}
long factoria1(int x)
 {
    if (x == 1) return 0;
    if (x == 2) return 1;
    return factoria1(x-2) + factoria1(x-1);


}


//long febonacci(int x)
{
    int previous1 = 0;
    int previous2 = 1;
    int current = 0;
    if (x == 1) {
        return 0;
    }
    if (x == 2) {
        return 1;
    }
    int i = 2;
    while (i<x)
    {
        current = previous1 + previous2;
        previous1 = previous2;
        previous2 = current;
        i++;
    }
    return current;

}












0 0