数据结构课程设计

来源:互联网 发布:电子宠物机多少钱知乎 编辑:程序博客网 时间:2024/06/05 19:40
/*
*Copyright (c) 2017,烟台大学计算机与控制工程学院  
*文件名称:学生成绩管理系统*文件标识:无*内容摘要:实现学成成绩的输入、插入、删除、修改、排序和输出*其他内容:无*当前版本:v1.0*完成日期:2017.12.20*创作人:董丽娟*/

头文件xuesheng.h

#ifndef XUESHENG_H_INCLUDED#define XUESHENG_H_INCLUDED#include <string>  //string 类型using namespace std;typedef struct LNode//学生结构体{string name;string num;string classname;float C;float Math;float English;float sum;struct LNode *next;//指针域,指向后继节点} LinkList;void InitList(LinkList *&head);void addstudent(LNode *a,LNode *head);//添加学生函数的声明int num(string a,LNode *head);//判断学号是否重复的函数的声明void NewNode(LNode *head);//创建新的一个学生结构体,新节点void DestroyList(LinkList *&head);//销毁线性表int ListLength(LinkList *head);//求学生个数void DispList(LNode *head);//输出线性表void deletestudent(LNode *head);//删除学生信息void findstudentname();//按姓名查找学生void findstudentnum();//按学号查找学生void ChangeMarkByNum();//根据学号修改学生成绩void shuchujiedian(LNode *p);//输出一个结点信息void paixu();//按成绩排序并输出成绩void pai();//按学号排序void huan(LNode *q,LNode *p);//交换数据void Save();//保存链表数据到文件void duchu();void qing();void Menu(LNode *head);//显示菜单#endif // XUESHENG_H_INCLUDED

xuesheng.cpp:

