(一)新生基本信息统计软件
有新生来报到,要逐个录入其信息,如:学生姓名,性别,专业,出生日期,家庭地址,英语入学成绩。要求设计链表类来实现,并统计学生人数。文本界面为:
1. 新增学生信息
2. 删除学生信息
3. 导入学生信息(已经保存于的文件信息)
4. 学生信息搜索(按姓名)
5. 学生信息统计(按专业或性别或年龄---年龄要自动计算)
6. 按英语成绩排序
7. 学生信息保存
8. 退出
******请选择:1
————————————————————————————————————————————————
年龄的统计需要 提取系统当前日期
#include <time.h>
time_t tt = time(NULL); //这句返回的只是一个时间cuo
tm* t= localtime(&tt);
printf("%d-%02d-%02d %02d:%02d:%02d\n",
t->tm_year + 1900,
t->tm_mon + 1,
t->tm_mday,
t->tm_hour,
t->tm_min,
t->tm_sec);
#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>
#include <time.h>
using namespace std;
class Student //学生信息类
{
public:
Student() //构造
{
id="/0";
name="/0";
sex="/0";
major="/0";
adress="/0";
score=0;
year=0;
month=0;
date=0;
}
~Student() //析构
{
}
void Creat_List(Student *head); //创建链表
bool Unique_List(Student *head,string id);//查重链表
void Print_List(Student *head); //输出链表
void Delete_List(Student *head); //删除链表节点(根据学号)
void Search_List(Student *head); //链表检索 (根据姓名)
void Find_List(Student *head); //链表查找 (根据学号)
void Statistics_List(Student *head); //链表统计 (专业或性别或年龄)
void Sort_List(Student *head); //链表排序 (英语成绩)
void Out_File(Student *head); //写入文件
void In_File(Student *head); //读取文件
void Menu_List(Student *head); //菜单
private: //成员信息
string id;
string name;
string sex;
string major;
string adress;
int score;
int year;
int month;
int date;
Student *next;
};
bool Student::Unique_List(Student *head,string id)
{
while(head->next!=NULL)
{
if(head->id==id)
{
cout<<"请勿重复添加相同学号学生!";
return true;
}
}
return false;
}
void Student::Creat_List(Student *head) //创建链表的实现 (可重复添加到尾部)
{
Student *p1=head;
int num;
cout<<"请输入您要添加的学生数量: \n";
cin>>num;
if(p1->next==NULL)
{
for(int i=0;i<num;i++)
{
Student *p2=new Student;
cout<<"请输入第"<<i+1<<"位学生的学号: \n";
cin>>p1->id;
if(Unique_List(head,p1->id))
{
return ;
}
cout<<"请输入第"<<i+1<<"位学生的姓名: \n";
cin>>p1->name;
cout<<"请输入第"<<i+1<<"位学生的性别: \n";
cin>>p1->sex;
cout<<"请输入第"<<i+1<<"位学生的专业: \n";
cin>>p1->major;
cout<<"请输入第"<<i+1<<"位学生的住址: \n";
cin>>p1->adress;
cout<<"请输入第"<<i+1<<"位学生的英语成绩: \n";
cin>>p1->score;
cout<<"请输入第"<<i+1<<"位学生的出生日期: \n";
cin>>p1->year>>p1->month>>p1->date;
p1->next=p2;
p2->next=NULL;
p1=p2;
}
}
else if(p1->next!=NULL) //之前已经有学生信息则插在尾部
{
while(p1->next!=NULL)
{
p1=p1->next;
}
for(int i=0;i<num;i++)
{
Student *p2=new Student;
loopad:cout<<"请输入第"<<i+1<<"位学生的学号: \n";
cin>>p1->id;
if(Unique_List(head,p1->id))
{
return ;
}
cout<<"请输入第"<<i+1<<"位学生的姓名: \n";
cin>>p1->name;
cout<<"请输入第"<<i+1<<"位学生的性别: \n";
cin>>p1->sex;
cout<<"请输入第"<<i+1<<"位学生的专业: \n";
cin>>p1->major;
cout<<"请输入第"<<i+1<<"位学生的住址: \n";
cin>>p1->adress;
cout<<"请输入第"<<i+1<<"位学生的英语成绩: \n";
cin>>p1->score;
cout<<"请输入第"<<i+1<<"位学生的出生日期: \n";
cin>>p1->year>>p1->month>>p1->date;
p1->next=p2;
p2->next=NULL;
p1=p2;
}
}
}
void Student::Print_List(Student *head) //输出链表的实现
{
Student *p1=head;
if(p1->next==NULL)
{
cout<<"无学生信息!\n";
}
else
{
cout<<"学号\t\t姓名\t\t性别\t\t专业\t\t住址\t\t英语成绩\t出生日期\t\t";
while(p1->next!=NULL)
{
cout<<p1->id<<"\t\t"<<p1->name<<"\t\t"<<p1->sex<<"\t\t"<<p1->major<<"\t\t"<<p1->adress<<"\t\t"<<p1->score<<"\t\t"<<p1->year<<"."<<p1->month<<"."<<p1->date<<"\n";
p1=p1->next;
}
cout<<"输出完毕!\n";
}
}
void Student::Delete_List(Student *head) //删除节点的实现
{
Student *p1=head;
string num;
cout<<"请输入您要删除学生的学号: \n";
cin>>num;
if(p1->next==NULL)
{
cout<<"无学生信息!\n";
}
else
{
while(p1->next!=NULL)
{
if(num==p1->id)
{
Student *temp;
temp=p1->next;
temp->next=p1->next->next;
p1->next=temp->next;
delete temp;
temp=NULL;
cout<<"\n删除学号为:"<<num<<"的学生成功!\n";
break;
}
p1=p1->next;
}
}
}
void Student::Search_List(Student *head) //按姓名检索链表的实现
{
Student *p1=head;
string exam;
cout<<"请输入您要检索的学生姓名:\n";
cin>>exam;
if(p1->next==NULL)
{
cout<<"无学生信息!\n";
}
else
{
cout<<"学号\t\t姓名\t\t性别\t\t专业\t\t住址\t\t英语成绩\t出生日期\t\t";
while(p1->next!=NULL)
{
if(p1->name==exam)
{
cout<<p1->id<<"\t\t"<<p1->name<<"\t\t"<<p1->sex<<"\t\t"<<p1->major<<"\t\t"<<p1->adress<<"\t\t"<<p1->score<<"\t\t"<<p1->year<<"."<<p1->month<<"."<<p1->date<<"\n";
}
p1=p1->next;
}
}
}
void Student::Find_List(Student *head) //按学号查找学生
{
Student *p1=head;
string num;
cout<<"请输入您要查找的学生学号:\n";
cin>>num;
if(p1->next==NULL)
{
cout<<"无学生信息!\n";
}
else
{
cout<<"学号\t\t姓名\t\t性别\t\t专业\t\t住址\t\t英语成绩\t出生日期\t\t";
while(p1->id==num)
{
cout<<p1->id<<"\t\t"<<p1->name<<"\t\t"<<p1->sex<<"\t\t"<<p1->major<<"\t\t"<<p1->adress<<"\t\t"<<p1->score<<"\t\t"<<p1->year<<"."<<p1->month<<"."<<p1->date<<"\n";
cout<<"输出完毕!\n";
break;
p1=p1->next;
}
}
}
void Student::Statistics_List(Student *head) //链表统计的实现
{
Student *p1=head;
string a="专业",b="性别",c="年龄";
string category;
cout<<"请选择您要统计的类别:(请输入“专业”或“性别”或“年龄”)\n";
cin>>category;
if(category==a) //按专业统计
{
string exam;
cout<<"请输入专业名称:\n";
cin>>exam;
int sum=0;
if(p1->next==NULL)
{
cout<<"无学生信息!\n";
}
else
{
while(p1->next!=NULL)
{
if(p1->major==exam)
{
sum++;
}
p1=p1->next;
}
cout<<exam<<"专业的人数:\n"<<sum<<endl;
}
}
else if(category==b) //按性别统计
{
string exam;
cout<<"请输入要统计的性别:(请输入“男”或“女”)\n";
cin>>exam;
int sum=0;
if(p1->next==NULL)
{
cout<<"无学生信息!\n";
}
else
{
while(p1->next!=NULL)
{
if(p1->sex==exam)
{
sum++;
}
p1=p1->next;
}
cout<<exam<<"性的人数:\n"<<sum<<endl;
}
}
else if(category==c) //按年龄统计
{
int age;
cout<<"请输入您要统计的年龄:\n";
cin>>age;
if(p1->next==NULL)
{
cout<<"无学生信息!\n";
}
else
{
int total=0;
time_t tt = time(NULL); //这句返回的只是一个时间cuo
tm* t= localtime(&tt);
while(p1->next!=NULL)
{
int agenow=0;
if(t->tm_year+1900>=p1->year&&t->tm_mon+1>=p1->month&&t->tm_mday>=p1->date)
{
agenow=t->tm_year+1900-p1->year;
}
else
{
agenow=t->tm_year+1900-p1->year-1;
}
if(agenow==age)
{
total++;
}
p1=p1->next;
}
cout<<"年龄为"<<age<<"的学生人数为:\t"<<total<<endl;
}
}
}
void Student::Sort_List(Student *head) //链表排序的实现
{
Student *p1=head;
Student temp;
if(p1->next==NULL)
{
cout<<"无学生信息!\n";
}
else
{
cout<<"以英语成绩排序:\n";
while(p1->next!=NULL)
{
Student *p2=p1->next;
while(p2!=NULL)
{
if(p1->score<p2->score)
{
temp.id=p2->id;p2->id=p1->id;p1->id=temp.id;
temp.name=p2->name;p2->name=p1->name;p1->name=temp.name;
temp.sex=p2->sex;p2->sex=p1->sex;p1->sex=temp.sex;
temp.major=p2->major;p2->major=p1->major;p1->major=temp.major;
temp.adress=p2->adress;p2->adress=p1->adress;p1->adress=temp.adress;
temp.score=p2->score;p2->score=p1->score;p1->score=temp.score;
temp.year=p2->year;p2->year=p1->year;p1->year=temp.year;
temp.month=p2->month;p2->month=p1->month;p1->month=temp.month;
temp.date=p2->date;p2->date=p1->date;p1->date=temp.date;
}
p2=p2->next;
}
p1=p1->next;
}
}
}
void Student::Out_File(Student *head)
{
Student *p1=head;
ofstream file_d("students.dat",ios::out);
if(file_d==NULL)
{
cout<<"打开dat文件失败!\n";
}
else
{
while(p1->next!=NULL)
{
file_d<<p1->id<<" "<<p1->name<<" "<<p1->sex<<" "<<p1->major<<" "<<p1->adress<<" "<<p1->score<<" "<<p1->year<<" "<<p1->month<<" "<<p1->date<<" ";
p1=p1->next;
}
file_d.close();
cout<<"写入dat文件成功!\n";
}
ofstream file_t("students.txt",ios::out);
if(file_t==NULL)
{
cout<<"打开txt文件失败!\n";
}
else
{
p1=head;
while(p1->next!=NULL)
{
file_t<<p1->id<<"\t\t"<<p1->sex<<"\t\t"<<p1->major<<"\t\t"<<p1->adress<<p1->score<<"\t\t"<<p1->year<<"\t\t"<<p1->month<<"\t\t"<<p1->date<<"\n";
p1=p1->next;
}
file_t.close();
cout<<"写入txt文件成功!\n";
}
}
void Student::In_File(Student *head)
{
Student *p1=head;
Student *p=new Student;
ifstream file_d("students.dat",ios::in);
if(file_d==NULL)
{
cout<<"打开文件dat文件失败!\n";
}
else
{
int total=0;
while(!file_d.eof())
{
file_d>>p->id>>p->name>>p->sex>>p->major>>p->adress>>p->score>>p->year>>p->month>>p->date;
total++;
}
cout<<"学生数量为:\t"<<total-1<<endl;
delete p;
p=NULL;
file_d.close();
file_d.open("students.dat",ios::in);
while(!file_d.eof())
{
Student *p2=new Student;
file_d>>p1->id>>p1->name>>p1->sex>>p1->major>>p1->adress>>p1->score>>p1->year>>p1->month>>p1->date;
p1->next=p2;
p2->next=NULL;
p1=p2;
if(--total==1)
{
break;
}
}
file_d.close();
}
}
void Student::Menu_List(Student *head)
{
Student STU;
while(1)
{
system("color B1");
system("date/t");
system("time/t");
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<<"请输入您要选择的服务种类:(‘0’~‘9’)\n";
int num;
cin>>num;
switch(num)
{
case 1 :system("CLS");STU.Creat_List(head);system("pause");break;
case 2 :system("CLS");STU.Delete_List(head);system("pause");break;
case 3 :system("CLS");STU.In_File(head);system("pause");break;
case 4 :system("CLS");STU.Search_List(head);system("pause");break;
case 5 :system("CLS");STU.Statistics_List(head);system("pause");break;
case 6 :system("CLS");STU.Sort_List(head);system("pause");break;
case 7 :system("CLS");STU.Find_List(head);system("pause");break;
case 8 :system("CLS");STU.Out_File(head);system("pause");break;
case 9 :system("CLS");STU.Print_List(head);system("pause");break;
case 0 :system("CLS");cout<<"谢谢使用!";exit(0);system("pause");
default:system("CLS");printf("无效输入!\n\n");system("pause");
}
}
}
int main()
{
Student student;
Student *head=new Student;
student.Menu_List(head);
return 0;
}