C++新生基本信息管理系统(类、继承与多态及析构函数的运用)

来到代码世界:让我们一起编写自己的代码吧!!

#include<iostream>
#include<string>
#include<cstdlib>
#include<stdio.h>
#include<cstring>
#include<fstream>
#include<windows.h>
using namespace std;
   class node;
   int t=0;
   class StudentList; 
	void  newnode(node*newn); 
	void delete1(node*head);
	node*search(node*head); 
	void newnode(node*head,node data);
	void print(node*head);
	void print2(node*head);
	void add(node*head);
    void menu();
    node* rank(node*head); 
    void subject(node*head); 
   	void savefile(node*head); 
   	void old(node*head);
   	 void reading(node*head);
typedef class birthday //
{
	public:
	string year;
	string month;
	string day;
}bir;
class node{
	public:
	string name;
	string sex;
	string sub;
    bir bir1;//
	string id;
	int english;
	node *next; 
    node(){}
    ~node(){}	
};
class StudentList
{
	public:
		StudentList(){}
		~StudentList(){}
node*creatnode(){

	node*head=new node;
	if(head!=NULL){
	head->next=NULL;
	return head;
}
}
	void  newnode(node*newn); 
	void delete1(node*head);
	node*search(node*head); 
	void newnode(node*head,node data);
	void print(node*head);
	void print2(node*head); 
	void add(node*head);
    void menu();
    node* rank(node*head);
	void subject(node*head); 
	void savefile(node*head); 
	void old(node*head);
    void reading(node*head);
	//创建节点 
    node*creatnode1(node data){//node 类对象 data 
	node*newn=new node(data);
		newn->next=NULL;
	return newn;
} 
};
//录入函数 
void StudentList::add(node*head)
{   
    node add1;
        cout<<"请输入姓名:" << endl;
        cin >> add1.name;
        cout<<"请输入性别:" << endl;
        cin >> add1.sex;
        cout<<"请输入专业:" << endl;
        cin >> add1.sub;
        cout<<"请输入出生日期:" << endl; 
		cout<<"哪一年:";
        cin >> add1.bir1.year;
        cout<<"哪一月:";
		cin>>add1.bir1.month;
		cout<<"那一天:";
		cin>>add1.bir1.day;
        cout<<"请输入家庭住址:"<< endl;
		cin>>add1.id;
        cout<<"请输入英语分:" << endl;
        cin >> add1.english;
        newnode(head,add1);
        cout<<"录入成功!\n";
}

//打印
void  StudentList::print(node*head){
	node*p=head->next;
	
	while(p!=NULL){
    cout<<"-------------------------------------------------"<<endl;
    cout<<"学生姓名为:\t"<<p->name<<endl;
	cout<<"学生性别为:\t"<<p->sex<<endl;
	cout<<"学生专业为:\t"<<p->sub<<endl;
	cout<<"出生日期为:\t"<<p->bir1.year<<"/"<<p->bir1.month<<"/"<<p->bir1.day<<endl;
	cout<<"家庭住址为:\t"<<p->id<<endl;
	cout<<"英语成绩为:\t"<<p->english<<endl; 
	p=p->next;
	}	
cout<<endl;
} 