#include <iostream>#include <iomanip>#include <stdio.h>#include <fstream>#include <string.h>#include <stdlib.h>#include <conio.h>//catch() 任意键继续#include <windows.h>//system("cls") 清屏#include "xuesheng.h"/************************************* 功能描述:申请一个新结点,并将其初始化* 输入参数:无* 输出参数:无************************************/void InitList(LinkList *&head)//创建一个头结点{head=(LinkList *)malloc(sizeof(LinkList));head->next=NULL;}void NewNode(LNode *head)// 创建完成调用addstudent()函数将新节点尾插法插入链表{        LNode *a;a=new LNode;string s;cout<<"学生学号"<<endl;cin>>s;if(num(s,head)==1){    a->num=s;cout<<"学生姓名"<<endl;cin>>a->name;cout<<"学生班级: "<<endl;cin>>a->classname;cout<<"高数:   "<<endl;cin>>a->Math;cout<<"英语:    "<<endl;cin>>a->English;cout<<"c++:  "<<endl;cin>>a->C;a->sum=a->C+a->Math+a->English;addstudent(a,head);}elsecout<<"学号已重复"<<endl;}/************************************* 功能描述:创建新的一个学生结构体,新节点。* 输入参数:无* 输出参数:无************************************/void addstudent(LNode *a,LNode *head)//添加学生{   LNode *p;if(head==NULL)//如果链表为空{head =a;a->next=NULL;return;//插入完成返回}else{    p = head;//从头结点开始找     while(p)//p非空则一直循环 {  if(p->next==NULL)//找到尾节点  {    p->next =a;   a->next =NULL;return;//插入完成返回  }   p= p->next;//找下一个节点 }}}/************************************* 功能描述:销毁线性表* 输入参数:无* 输出参数:无************************************/void DestroyList(LinkList *&head)//销毁线性表{LinkList *pre=head->next,*p=head->next->next; //准备好前驱后置指针while (p!=NULL){free(pre);//释放内存空间pre=p;p=pre->next;}free(pre);head->next=NULL;cout<<"学生信息已清空"<<endl;}/************************************* 功能描述:求学生个数* 输入参数:无* 输出参数:无************************************/int ListLength(LinkList *head)//求学生个数{    int n=0;  LinkList *p=head->next;  while (p->next!=NULL)  { n++; p=p->next;  }  return (n+1);}/************************************* 功能描述:显示所有学生的信息,调用shuchujiedian()函数* 输入参数:无* 输出参数:输出学生的姓名、学号、班级、三个成绩、总成绩,学生人数************************************/void DispList(LNode *head)//输出线性表{LinkList *p=head->next;if(p==NULL){cout<<"现在还没学生信息,请先输入学生信息"<<endl;return;}else{while (p!=NULL) {shuchujiedian(p);p=p->next; }}cout<<"共有"<<ListLength(head)<<"个学生"<<endl;}/************************************* 功能描述:根据输入的学号删除学生信息* 输入参数:学生学号* 输出参数:无************************************/void deletestudent(LNode *head)//删除学生信息{string num;int m;LNode *p,*pre;cout<<"请输入要删除学生的学号:";   cin>>num; if (head==NULL) { cout<<"学号输入有误"<<endl;     return; } else {    p=pre=head;while(p){     if (p->num==num) {    shuchujiedian(p);    cout<<"是否要删除学生成绩?"<<endl;    cout<<" 1.是    2.不是    "<<endl;    cin>>m;if(m==1){if(p==head){head = head->next;free(p);p=NULL;cout<<"学生成绩信息删除!"<<endl;return;} else {pre->next =p->next;free(p);p=NULL;cout<<"学生成绩信息已删除!"<<endl;return; }}if(m==2){return;}}else {pre=p;p=p->next; } }}cout<<"学号输入有误"<<endl;}/************************************* 功能描述:根据输入的姓名查找成绩* 输入参数:学生姓名* 输出参数:无************************************/void findstudentname(LNode *head)//按姓名查找学生{   string name;   int i=0;cout<<"请输入要查找的学生姓名"<<endl;cin>>name;LNode *p=head;if (head==NULL){cout<<"现在还没学生信息,请先输入学生信息"<<endl;return;}while(p){  if (p->name==name)  {        shuchujiedian(p);        i++;  }   p= p->next;}if(i==0)cout<<"没有该学生!"<<endl;}void findstudentnum(LNode *head)//按学号查找学生{        string num;cout<<"请输入要查找的学生学号"<<endl;cin>>num;LNode *p=head;if (head==NULL){cout<<"现在还没学生信息,请先输入学生信息"<<endl;return;}p=head;while(p){  if (p->num==num)  {       shuchujiedian(p);       return;  }   p= p->next;}cout<<"没有该学生!"<<endl;}/************************************* 功能描述:判断学号是否重复* 输入参数:无* 输出参数:无************************************/int num(string a,LNode *head)//判断学号是否重复{    for(LNode *p=head;p!=NULL;p=p->next)   if(p->num==a)    return 0;   return 1;}/************************************* 功能描述:根据输入的学号查找成绩* 输入参数:学生学号* 输出参数:无************************************/void ChangeMarkByNum(LNode *head)//根据学号修改学生成绩{    LNode *p;    string num;    int m;    float mark1;    float mark2;    float mark3;    p=head;    cout<<"请输入学生学号: "<<endl;    cin>>num;    while(p)    {        if(p->num==num)        {           shuchujiedian(p);           cout<<"是否要修改学生成绩?"<<endl;           cout<<" 1.是    2.不是    "<<endl;           cin>>m;            if(m==1){cout<<"请输入新的英语成绩"<<endl;cin>>mark3;cout<<"请输入新的高数成绩"<<endl;cin>>mark2;cout<<"请输入新的c++成绩"<<endl;cin>>mark1;p->C=mark1;p->Math=mark2;p->English=mark3;p->sum=p->C+p->English+p->Math;cout<<"成绩修改成功!"<<endl;shuchujiedian(p);return;}if(m==2){cout<<"学生成绩保留!"<<endl;return;}            break;}        p=p->next;}       cout<<"对不起,不存在学号为"<<num<<"的学生"<<endl;}/************************************* 功能描述:输出一个结点信息* 输入参数:无* 输出参数:输出学生的姓名、学号、班级、三科成绩和总成绩************************************/void shuchujiedian(LNode *p)//输出一个结点信息{           cout<<"姓名:"<<p->name;           cout<<setw(8)<<"班级:"<<p->classname;           cout<<setw(8)<<"学号:"<<p->num;           cout<<setw(8)<<"英语:"<<p->English;           cout<<setw(8)<<"高数:"<<p->Math;           cout<<setw(8)<<"c++:"<<p->C;           cout<<setw(8)<<"总成绩:"<<p->sum<<endl;}/************************************* 功能描述:按成绩排序* 输入参数:无* 输出参数:学生成绩信息************************************/void paixu(LNode *head)//按成绩排序并输出成绩{LNode change,*p,*q,*r;r=head->next;if(r==NULL){cout<<"现在还没学生信息,请先输入学生信息"<<endl;return;} while(r) {    p=r;    q=r->next;    while(q){if((q->sum)>(p->sum))huan(q,p);q=q->next;}r=r->next; } DispList(head);}void pai(LNode *head){   LNode change,*p,*q,*r;r=head->next;if(r==NULL){cout<<"现在还没学生信息,请先输入学生信息"<<endl;return;} while(r) {    p=r;    q=r->next;    while(q)    {        if((q->num)<(p->num))            huan(q,p);        q=q->next;}r=r->next; } DispList(head);}/************************************* 功能描述:交换两个学生信息* 输入参数:无* 输出参数:无************************************/void huan(LNode *q,LNode *p){  LNode change;change.name=q->name;//将q结点的信息给changechange.num=q->num;change.classname=q->classname;change.C=q->C;change.Math=q->Math;change.English=q->English;change.sum=q->sum;q->name=p->name;//将p结点的信息给qq->num=p->num;q->classname=p->classname;q->C=p->C;q->Math=p->Math;q->English=p->English;q->sum=p->sum;p->name=change.name;//将change结点的信息给pp->num=change.num;p->classname=change.classname;p->C=change.C;p->Math=change.Math;p->English=change.English;p->sum=change.sum;}/************************************* 功能描述:保存链表数据到文件* 输入参数:无* 输出参数:无************************************/void Save(LNode *head)//保存链表数据到文件{    LNode *p1;     int h;     ofstream out;     h=ListLength(head);     out.open("xuesheng.txt",ios::app);     if(!out)        {        cout<<"打开文件失败!"<<endl;        }        p1=head->next;        out<<h<<endl;     while(p1)        {            out<<p1->num<<setw(5)<<p1->name<<setw(5)<<p1->classname<<setw(5)<<p1->C<<setw(5)<<p1->English<<setw(5)<<p1->Math<<setw(5)<<p1->sum<<endl;            p1=p1->next;        }        out.close();        cout<<"保存成功!"<<endl;        cout<<"输入任意字符!继续……";        getch();}/************************************ 功能描述:保存文件数据到链表* 输入参数:无* 输出参数:无************************************/void duchu(LNode *head){    string num;    string classname;    int h,s;    string name;    int c;    int i=0;    int english;    int shuxue;    int sum;    LNode *p1,*p2;    p1=p2=new LNode;    ifstream in;    in.open("xuesheng.txt",ios::in);    if(!in)        {            cout<<"打开文件失败!"<<endl;        }    in>>h;    for(s=0;s<h;s++)    {        in>>num>>name>>classname>>c>>english>>shuxue>>sum;        p1->num=num;        p1->name=name;        p1->classname=classname;        p1->C=c;        p1->English=english;        p1->Math=shuxue;        p1->sum=sum;        i++;        if(i==1)            {                head->next=p1;            }    else                p2->next=p1;                p2=p1;                p1=new LNode;    }     p2->next=NULL;     delete p1;     in.close();      cout<<"输入任意字符!继续……";        getch();}/************************************* 功能描述:清除文件信息* 输入参数:无* 输出参数:无************************************/void qing(){     ofstream out;     out.open("xuesheng.txt",ios::trunc);     if(!out)        {        cout<<"打开文件失败!"<<endl;        }        cout<<"清除成功!"<<endl;        out.close();     cout<<"输入任意字符!继续……";        getch();}/************************************* 功能描述:显示菜单,根据用户的输入完成相应的功能* 输入参数:你要选择的功能的序号* 输出参数:选择的功能************************************/void Menu(LNode *head)// 显示菜单,根据用户的输入{    int choose;    int m=0;  cout<<"〓〓〓〓〓〓〓〓〓  ☆   学 生 成 绩 管 理 系  统      ☆  〓〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          1.录入学生成绩         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          2.显示学生成绩         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          3.删除学生成绩         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          4.查找学生成绩         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          5.排序学生成绩         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          6.修改学生成绩         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          7.保存学生成绩         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          8.读出学生成绩         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          9.清除已有信息         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          10.清除文件信息        ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;  cout<<"〓〓〓〓〓〓〓〓★  ☆          0.安全退出系统         ☆  ★〓〓〓〓〓〓〓〓〓"<<endl;   cin>>choose;/*取得用户的选择*/    while(1)  {  if(choose<0||choose>10)  {      cout<<"您输入的选项不存在,请重新输入"<<endl;      cin>>choose;  }  else    break;  }    switch(choose)    {    case 0:        exit(0);    case 1:        system("cls");        NewNode(head);        //创建新的一个学生结构体,新节点        cout<<"输入任意字符!继续……"<<endl;        getch();        break;    case 2:        system("cls");        DispList(head);//输出学生信息        cout<<"输入任意字符!继续……"<<endl;        getch();        break;    case 3:        system("cls");        deletestudent(head);//删除学生信息        cout<<"输入任意字符!继续……"<<endl;        getch();        break;    case 4:       system("cls");       cout<<"请选择"<<endl;       cout<<"1.按姓名查找     2.按学号查找"<<endl;       cin>>m;       if(m==1)       findstudentname(head);//根据姓名查找学生信息       else       findstudentnum(head);//按照学生学号查找信息       cout<<"输入任意字符!继续……"<<endl;       getch();       break;    case 5:        system("cls");        cout<<"请选择"<<endl;       cout<<"1.按成绩排序     2.按学号排序"<<endl;       cin>>m;       if(m==1)         paixu(head);        else            pai(head);        getch();//按成绩排序        break;    case 6:       system("cls");       ChangeMarkByNum(head);//根据用户输入的学号修改学生成绩        getch();        break;    case 7:       system("cls");       Save(head);//保存数据        break;    case 8:        system("cls");        duchu(head);        break;    case 9:       system("cls");       DestroyList(head);//清空学生信息        getch();        break;    case 10:        system("cls");        qing();        break;        default:        break;    }    Menu(head);}
main.cpp:

#include <stdlib.h>#include "xuesheng.h"int main(){    LNode *head;    head=NULL;    InitList(head);   Menu(head);   return 0;}
运行结果:

1.主界面



2.录入学生成绩


3.显示学生成绩


4.删除学生成绩


5.查找学生成绩


6.排序


7.修改学生成绩


8.保存链表文件到文件


9.保存文件信息到链表


10.清除已有的信息


11.清除文件信息


原创粉丝点击