数据结构 课程设计
(1)学生成绩管理问题(建议数据结构:单链表)
学生成绩管理是学校教务管理的重要组成部分,其处理信息量很大,本设计是对学生的成绩管理做一个简单的模拟,用菜单选择操作方式完成下列主要功能:
(1)登记学生成绩;
(2)查询学生成绩; (3)插入学生成绩; (4)删除学生成绩;
要求对每个功能分别用函数实现。
(2)学生成绩管理系统需求与功能分析
学生成绩的录入、统计、查询、修改、删除、输出。
(3)学生成绩管理系统的数据结构表
序号 成员名(字段名) 数据类型 长度 字段含义
1 cla_0 char 20 班级
2 num int 学号
3 name char 8 姓名
4 elec flaot 电子技术
5 c_prog float C 程序设计
6 media flaot 多媒体技术
7 eng float 大学英语
8 math float 高等数学
9 sport float 大学体育
10 polity float 马克思主义政治经济学
11 ave float平均成绩
12 order int 名次
画出功能结构图。
(4)学生成绩管理系统测试数据表
cla_0 num name elec c_prog media eng math sport polity ave order
网络30331 3033101 办公费 80 70 60 70 70 60 80 表中其余数据自己编造。
(5)使用链表编写程序(手写源程序代码,并给出注解)
0)定义链表结点
1)主函数main():定义链表头指针,调用录入、统计等函数对成绩表进行处理;
2)建立链表函数Create():输入班级到政治课成绩信息;
3)统计函数Statistic():计算平均成绩;
4)查询函数Lookup():查询指定学号学生成绩记录;
5)修改函数Modify():修改指定学号学生成绩记录;
6)删除函数删除():删除指定学号学生记录;
7)输出函数Output():输出班级所有学生成绩记录;
8)插入函数Insert():按平均分顺序插入新结点。
9) 排序函数Sort():按平均分对学生成绩记录项进行降序排序;
程序如下:
#include #include #include #include #include
using namespace std;
struct Node//定义链表结点 {
char* cla_0;//班级 int number; char* name;//姓名 float elec;//电子技术成绩 float c_prog;//C程序设计成绩 float media;//多媒体技术成绩 float eng;//大学英语成绩 float math;//高等数学成绩
float sport;//大学体育成绩
float polity;//马克思主义政治经济学成绩 float ave;//平均成绩 int order;//名次 Node* link; Node(){link=NULL;} Node(int _number,char* _cla_0,char* _name,float _elec, float _c_prog,float _media,float _eng,float _math, float _sport,float _polity,float _ave,int _order,Node* next) { number=_number; cla_0=new char[21]; strcpy(cla_0,_cla_0); name=new char[9]; strcpy(name,_name); elec=_elec; c_prog=_c_prog; media=_media; eng=_eng; math=_math; sport=_sport; polity=_polity; ave=_ave; order=_order; link=next; } ~Node() { 删除 []cla_0; 删除 []name; }
}; cla StudentScore { private: Node* first;//链表的头指针
int choice;//选择数据的读入方式 int fileNum;//当前文件数减一 int fileLoc;//定位当前文件 string* fileName; int operChoice; int RecordLength; public: StudentScore(); void Save(); void BuildList();//手工建立成绩链表
void ReadInfo(int k);//从内存中读入学生信息 void ClearList(); void Statistic(); void Sort(); void Add(); void 删除(); void PrintList(); void Menu(); }; StudentScore::StudentScore() { RecordLength=0; operChoice=0; first=NULL; choice=0; fileLoc=0; fileNum=0; fileName=new string[10];//最多可以存10个文件 } int GetOrder(Node* first,float ave); void StudentScore::BuildList() { int _number;//学号
char* _cla_0=new char[21];//班级 char* _name=new char[9];//姓名 float _elec;//电子技术成绩 float _c_prog;//C程序设计成绩 float _media;//多媒体技术成绩 float _eng;//大学英语成绩 float _math;//高等数学成绩 float _sport;//大学体育成绩
float _polity;//马克思主义政治经济学成绩 float _ave;//平均成绩 int _order;//名次 char c; Node *p,*r=NULL; first=NULL; cout>_cla_0;//班级 cin>>_number; cin>>_name;//姓名 cin>>_elec; cin>>_c_prog; cin>>_media; cin>>_eng; cin>>_math; cin>>_sport; cin>>_polity; _ave=(_elec+_c_prog+_media+_eng+_math+_sport+_polity)/7;//求得平均成绩 _order=GetOrder(first,_ave);
p=new Node(_number,_cla_0,_name,_elec, _c_prog,_media,_eng,_math,_sport,_polity, _ave,_order,NULL);//建立一个新的结点储存信息
if(first!=NULL) r->link=p; else first=p; r=p; RecordLength++; coutlink) { if(temp->ave>ave) order++; if(temp->aveorder)++; } return order; }
void StudentScore::Statistic() { Node* temp=first; float a_elec=0.0;//电子技术成绩 float a_c_prog=0.0;//C程序设计成绩 float a_media=0.0;//多媒体技术成绩 float a_eng=0.0;//大学英语成绩 float a_math=0.0;//高等数学成绩 float a_sport=0.0;//大学体育成绩
float a_polity=0.0;//马克思主义政治经济学成绩 int i=0; while(temp) { a_elec+=temp->elec; a_c_prog+=temp->c_prog; a_media+=temp->media; a_eng+=temp->eng; a_math+=temp->math; a_sport+=temp->sport; a_polity+=temp->polity; i++; temp=temp->link; } a_elec=a_elec/i; a_c_prog=a_c_prog/i; a_media=a_media/i; a_eng=a_eng/i; a_math=a_math/i; a_sport=a_sport/i; a_polity=a_polity/i; cout>studNum; float average; for(;temp;temp=temp->link) { cout
if(temp->number==studNum) { average=temp->ave; if(temp==first)//说明是第一次
{ first=first->link; 删除 temp; break;//如果不跳出的话 temp=temp->link会出错 } else { p->link=temp->link; 删除 temp; break; } } p=temp; RecordLength--;
}
//下面修改学生排名 temp=first; for(;temp;temp=temp->link) if(temp->aveorder--; } void StudentScore::Add() { int _number;//学号
char* _cla_0=new char[21];//班级 char* _name=new char[9];//姓名 float _elec;//电子技术成绩 float _c_prog;//C程序设计成绩 float _media;//多媒体技术成绩 float _eng;//大学英语成绩 float _math;//高等数学成绩
float _sport;//大学体育成绩
float _polity;//马克思主义政治经济学成绩 float _ave;//平均成绩 int _order;//名次 char c; Node *p,*r=NULL; r=first; while(r->link) r=r->link; // first=NULL; cout>_cla_0;//班级 cin>>_number; cin>>_name;//姓名 cin>>_elec; cin>>_c_prog; cin>>_media; cin>>_eng; cin>>_math; cin>>_sport; cin>>_polity; _ave=(_elec+_c_prog+_media+_eng+_math+_sport+_polity)/7;//求得平均成绩 //写一个返回排名的程序
_order=GetOrder(first,_ave);
p=new Node(_number,_cla_0,_name,_elec, _c_prog,_media,_eng,_math,_sport,_polity, _ave,_order,NULL);//建立一个新的结点储存信息
if(first!=NULL) r->link=p; else first=p; r=p; RecordLength++; cout
} void StudentScore::Sort()//简单bubble排序从高分到低分排序 { int i=0; Node* temp=first; Node* before; // Node* p=first; for(;temp->link;) { if(temp->avelink->ave) {
if(temp==first)//说明是第一个结点 { first=first->link; // p=temp; // temp=temp->link; temp->link=temp->link->link; first->link=temp; before=first; } else//不是第一个结点 { before->link=temp->link; temp->link=temp->link->link; before->link->link=temp; before=before->link; } } else { temp=temp->link; }
i++;//计算次数 } for(;i>0;i--) { temp=first; for(int j=0;j { if(temp->avelink->ave) { cout
first=first->link; // p=temp; // temp=temp->link; temp->link=temp->link->link; first->link=temp; before=first; } else//不是第一个结点 { before->link=temp->link; temp->link=temp->link->link; before->link->link=temp; before=before->link; } } else { temp=temp->link; } } } } /* bool IsSorted(Node* first) { for(;first;first=first->link) if(first->avelink->ave) return false; return true; }*/
void StudentScore::PrintList()//打印链表程序 { coutlink) { cout
void StudentScore::ClearList()//清除链表 { Node* p=new Node; while(first) { p=first->link; 删除 first; first=p; } }
//读函数
void StudentScore::ReadInfo(int k)//读第k个文件的信息存入链表 { // int wordLength;//记录子段长度 int _number;//学号
char* _cla_0=new char[21];//班级 char* _name=new char[9];//姓名 float _elec;//电子技术成绩 float _c_prog;//C程序设计成绩 float _media;//多媒体技术成绩 float _eng;//大学英语成绩 float _math;//高等数学成绩
float _sport;//大学体育成绩
float _polity;//马克思主义政治经济学成绩 float _ave;//平均成绩 int _order;//名次 Node *p,*r=NULL; first=NULL; ifstream Infile(fileName[k].c_str()); if(!Infile) { cout>RecordLength; cout>_cla_0;//班级 // cout>_number; Infile>>_name;//姓名 Infile>>_elec; Infile>>_c_prog; Infile>>_media; Infile>>_eng; Infile>>_math; Infile>>_sport; Infile>>_polity; Infile>>_ave; Infile>>_order; _ave=(_elec+_c_prog+_media+_eng+_math+_sport+_polity)/7;//求得平均成绩 //写一个返回排名的程序 _order=GetOrder(first,_ave);
p=new Node(_number,_cla_0,_name,_elec, _c_prog,_media,_eng,_math,_sport,_polity, _ave,_order,NULL);//建立一个新的结点储存信息
if(first!=NULL) r->link=p; else first=p; r=p; }
} void StudentScore::Save() { string tempName; cout>tempName; ofstream savefile(tempName.c_str());//要做一个转换 Node* temp=first; savefilelink) { savefile
//读取文件表信息
ifstream Readfileinfo("FileRecord.txt"); Readfileinfo>>fileNum; for(int i=0;i Readfileinfo>>fileName[i]; Readfileinfo.close(); fileNum++; fileName[i]=tempName; //修改文件表信息
ofstream changefile("FileRecord.txt"); changefile
void StudentScore::Menu() { cout>choice; if(choice==1) { BuildList(); } if(choice==2) { cout>fileNum; cout>fileName[i]; cout>fileLoc; ReadInfo(fileLoc-1); PrintList();
} cout>operChoice; if(operChoice==1) Statistic(); if(operChoice==2) 删除(); if(operChoice==3) Add(); if(operChoice==4) Sort(); Save(); ClearList(); }
int main() { cout