您的位置:首页 > 理论基础 > 数据结构算法

数据结构实训-学生成绩管理系统

2015-12-30 10:09 603 查看
问题及代码:

sty.h

void menu();
void select();
void SearchNum(struct stu *head);
void SearchName(struct stu *head);
void print(struct stu *head);
void No(struct stu *head);
void selectmh(struct stu *head);
void sort(struct stu *head);
void insert(struct stu *head);
void del(struct stu *head);
struct stu *creat();

struct stu
{
int num;
char name[20];
float Ma;
float En;
float Ch;
float all;
int order;
struct stu *next;
};
sty.cpp

#include "sty.h"
#include<iostream>
#include<malloc.h>
#include<iomanip>
#define NULL 0
#define LinkList sizeof(struct stu)
using namespace std;
int n=0;
struct stu *creat()
{
system("cls");
struct stu *head,*p1,*p2;
n=0;
p1=p2=(struct stu *)malloc(LinkList);
int a=0,i;
cout<<"请输入要创建的学生人数"<<endl;
cin>>a;
system("cls");
head=p1;
for(i=0;i<a;i++)
{
cout<<"请输入第"<<i+1<<"名学生学号"<<endl;
cin>>p1->num;
cout<<"请输入第"<<i+1<<"名学生姓名"<<endl;
cin>>p1->name;
cout<<"请输入第"<<i+1<<"名学生数学成绩"<<endl;
cin>>p1->Ma;
cout<<"请输入第"<<i+1<<"名学生英语成绩"<<endl;
cin>>p1->En;
cout<<"请输入第"<<i+1<<"名学生语文成绩"<<endl;
cin>>p1->Ch;
p1->all=p1->Ma+p1->En+p1->Ch;
p2->next=p1;
p2=p1;
p1=(struct stu*)malloc(LinkList);
system("cls");
n++;
}
p2->next=NULL;
return head;
}

