C++学籍管理系统的实现

最近老师要求做的。大二以后想到要将自己的代码好好保存下,就贴到这里来啦。开张第一篇,不好勿怪!

 

 

设计学生学籍管理系统,基本要求:

学生信息包括学号,姓名,性别,年龄,住址

能够添加新的学生信息

能够删除已有的学生的信息

能够修改已有的学生的信息

 

 

有两个地方一直没弄好:

1,在处理文件流的时候,不懂文件流的指针,傻乎乎的不懂写学生类的构造函数,一个个读入,vector的语法也没咱弄懂

2,在读入的时候,while(!infile.eof()); 居然没写叹号!!!基础不扎实啊。以后要注意

 

好了,代码如下,十分简单的小程序:

 

 

#include<iostream>
#include<vector>
#include<string>
#include<fstream>

using namespace std ;

/***
	VS2010测试通过 基本功能测试通过  
	处于方便的考虑写了一个display函数做测试 已经注释掉
	                                                   ***/
/***
	张海强**********2012.10.27************************
	                                                   ***/


class  Student{
		friend bool readFile(fstream &) ;
		friend bool dispaly() ;
		friend bool edit(string &) ;
		friend bool deleteStudent(string &) ;
		friend bool saveFile(ofstream &) ;

public:
	Student(fstream &) ;
	Student(string ,long ,int ,int ,string) ;

private:
	string name ;
	long StuID ;
	int sex ;
	int age ;
	string addres ;
};

Student::Student(fstream &infile){
	infile>>name ;
	infile>>StuID ;
	infile>>sex ;
	infile>>age ;
	infile>>addres ;
}

Student::Student(string name ,long StuID ,int sex ,int age ,string addres){
	this->name = name ;
	this->StuID = StuID ;
	this->age = age ;
	this->sex = sex ;
	this->addres = addres ;
}


vector<Student> vec ;
vector<Student> ::iterator it ;



bool readFile(fstream &infile){
	while (!infile.eof())
	{
		vec.push_back(Student(infile)) ;
	}
	return true ;
}


/**
bool dispaly(){
	it = vec.begin() ;
	while (it != vec.end())
	{
		cout<<it->name <<endl ;
		it ++ ;
	}
	return true ;
}
**/

bool add(){
	string name ;
	long StuID ;
	int sex ;
	int age ;
	string addres ;


	cout<<"please input the new student's data:"<<endl ;
	cout<<"the name:"<<endl ;
	cin>>name ;
	cout<<"the student's id:"<<endl ;
	cin>>StuID ;
	cout<<"the sex:(0/1)"<<endl ;
	cin>>sex ;
	cout<<"the age:"<<endl ;
	cin>>age ;
	cout<<"the addres:"<<endl ;
	cin>>addres ;


	vec.push_back(Student(name ,StuID ,sex ,age ,addres)) ;
	return true ;
}

bool edit(string & name){
	it = vec.begin() ;
	while (it != vec.end())
	{
		if(it->name == name)
		{
			break ;
		}
		it ++ ;
	}
	if(it == vec.end()){
		cout<<"there is no such a person!"<<endl ;
	}else{
		cout<<"please input the datatype you wantna edit"<<endl ;
		cout<<"a:name b:studnet's id c:the sex d:the age e:the addres"<<endl ;

		char ch ;
		cin>>ch ;
		string name ;
		long StuID ;
		int sex ;
		int age ;
		string addres ;

		switch (ch)
		{
		case 'a' :
			
			cin>>name ;
			it->name = name ;
			break ;
		case 'b':
			cin>>StuID ;
			it->StuID = StuID ;
			break ;
		case 'c':
			cin>>sex ;
			it->sex = sex ;
			break ;
		case 'd':
			cin>>age ;
			it->age = age ;
			break ;
		case 'e':
			cin>>addres ;
			it->addres = addres ;
			break ;
		default:
			break;
		}
	}
	return true ;
}

bool deleteStudent(string & name){
	it = vec.begin() ;
	while (it != vec.end())
	{
		if(it->name == name){
			vec.erase(it) ;
			cout<<"erase success!"<<endl ;
			break ;
		}

		it ++ ;
	}

	return true ;
}


bool saveFile(ofstream &outfile){
	it = vec.begin() ;
	while (it != vec.end())
	{
		outfile<<it->name<<" "<<it->StuID<<" "<<it->sex<<" "<<it->age<<" "<<it->addres<<endl ;
		it ++ ;
	}
	return true ;
}


int main(){
	string str_1 ;
	cout<<"A:read from a data;B:make a new data;"<<endl ;

	cin>>str_1 ;
	//dispaly() ;

	if(str_1 == "A"){
		cout<<"please input the location of the data:"<<endl ;
		string str_2 ;
		cin>>str_2 ;

		fstream infile(str_2) ;
		readFile(infile) ;
		cout<<"A:add a new student B:edit a student's data: C:delete a student's data"<<endl ;
		string str_3 ;
		cin>>str_3 ;
		if(str_3 == "A")
			add() ;
		else if(str_3 == "B"){
			cout<<"please input the name of student:"<<endl ;
			string str_4 ;
			cin>>str_4 ;
			edit(str_4) ;
		}else if(str_3 == "C"){
			string str_5 ;
			cout<<"please input the person you wantna delete:" ;
			cin>>str_5 ;
			deleteStudent(str_5) ;
		}else
			cout<<"error" ;
		//dispaly() ;
		infile.close() ;
	}
	
	
	
	
	else if(str_1 == "B"){
		cout<<"please input the student number:"<<endl ;
		int  n;
		cin>>n ;
		for(int i = 0 ;i < n ;i ++)
			add() ;
	}


	cout<<"do you wanna save the new data:(Y/N)"<<endl ;
	string str_2 ;
	cin>>str_2 ;
	if(str_2 == "Y"){
		cout<<"the location of new data:"<<endl ;
		string str_3 ;
		cin>>str_3 ;
		ofstream outFile ;
		outFile.open(str_3 ,fstream::trunc | fstream::out) ;
		saveFile(outFile) ;
		cout<<"save success!!!"<<endl ;
		outFile.close() ;
	}
		

	return 0 ;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值