2.函数与程序结构

2022-11-29  本文已影响0人  billgege

1.函数基础

//
// Created by billge on 2022/11/20.
//

#include <stdio.h>

/**
 * x 形式参数
 * @param x
 * @return
 */
double F(double x){
    return x*x + x+1;
}

double G(double x,double y,double z){
    return x*x+y*y+z*z;
}


/**
*  函数基础
*/

int main(){

    puts("Hello-word");


    /**
     * 2.0 是实际参数
     */
    double result_f = F(2.0);
    double result_g = G(3.0,4.0,5.0);

    printf("result of f: %f\n",result_f);
    printf("result of f: %f\n",result_g);


    return 0;
}

2.函数的原型

//
// Created by billge on 2022/11/20.
//

#include <stdio.h>

/**
*   函数的原型
*/


void EmptyParamList() {
  puts("Hello");
}

void EmptyParamListNew(void);

/**
 * 原型
 * 1.函数名
 * 2.函数返回值类型,如果没有写,默认为int
 * 3.函数参数列表,参数类型,和参数的顺序,参数形参名不重要
 * @return
 */
int Add(int left, int right) { return left + right; };

int main(void) {

  EmptyParamList();
  //和C++的区别,如果调用的函数参数是void,则编译不通过
  EmptyParamList(1);
  EmptyParamList(1.0, "Helllo");

  //函数调用
  int result = Add(1, 2);
  printf("result of add: %d\n", result);

  return 0;
}

3.变量的类型和作用域

//
// Created by billge on 2022/11/20.
//
#include <stdio.h>

/**
* 变量的类型与作用域
*/


//文件作用域
int gloabl_var = 1;

void LocalStaticVar(void){
  //静态变量
  //文件作用域,不赋初始值,也会有初始值0
  static int static_var;
  //自动变量 auto
  // 函数,块,作用域,没有初始值
  int non_static_Var;//出了函数作用域就会被销毁

  printf("static_var: %d\n",static_var++);
  printf("non_static_Var: %d\n",non_static_Var++); //msvc运行调用会报错,gcc编译不会出错,但是会有意向不到的结果错误

}

double Add(double  a, double b);

void CleanMemory(){
  int eraser = -1;
}


//函数原型作用域,例如 size
double Sort(int size,int arrary[size]);



void PassByMemory(int parameter){
  printf("%d/n",parameter);
}

//寄存器变量 : C语言编译完成生成了汇编指令
//装个插件:Compiler expoler 观察
//装完插件后,设置服务器地址:https://godbolt.org
void PassByRegister(register int parameter){
  printf("%d\n",parameter);
}

int main(){ //函数作用域


  //自动变量: 内存自动分配和自动销毁
  auto int value = 0;

  {
    //块作用域
    auto int a = 1;
  }

  if(value>0){
    //块作用域
    int is_value_equals_0 = 0;
  }else{
     gloabl_var =20;
  }


  return 0;
}

4.函数的变长参数

//
// Created by billge on 2022/11/21.
//

#include <stdio.h>
#include <stdarg.h>
/**
* 函数的边长参数
*/

void HandleVarargs(int arg_count, ...) {
  //1.定义va_list 用于获取我们变长参数
  va_list args;

  //2.开始遍历
      va_start(args, arg_count);
  for (int i = 0; i < arg_count; ++i) {
    //3.取出对应的参数,(va_list,type)
    int arg = va_arg(args, int);
    printf("%d : %d\n", i, arg);
  }

  //4、结束遍历
      va_end(args);
}

int main() {

  HandleVarargs(4,1,2,3,4);

  return 0;
}

5.函数的递归

//
// Created by billge on 2022/11/21.
//
#include <stdio.h>

//函数的递归


/**
 *
 * 实现阶乘
 * f(n) = nf(n-1)
 *    f(n-1) = (n-1)f(n-2)
 *    ......
 *    f(1)=f(0)
 *
 *  终止条件: f(0)=1  初值
*/

