一是因为上学期学了数据结构,二是因为面对对象的程序设计学的不精,我便用数据结构做了一个信息管理系统作为C艹大作业。
没想到居然拿了优秀 ψ(`∇´)ψ (不管难否,反正是笔者五级分制中唯一的优秀)
先上数据结构图
贴代码
结构体:
typedef struct Grade { //某个学生的成绩信息
char TextName[12];
int Weight;//权重
int Chinese;
int Math;
int English;
int Science;
Grade* next;
}Grade, *GradePtr;
typedef struct Information { //单个学生的信息
char Name[12];
int ID;
char sex[5];
char birthday[12];
char phone[15];
GradePtr G; //指向成绩信息
Information* next;
}Information, *InformationPtr;
基本操作:
//链表基本操作函数
//创建链表
int CreateList(InformationPtr &I) {
I = (InformationPtr)malloc(sizeof(Information));
if (!I) return ERROR;
I->next = NULL; //I的next域用于存储末尾指针
return OK;
}
//数据入链表(一个学生的数据)
int EnList(InformationPtr &I, char a[IAmount + GAmount * TextLimit][20]) {
//IAmount、GAmount、TextLimit为全局变量。前两者又文件流读取确定学生个数和考试个数,后者为自定义能储存的考试数目的上限,数组a为储存从文件中读取数据的数组(一次读一个学生的个人信息和成绩信息)。
InformationPtr q;
q = (InformationPtr)malloc(sizeof(Information));
//数据输入(对Information结构体)
strcpy_s(q->Name, a[0]); /*名字*/ //使用串复制
q->ID = atoi(a[1]); /*学号*/ //将字符串数组型式存储的数据转化为int型
strcpy_s(q->sex, a[2]); /*性别*/
strcpy_s(q->birthday, a[3]);/*生日*/
strcpy_s(q->phone, a[4]); /*电话*/
//指针操作(对Information结构体)
InformationPtr Irear = I;
while (Irear->next) {
Irear = Irear->next;
}
Irear->next = q; q->next = NULL;
GradePtr Ghead;
Ghead = (GradePtr)malloc(sizeof(Grade));
q->G = Ghead;
Ghead->next = NULL;
for (int i = 0; i<TextAmount; i++) {
GradePtr p;
p = (GradePtr)malloc(sizeof(Grade));
//数据输入(对Grade结构体)
strcpy_s(p->TextName, a[IAmount + i * GAmount]);/*考试名*/
p->Weight = atoi(a[IAmount + i * GAmount + 1]);/*权重*/
p->Chinese = atoi(a[IAmount + i * GAmount + 2]);/*语文*/
p->Math = atoi(a[IAmount + i * GAmount + 3]);/*数学*/
p->English = atoi(a[IAmount + i * GAmount + 4]);/*英语*/
p->Science = atoi(a[IAmount + i * GAmount + 5]);/*理综*/
//指针操作(对Grade结构体)
GradePtr Grear = Ghead;
while (Grear->next) {
Grear = Grear->next;
}
Grear->next = p; p->next = NULL;
}
ListLength++;//结点个数增加(全局变量)
return OK;
}
//清空链表(注意该链表实际上含有两个链表(Information在外,Grade在内),要先把内部链表free(释放))
int ClearList(InformationPtr &I) {
ListLength = 0;
InformationPtr q1 = 0, q2 = 0;
GradePtr p1 = 0, p2 = 0;
q1 = I->next;
while (q1) {
p1 = q1->G->next;
while (p1) {
p2 = p1->next;
free(p1);
p1 = p2;
}
q2 = q1->next;
free(q1);
q1 = q2;
}
I->next = NULL;
return OK;
}
//删除以I为头节点的第n个结点(n=0指头结点(不可删除))(n需大于等于1)
int I_DeleteList(InformationPtr &I, int n) {
InformationPtr q1, q2;
q1 = I; q2 = q1->next;
int i = 1;
while (q2) {
if (i == n) {
q1->next = q2->next;
//释放malloc分配的G空间
GradePtr p1, p2;
p1 = q2->G->next;
while (p1) {
p2 = p1->next;
free(p1);
p1 = p2;
}
free(q2);
ListLength--;//结点数-1(全局变量)
return OK;
}
i++;
q1 = q2;
q2 = q2->next;
}
return ERROR;
}
//删除所有Information结点的第n个Grade结点
int G_DeleteList(InformationPtr &I, int n) {
InformationPtr q = I->next;
GradePtr p1, p2; int t;
for (int i = 0; i<ListLength; i++) {
p1 = q->G; p2 = p1->next;
t = 1;
while (p2) {
if (t == n) {
p1->next = p2->next;
free(p2);
p2 = NULL;
break;
//用break跳出while,以免p2=p2->next中 p2free后p2->next无意义
}
p1 = p2; p2 = p2->next;
t++;
}
q = q->next;
}
TextAmount--;//考试数减一
return OK;
}
//生成新的Information空间(学生),返回空间的地址
InformationPtr I_Increase(InformationPtr I) {
InformationPtr q;
q = (InformationPtr)malloc(sizeof(Information));
cout << "请输入新建学生信息:\n";
cout << "姓名:"; cin >> q->Name;
cout << "学号:"; cin >> q->ID;
cout << "性别:"; cin >> q->sex;
cout << "生日:"; cin >> q->birthday;
cout << "电话:"; cin >> q->phone;
q->next = NULL;
q->G = NULL;
GradePtr Ghead;
Ghead = (GradePtr)malloc(sizeof(Grade));
Ghead->next = NULL;
q->G = Ghead;
if (ListLength == 0)return q;
GradePtr k = I->next->G;
for (int i = 0; i<TextAmount; i++) {
GradePtr p;
p = (GradePtr)malloc(sizeof(Grade));
k = k->next;
strcpy_s(p->TextName, k->TextName);
p->Weight = k->Weight;
cout << "请录入" << q->Name << "同学的" << p->TextName << "成绩:\n";
cout << "语文:"; cin >> p->Chinese;
cout << "数学:"; cin >> p->Math;
cout << "英语:"; cin >> p->English;
cout << "理综:"; cin >> p->Science;
GradePtr Grear = Ghead;
while (Grear->next) {
Grear = Grear->next;
}
Grear->next = p; p->next = NULL;
}
cout << "学生信息添加完成"; system("pause");
return q;
}
数据结构的难点主要在于指针操作,修改代码中的数据操作就可以实现自己想实现的功能了o(* ̄▽ ̄*)o