九、函数和递归(2)

1.避免同一个函数头文件被多次引用

在函数声明中,例:Add.h头文件

#ifndef  _ADD_H_        //如果没有定义,再往下

#define  _ADD_H_       //定义这个函数

//函数声明

int Add (int x, int y);

#endif

2.函数递归

把大事化小,与原问题相似的问题(函数自己调用自己)

 递归常见错误:stack  overflow(栈溢出)

                栈区:局部变量、函数形参

 内存        堆区: 动态开辟的内存

                静态区:全局变量、static修饰的变量

(1)最简单的递归-栈溢出

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//最简单递归-栈溢出
int main()
{	
	printf("hehe\n");
	main();
	return 0;
}
	

(2)接收一个无符号整型数,并按顺序打印它的每一位。

从后往前依次输出,因为后面的调用执行完,前面函数才能继续往下。

例:输入1234

逻辑抽象,逐次拆解:

print(1234)

print(123)4

print(12)34

print(1)234

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//接收无符号数,按顺序打印每一位
void print (unsigned int n)
{
	if(n>9)
	{
		print(n/10);
	}
	printf("%d ",n%10);
}
int main()
{	
	unsigned int num = 0;
	printf("请输入:\n");
	scanf("%d",&num);
	print(num);
	return 0;
}
	

(3)函数递归的两个必要条件

1.存在限制条件,当满足这个限制条件时,递归不再继续;

2.每次递归调用后越来越接近这个限制条件。

(4)循环:求字符串长度

数组名是数组首元素地址,数组元素地址连续,对数组名解引用就是数组元素内容,对数组名++,可访问数组各个元素地址,再解引用可访问数组各个元素。

对字符串数组来说,char占一个字节,所以arr++就可以访问字符数组各个元素,一个内存单元占一个字节。

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//循环:求字符串长度
int my_strlen (char* str)
{
	int count = 0;
	while(*str!='\0')
	{
		count++;
		str++;
	}
	return count;
}
int main()
{	
	char arr [20] = {0};
	int len = 0;
	printf("请输入字符串:\n");
	scanf("%s",arr);
	len = my_strlen(arr);
	printf("字符串长度为%d\n",len);
	return 0;
}

(5)递归:求字符串长度(不创建临时变量)

把大事化小(相似问题求解)

my_strlen("hello")

1+my_strlen("ello")

1+1+my_strlen("llo")

......

1+1+1+1+1+0//到 \0

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//递归:求字符串长度(不创建临时变量)
int my_strlen (char*str)
{
	if(*str!='\0')
	{
		return 1+my_strlen(str+1);
	}
	else
	{
		return 0;
	}
} 
int main()
{	
	char arr [20] = {0};
	int len = 0;
	printf("请输入字符串:\n");
	scanf("%s",arr);
	len = my_strlen(arr);
	printf("字符串长度为:%d\n",len);
	return 0;
}

3.思维导图

相关推荐

  1. 函数的介绍实现

    2024-07-21 13:54:01       52 阅读
  2. Python:函数

    2024-07-21 13:54:01       41 阅读
  3. 函数

    2024-07-21 13:54:01       19 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 13:54:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 13:54:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 13:54:01       45 阅读
  4. Python语言-面向对象

    2024-07-21 13:54:01       55 阅读

热门阅读

  1. Python之后端Django(五)

    2024-07-21 13:54:01       18 阅读
  2. Python基础学习攻略:从入门到进阶的完整路径

    2024-07-21 13:54:01       14 阅读
  3. 前端算法入门【栈】

    2024-07-21 13:54:01       16 阅读
  4. watch监听vue2与vue3的写法

    2024-07-21 13:54:01       21 阅读
  5. 类 WAS_CLIPSeg_Model_Loade

    2024-07-21 13:54:01       20 阅读
  6. powerbulder中的destroy 和 setnull

    2024-07-21 13:54:01       13 阅读
  7. pyquery 的使用

    2024-07-21 13:54:01       19 阅读
  8. 本周你可能错过的 AI 新闻

    2024-07-21 13:54:01       20 阅读
  9. Python如何优雅地在Terminal打印下标

    2024-07-21 13:54:01       21 阅读