/*
* c++课程设计
* 彩洋威
*/
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <stdio.h>
using namespace std;
class people /* 定义基类 */
{
public:
void get_people(); /* 输入 */
void show_people(); /* 输出函数 */
virtual void say(); /* 虚函数 */
people();
protected:
string name; /* 定义成员函数 */
string sex;
string address;
string phone;
};
void people::say() /* 定义虚函数 */
{
cout << "I im a people ." << endl;
}
people::people() /* 定义构造函数 */
{
name = "彩";
sex = "男";
address = "china";
phone = "15093012130";
}
void people::get_people() /* 定义输入函数 */
{
cout << "请输入学生姓名:"; cin >> name;
cout << "请输入学生性别:"; cin >> sex;
cout << "请输入学生家庭住址;"; cin >> address;
cout << "请输入学生电话号码:"; cin >> phone;
}
void people::show_people() /* 定义输出函数 */
{
say();
cout << "姓名:" << name << endl;
cout << "性别:" << sex << endl;
cout << "家庭住址:" << address << endl;
cout << "电话号码:" << phone << endl;
}
class student : public people { /* 定义类 */
public: /* 公有成员 */
student();
void get_student(); /* 类内涵数 */
void show_student();
void say();
friend void Add(); /* 定义友元函数,方便下面的调用 */
friend void Modify();
friend void Search();
friend void Delete();
friend void See();
private: /* 定义私有成员 */
string number;
} stu[1000];
student::student()
{
number = "1106655030";
people();
}
void student::say()
{
cout << "I am a syudent . " << endl;
}
void student::get_student() /* 定义get_student()输入函数 */
{
cout << "请输入学生学号:"; cin >> number;
get_people();
}
void student::show_student() /* 定义show_student()输出函数 */
{
cout << endl;
cout << "学号:" << number << endl;
show_people();
}
void Add() /* 添加函数 */
{
int i;
ofstream fp( "D://123.txt", ios::app ); /* 以在末尾添加形式打开文件,定义指向文件的指针fp */
if ( !fp ) /* 判断文件是否打开 */
{
cout << "文件无法打开。\n";
exit( 1 );
}
cout << "请添加学生信息:" << endl;
int n;
cout << "添加人数;"; cin >> n;
for ( i = 0; i < n; i++ ) /* 循环添加对象 */
{
cout << "请输入第" << i + 1 << "个学生的信息" << endl;
stu[i].get_student(); /* 调用get_student()输入函数 */
}
i = 0;
while ( n-- ) /* 循环向fp指向的文件中添加学生信息 */
{
fp << stu[i].number << " " << stu[i].name << " " << stu[i].sex << " " << stu[i].address << " " << stu[i].phone << endl;
i++;
}
fp. close(); /* 关闭文件 */
cout << "请确认您所存储的学生信息\n" << endl; /* 确认输入信息是否正确 */
for ( i = 0; i < n; i++ )
stu[i].show_student();
cout << "是否继续添加[Yes] [No] ";
string ch;
cin >> ch;
if ( ch == "Yes" )
Add();
}
void Modify() /* 定义修改函数 */
{
int y;
string number;
cout << "请输入修改的学生的学号:->"; /* 输入需要修改的学生信息 */
cin >> number;
ifstream fp( "D://123.txt", ios::in ); /* 打开文件 */
if ( !fp )
{
cout << "文件无法正常打开。\n";
exit( 1 );
}
student *p, stu[1000]; /* 定义指针p,和对象数组student */
int i = 0;
p = new student;
while ( fp.good() ) /* 循环读取文件中信息 */
{
fp >> p->number >> p->name >> p->sex >> p->address >> p->phone; /* 将文件中的信息读入对象数组student */
if ( fp.good() ) /* 判断文件中信息是否读完 */
{
stu[i].number = p->number; /* 文件中信息付给stu[i]中 */
stu[i].name = p->name;
stu[i].sex = p->sex;
stu[i].address = p->address;
stu[i].phone = p->phone;
i++;
}
}
fp.close(); /* 关闭文件 */
for ( y = 0; y < i; y++ )
{
if ( stu[y].number == number ) /* 查找文文件中符合要求的那个学生信息 */
{
stu[y].get_student(); /* 将该学生信息修改 */
break;
}
}
stu[y].show_student(); /* 输出新的信息 */
ofstream f( "D://123.txt", ios::out ); /* 打开文件 */
if ( !f )
{
cout << "文件无法正常打开。\n";
exit( 1 );
}
for ( y = 0; y < i; y++ ) /* 把信息从新存入文件中 */
{
f << stu[y].number << " " << stu[y].name << " " << stu[y].sex << " " << stu[y].address << " " << stu[y].phone << endl;
}
f.close(); /* 关闭文件 */
}
void Delete() /*删除函数 */
{
cout << "请选择需要删除的学生信息:" << endl;
ifstream fp( "D://123.txt", ios::in ); /* 打开文件,与上同,一下不再介绍 */
if ( !fp )
{
cout << "文件无法正常打开。\n";
exit( 1 );
}
student *p, stu[1000];
int i = 0;
p = new student;
while ( fp.good() )
{
fp >> p->number >> p->name >> p->sex >> p->address >> p->phone;
if ( fp.good() )
{
stu[i].number = p->number;
stu[i].name = p->name;
stu[i].sex = p->sex;
stu[i].address = p->address;
stu[i].phone = p->phone;
i++;
}
}
fp.close();
cout << "请选择你需要删除的学生学号:-> "; /* 输入需要删除的学生学号 */
string number; /* 定义一个临时存放学号的 */
cin >> number;
int y, l;
for ( y = 0; y < i; y++ ) /* 寻找符合条件的学生信息 */
{
if( stu[y].number == number )
{
l = y; break;
}
/* 记录下标 */
}
for ( y = l; y < i - 1; y++ ) /* 将要删除的那个学生后的所有学生信息向前替一位,补充空白处 */
{
stu[y].number = stu[y + 1].number;
stu[y].name = stu[y + 1].name;
stu[y].sex = stu[y + 1].sex;
stu[y].address = stu[y + 1].address;
stu[y].phone = stu[y + 1].phone;
}
ofstream f( "D://123.txt", ios::out ); /* 把新信息再次存入文件中 */
if ( !f )
{
cout << "cannot open output file.\n";
exit( 1 );
}
for ( y = 0; y < i - 1; y++ )
{
f << stu[y].number << " " << stu[y].name << " " << stu[y].sex << " " << stu[y].address << " " << stu[y].phone << endl;
}
f.close();
cout << "是否需要继续删除【1.是 0.否】 "; /* 是否学要继续删除 */
int que;
cin >> que;
if ( que == 1 )
Delete();
}
void Search() /* 定义查找文件 */
{
int y;
ifstream fp( "D://123.txt", ios::in ); /* 打开文件,并把文件中内容赋予student对象数组中。与上同, */
if ( !fp )
{
cout << "文件无法正常打开。\n"; /* */
exit( 1 );
}
student *p, stu[1000]; /* */
int i = 0;
p = new student; /* 动态开辟空间以存储临时信息 */
while ( fp.good() ) /* */
{
fp >> p->number >> p->name >> p->sex >> p->address >> p->phone;
if ( fp.good() )
{
stu[i].number = p->number;
stu[i].name = p->name;
stu[i].sex = p->sex;
stu[i].address = p->address;
stu[i].phone = p->phone;
i++;
}
}
fp.close();
cout << "请选择您需要查找的学生信息的方式:【1.学号 2.姓名】" << endl; /*选择不同的查找方式 */
int n;
cin >> n;
string number;
string name;
if ( n == 1 ) /* 这是以学号为准的查找 */
{
cout << "请输入学生学号:-> ";
cin >> number;
for ( y = 0; y < i; y++ )
if ( stu[y].number == number ) /* 判定成功后,输出学生信息 */
{
stu[y].show_student();
break;
}
}
if ( n == 2 ) /* 此为以学生姓名为准的查找 */
{
cout << "请输入学生姓名;-> ";
cin >> name;
for ( y = 0; y < i; y++ )
if ( stu[y].name == name ) /* 判定成功后,输出学生信息 */
{
stu[y].show_student();
break;
}
}
cout << "请确认信息,是否继续查找【1.是 0.否】";
/* 是否继续查找学生信息 */
int t;
cin >> t;
if ( t == 1 )
Search();
}
void See() /* 定义显示所有学生信息的函数 */
{
cout << "这是全部学生的信息:" << endl;
ifstream fp( "D://123.txt", ios::in ); /* 打开文件 */
if ( !fp ) /* 判断文件是否已打开 */
{
cout << "文件无法正常打开。\n";
exit( 1 );
}
student *p, stu[1000]; /* 定义类指针,数组 */
p = new student; /* 动态开辟空间 */
while ( fp.good() ) /* 读取文件中信息 */
{
fp >> p->number >> p->name >> p->sex >> p->address >> p->phone;
if ( fp.good() )
{
cout << endl;
cout << "学号:" << p->number << endl;
cout << "姓名:" << p->name << endl;
cout << "性别:" << p->sex << endl;
cout << "家庭住址:" << p->address << endl;
cout << "电话号码:" << p->phone << endl;
}
}
fp.close();
cout << "请确认信息:->" << endl; /* 确认信息是否合乎要求 */
cout << "输入【Yes No】";
string ch;
cin >> ch;
if ( ch == "No" )
See();
}
string getPW( char p = '*' )
/* 返回加密的密码(password),需要 include <conio.h> */
{
const int Len = 17; /* 最大密码长度=16 */
char pw[Len] = { 0 };
for( int i = 0; i < Len - 1; i++ )
{
pw[i] = getch();
if ( pw[i] == '\r' )
{
pw[i] = 0;
cout << endl;
return(string( pw ) );
}
else
cout << p;
}
cout << endl;
return string( pw );
}
int main() /* 定义主函数 */
{
cout << "c++课程设计" << endl;
cout << "彩洋威" << endl;
system( "color 0a" );
cout << "\t\t\t学生信息管理系统登录器" << endl;
string name = "110", password( "120" ); /* 账号,密码设置 */
while ( 1 )
{
cout << "\t\t输入账号: "; cin >> name;
cout << "\t\t输入密码: ";
if( !password.compare( getPW() ) ) if( name == "110" ) break;
cout << "\t\t密码或账号不正确,请从新输入正确的信息\n";
cout << "\t\t账号:110 密码:120" << endl;
}
while ( 1 )
{
system( "cls" );
int n;
cout << endl;
cout << "\t\t\t\t学生信息管理系统" << endl; /* 首页显示信息,功能菜单显示 */
cout << endl;
cout << "\t\t\t\t1.添加学生信息" << endl;
cout << "\t\t\t\t2.修改学生信息" << endl;
cout << "\t\t\t\t3.删除学生信息" << endl;
cout << "\t\t\t\t4.查找学生信息" << endl;
cout << "\t\t\t\t5.显示所有信息" << endl;
cout << "\t\t\t\t0.退出系统" << endl;
cout << endl;
cout << endl;
printf( "请选择您需要的功能:<0--5>\n" );
cin >> n; /* 输入所需功能 */
while ( n > 5 || n < 0 )
{
printf( "请正确选择:\n" ); /* 判断选择功能是否合法 */
cin >> n;
}
int q;
switch ( n ) /* 调用不同的功能函数 */
{
case 1: Add(); break;
case 2: Modify(); break;
case 3: Delete(); break;
case 4: Search(); break;
case 5: See(); break;
case 0: q = 0; break;
}
if ( q == 0 )
break;
}
cout << endl;
cout << "谢谢使用" << endl;
cout << "你的支持是我前进的动力!" << endl;
cout << endl;
return(0);
}