zoukankan      html  css  js  c++  java
  • list容器实现简易的学生管理系统

    #include<iostream>
    #include<list>
    #include<string> 
    #include<cstdio>
    using namespace std;
    //使用list(双向链表)容器对象实现简单的学生管理系统 
    //list简单使用 
    
    class Student{    //学生基本信息结构 
    	public:
    		char num[20]; //学号 
    		char name[20];  //姓名 
    		char sex[10];  //性别 
    		int age; 	//年龄 
    		
    };
    void menu(){
    	cout<<"
    
    
    
    
    
    
    "; 
    	cout<<"					**********学生信息管理系统********"<<endl;
    	cout<<"						1.追加学生信息"<<endl;
    	cout<<"						2.删除最后一个学生的信息"<<endl;
    	cout<<"						3.显示学生信息"<<endl;
    	cout<<"						4.删除所有学生信息"<<endl;
    	cout<<"						5.统计学生个数"<<endl;
    	cout<<"						6.将学生信息保存到文件中"<<endl;
    	cout<<"						0.退出"<<endl;
    	cout<<"					请输入操作序号:";
    } 
    void fun1(list<Student> &L){
    	system("cls");
    	cout<<"					************追加学生信息的界面************"<<endl;
    	Student s;
    	int n;
    	cout<<"						请输入要追加的学生个数:";
    	cin>>n;
    	if(n>0){
    		cout<<"					学号,学生姓名,学生性别,学生年龄:
    ";
    	}
    	for(int i=0;i<n;i++){	
    		cout<<"					";
    		cin>>s.num>>s.name>>s.sex>>s.age; 
    		L.push_back(s);
    	}
    	cout<<"					";
    	system("pause");
    	system("cls");
    }
    void fun2(list<Student> &L){
    	system("cls");
    	cout<<"				*********删除最后一个学生的信息界面************"<<endl;
    	if(!L.empty()){
    		L.pop_back();
    		cout<<"					删除成功!"<<endl;
    	}else{
    		cout<<"					数据库为空!"<<endl; 
    	}
    	cout<<"				";
    	system("pause");
    	system("cls");
    }
    void fun3(list<Student> &L){
    	system("cls");
    	cout<<"					*********显示学生信息的界面*********"<<endl;
    	if(L.empty()){
    		cout<<"			数据库为空,无法显示!"<<endl;
    	}else{
    		int i=1;
    		for(list<Student>::iterator it=L.begin();it!=L.end();it++,i++){
    			cout<<"			"<<i<<". 学号:"<<(*it).num<<"	"<<"学生姓名:"<<(*it).name<<"		"<<"学生性别:"<<(*it).sex<<"		"<<"学生年龄:"<<(*it).age<<endl;
    		}
    	} 
    	cout<<"			";
    	system("pause");
    	system("cls");
    }
    void fun4(list<Student> &L){
    	system("cls");
    	cout<<"				*********删除所有学生信息的界面*********"<<endl;
    	if(L.empty()){
    		cout<<"					数据库为空!"<<endl;
    	} else{
    		L.clear();
    		cout<<"					删除成功!"<<endl; 
    	}
    	cout<<"				";
    	system("pause");
    	system("cls");
    }
    void fun5(list<Student> &L){
    	system("cls");
    	cout<<"				*********统计学生人数界面********"<<endl;
    	cout<<"					学生总人数为:"<<L.size()<<endl;
    	cout<<"				";
    	system("pause");
    	system("cls");
    }
    void fun6(list<Student> &L,FILE *fp){
    	system("cls");
    	cout<<"					*********保存界面********"<<endl;
    	fp=fopen("E:\桌面存储位置\a.txt","wb+");
    	if(L.empty()){
    		cout<<"				数据库为空,保存无效!"<<endl;
    	}else{
    		for(list<Student>::iterator it=L.begin();it!=L.end();it++){
    			fprintf(fp,"%s %s %s %d
    ",(*it).num,(*it).name,(*it).sex,(*it).age);
    		}
    		cout<<"				保存成功!"<<endl;
    	} 
    	fclose(fp);
    	cout<<"				"; 
    	system("pause");
    	system("cls");
    } 
    void fun0(){
    	cout<<"					是否退出?(y是,n不是):";
    	char ch;
    	cin>>ch;
    	switch(ch){
    		case 'y':{
    			exit(0);
    			break;
    		}
    		case 'n':{
    			system("cls");
    			break;
    		}
    	} 
    }
    
    int main()
    {
    	FILE *fp;	//定义一个文件结构体的指针 
    	Student stu;
    	list<Student> L;
    	list<Student>::iterator pos;
    	char ch;
    	if((fp=fopen("E:\桌面存储位置\a.txt","r+"))==NULL){
    		
    	} 
    	else{
    		fscanf(fp,"%s%s%s%d",stu.num,stu.name,stu.sex,&stu.age);
    		while(!feof(fp)){ //feof(fp)用于判断文件是否到结尾,是返回0 
    		//使用feof一定要先读一次 
    			L.push_back(stu);
    			fscanf(fp,"%s%s%s%d",stu.num,stu.name,stu.sex,&stu.age);
    		}
    		fclose(fp);
    	}
    	while(1){
    		menu();
    		cin>>ch;
    		switch(ch){
    			case '1':{
    				fun1(L);
    				break;
    			}
    			case '2':{
    				fun2(L);
    				break;
    			}
    			case '3':{
    				fun3(L);
    				break;
    			}
    			case '4':{
    				fun4(L);
    				break;
    			}
    			case '5':{
    				fun5(L);
    				break;
    			}
    			case '6':{
    				fun6(L,fp);
    				break;
    			}
    			case '0':{ 
    				fun0();
    				break;
    			}
    			default:{
    				system("cls");
    				break;
    			}
    		} 
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    漏洞扫描
    端口探测
    IP探测
    kali linux基础命令
    python学习07
    python学习06
    openoffice+jquery.media.js实现Linux与Windows中文档在线预览
    Oracle10g安装包
    MyEclipse2014安装包附注册破解包、eclipse安装包
    外层div自适应内层div高度
  • 原文地址:https://www.cnblogs.com/nanfengnan/p/14408739.html
Copyright © 2011-2022 走看看