欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

学生信息管理系统.cpp(大二上)

程序员文章站 2022-03-08 12:31:02
#include #include #include #include #include #include using namespace std; #define M 200 //可以存 ......
 
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<conio.h>
#include<windows.h>
using namespace std;
#define m 200 //可以存信息总量
int n=0; //学生总量
class student{
public:
student();
void set(); //初始化信息,第一次对信息的录入
void add(); //添加学生信息
void del(); //删除某学生信息
void change(); //修改某学生信息
void search(); //查询某学生信息
void display(); //显示全部学生信息
private:
static int num;
string name,sex,age,birthday,address,tel,email;
};
 
class denglu{//定义登录密码类
public:
denglu();//构造函数 设置账号、初始密码
int input();
int newpw();
private:
string id;
char password[10];
};
 
int student::num=1000;
///1、构造函数 设置账号、初始密码
denglu::denglu()
{
id = "100001";
for(int i=0;i<6;i++){
password[i] = '1';
}
}
 
///2、密码输入、校验
 
int denglu::input()
{
int i=0,j=0,t,flag=1;
char ch,pw[10],a[10];
string id0,dl1;
cout<<"退出登录界面请输入“-1 ”,否则输入任意数字"<<endl;
cin>>flag;
if(flag == -1) exit(0);
//cout<<"请输入管理员账号,密码:\n";
cout<<"输入管理员账号:";
cin>>id0;
cout<<"输入六位数密码:";
while((ch=getch())!='\r') {//不回显函数。getch()函数输入字符时不显示出来,其他功能同getchar()
if(ch!=8) { //不是退格(退格键的ascii码为:8)就录入
pw[i]=ch;
cout<<'*';//并且输出*号
i++;
}
else {
cout<<'\b';//让光标回退一位,下一次输入字符时,会把光标所在位置的字符替换掉
cout<<' ';//再显示空格符把刚才的*给盖住,
cout<<'\b';//然后再回撤一格等待录入。
i--;
}
}
pw[i]='\0';
system("cls");
//判断账号密码是否正确
if(id0 != id){
cout<<"\n账号错误!"<<endl;
return 0;
}
for(i=0;i<6;i++){
if(pw[i] != password[i]){
cout<<"\n密码错误!"<<endl;
return 0;
}
}
return 1;
}
 
///3、密码修改
int denglu::newpw()
{
int j=0;
char c;
cout<<"\n请输入六位新密码:";
while((c=getch())!='\r') {//getch()函数输入字符时不显示出来,其他功能同getchar()
if(c!=8) { //不是回(回退键的ascii码为:8)就录入
password[j]=c;
cout<<'*';//并且输出*号
j++;
}
else {
cout<<'\b';//这里是删除一个,通过输出回撤符 /b,回撤一格,
cout<<' ';//再显示空格符把刚才的*给盖住,
cout<<'\b';//然后再回撤一格等待录入。
j--;
}
}
system("cls");
cout<<"\n密码修改成功!"<<endl;
return 0;
}
 
student::student() {}
///4、初始化文件
 
void student::set()
{
int i;
string setname,setsex,setage,setbirthday,setaddress,settel,setemail;
ofstream outfile;
outfile.open("f1.txt",ios::out); //----打开文件----
if(!outfile)
{
cerr<<" open error"<<endl;
exit(1);
}
//----依次录入相应个数的学生信息----
for(i=0;i<m;i++)
{
cout<<"当姓名输入为'0'时,停止输入!!!"<<endl;
cout<<"请输入姓名:"<<endl;
cin>>setname;
name=setname;
if(setname=="0") break;
if(setname!="0")
{
n++;
num++;
outfile<<num<<" ";
outfile<<setname<<" ";
cout<<"请输入性别:"<<endl;
cin>>setsex;
sex=setsex;
outfile<<setsex<<" ";
cout<<"请输入年龄:"<<endl;
cin>>setage;
age=setage;
outfile<<setage<<" ";
cout<<"请输入出生日期:"<<endl;
cin>>setbirthday;
birthday=setbirthday;
outfile<<setbirthday<<" ";
cout<<"请输入家庭住址:"<<endl;
cin>>setaddress;
address=setaddress;
outfile<<setaddress<<" ";
cout<<"请输入电话:"<<endl;
cin>>settel;
tel=settel;
outfile<<settel<<" ";
cout<<"请输入电子邮箱:"<<endl;
cin>>setemail;
email=setemail;
outfile<<setemail<<" "<<endl;
}
}
outfile.close(); //----关闭文件----
}
 
