C language learning (2) - function (1)

1. Write code to input a string of numbers and output the character form of this string of numbers: 123 – '1' '2' '3'

#include "main.h"

// 编写一个函数,将得到的一个整数,转换为字符串
char charSwap(char *ntr)
{
    
    
	char temp = 0;
	char i = 0, j;
	j = strlen(ntr) - 1;
	while (i < j)
	{
    
    
		temp = *(ntr + i);
		*(ntr + i) = *(ntr + j);
		*(ntr + j) = temp;
		i++;
		j--;
	}
}
int swap(int *pnum, char *pnts)
{
    
    
	int len = 0;
	int num = 0;
	int yushu = 0;
	char *pnt2 = pnts;
	num = *pnum;
	// 得到数字长度
	while (num != 0)
	{
    
    
		num = num / 10;
		len ++;
	}
	num = *pnum;
	// 进行转换,不过因为先得到余数,所以字符是颠倒的
	while (len != 0)
	{
    
    
		yushu = num % 10;
		num = num / 10;
		*pnts = yushu + 48;
		len --;
		pnts ++;
	}
	// 在这里,将字符串倒置一下,因为传入的是地址,所以不需要返回值
	charSwap(pnt2);
	return 0;
}

int main()
{
    
    
	int num = 0;
	char nts[256];
	char str[] = "hello";
	printf("Please enter a num like 256:\n");
	scanf("%d", &num);
	
	swap(&num, nts);
	printf("%s\n", nts);	

	return 0;
}

2. Use recursive calls of functions to solve the factorial problem

#include "main.h"
int func(int n)
{
    
    
	if (n < 0)
		return -1;
	if (n == 0 || n == 1)
		return 1;
	// 只有当输入数不为0和1时,阶乘才有意义
	// n! = n * (n-1)!
	return n * func(n-1);
}

int main()
{
    
    
	int n;
	int res = 0;
	// 输入想要阶乘的数
	scanf("%d", &n);
	// 得到阶乘的结果
	res = func(n);
	printf("%d! = %d\n", n, res);
	exit(0);
}

3. Use function recursion to solve Fibonacci problems

#include "main.h"

// fib数列
#if 0
n = (n - 1) + (n - 2)
n-1 = (n - 1 - 1) + (n - 1 -2)
n-2 = (n - 2 - 1) + (n - 2 -2)
#endif

int func(int num)
{
    
    
	if (num < 1)
		return 0;
	if (num == 1 || num == 2)
		return 1;
	return func(num-1) + func(num-2);
}

int main()
{
    
    
	int num = 0;
	int res = 0;
	printf("请输入想要输出的个数:\n");
	scanf("%d", &num);
	res = func(num);
	
	printf("%d时是%d\n", num, res);
	return 0;
}

4. The relationship between functions and one-dimensional arrays

Example: Define a one-dimensional array, invert the one-dimensional array by passing parameters, and output the result

#include "main.h"

void func(int *arr, int len)
{
    
    
   int i = 0;
   int j = len-1;
   int tmp;
   while (i < j)
   {
    
    
   	tmp = *(arr + i);
   	*(arr + i) = *(arr + j);
   	*(arr + j) =  tmp;
   	i++;
   	j--;
   }
}

int main()
{
    
    
   int i = 0;
   int arr[5] = {
    
    1, 2, 3, 4 , 5};
   	
   for (i = 0; i < sizeof(arr)/sizeof(*arr); i ++)
   {
    
    
   	printf("%d ", arr[i]);
   }
   printf("\n");

   func(arr, sizeof(arr)/sizeof(*arr));
   for (i = 0; i < sizeof(arr)/sizeof(*arr); i ++)
   {
    
    
   	printf("%d ", arr[i]);
   }
   printf("\n");
   return 0;
}

5. The relationship between functions and secondary pointers

Suppose there is a two-dimensional array with two rows and three columns. The rows represent two students and the columns represent the three scores of each student. Then how to calculate the average score of all students and how to calculate the scores of all subjects for each student?
code show as below

#include "main.h"

#define M 2
#define N 3

float averange_score(int *p, int len)
{
    
    
	float sum = 0;
	int i=  0;
	for(i = 0; i < len; i++)
	{
    
    
		sum += p[i];
	}
	return sum / len;
}
// 或者也可以写为
// void find_score(int p[][N], int num)
void find_score(int (*p)[N], int num)
{
    
    
	int i = 0;
	for (i = 0; i < N; i++)
		printf("%d ", *(*(p+num) + i));
	printf("\n");
}

int main()
{
    
    
	int i, j;
	int a[M][N] = {
    
    1,2, 3, 4, 5, 6};
	float ave;
	int num = 0;			//	假设每一行是一个同学,每一列是成绩,共三个成绩,两个同学
	ave = averange_score(*a, M*N);
	// *a就是行指针
	printf("%f\n", ave);
	
	find_score(a, num);
	// a是二级指针
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44463519/article/details/126540501