当前位置:网站首页>Practice: how to reasonably design functions to solve practical problems in software development (II) -- improving reusability

Practice: how to reasonably design functions to solve practical problems in software development (II) -- improving reusability

2022-06-11 06:46:00 Dare you look at the avatar for three seconds?

The last blog used two simple examples to complete the principles and techniques of basic function construction
**

This time we use the skills to improve the reusability of the program and dynamic memory allocation to construct the student information management system

requirement :1 Dynamically construct an array , Student information stored
2 Then output by score

**

#include<iostream>
#include<stdio.h>
#include<malloc.h>
using namespace std;
struct  student
{
    
	int age;
	char sex;
	int score;
};
int main(void)
{
    
	struct student* parr;
	int len;
	int i,j;
	struct student t;
	printf(" Please enter the number of students \n");
	printf("len = ");
	cin >> len;
	// Dynamically construct one-dimensional array 
	parr=(struct  student *)malloc(len*sizeof(struct student));
	// Input 
	for (i = 0; i < len; ++i)
	{
    
		printf(" Please enter the first  %d  Student information \n", i + 1);
		printf("age=");
		cin >> parr[i].age;
		//scanf("%d", &parr[i].age);
		printf("\n");
		printf("sex=");
		//cin >> parr->sex;
		cin >> parr[i].sex;
		//scanf("%c", &parr[i].sex);
		printf("\n");
		printf("score=");
		//cin >> parr->score;
		cin >> parr[i].score;
		//scanf("%d", parr[i].name);// name It's an array name , It is already the address of the first element of the array 
													//  therefore  parr[i].name  Cannot be changed to  &parr[i].name
	}
	// Sort 
	for (i = 0; i < len - 1; ++i)
	{
    
		for (j = 0; j < len - 1 - i; ++j)
		{
    
			if (parr[j].score < parr[j + 1].score)
			{
    
				t= parr[j];
				parr[j] = parr[j + 1];
				parr[j + 1] = t;
			}
		}
	}


	// Output 
	for (i = 0; i < len; ++i)
	{
    
		printf(" Please enter the first  %d  Student information \n", i + 1);
		//printf("age= %d",parr->age);
		printf("age= %d ,", parr[i].age);

		//printf("sex= %c ,", parr->sex);
		printf("sex= %c", parr[i].sex);

		//printf("score= %d\n", parr->score);
		printf("score= %d\n", parr[i].score);

		printf("\n");

	}



	//printf("%d",len);

	return 0;
}

Conclusion : This is the first edition , The whole process uses a main function to complete the whole function , Jumbled steps for two weeks , I guess I can't understand it myself , Poor legibility , Not to mention function reusability

The second edition , We try to start adjusting the structure of the program

#include<iostream>
#include<stdio.h>
#include<malloc.h>
using namespace std;
struct  student
{
    
	int age;
	char sex;
	int score;
};
void shuru(int len, struct student* parr)
{
    
	int i;
	for (i = 0; i < len; ++i)
	{
    
		printf(" Please enter the first  %d  Student information \n", i + 1);
		printf("age=");
		cin >> parr[i].age;
		//scanf("%d", &parr[i].age);
		printf("\n");
		printf("sex=");
		//cin >> parr->sex;
		cin >> parr[i].sex;
		//scanf("%c", &parr[i].sex);
		printf("\n");
		printf("score=");
		//cin >> parr->score;
		cin >> parr[i].score;
		//scanf("%d", parr[i].name);// name It's an array name , It is already the address of the first element of the array 
												  //  therefore  parr[i].name  Cannot be changed to  &parr[i].name
	}
}
// Output 
void shuchu(int len, struct student* parr)
{
    
	int i;
	for (i = 0; i < len; ++i)
	{
    
		printf(" Please enter the first  %d  Student information \n", i + 1);
		//printf("age= %d",parr->age);
		printf("age= %d ,", parr[i].age);

		//printf("sex= %c ,", parr->sex);
		printf("sex= %c\t", parr[i].sex);

		//printf("score= %d\n", parr->score);
		printf("score= %d\n", parr[i].score);

		printf("\n");
	}
}
// Sort 
void	paixu(int len, struct student* parr)
{
    
	int i, j;
	struct student t;
	for (i = 0; i < len - 1; ++i)
	{
    
		for (j = 0; j < len - 1 - i; ++j)
		{
    
			if (parr[j].score < parr[j + 1].score)
			{
    
				t = parr[j];
				parr[j] = parr[j + 1];
				parr[j + 1] = t;
			}
		}
	}
}