///5、添加文件
void student::add()
{
string addname,addsex,addage,addbirthday,addaddress,addtel,addemail;
 
//----以追加的方式录入信息,直接将信息追加到以前文件的末尾----
ofstream outfile("f1.txt",ios::app);
if(!outfile)
{
cerr<<" open error"<<endl;
exit(1);
}
n=n+1;
//----依次输入相应的数据,并且追加到之前的文件中去----
cout<<"请输入您要添加的数据"<<endl;
num++;
outfile<<num<<" ";
cout<<"请输入姓名:"<<endl;
cin>>addname;
outfile<<addname<<" ";
cout<<"请输入性别:"<<endl;
cin>>addsex;
outfile<<addsex<<" ";
cout<<"请输入年龄:"<<endl;
cin>>addage;
outfile<<addage<<" ";
cout<<"请输入出生日期:"<<endl;
cin>>addbirthday;
outfile<<addbirthday<<" ";
cout<<"请输入家庭住址:"<<endl;
cin>>addaddress;
outfile<<addaddress<<" ";
cout<<"请输入电话:"<<endl;
cin>>addtel;
outfile<<addtel<<" ";
cout<<"请输入电子邮件:"<<endl;
cin>>addemail;
outfile<<addemail<<endl;
outfile.close();
cout<<"已添加成功!"<<endl;
}
 
///6、删除文件
void student::del()
{
int k,i;
int num[m];
string name[m],sex[m],age[m],birthday[m],address[m],tel[m],email[m];
ifstream infile("f1.txt",ios::in);
if(!infile)
{
cerr<<" open error"<<endl;
exit(1);
}
for(i=0;i<n;i++)
{
cout<<" ";
infile>>num[i];
cout<<setw(4)<<num[i];
infile>>name[i];
cout<<setw(7)<<name[i];
infile>>sex[i];
cout<<setw(6)<<sex[i];
infile>>age[i];
cout<<setw(6)<<age[i];
infile>>birthday[i];
cout<<setw(12)<<birthday[i];
infile>>address[i];
cout<<setw(11)<<address[i];
infile>>tel[i];
cout<<setw(14)<<tel[i];
infile>>email[i];
cout<<setw(18)<<email[i]<<endl;
}
cout<<"您想删除第几个学生信息?"<<endl;
cin>>k;
k=k-1;
infile.close();
ofstream outfile("f1.txt",ios::out);
if(!outfile)
{
cerr<<" open error"<<endl;
exit(1); //退出程序
}
//----将数组存取的信息输出到文件中去----
for(i=0;i<n;i++)
{
if(i!=k)
{
outfile<<num[i]<<" ";
outfile<<name[i]<<" ";
outfile<<sex[i]<<" ";
outfile<<age[i]<<" ";
outfile<<birthday[i]<<" ";
outfile<<address[i]<<" ";
outfile<<tel[i]<<" ";
outfile<<email[i]<<" "<<endl;
}
}
outfile.close();
n=n-1;
cout<<"已删除成功!"<<endl;
}
 
///7、修改文件
 
void student::change()
{
int i,k;
int num[m];
string name[m],sex[m],age[m],birthday[m],address[m],tel[m],email[m];
ifstream infile("f1.txt",ios::in);
if(!infile)
{
cerr<<" open error"<<endl;
exit(1);
}
for(i=0;i<n;i++)
{
cout<<" ";
infile>>num[i];
cout<<setw(4)<<num[i];
infile>>name[i];
cout<<setw(7)<<name[i];
infile>>sex[i];
cout<<setw(6)<<sex[i];
infile>>age[i];
cout<<setw(6)<<age[i];
infile>>birthday[i];
cout<<setw(12)<<birthday[i];
infile>>address[i];
cout<<setw(11)<<address[i];
infile>>tel[i];
cout<<setw(14)<<tel[i];
infile>>email[i];
cout<<setw(18)<<email[i]<<endl;
}
infile.close();
ofstream outfile("f1.txt",ios::out);
if(!outfile)
{
cerr<<" open error"<<endl;
exit(1);
}
cout<<"您想修改第几个学生信息?"<<endl;
cin>>k;
k=k-1;
cout<<"请输入新的数据:"<<endl;
cout<<"请输入姓名:"<<endl;
cin>>name[k];
cout<<"请输入性别:"<<endl;
cin>>sex[k];
cout<<"请输入年龄:"<<endl;
cin>>age[k];
cout<<"请输入出生日期:"<<endl;
cin>>birthday[k];
cout<<"请输入家庭住址:"<<endl;
cin>>address[k];
cout<<"请输入电话:"<<endl;
cin>>tel[k];
cout<<"请输入电子邮箱:"<<endl;
cin>>email[k];
for(i=0;i<n;i++)
{
outfile<<num[i]<<" ";
outfile<<name[i]<<" ";
outfile<<sex[i]<<" ";
outfile<<age[i]<<" ";
outfile<<birthday[i]<<" ";
outfile<<address[i]<<" ";
outfile<<tel[i]<<" ";
outfile<<email[i]<<" "<<endl;
}
outfile.close();
cout<<"已修改成功!"<<endl;
}
 
///8、查询文件
 
