当前位置:网站首页>11(3). Through the structure and pointer to achieve a simple student management system (storage, sorting, output)
11(3). Through the structure and pointer to achieve a simple student management system (storage, sorting, output)
2022-07-19 18:58:00 【A programmer who loves playing badminton】
I insist on updating C Knowledge of language and data structure , Can collect + Follow and keep abreast
Catalog
Here we implement the most basic student management system , There is no chain storage , Instead, it uses a sequential storage structure , Structure used , The pointer , And dynamic memory allocation ( Dynamic memory allocation There is an update in my previous article ).
Code up
#include <iostream>
#include <malloc.h>
#include <stdio.h>
using namespace std;
// void sort_student(struct *q, int len);
// Defining structure
struct Student
{
char name[20]; // full name
float score; // fraction
};
// Arrange students' grades according to the descending order of scores , Selective sorting is adopted
void sort_student(struct Student *q, int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
if ((q + i)->score < (q + j)->score)
{
struct Student temp; // Exchange student information
temp = *(q + i);
*(q + i) = *(q + j);
*(q + j) = temp;
}
}
}
}
int main(int argc, char const *argv[])
{
// cout << sizeof(struct Student) << endl;
int len; // Define the number of students
cout << "please input the number of students:" << endl;
cin >> len;
struct Student *p = (struct Student *)malloc(sizeof(struct Student) * len);
// We need to dynamically define an array to store structural variables , therefore p The type of must be struct Student, The memory required is (sizeof Student)*len;
for (int i = 0; i < len; i++)
{
cout << "Please input N0." << i + 1 << "student information" << endl;
cout << "Please input the name of student:" << endl; // Enter student name
cin >> (p + i)->name;
// scanf("%s",p[i].name) name It's an array name , Itself is already the address of the first element of the array
cout << "Please input the score of student:" << endl;
cin >> (p + i)->score;
}
sort_student(p, len); // Arrange the students in descending order of scores
// Output student information
cout << "--------------------The Student Score Information--------------------" << endl;
for (int i = 0; i < len; i++)
{
cout << "name:" << (p + i)->name << " score:" << (p + i)->score << endl;
}
cout << "---------------------------------------------------------------------" << endl;
return 0;
}
Realize the idea
1. We define the structure struct Student;
2. Determine the number of students ,int len
3. According to the number of students , Dynamic memory allocation
struct Student *p = (struct Student *)malloc(sizeof(struct Student) * len);
4. Use for Loop in student information
5. Write sorting function
void sort_student(struct Student *q, int len)
6. Use for Loop out the sorted student information
Running results
We can see , In the student information output , Output student information according to grade ranking
边栏推荐
- Tags & tag selectors & Notes
- The data of the tables in the datagrip database is inexplicably gone, and so is the self-contained data
- SQL查询速度慢的原因分析及解决方案【案例分享】
- 国际NFT交易所排行榜前10名
- DRF--Router路由器对象自动生成路由,自定义action生成路由
- BeanFactory与FactoryBean的区别(入门学习)
- 极客大挑战2019(复习一下漏洞)
- [Niuke algorithm - binary search] it's up to you to give consideration to question brushing and interview
- Will Web3 reshape Internet business?
- sql join和correlated subenquiry的区别
猜你喜欢
以存储为支点 探寻如何撬动Web3.0时代
LabVIEW中使用队列,通知器,信号量或集合点时的潜在竞争情况
DRF--validators检验函数,单字段校验,多字段联合校验
Practice of data storage scheme in distributed system
11(3).通过结构体与指针实现简单的学生管理系统(存储,排序,输出)
DRF-- 对drf中的功能模块进行二次开发
认证与授权,JWT 认证
极客大挑战2019(复习一下漏洞)
What are the advantages of Tencent cloud lightweight application server?
SQL查询速度慢的原因分析及解决方案【案例分享】
随机推荐
identity server4 授权成功页面跳转时遇到错误:Exception: Correlation failed. Unknown location的解决方法
2022 melting welding and thermal cutting examination question bank and online simulation examination
Explore how to leverage the Web3.0 era with storage as the fulcrum
Quels sont les avantages de Tencent Cloud Lightweight Application Server?
国际NFT交易所排行榜前10名
Boolean expression satisfiability problem (SAT) and cook Levin theorem (Part 2)
Weston中shm window渲染显示过程分析
2022熔化焊接与热切割考试题库及在线模拟考试
Some common methods of wechat applet
DRF--api接口文档的生成
Cross compilation tool
Use of nano editor
Codeforces Round #807 (Div.2) A-C
AutoJs学习-实现手指动作录制
[Niuke algorithm - binary search] it's up to you to give consideration to question brushing and interview
元宇宙、NFT数字藏品是什么?
复杂SQL_01
DRF--地址访问权限设置,logging日志模块配置
presto+yanagishima环境安装
Practical application of block in development