int main(void)
{
    
	struct student * parr;
	int len;
	int i;
	printf(" Please enter the number of students \n");
	printf("len = ");
	cin >> len;
	
	// Dynamically construct one-dimensional array 
	
	parr = (struct  student*)malloc(sizeof(struct student) * len);
	// Input 
	shuru(len, parr);
	// Sort 
	paixu(len, parr);
	// Output 
	shuchu(len,parr);

	//printf("%d",len);

	return 0;
}

summary : We can see , We improve the reusability of the whole program , By input , Sort , Output , The three called functions greatly reduce the length of the main function , Easier to read , In fact, we don't need to understand so many steps in detail, do we ? We can read on after we understand his answer function ,

however , I don't think that's enough !

If you look carefully, you will find that our function of dynamically constructing one-dimensional arrays is still in the main function , That means that if we want to change the student information in the future , We also need to find the code for dynamically building student information in the main function , Then make changes ; Or I wish my main function could be simpler , I want to take out the code of dynamic memory allocation , I just want to see that in the main function, I see a function called “ Dynamically construct memory ” Instead of looking in the main function , Because the team is really easy to see with this small student information management program , But if we face the author's huge and multiple programs, we may have no way to start

therefore , Here comes the third attempt :

#include<iostream>
#include<stdio.h>
#include<malloc.h>
using namespace std;
struct  student
{
    
	int age;
	char sex;
	int score;
};
void goujian(char name)
{
    



}
void shuru(int len, struct student* parr)
{
    
	int i;
	for (i = 0; i < len; ++i)
	{
    
		printf(" Please enter the first  %d  Student information \n", i + 1);
		printf("age=");
		cin >> parr[i].age;
		//scanf("%d", &parr[i].age);
		printf("\n");
		printf("sex=");
		//cin >> parr->sex;
		cin >> parr[i].sex;
		//scanf("%c", &parr[i].sex);
		printf("\n");
		printf("score=");
		//cin >> parr->score;
		cin >> parr[i].score;
		//scanf("%d", parr[i].name);// name It's an array name , It is already the address of the first element of the array 
												  //  therefore  parr[i].name  Cannot be changed to  &parr[i].name
	}
}
void shuchu(int len, struct student* parr)
{
    
	int i;
	for (i = 0; i < len; ++i)
	{
    
		printf(" Please enter the first  %d  Student information \n", i + 1);
		//printf("age= %d",parr->age);
		printf("age= %d ,", parr[i].age);

		printf("sex= %c ,", parr->sex);
		//printf("sex= %c", parr[i].sex);

		printf("score= %d\n", parr->score);
		//printf("score= %d\n", parr[i].score);

		printf("\n");
	}
}
void vary(int ll, struct student* parr)
{
    
	parr = (struct  student*)malloc(sizeof(struct student)* ll);


}
int main(void)
{
    
	struct student* parr;
	int len;
	int i;
	printf(" Please enter the number of students \n");
	printf("len = ");
	cin >> len;
	// Dynamically construct one-dimensional array 
	vary(len, parr);
	// Input 
	shuru(len, parr);
	// Output 
	shuchu(len, parr);

	//printf("%d",len);

	return 0;
}

unfortunately , Initializing dynamic memory must be in the main function , Otherwise, it will cause memory crabs .

Sneaking away, my brothers and sisters are cute , I hope it helps you .

See you next time

原网站

版权声明
本文为[Dare you look at the avatar for three seconds?]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020525471768.html