void student::search()
{
int i,n,w=0,num[m],a[m];
char ch;
string searchname, name[m],sex[m],age[m],birthday[m],address[m],tel[m],email[m],b[m][7];
cout<<"a.按学号查找 b.按姓名查找"<<endl;
ifstream infile("f1.txt",ios::in);
if(!infile)
{
cerr<<" open error"<<endl;
exit(1);
}
for(i=0;i<n;i++)
{
infile>>num[i];a[i]=num[i];
infile>>name[i];b[i][0]=name[i];
infile>>sex[i];b[i][1]=sex[i];
infile>>age[i];b[i][2]=age[i];
infile>>birthday[i];b[i][3]=age[i];
infile>>address[i];b[i][4]=age[i];
infile>>tel[i];b[i][5]=address[i];
infile>>email[i];b[i][6]=age[i];
}
cout<<"请输入a或者b!"<<endl;
cin>>ch;
if(ch=='a')
{
cout<<"请输入学号!"<<endl;
cin>>n;
for(i=0;i<n;i++)
if(a[i]==n)
{
w=1;
cout<<" 学号 姓名 性别 年龄 出生日期 家庭住址 电话 邮箱"<<endl;
cout<<" ";
cout<<setw(4)<<a[i];
cout<<setw(7)<<b[i][0];
cout<<setw(6)<<b[i][1];
cout<<setw(6)<<b[i][2];
cout<<setw(12)<<b[i][3];
cout<<setw(11)<<b[i][4];
cout<<setw(14)<<b[i][5];
cout<<setw(18)<<b[i][6]<<endl;
}
if(w==0)
cout<<"无此人信息!"<<endl;
}
else if(ch=='b')
{
cout<<"请输入姓名!"<<endl;
cin>>searchname;
for(i=0;i<n;i++)
if(name[i]==searchname)
{
w=1;
cout<<" 学号 姓名 性别 年龄 出生日期 家庭住址 电话 邮箱"<<endl;
cout<<setw(4)<<a[i];
cout<<setw(7)<<b[i][0];
cout<<setw(6)<<b[i][1];
cout<<setw(6)<<b[i][2];
cout<<setw(12)<<b[i][3];
cout<<setw(11)<<b[i][4];
cout<<setw(14)<<b[i][5];
cout<<setw(18)<<b[i][6]<<endl;
}
if(w==0)
cout<<"无此人信息!"<<endl;
}
else cout<<"没有此选项!"<<endl;
}
 
///9、显示文件
 
void student::display()
{
int k,num;
string name,sex,age,birthday,address,tel,email;
ifstream infile("f1.txt",ios::in);
if(!infile)
{
cerr<<" 0open error"<<endl;
exit(1);
}
cout<<" 学号 姓名 性别 年龄 出生日期 家庭住址 电话 邮箱"<<endl;
for(k=0;k<n;k++)
{
cout<<" ";
infile>>num;
cout<<setw(4)<<num;
infile>>name;
cout<<setw(7)<<name;
infile>>sex;
cout<<setw(6)<<sex;
infile>>age;
cout<<setw(6)<<age;
infile>>birthday;
cout<<setw(12)<<birthday;
infile>>address;
cout<<setw(11)<<address;
infile>>tel;
cout<<setw(14)<<tel;
infile>>email;
cout<<setw(18)<<email<<endl;
}
infile.close();
}
 
 
student stu;
denglu dl;
 
///10、菜单
 
void menu()
{
cout<<"\n\t\t\t**************欢迎进入学生信息管理系统*******************"<<endl;
cout<<"\t\t\t* 1.输入学生信息 *"<<endl;
cout<<"\t\t\t* 2.添加学生信息 *"<<endl;
cout<<"\t\t\t* 3.删除学生信息 *"<<endl;
cout<<"\t\t\t* 4.修改学生信息 *"<<endl;
cout<<"\t\t\t* 5.查询学生信息 *"<<endl;
cout<<"\t\t\t* 6.浏览全部学生信息 *"<<endl;
cout<<"\t\t\t* 7.修改密码 *"<<endl;
cout<<"\t\t\t* 0.退出学生信息 *"<<endl;
cout<<"\t\t\t*********************************************************"<<endl;
}
 
///11、主函数
 
int main()
{
char ch='1';
int f;
while(1){
f = dl.input();
if(f==1) break;
}
 
while(f || ch!='0'){
system("color 1f");
menu();
cin>>ch;
while(ch!='0')
{
system("cls");
switch(ch)
{
case '1':stu.set();break;
case '2':stu.add();break;
case '3':stu.del();break;
case '4':stu.change();break;
case '5':stu.search();break;
case '6':stu.display();break;
case '7':dl.newpw();break;
case '0':cout<<"退出系统!";break;
default:cout<<"没有此选项!"<<endl;break;
}
menu();
cin>>ch;
}
system("cls");
f = dl.input();
}
return 0;
}