学生信息管理系统

使用vector存储数据,最后保存在文件中

#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<conio.h>
#include "windows.h"

#define FILENAME "stuInfo.txt"

using namespace std;

//学生类
class StuInfo
{
private:
	char sno[20];  //学号
	char sname[20];//姓名
	char cname[20]; //班级
	char sex[4]; //性别
	int age; //年龄
	char tel[15]; //电话

public:
	StuInfo()
	{

	}
	StuInfo(char sno[], char sname[], char cname[], char sex[], int age, char tel[])
	{
		setSno(sno);
		setSname(sname);
		setCname(cname);
		setSex(sex);
		setAge(age);
		setTel(tel);
	}

	void setSno(char sno[])
	{
		strcpy(this->sno,sno);
	}
	void setSname(char sname[])
	{
		strcpy(this->sname,sname);
	}
	void setCname(char cname[])
	{
		strcpy(this->cname,cname);
	}
	void setSex(char sex[])
	{
		strcpy(this->sex,sex);
	}
	void setAge(int age)
	{
		this->age=age;
	}
	void setTel(char tel[])
	{
		strcpy(this->tel,tel);
	}
	
	
	void show()
	{
		
		cout<<"\n\t"<<sno<<"\t"<<sname<<"\t"<<cname<<"\t"<<sex<<"\t"<<age<<"\t"<<tel<<endl;
	}
	char* getSno()
	{
		return this->sno;
	}
};

class Controller
{
public:
	void showCopy();//显示版权
	bool login();//用户登陆
	void menu();//主菜单
	void initData();//初始化数据
	void addStuInfo();//增加学生信息
	void showStuInfo(StuInfo stu);//显示单个学生信息
	void searchStuInfo();//查找学生信息
	bool save(StuInfo stu);//保存到文件
	void viewStuInfo();//浏览学生信息
	void updateStuInfo();//修改学生信息
	void deleteStuInfo();//删除学生信息
	bool rewrite();//重写数据文件

};


vector<StuInfo> stuInfovec;

//显示版权信息
void Controller::showCopy()
{
	cout<<"\n\n\t*****************欢迎使用教务系统*************";
	cout<<"\n\n\t*****************小虾米信息版权所有************";
}
bool Controller::login()
{
	system("cls");//清屏
	int index=0,count=0;
	string account;
	char pwd[20];
	char ch;
	showCopy();

	do
	{
		index=0;
		count++;
		cout<<"\n\n\tplease enter your account:";
		cin>>account;

		cout<<"\n\tplease enter your password:";
		
		//获取用户的输入密码
		while((ch=getch())!=13)//回车键的ASCII值  //密码只能是字母数字
		{
			if(ch==8) // backspace 回退
			{
				if(index<=0)
					index=0;
				else
				{
					cout<<"\b";  //\b退格
					index--;
				}
			}
			else
			{
				pwd[index++]=ch;
				cout<<'*';
			}
		}

		pwd[index]='\0';

		//判断用户输入的账号密码是否正确
		if(account=="xxm" && strcmp(pwd,"123")==0)
		{
			initData();
			return true;
		}
		else
		{
			if(count==3)
			{
				cout<<"\n\tsorry,you haven't access to login,the system is exiting!\t\n";
				exit(0);
			}
			else
				cout<<"\n\t account or password is wrong,please enter again,you still have "<<3-count<<" times"<<endl;
		}
	}while(count>0);

	return false;
}

void Controller::initData()
{
	fstream file(FILENAME, ios::in|ios::binary);
	if(file.is_open())
	{
		StuInfo stu;
		while(true)
		{
			file.read((char*)&stu,sizeof(StuInfo)); //从文件中读取sizeof(StuInfo)个字节给stu
			if(!file)
				break;
			stuInfovec.push_back(stu);
		}
		file.close();
	}
}
void Controller::menu()
{
	int option=0,count=0;

again:system("cls");   //again表示标签,标签+:组成一个带标签语句,可以作为goto的目标
	count=0;
	showCopy();
	cout<<"\n\n\t***************main menu**************\n";
	cout<<"\n\n\t*************1.浏览学生信息***********\n";
	cout<<"\n\n\t*************2.查询学生信息***********\n";
	cout<<"\n\n\t*************3.添加学生信息***********\n";
	cout<<"\n\n\t*************4.修改学生信息***********\n";
	cout<<"\n\n\t*************5.删除学生信息***********\n";
	cout<<"\n\n\t*************6.退出系统***************\n";

	do
	{
		if(count!=0) // 说明不是第一次选择
			cout<<"\n\n\t 请重新选择1-6:";
		else
			cout<<"\n\t 请选择1-6:";

		cin>>option;
		count++;
	}while(option<=0||option>6);

	switch(option)
	{
	case 1:viewStuInfo();break;
        case 2:searchStuInfo();break;
	case 3:addStuInfo();break;
	case 4:updateStuInfo();break;
	case 5:deleteStuInfo();break;
	case 6:exit(0);break;
	}
	goto again;   //goto只能在一个函数内跳转
}

