#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;
///一、構造函數 設置帳號、初始密碼
denglu::denglu()
{
id = "100001";
for(int i=0;i<6;i++){
password[i] = '1';
}
}
///二、密碼輸入、校驗
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;
}
///三、密碼修改
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() {}
///四、初始化文件
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(); //----關閉文件----
}
///五、添加文件
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;
}
///六、刪除文件
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;
}
///七、修改文件
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;
}
///八、查詢文件
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;
}
///九、顯示文件
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;
///十、菜單
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;
}
///十一、主函數
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;
}