unsigned int Factorial(unsigned int n) {
  if (n == 0 || n == 1) {
    return 1;  //f(0)=1, f(1)=1
  } else {
    return n * Factorial(n - 1);//f(n)=nf(n-1)
  }

}

/**
 * 通过迭代的方式实现阶乘 n!
 */
unsigned int FactorialByIteration(unsigned int n) {
  unsigned int result = 1;
  for (unsigned int i = n; i > 0; --i) {
    //result = result * i;
    result *= i; // n * (n-1)
  }
  return result;
}

/**
 * 斐波那契数列规则:f(n)=f(n-1) + f(n-2)
 * 初值:f(0)=0;f(1)=1
 * @return
 */
unsigned int Fabonacii(unsigned int n) {
  if (n == 0 || n == 1) {
    return n;//f(0)=1, f(1)=1
  } else {
    return Fabonacii(n - 1) + Fabonacii(n - 2); //f(n) = f(n-1) + f(n-2)
  }
}

/**
 * 斐波那契数列 通过迭代方式实现
 * @return
 */
unsigned int FabonacciByIteration(unsigned int n) {
  if (n == 0 || n == 1) {
    return n;
  }

  unsigned int last = 0;
  unsigned int current = 1;
  for (int i = 0; i < n - 1; ++i) {
    unsigned int temp = current;
    current += last;
    last = temp;
  }

  return current;
}

/**
 * 汉诺塔游戏:
1、有三根相邻的柱子,标号为A,B,C。
2、A柱子上从下到上按金字塔状叠放着n个不同大小的圆盘。
3、现在把所有盘子一个一个移动到柱子B上,并且每次移动同一根柱子上都不能出现大盘子在小盘子上方。

对于汉诺塔问题,可以分下面几种情况:
当只移动一个圆盘时,直接将圆盘从 A 针移动到 C 针。
若移动的圆盘为 n(n>1),则分成几步走:
把 (n-1) 个圆盘从 A 针移动到 B 针(借助 C 针);
A 针上的最后一个圆盘移动到 C 针;
B 针上的 (n-1) 个圆盘移动到 C 针(借助 A 针);
每做一遍,移动的圆盘少一个,逐次递减,最后当 n 为 1 时,完成整个移动过程。
 *
 * @param n  The count of plates
 * @param src  The source of the plates to move from
 * @param dest The destination of th plates to move to
 * @param tmp The temporary place to use
 */
void Move(int n, char src, char dest, char tmp) {
  if (n == 0) return;
  else if (n == 1) printf("%c--->%c\n", src, dest);
  else {
    Move(n - 1, src, tmp, dest); //n-1的盘子移动到临时的柱子上 Move(n-1,A,B,C)
    Move(1, src, dest, tmp); //把剩下的最大的移动到目标位置上  Move(1,A,C,B)
    Move(n - 1, tmp, dest, src); //把移动到临时柱子上的n-1个盘子移动到目标盘子上 Move(n-1,B,C,A)
  }

}

int main() {
  //阶乘打印
  printf("0!: %d\n", Factorial(0));// 1
  printf("1!: %d\n", Factorial(1));// 1
  printf("2!: %d\n", Factorial(2));// 2
  printf("3!: %d\n", Factorial(3));// 6
  printf("5!: %d\n", Factorial(5));// 120
  printf("10!: %d\n", Factorial(10));//3628800

  printf("10! by Iteration: %d\n", FactorialByIteration(10));//3628800

  //斐波那契打印
  printf("Fabonacii(0): %d\n", Fabonacii(0));// 0
  printf("Fabonacii(1): %d\n", Fabonacii(1));// 1
  printf("Fabonacii(2): %d\n", Fabonacii(2));// 1
  printf("Fabonacii(3): %d\n", Fabonacii(3));// 2
  printf("Fabonacii(5): %d\n", Fabonacii(5));// 5

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

  //汉诺塔移动过程模拟打印
  Move(3, 'A', 'C', 'B');  //A--->C   A--->B   C--->B   A--->C    B--->A    B--->C  A--->C

  return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读