void Controller::addStuInfo()
{
	system("cls");
	int result;
	char sno[20];
	char sname[20];//姓名
	char cname[20]; //班级
	char sex[4]; //性别
	int age; //年龄
	char tel[15]; //电话

	cout<<"\n\n\t***************请添加学生信息*******************";
	cout<<"\n\n\t***************请输入以下学生信息****************";

	cout<<"\n\t请输入学生学号:";
	cin>>sno;
	cout << "\n\t请输入学生姓名:";
    cin >> sname;
    cout << "\n\t请输入所在班级:";
    cin >> cname;
    cout << "\n\t请输入学生性别:";
    cin >> sex;
    cout << "\n\t请输入学生年龄:";
    cin >> age;
    cout << "\n\t请输入联系方式:";
    cin >> tel;
	StuInfo stu(sno,sname,cname,sex,age,tel);

	cout<<"\n\t您要添加的学生信息如下,请确认:"<<endl;
	showStuInfo(stu);

	result=MessageBox(NULL,LPCTSTR("您确定要添加数据吗"),LPCTSTR("确认提示"),MB_YESNO|MB_ICONWARNING); //_T标注为使用Unicode
	if(result==6)
	{
		if(save(stu))
		{
			stuInfovec.push_back(stu);
			cout<<"\n\t添加成功"<<endl;
		}
		else
		{
			cout<<"添加失败"<<endl;
		}
	}
	cout<<"\n\t按任意键返回"<<endl;
	getch();//getch()接受一个任意字符,不用按回车就返回,不显示在屏幕上,返回该字符的ASCII,在头文件conio.h中
	   //  getchar()从键盘上读取一个字符并输出,输入回车时结束,在头文件stdio.h中
}
bool Controller::save(StuInfo stu)
{
	fstream out(FILENAME,ios::out|ios::binary);
	if(out.is_open())
	{
		out.write((char*)&stu,sizeof(StuInfo));
		out.close();
		return true;
	}
	else
	{
		return false;
	}
}
bool Controller::rewrite()
{
	int len=stuInfovec.size();
	if(len<=0)
	{
		ofstream file(FILENAME,ios::out|ios::trunc);// trunc写的方式是覆盖,app表示追加
		file.close();
	}
	else
	{
		fstream out(FILENAME,ios::out|ios::binary);
		if(out.is_open())
		{
			for(int i=0;i<len;i++)
			{
				out.write((char*)&stuInfovec[i],sizeof(StuInfo));
			}
			out.close();
			return true;
		}
		else
			return false;
	}
	return false;
}

void Controller::showStuInfo(StuInfo stu)
{
	
    stu.show();
}


void Controller::viewStuInfo()
{
	system("cls");
	showCopy();
	cout<<"\n\t*************浏览学生信息******************"<<endl;

	if(stuInfovec.size()<=0)
		cout<<"\n\t暂无学生\n";
	else
	{
		cout << "\n\n\t************************** 共有 " << stuInfovec.size() << " 个学生 **************************" << endl;
        cout << "\n\n\t学号" << "\t 姓名" << "\t   班级" << "\t        性别" << "\t年龄" << "\t  电话" << endl;
        for(int i=0; i<stuInfovec.size(); i++)
        {
            stuInfovec[i].show();
        }
    }
    cout << "\n\n\t<请按回车返回>" << endl;
    getch();
}

