用线性表实现班级成绩管理的程序,我用C++实现的,但是用面向过程的方式写的,有很多缺点,望大家指正并相互学习!
运行结果截图:
程序源码:
main.cpp
#include
#include
#include "LinkList.h"
using namespace std;
#define ARRAYSIZE 4
int main()
{
LinkList list[ARRAYSIZE];//存放每个班级的链表头指针
int number,totalClass;
Student student;
///忘记初始化造成了问题,所以做最好在声明时初始化
LinkList totalGradeInfo;//整个年级的同学的链表头指针;
cout << "Please enter the number of the class: ";
cin >> totalClass;
if(totalClass > 4)//限定输入的班级数目
{
cout << "\nSorry! We can maximum support 4 class's managment!" << endl;
totalClass = 4;
}
for(int i=0; i> number;
if(number > 30)//限定每个班级的人数
{
cout << "\nSorry! We can maximum support 30 student in one class!" << endl;;
}
//start add node
for(int j=0; j> student.classID;
cout << "Please enter your studentID: ";
cin >> student.studentID;
cout << "Please enter your name: ";
cin >> student.name;
cout << "Please enter your score: ";
cin >> student.score;
if(student.score < 0 || student.score > 100 )//处理输入成绩不合法的情况
{
cout << "\nThe score is unavailable!";
student.score = 0;
}
AddNode(&list[i], student);//添加节点
}
SortList(&list[i]);//每个班级添加完成之后排序
ShowInformation(&list[i], "Class");//输出班级成绩信息
}
//合并整个年级的成绩情况
InitList(&totalGradeInfo);
CombineLists(list, totalClass, &totalGradeInfo);
SortList(&totalGradeInfo);
ShowInformation(&totalGradeInfo, "Total Grade");
cout << "\n-------------------------------------------------------" << endl;
for(int i=0; inext != NULL)//销毁合并之后的链表
{///为什么上一条语句用 ->就会出错?
DestoryList(&totalGradeInfo);
}
return 0;
}
LinkList.h
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
#include
#include
#include
using namespace std;
typedef struct Student
{
int classID;
long studentID;
string name;
int score;
}Student;
typedef struct LinkNode
{
Student data;
LinkNode *next;
}LinkNode;
typedef struct LinkList
{
LinkNode *pHead;
int length;
}LinkList;
bool InitList(LinkList *list);
bool AddNode(LinkList *list, Student e);
bool SortList(LinkList *list);
bool CombineLists(LinkList list[], int arrayLen, LinkList *destList);
bool ShowInformation(LinkList *list, string info);
bool ClearList(LinkList *list);
bool DestoryList(LinkList *list);
#endif // LINKLIST_H_INCLUDED
LinkList.cpp
#include "LinkList.h"
bool InitList(LinkList *list)//初始化链表
{
if(!(list->pHead=new LinkNode))
{
cout << "No free memory!";
return false;
}
list->pHead->next = NULL;
// list->pHead.data->classID = "";
// list->pHead->data->name = "";
// list->pHead->data->score = 0;
// list->pHead->data->studentID;
list->length = 0;
return true;
}
bool AddNode(LinkList *list, Student e)//头插法,添加节点
{
if(list->pHead==NULL)//如果给定的链表不存在,返回
{
cout << "\nThe list don't exist!" << endl;
return false;
}
LinkNode *node = new LinkNode;
//LinkNode *node = (LinkNode*)malloc(sizeof(LinkNode));
if(node == NULL)
{
cout << "\nDon't have more memory to add element!" << endl;
return false;
}
node->data.classID = e.classID;
node->data.studentID = e.studentID;
node->data.name = e.name;
node->data.score = e.score;
list->length++;
node->next = list->pHead->next;
list->pHead->next = node;
return true;
}
bool SortList(LinkList *list)//排序链表,需要两个循环多次遍历
{
if(list->pHead->next==NULL)
{
cerr << "\nThe list can't sort in result of the list don't exits or empty!" << endl;
return false;
}
LinkNode *src,*srcNext;
src = list->pHead->next;
while(src->next != NULL)
{
///曾经在此处出错,程序语句的顺序十分重要
srcNext = src->next;//要排序的指针的下一个,以方便比较
while(srcNext != NULL)
{
if(src->data.score < srcNext->data.score)//按照降序方式排列
{
Student temp;//交换两个数据
temp.classID = srcNext->data.classID;
temp.studentID = srcNext->data.studentID;
temp.name = srcNext->data.name;
temp.score = srcNext->data.score;
srcNext->data.classID = src->data.classID;
srcNext->data.studentID = src->data.studentID;
srcNext->data.name = src->data.name;
srcNext->data.score = src->data.score;
src->data.classID = temp.classID;
src->data.studentID = temp.studentID;
src->data.name = temp.name;
src->data.score = temp.score;
}
srcNext = srcNext->next;
}
src = src->next;
}
return true;
}
bool CombineLists(LinkList list[], int arrayLen, LinkList *destList)
{
if(arrayLen == 0)//如果传入的链表数组长度为空,错误,返回
{
cerr << "\nERROR in CombineLists()! Those lists you want to combine don't exist!" << endl;
return false;
}
int i = 0;
LinkNode *temp;//声明一个临时指针变量,遍历访问每条链表
destList->pHead->next = list[0].pHead->next;
for(i=1; inext != NULL)//如果链表头的指向不为空,表明这个链表不为空
{
temp = list[i].pHead->next;
while(temp->next)//找到链表尾元素,将整个链表加入到已经存在的链表中
{
temp = temp->next;
}
temp->next = destList->pHead->next;//将当前链表插入到目标链表中
destList->pHead->next = list[i].pHead->next;
}
}
return true;
}
bool ShowInformation(LinkList *list, string info)//显示链表元素
{
if(list->pHead==NULL || list->pHead->next == NULL)
{
cout << "\nThe link list is empty or don't exist!" << endl;
return false;
}
LinkNode *temp = list->pHead->next;
cout << "----------------------------------------------";
if(info == "Total Grade")//如果是总成绩
{
cout << "-----";
}
cout << "\nThis "<< info << " Information:" << endl;
while(temp != NULL)
{
cout << temp->data.classID << "\t" << temp->data.studentID << "\t" << temp->data.name
<< "\t" << temp->data.score << endl;
temp = temp->next;
}
cout << endl;
return true;
}
bool ClearList(LinkList *link)//清空链表
{
if(link->pHead == NULL)
{
cerr << "\nERROR! The list don't exist!" << endl;
return false;
}
if(link->pHead->next == NULL)
{
cout << "The list is empty already!" << endl;
return true;
}
LinkNode *temp = link->pHead->next;
while(temp)
{
link->pHead->next = temp->next;
delete temp;
temp = link->pHead->next;
}
if(link->pHead->next == NULL)
{
cout << "The list has been cleard!" << endl;
}
return true;
}
bool DestoryList(LinkList *list)
{
if(!ClearList(list))
{
return false;
}
delete list->pHead;
cout << "The list has been destoryed!" << endl;
return true;
}