//打印2 
void  StudentList::print2(node*head){
	node*q=head->next;
	cout<<"学生姓名 "<<"学生性别 "<<"学生专业 "<<"出生日期 "<<"家庭住址 "<<"英语成绩"<<endl;
	while(q!=NULL){
		cout<<q->name<<"\t"
	     <<q->sex<<"\t"
	     <<q->sub<<"\t"
	     <<q->bir1.year<<"/"<<q->bir1.month<<"/"<<q->bir1.day<<"\t"
	     <<q->id<<"\t"
	     <<q->english<<"\t"<<endl;
		q=q->next;
	} 
cout<<endl;	
}

    //查找
	node*StudentList :: search(node*head)
{
	string name1;
 	node*sea1=head->next;
 	cout<<"请输入想要查找信息的学生姓名:"<<endl;
	cin>>name1; 
while(sea1!=NULL&&sea1->name!=name1)
	 {//通过姓名查找 
 	sea1=sea1->next;	
	 }
	 return sea1;
}
    //插入新节点 
   	void StudentList::newnode(node*head,node data)
	{ 
	    node*newn=creatnode1(data);
		newn->next = head->next;
		head->next=newn;
	}     
	
    //删除      
	void StudentList :: delete1(node*head){	
	string name1;
	node*del1=head; 
	node*del2=head->next;
	cout<<"请输入你想要删除的学生信息的姓名:"<<endl;
	cin>>name1;  
while(del2!=NULL&&del2->name!=name1)
	{
		del1=del2;
		del2=del1->next;
	} 
	if(del2!=NULL)
	{
		 del1->next=del2->next;
		 delete del2;
		 cout<<"删除成功!\n";
	}
	else
	{
	cout<<"未查询到相关信息!";
	cout<<endl; 	
	}
} 
//按英语成绩排序
node* StudentList::rank(node*head){
 	node*ra1=head->next;
 	node*ra2=ra1->next;
 	node*t=head;
 	string temp;
 	int tem;
	 while(ra2!=NULL){
	 	if(ra1->english<ra2->english){
	            temp=ra1->name;
	              ra1->name=ra2->name;
	                 ra2->name=temp;
	            temp=ra1->sex;
	               ra1->sex=ra2->sex;
	                  ra2->sex=temp;
	            temp=ra1->sub;
	                 ra1->sub=ra2->sub;
		               ra2->sub=temp;  
                 temp=ra1->bir1.year;
					  ra1->bir1.year=ra2->bir1.year;
						ra2->bir1.year=temp;
                 temp=ra1->bir1.month;
				      ra1->bir1.month=ra2->bir1.month;
				     	ra2->bir1.month=temp;
                 temp=ra1->bir1.day;
					  ra1->bir1.day=ra2->bir1.day;
						ra2->bir1.day=temp;  
		        temp=ra1->id;
					  ra1->id=ra2->id;
						ra2->id=temp;		        
                tem=ra1->english;
				      ra1->english=ra2->english;
				        ra2->english=tem; 
	 }
	 	ra1=ra2;
		ra2=ra2->next;
	 }  
    return(head);
}
//按专业或性别挑选
void StudentList:: subject(node*head)
{   char c; 
    int x;
	string sub1,sex1;
	node*p=head;
	node*q=head->next;
	cout<<"如果按照性别(sex)挑选请输入w按专业(sub)请输入e"<<endl;
	cin>>c;
	if(c=='e'||c=='E'){
	x=0;
	cout<<"请输入想挑选学生专业的名字(sub1):"<<endl;
	cin>>sub1;
	cout<<"专业相同的学生为:\n";
	cout<<"学生专业 "<<"学生姓名 "<<" 学生性别"<<"出生日期 "<<"家庭住址 "<<"英语成绩"<<endl;
	while(q!=NULL){
	  if(q->sub!=sub1){
	  	p=q;
		q=q->next;
	  }else
	  {
		cout<<q->sub<<" \t "
	     <<q->name<<" \t "
	      <<q->sex<<" \t "
	        <<q->bir1.year<<"/"<<q->bir1.month<<"/"<<q->bir1.day<<" \t "
	     <<q->id<<" \t "
	     <<q->english<<" \t "<<endl;
	    x=1;
	   	p=q;
		q=q->next;
		continue;
	  }
	}
	 if(x==0){
	  	cout<<"未查询到相关信息!"<<endl; 
	  }
	if(head->next==NULL){
		cout<<"还未录入学生信息!!"<<endl; 
	}
	}else if(c=='w'||c=='W'){
	x=0;
	cout<<"请输入想挑选学生的性别(sex1):"<<endl;
	cin>>sex1;
	cout<<"性别相同的学生为:\n";
	cout<<"学生性别 "<<"学生姓名 "<<"学生专业 "<<"出生日期 "<<"家庭住址 "<<"英语成绩"<<endl;
		while(q!=NULL){
	  if(q->sex!=sex1){
	  	p=q;
		q=q->next;
	  }else
	  {
		cout<<q->sex<<" \t "
	     <<q->name<<" \t "
	        <<q->sub<<" \t "
	          <<q->bir1.year<<"/"<<q->bir1.month<<"/"<<q->bir1.day<<" \t "
	        <<q->id<<" \t "
	     <<q->english<<" \t "<<endl;
	   	p=q;
		q=q->next;
		x=1;
		continue;
	  }
	} 
	 if(x==0){
	  	cout<<"未查询到相关信息!"<<endl; 
	  }
	if(head->next==NULL){
		cout<<"还未录入学生的信息!!"<<endl; 
	}	
	}
} 
//年龄信息统计
void StudentList::old(node*head)
{
int e=2022;
int w=0,n=0,x=0,i=0;
node*q=head->next; 
	cout<<"请输入想要查询学生的年龄:"<<endl; 
	cin>>n;
	cout<<"学生姓名 "<<"学生性别 "<<"学生专业 "<<"出生日期 "<<"家庭住址 "<<"英语成绩"<<endl;
while(q!=NULL){
	int b=atoi(q->bir1.year.c_str());
	w=e-b;
	if(w==n){
	   	cout<<q->name<<" \t "
	     <<q->sex<<" \t "
	        <<q->sub<<" \t "
	         <<q->bir1.year<<"/"<<q->bir1.month<<"/"<<q->bir1.day<<" \t "
	        <<q->id<<" \t "
	     <<q->english<<" \t "<<endl;
	    cout<<"学生年龄为:"<<w<<endl; 
	     x=1;
	     i++;
	}
	if(x==0){
		 	cout<<"未查询到相关信息!"<<endl; 
	}
	q=q->next;
}
if(head->next==NULL){
	 	cout<<"未查询到相关信息!"<<endl; 
}
  cout<<"共"<<i<<"人"<<endl; 
}
//读文件  暂时汉字读不出 
 void StudentList::reading(node*head){
  node*q=head;
  node d;
  ifstream of;
  of.open("student1.txt",ios::in);
  if(!of){
  	cerr<<"打开失败!"<<endl;
	  exit(1); 
  }
  while(!of.eof()){
	   	of>>d.name
	     >>d.sex
	        >>d.sub
	         >>d.bir1.year>>d.bir1.month>>d.bir1.day
	        >>d.id
	     >>d.english;
	     t++;
        newnode(q,d);
		} 
		of.close();
 }