void Controller::searchStuInfo()
{ 
	system("cls");

	showCopy();
	cout<<"\n\n\t****************查询学生信息********************";
	if(stuInfovec.size()<=0)
		cout<<"\n\t暂无学生信息"<<endl;
	else
	{
		char sno[20];
		cout<<"\n\t请输入要查询的学生的学号:";
		cin>>sno;
		cout<<"\n\t 学号"<<"\t姓名"<<"\t班级"<<"\t性别"<<"\t年龄"<<"\t电话"<<endl;

		int count=0;//记录满足条件的学生的个数
		int length=strlen(sno);//求字符数组的长度,不包括\0
		int temp=0,j=0;
		char arr[20];

		for(int i=0;i<stuInfovec.size();i++)
		{
			strcpy(arr,stuInfovec[i].getSno());    //strcpy(char* dst,char* src)
			temp=strlen(arr);

			length=length>temp?temp:length;
			for(j=0;j<length;j++)
			{
				if(arr[j]!=sno[j])
					break;

			}
			if(j>=length)
			{
				count++;
				stuInfovec[i].show();
			}
		}
		if(count>0)
		{
			cout<<"\n\t***************共有"<<count<<"个满足查找条件***************"<<endl;
		}
		else
			cout<<"\n\t没有满足条件的信息"<<endl;
	}
	cout<<"\n\t请按任意键返回"<<endl;
	getch();

}

void Controller::updateStuInfo()
{
	system("cls");

	showCopy();
	cout<<"\n\t**************修改学生信息*********************";

	if(stuInfovec.empty())
	{
		cout<<"\n\t暂无学生信息";
	}
	else
	{
		char sno[20];
		cout<<"\n\t请输入你要修改的学生的学号";
		cin>>sno;

		int i,len=stuInfovec.size();
		for(i=0;i<len;i++)
		{
			if(strcmp(sno,stuInfovec[i].getSno())==0)
			{
				cout<<"\n\t您要修改的学生信息如下"<<endl;
				cout<<"\n\t 学号"<<"\t姓名"<<"\t班级"<<"\t性别"<<"\t年龄"<<"\t电话"<<endl;
				stuInfovec[i].show();
				break;
			}
		}
		if(i>=len)
		{
			cout<<"\n\t没有您要查找的学生信息"<<endl;
		}
		else
		{
			 char sname[20]; // 姓名
            char cname[20]; // 班级
            char sex[4]; // 性别
            int age; // 年龄
            char tel[15]; // 电话号码
            cout << "\n\t请输入学生姓名:";
            cin >> sname;
            cout << "\n\t请输入所在班级:";
            cin >> cname;
            cout << "\n\t请输入学生性别:";
            cin >> sex;
            cout << "\n\t请输入学生年龄:";
            cin >> age;
            cout << "\n\t请输入联系方式:";
            cin >> tel;

			int result=MessageBox(nullptr,LPCTSTR("您确认要修改次学生的信息吗?"),LPCTSTR("确认提示"),MB_YESNO|MB_ICONWARNING);
			if(result==6)
			{
				stuInfovec[i].setSname(sname);
				stuInfovec[i].setCname(cname);
				stuInfovec[i].setSex(sex);
				stuInfovec[i].setAge(age);
				stuInfovec[i].setTel(tel);


				//重写数据文件
				if(rewrite())
				{
					cout<<"\n\t学生信息修改成功!"<<endl;
				}
				else
				{
					cout<<"\n\t修改失败"<<endl;
				}
			}
		}
	}
	cout<<"\n\t请按回车返回"<<endl;
	getch();
}
void Controller::deleteStuInfo()
{
	system("cls");

	showCopy();
	cout<<"\n\t**************删除学生信息**********************"<<endl;

	if(stuInfovec.empty())
	{
		cout<<"\n\t暂无学生信息可以删除"<<endl;
	}
	else
	{
		char sno[20];
		cout<<"\n\t请输入要删除学生的学号:";
		cin>>sno;
		int i=0;
		for(i=0;i<stuInfovec.size();i++)
		{
			if(strcmp(sno,stuInfovec[i].getSno())==0)
			{
				cout << "\n\n\t学号" << "\t 姓名" << "\t   班级" << "\t        性别" << "\t年龄" << "\t  电话" << endl;
                stuInfovec[i].show();
                break;
			}
		}
		if(i>=stuInfovec.size())
		{
			cout<<"\n\t没有您要删除的学生信息"<<endl;
		}
		else
		{
			int result=MessageBox(NULL,LPCWSTR("确认删除该学生的信息吗?"),LPCWSTR("确认删除"),MB_YESNO|MB_ICONWARNING);
			if(result==6)
			{
				stuInfovec.erase(stuInfovec.begin()+i);
				if(rewrite())
				{
					cout<<"\n\t学生信息删除成功"<<endl;
				}
				else
					cout<<"\n\t学生信息删除失败"<<endl;
			}
		}
	}
	cout<<"\n\t请按任意键返回"<<endl;
	getch();
}

int main()
{
	Controller control;
	if(control.login())
	{
		control.menu();
	}
	else
	{
		cout<<"\n\t对不起,你暂无权限,系统将退出"<<endl;
		exit(0);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值