当前位置:网站首页>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
边栏推荐
- Teach everyone how to implement an electronic signature
- 网狐游戏服务器房间配置约战定制功能实现
- A promise with bare hands
- fatal: refusing to merge unrelated histories
- 数组去重。。。。
- How exactly does instanceof judge the reference data type!
- The nearest common ancestor of 235 binary search tree
- Oracle提示无效数字
- 搜狐员工遭遇工资补助诈骗 黑产与灰产有何区别 又要如何溯源?
- 数学方法论的含义和研究意义
猜你喜欢

UEFI finding PCI devices

洛谷P1091合唱队形(最长上升子序列)

563. slope of binary tree

Do you use typescript or anyscript

Do you know what the quotation for it talent assignment service is? It is recommended that programmers also understand

JS judge whether the year is a leap year and the number of days in the month

C language war "minesweeping"

MongoDB安装

235-二叉搜索树的最近公共祖先

Biweekly investment and financial report: capital rush yuan universe game
随机推荐
Handwritten promise [01] - Implementation of promise class core logic
Do you use typescript or anyscript
Jenkins different styles of project construction
Vulnhub's breach1.0 range exercise
MongoDB安装
Oracle提示无效数字
JVM from getting started to giving up 2: garbage collection
UEFI查找PCI设备
Aircraft war from scratch (II) simple development
Use of qscriptengine class
MMEditing中超分模型训练与测试
572. subtree of another tree
538. convert binary search tree to cumulative tree
Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically
Sentinel annotation support - @sentinelresource usage details
SQL language - query statement
Zabbix 监控主机是否在线
Vulhub 8.1-backdoor vulnerability recurrence
Sharing of personal common software and browser plug-ins
June training (day 11) - matrix