//保存到文本文档中 
void StudentList::savefile(node*head){
   node*x=head->next;
   ofstream in;
   in.open("student.txt",ios::out|ios::app);
     if(!in.is_open()){
  	cerr<<"打开失败!"<<endl;
	  exit(0); 
  }
//   in<<"学生姓名 "<<"学生性别 "<<"学生专业 "<<"出生日期 "<<"家庭住址 "<<"英语成绩"<<endl;
   while(x!=NULL){
   	 	in<<x->name<<" \t "
	     <<x->sex<<" \t "
	        <<x->sub<<" \t "
	         <<x->bir1.year<<"/"<<x->bir1.month<<"/"<<x->bir1.day<<" \t "
	        <<x->id<<" \t "
	     <<x->english<<" \t "<<endl;
    x=x->next;
   }
   in.close();
} 
//菜单 
void StudentList::menu()
{
        cout << "\n"
             << endl;
        cout<<"        \t        "
		<< "=================================" << endl;
        cout<<"        \t        "
		<< "1.新增学生信息" << endl;
        cout<<"        \t        "
		<< "---------------------------------" << endl;
        cout<<"        \t        "
		<< "2.打印所有学生信息" << endl;
        cout<<"        \t        "
		<< "---------------------------------" << endl;
        cout<<"        \t        "
		<< "3.查找学生信息" << endl;
        cout<<"        \t        "
		<< "---------------------------------" << endl;
        cout<<"        \t        "
		<< "4.修改学生信息" << endl;
        cout<<"        \t        "
		<< "---------------------------------" << endl;
        cout<<"        \t        "
		<< "5.按专业或性别信息统计" << endl;
        cout<<"        \t        "
		<< "---------------------------------" << endl;
        cout<<"        \t        "
		<< "6.删除学生信息" << endl;
        cout<<"        \t        "
		<< "---------------------------------" << endl;
        cout<<"        \t        "
		<< "7.按年龄统计学生信息" << endl;
        cout<<"        \t        "
		<< "---------------------------------" << endl;
        cout<<"        \t        "
		<< "8.按英语成绩排序" << endl;
        cout<<"        \t        "
		<< "---------------------------------" << endl;
        cout<<"        \t        "
		<< "9.年龄信息统计" << endl;
        cout<<"        \t        "
		<< "---------------------------------" << endl;
        cout<<"        \t        "
		<< "0.退出程序!" << endl;
        cout<<"        \t        "
		<< "=================================" << endl; 
		cout<<"请选择功能选项:\n"; 
}
void byebye() {
	cout<<"\n";
	cout<<"                * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
	cout<<"                *                                               *"<<endl;
	cout<<"                *        感谢使用新生信息管理系统               *"<<endl;
	cout<<"                *                                               *"<<endl;
	cout<<"                * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
	cout<<endl;
}

node*list=NULL; 
int main(){
node*h=NULL;
	int number,n=0,z=100;
	StudentList Student;
    node*list=Student.creatnode();
	    cout<<"正在读取文件........"<<endl;
        Student.reading(list);
        while(z--){
        	cout<<"*";
        	Sleep(20);	
		}
		cout<<"读取完成........"<<endl;
		system("pause");
		system("cls"); 
	    system("color 0");
	   	int i=100;
		while(i--){
		cout<<"*";
		Sleep(20);	
	}
		cout<<"....进行100%!"<<endl;
		system("pause");
		  system("cls");
    while (1)
    {  
	    Student.menu();
    	cin>>number;
		system("pause");
	    system("cls");
        switch (number)
        {
        case 0:
        	  byebye(); 
			    cout<<"退出成功\n";
			      system("color c1");
			        cout<<"*******************¥¥期待您的下一次的使用¥¥*******************";
			      system("pause");
			    exit(0);
            break;
        case 1:
        	  Student.add(list);
        	  t++;
        	  Student.savefile(list);
        		system("pause");
        		system("cls");
            break;
        case 2:
        Student.print(list);
        cout<<"总人数为:"<<endl;
		cout<<"  "<<t<<endl; 
        	system("pause");
        	system("cls");
            break;
        case 3:
        	h=Student.search(list);
        	if(h!=NULL){
        	    cout<<"学生姓名为:\t"<<h->name<<endl;
				cout<<"学生性别为:\t"<<h->sex<<endl;
				cout<<"学生专业为:\t"<<h->sub<<endl;
				cout<<"出生日期为:\t"<<h->bir1.year<<"/"<<h->bir1.month<<"/"<<h->bir1.day<<endl;
				cout<<"家庭住址为:\t"<<h->id<<endl;
				cout<<"英语成绩为:\t"<<h->english<<endl; 
			}else
			cout<<"未查询到相关信息!"<<endl;
        	    	system("pause");
        	    	system("cls");
//        	    	delete h;
            break;
        case 4:
        	h=Student.search(list);
        	if(h!=NULL){
        		cout<<"请输入新的学生信息"<<endl;
				cout<<"请输入姓名:" << endl;
		        cin >> h->name;
		        cout<<"请输入性别:" << endl;
		        cin >> h->sex;
		        cout<<"请输入专业:" << endl;
		        cin >> h->sub;
		        cout<<"请输入出生日期:" << endl; 
				cout<<"哪一年:";
		        cin >> h->bir1.year;
		        cout<<"哪一月:";
				cin>>h->bir1.month;
				cout<<"那一天:";
				cin>>h->bir1.day;
		        cout<<"请输入家庭住址:"<< endl;
				cin>>h->id;
		        cout<<"请输入英语分:" << endl;
		        cin >>h->english; 
		        cout<<"修改成功!"<<endl; 
			}else{
				cout<<"未找到相关信息!"<<endl; 
			} 
        	    	system("pause");
        	    	system("cls");
            break;
        case 5:
        	Student.subject(list);
        		  system("pause");
        		  system("cls");
            break;
        case 6:
                Student.delete1(list);
                t--;
        		  system("pause");
        		  system("cls");
            break;
        case 7:
        		Student.old(list);
        		  system("pause");
        		  system("cls");
            break;
        case 8:
        	cout<<"总人数为:"<<t<<endl; 
			cout<<"请输入总人数\n";
			cin>>n;
			while(n--){
			h=Student.rank(list);
			}
			cout<<"以下是按英语成绩进行的排序:"<<endl; 
			Student.print2(h);
        		  system("pause");
        		  system("cls");
            break;
        case 9:
            Student.old(list);
        		  system("pause");
        		  system("cls");
            break;
        default:
        	cout<<"您所输入的功能选项超出了系统的能力!!    请加以改进后再次使用 "<<endl; 
            break;
        }
    }
      	system("pause");
	    system("cls");
	return 0;
}
//该系统仅供参考
//还有许多需要改进的地方
//建议:最后添加一个释放链表空间的函数,使得每次运行后空间得到释放噢

大家可以在下方讨论一下自己的观点噢,有什么好的想法都可以相互交流一下

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值