void print(struct stu *head)
{
system("cls");
cout<<"学号 "<<"姓名 "<<"数学 "<<"英语 "<<"语文 "<<"总分"<<endl;
struct stu *p;
p=head;
if(head!=NULL)
{
do
{
cout<<" "<<setiosflags(ios_base::left)<<setw(5)<<p->num<<setw(5)<<p->name<<setw(5)<<p->Ma<<setw(5)<<p->En<<setw(5)<<p->Ch<<setw(5)<<p->all<<resetiosflags(ios_base::left)<<endl;
p=p->next;
}
while(p!=NULL);
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
}
else exit(0);
}
void del(struct stu *head)
{
system("cls");
if(n==0)
{
cout<<"成绩表已经为空"<<endl;
exit(0);
}
int num;
cout<<"请输入要删除信息的学号:";
cin>>num;
while(num!=0)
{
struct stu *p1,*p2;
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
cout<<num<<"号已被删除"<<endl;
n--;
}
else cout<<"未找到此数据!"<<endl;
cout<<"请输入要删除的序号";
cin>>num;
}
if(n==0){cout<<"此时链表已为空!"<<endl;exit(0);}
}
void insert(struct stu *head)
{
system("cls");
struct stu *stu;
stu=(struct stu*)malloc(LinkList);
cout<<"请输入该学生学号"<<endl;
cin>>stu->num;
cout<<"请输入该学生姓名"<<endl;
cin>>stu->name;
cout<<"请输入该学生数学成绩"<<endl;
cin>>stu->Ma;
cout<<"请输入该学生英语成绩"<<endl;
cin>>stu->En;
cout<<"请输入该学生语文成绩"<<endl;
cin>>stu->Ch;
stu->all=stu->Ma+stu->En+stu->Ch;
n++;
struct stu *p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while(p0->num>p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p0->num<p1->num)
{
if(head==p1){head=p0;}
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
}

void sort(struct stu *head)
{
struct stu *p1,*p2,*p0;
float b;
char temp[20];
int NO=0;
p0=head;
p2=head;
p1=p2->next;
b=(p2->all);
while(p0->next!=NULL)
{
while(p1!=NULL)
{
if((p1->all)>b)
{
b=(p1->all);
p2=p1;
}
p1=p1->next;
};
p2->order=++NO;
b=p2->order;
p2->order=p0->order;
p0->order=b;
b=p2->num;
p2->num=p0->num;
p0->num=b;
b=p2->Ma;
p2->Ma=p0->Ma;
p0->Ma=b;
b=p2->En;
p2->En=p0->En;
p0->En=b;
b=p2->Ch;
p2->Ch=p0->Ch;
p0->Ch=b;
b=p2->all;
p2->all=p0->all;
p0->all=b;
strcpy(temp,p2->name);
strcpy(p2->name,p0->name);
strcpy(p0->name,temp);
p0=p0->next;
p2=p0;
p1=p2->next;
b=(p2->all);
}
if(p0->next==NULL)p2->order=++NO;
}
void menu()
{
cout<<"  ◢██████◣      ◢████◣          ╭︽╮      "<<endl;
cout<<"◢◤      ◥◣    ◢◤    ◥◣        (\"成\")    "<<endl;
cout<<"◤        ◥◣  ◢◤      █...     ╰︾╯    ╭︽╮    "<<endl;
cout<<"▎   ◢█◣   ◥◣◢◤    ◢█   █                  (\"绩\")   "<<endl;
cout<<"◣  ◢◤ ◥◣          ◢◣◥◣ ◢◤.       ╭︽╮    ╰︾╯   "<<endl;
cout<<"◥██◤  ◢◤         ◥◣              (\"管\")       "<<endl;
cout<<"      █ ●       ●  █              ╰︾╯    ╭︽╮    "<<endl;
cout<<"      █ 〃   ▄   〃 █                        (\"理\")   "<<endl;
cout<<"      ◥◣   ╚╩╝   ◢◤              ╭︽╮    ╰︾╯   "<<endl;
cout<<"       ◥█▅▃▃ ▃▃▅█◤                (\"系\")     "<<endl;
cout<<"         ◢◤   ◥◣                   ╰︾╯    ╭︽╮   "<<endl;
cout<<"         █     █                             (\"统\")   "<<endl;
cout<<"        ◢◤▕   ▎◥◣                            ╰︾╯    "<<endl;
cout<<"       ▕▃◣◢▅▅▅◣◢▃▕  "<<endl;
}

void select()
{
cout<<"┏━★━━★━━★━━★━━★━━★━━★━━★━━★━┓"<<endl;
cout<<"┃                                                      ┃"<<endl;
cout<<"★   输入学生成绩<1>          显示排名成绩<4>           ★"<<endl;
cout<<"┃                                                      ┃"<<endl;
cout<<"┃                                                      ┃"<<endl;
cout<<"★   输出学生成绩<2>          添加学生信息<5>           ★"<<endl;
cout<<"┃                                                      ┃"<<endl;
cout<<"┃                                                      ┃"<<endl;
cout<<"★   学生数据查询<3>          删除学生信息<6>           ★"<<endl;
cout<<"┃                                                      ┃"<<endl;
cout<<"┗━★━━★━━★━━★━━★━━★━━★━━★━━★━┛"<<endl;
cout<<"请输入您的选择"<<endl;
}

void selectmh(struct stu *head)
{
system("cls");
int a;
cout<<"┏━★━━★━━★━━★━━★━━★━━★━━★━━★━┓"<<endl;
cout<<"┃                                                      ┃"<<endl;
cout<<"★       按名查找<1>          按号查找<2>               ★"<<endl;
cout<<"┃                                                      ┃"<<endl;
cout<<"┗━★━━★━━★━━★━━★━━★━━★━━★━━★━┛"<<endl;
cout<<"请输入您的选择"<<endl;
cin>>a;
if(a>2||a<1) cout<<"无该选项,请从1~2中选择"<<endl;
else
switch(a)
{
case 1:SearchName(head);break;
case 2:SearchNum(head);break;
}

}

void SearchNum(struct stu *head)
{
system("cls");
int num;
struct stu *p;
p=head;
cout<<"请输入要查找的学生的学号:";
cin>>num;
while(p->num!=num&&p->next!=NULL)
{
p=p->next;
}
if(p->num==num)
{
cout<<"该生的成绩为:"<<endl;
cout<<"学号 "<<"姓名 "<<"数学 "<<"英语 "<<"语文 "<<"总分"<<endl;
cout<<" "<<setiosflags(ios_base::left)<<setw(5)<<p->num<<setw(5)<<p->name<<setw(5)<<p->Ma<<setw(5)<<p->En<<setw(5)<<p->Ch<<setw(5)<<p->all<<resetiosflags(ios_base::left)<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
}
else cout<<"该学生并未录入成绩!"<<endl<<endl<<endl;
}

void SearchName(struct stu *head)
{
system("cls");
struct stu *p;
p=head;
char name[20];
cout<<"请输入要查找的学生的“姓名”:";
cin>>name;
while(strcmp(p->name,name)!=0&&p->next!=NULL)
{
p=p->next;
}
if(strcmp(p->name,name)==0)
{
cout<<"该生的成绩为:"<<endl;
cout<<"学号 "<<"姓名 "<<"数学 "<<"英语 "<<"语文 "<<"总分"<<endl;
cout<<" "<<setiosflags(ios_base::left)<<setw(5)<<p->num<<setw(5)<<p->name<<setw(5)<<p->Ma<<setw(5)<<p->En<<setw(5)<<p->Ch<<setw(5)<<p->all<<resetiosflags(ios_base::left)<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
}
else cout<<"该学生并未录入成绩"<<endl<<endl<<endl;
}

void No(struct stu *head)
{
system("cls");
int i,j;
struct stu *p;
p=head;
for(j=1;j<=20;j++)
{
if(p->order==1&&p->next!=NULL)
{
cout<<"名次 "<<"学号 "<<"姓名 "<<"数学 "<<"英语 "<<"语文 "<<"总分"<<endl;
for(i=0;i<10;i++)
{
cout<<" "<<setiosflags(ios_base::left)<<setw(5)<<p->order<<setw(5)<<p->num<<setw(5)<<p->name<<setw(5)<<p->Ma<<setw(5)<<p->En<<setw(5)<<p->Ch<<setw(5)<<p->all<<resetiosflags(ios_base::left)<<endl;
if(p->next!=NULL)
p=p->next;
else
{
cout<<"已经输出全部成绩"<<endl;
break;
}
}
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
}
}
}
main.cpp

#include "sty.h"
#include<iostream>
using namespace std;
int main()
{

struct stu *head;
int a;
menu();
while(a)
{
select();
cin>>a;
switch(a)
{
case 1:head=creat();break;
case 2:print(head);break;
case 3:selectmh(head);break;
case 4:sort(head);No(head);break;
case 5:insert(head);break;
case 6:del(head);break;
}
}
return 0;
}
运行结果:







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: