简单的学生管理系统

/*本文件要先建一个file3.dat文件,呵呵,我已经很满意了*/
#include<iostream>
#include<fstream>
#include"string"
#include"conio.h"
using namespace std;
struct Score
{
    int num;
    char name[30];
    char sex;
    float score;
};
class Class:public Score
{
private:
    void AddData();
    void UpdateData();
    void SearchData();
    void DeleteData();
    void AllData();
public:
    Score myth;
    fstream file;
    void Run();
};
void Class::AddData()
{
    file.open("file3.dat",ios::out|ios::binary|ios::in);
    char tag;
    do
    {
        file.seekg(0,ios::end);
        cout<<"请输入学号:"<<endl;
        cin>>myth.num;
        cout<<"请输入姓名:"<<endl;
        cin>>myth.name;
        cout<<"请输入性别:"<<endl;
        cin>>myth.sex;
        cout<<"请输入成绩:"<<endl;
        cin>>myth.score;
        file.write((char*)&myth,sizeof(myth));
        cout<<"继续添加吗(Y/N):"<<endl;
        cin>>tag;
        //tag=tolower(tag);//将大写字母转换为小写字母
        while(tag!='Y'&&tag!='N')
        {
        cout<<"输入非法,重新输入(Y/N):";
        cin>>tag;
        }
    }while(tag=='Y');
    file.close();
}
void Class::UpdateData()
{
    cout<<"请输入要修改的学号:"<<endl;
    cin>>num;
   fstream file("file3.dat",ios::out|ios::binary|ios::in);
   file.seekg(0,ios::beg);
   while(!file.eof())
   {  
       file.read((char*)&myth,sizeof(myth));
       if(num==myth.num)break;
    }
    if(!file.eof())
    {
        cout<<"输出原来的学号:"<<myth.num<<endl;
        cout<<"输出原来的姓名:"<<myth.name<<endl;
        cout<<"输出原来的性别:"<<myth.sex<<endl;
        cout<<"输出原来的成绩:"<<myth.score<<endl;
        cout<<"输入要修改的学号:"<<endl;
        cin>>num;
        myth.num=num;
        cout<<"输入要修改的姓名:"<<endl;
        cin>>name;
        strcpy(myth.name,name);
        cout<<"输入要修改的性别:"<<endl;
        cin>>sex;
        myth.sex=sex;
        cout<<"输入要修改的成绩:"<<endl;
        cin>>score;
        myth.score=score;
        cout<<"输出修改后的学号:"<<myth.num<<endl;
        cout<<"输出修改后的姓名:"<<myth.name<<endl;
        cout<<"输出修改后的性别:"<<myth.sex<<endl;
        cout<<"输出修改后的成绩:"<<myth.score<<endl;
        file.seekg(-sizeof(myth),ios::cur);
        file.write((char*)&myth,sizeof(myth));
    }
    else
    {
    cout<<"对不起,无学号可以修改:"<<endl;
    file.clear();//清除文件标志
    }
    file.close();
}    
void Class::SearchData()
{
    cout<<"请输入要查找的学号:"<<endl;
    cin>>num;
   file.open("file3.dat",ios::out|ios::binary|ios::in);
   file.seekg(0,ios::beg);
   while(!file.eof())
   {  
       file.read((char*)&myth,sizeof(myth));
       if(num==myth.num)break;
    }
    if(!file.eof())
    {
        cout<<"输出学号:"<<myth.num<<endl;
        cout<<"输出姓名:"<<myth.name<<endl;
        cout<<"输出性别:"<<myth.sex<<endl;
        cout<<"输出成绩:"<<myth.score<<endl;
    }
    else
    {
    cout<<"对不起,无此学号:"<<endl;
    file.clear();
    }
    //file.clear();
    file.close();
}
void Class::DeleteData()
{
    cout<<"请输入要删除的学号:"<<endl;
    cin>>num;
   fstream file("file3.dat",ios::out|ios::binary|ios::in);
   file.seekg(0,ios::beg);
   while(!file.eof())
   {  
       file.read((char*)&myth,sizeof(myth));
       if(num==myth.num)break;
   }
   if(!file.eof())
   {
        ofstream outfile("tem.dat",ios::app|ios::binary);
        cout<<"输出要删除的学号:"<<myth.num<<endl;
        cout<<"输出要删除的姓名:"<<myth.name<<endl;
        cout<<"输出要删除的性别:"<<myth.sex<<endl;
        cout<<"输出要删除的成绩:"<<myth.score<<endl;
        file.seekg(0,ios::beg);
        file.read((char*)&myth,sizeof(myth));
        while(!file.eof())
        {
            if(num!=myth.num)
            {
            outfile.write((char*)&myth,sizeof(myth));
            }
            file.read((char*)&myth,sizeof(myth));
        }
        file.close();
        outfile.close();
        remove("file3.dat");//删除文件
        rename("tem.dat","file3.dat");//更改文件名
        file.open("file3.dat",ios::in|ios::out|ios::binary);//重新打开
   }
   else
   {
       cout<<"删除失败!"<<endl;
       file.clear();
   }
   file.clear();
   file.close();
}
void Class::AllData()
{
    file.open("file3.dat",ios::out|ios::binary|ios::in);
    file.seekg(0,ios::beg);
    while(!file.eof())
    {
        if(file.peek()==EOF)//保证无数据时不会输出乱码
            break;
        file.read((char*)&myth,sizeof(myth));
        cout<<"输出学号:"<<myth.num<<endl;
        cout<<"输出姓名:"<<myth.name<<endl;
        cout<<"输出性别:"<<myth.sex<<endl;
        cout<<"输出成绩:"<<myth.score<<endl;
        if(file.peek()==EOF)
            break;
    }
    file.clear();
    file.close();
}
void Class::Run()
{
    int select;
    do
    {
        cout<<"请选择:"<<endl;
        cout<<"1.增加数据"<<endl;
        cout<<"2.修改数据"<<endl;
        cout<<"3.查找数据"<<endl;
        cout<<"4.删除数据"<<endl;
        cout<<"5.全部数据"<<endl;
        cout<<"6.退出:"<<endl;
        cin>>select;
        while(cin.get()!='\n');
        switch(select)
        {
        case 1:
            AddData();
            break;
        case 2:
            UpdateData();
            break;
        case 3:
            SearchData();
            break;
        case 4:
            DeleteData();
            break;
        case 5:
            AllData();
            break;
        }
    }while(select!=6);
}


int main()
{
       int i; 

        printf("                        --------------------------------------------                                \n");
        printf("                        |                                          |                                \n");
        printf("                        |           Welcome to MIS of Student      |                                \n");
        printf("                        |                何青辉          |                                \n");
        printf("                        --------------------------------------------                                \n");

        printf("                         Please input a number(0..9) to continue!");
        scanf("%d",&i);
    system("title 简单学生管理系统-何青辉");
    system("color 84");
    system("cls");
   // system("mode con cols=80 lines=30");
    Class sco;
    sco.Run();
    getch(); 
    printf("谢谢使用,祝您好运,再见!\n");
    return 0;
}
/*不要建file3.dat文件,加强版*/
#include<iostream>
#include<fstream>
#include"string"
#include"conio.h"
using namespace std;
struct Score
{
    int num;
    char name[30];
    char sex;
    float score;
};
class Class:public Score
{
private:
    void AddData();//增加数据
    void UpdateData();//更新数据
    void SearchData();//查找数据
    void DeleteData();//删除数据
    void AllData();//所有数据
public:
    Score myth;
    fstream file;
    void Run();
    Class();
};
Class::Class()
{   
    ifstream infile("file3.dat",ios::in);
    if(!infile)
    {
        ofstream outfile("file3.dat",ios::out);
        outfile.close();        
    }
    else
        infile.close();
    file.open("file3.dat",ios::out|ios::binary|ios::in);
}
void Class::AddData()
{
    char tag;
    do
    {
        file.seekg(0,ios::end);
        cout<<"请输入学号:"<<endl;
        cin>>myth.num;
        cout<<"请输入姓名:"<<endl;
        cin>>myth.name;
        cout<<"请输入性别:"<<endl;
        cin>>myth.sex;
        cout<<"请输入成绩:"<<endl;
        cin>>myth.score;
        file.write((char*)&myth,sizeof(myth));
        cout<<"继续添加吗(Y/N):"<<endl;
        cin>>tag;
        //tag=tolower(tag);//将大写字母转换为小写字母
        while(tag!='Y'&&tag!='N')
        {
        cout<<"输入非法,重新输入(Y/N):";
        cin>>tag;
        }
    }while(tag=='Y');
}
void Class::UpdateData()
{
    cout<<"请输入要修改的学号:"<<endl;
    cin>>num;
   file.seekg(0,ios::beg);
   while(!file.eof())
   {  
       file.read((char*)&myth,sizeof(myth));
       if(num==myth.num)break;
    }
    if(!file.eof())
    {
        cout<<"输出原来的学号:"<<myth.num<<endl;
        cout<<"输出原来的姓名:"<<myth.name<<endl;
        cout<<"输出原来的性别:"<<myth.sex<<endl;
        cout<<"输出原来的成绩:"<<myth.score<<endl;
        cout<<"输入要修改的学号:"<<endl;
        cin>>num;
        myth.num=num;
        cout<<"输入要修改的姓名:"<<endl;
        cin>>name;
        strcpy(myth.name,name);
        cout<<"输入要修改的性别:"<<endl;
        cin>>sex;
        myth.sex=sex;
        cout<<"输入要修改的成绩:"<<endl;
        cin>>score;
        myth.score=score;
        cout<<"输出修改后的学号:"<<myth.num<<endl;
        cout<<"输出修改后的姓名:"<<myth.name<<endl;
        cout<<"输出修改后的性别:"<<myth.sex<<endl;
        cout<<"输出修改后的成绩:"<<myth.score<<endl;
        file.seekg(-sizeof(myth),ios::cur);
        file.write((char*)&myth,sizeof(myth));
    }
    else
    {
    cout<<"对不起,无学号可以修改:"<<endl;
    file.clear();//清除文件标志
    }
}    
void Class::SearchData()
{
    cout<<"请输入要查找的学号:"<<endl;
    cin>>num;
   file.seekg(0,ios::beg);
   while(!file.eof())
   {  
       file.read((char*)&myth,sizeof(myth));
       if(num==myth.num)break;
    }
    if(!file.eof())
    {
        cout<<"输出学号:"<<myth.num<<endl;
        cout<<"输出姓名:"<<myth.name<<endl;
        cout<<"输出性别:"<<myth.sex<<endl;
        cout<<"输出成绩:"<<myth.score<<endl;
    }
    else
    {
    cout<<"对不起,无此学号:"<<endl;
    file.clear();
    }
}
void Class::DeleteData()
{
    cout<<"请输入要删除的学号:"<<endl;
    cin>>num;
   file.seekg(0,ios::beg);
   while(!file.eof())
   {  
       file.read((char*)&myth,sizeof(myth));
       if(num==myth.num)break;
   }
   if(!file.eof())
   {
        ofstream outfile("tem.dat",ios::app|ios::binary);
        cout<<"输出要删除的学号:"<<myth.num<<endl;
        cout<<"输出要删除的姓名:"<<myth.name<<endl;
        cout<<"输出要删除的性别:"<<myth.sex<<endl;
        cout<<"输出要删除的成绩:"<<myth.score<<endl;
        file.seekg(0,ios::beg);
        file.read((char*)&myth,sizeof(myth));
        while(!file.eof())
        {
            if(num!=myth.num)
            {
            outfile.write((char*)&myth,sizeof(myth));
            }
            file.read((char*)&myth,sizeof(myth));
        }
        file.close();
        outfile.close();
        remove("file3.dat");//删除文件
        rename("tem.dat","file3.dat");//更改文件名
        file.open("file3.dat",ios::in|ios::out|ios::binary);//重新打开
   }
   else
   {
       cout<<"删除失败!"<<endl;
       file.clear();
   }
   file.clear();
}
void Class::AllData()
{
    file.seekg(0,ios::beg);
    while(!file.eof())
    {
        if(file.peek()==EOF)//保证无数据时不会输出乱码
            break;
        file.read((char*)&myth,sizeof(myth));
        cout<<"输出学号:"<<myth.num<<endl;
        cout<<"输出姓名:"<<myth.name<<endl;
        cout<<"输出性别:"<<myth.sex<<endl;
        cout<<"输出成绩:"<<myth.score<<endl;
        if(file.peek()==EOF)
            break;
    }
    file.clear();
}
void Class::Run()
{
    int select;
    do
    {
        cout<<"请选择:"<<endl;
        cout<<"1.增加数据"<<endl;
        cout<<"2.修改数据"<<endl;
        cout<<"3.查找数据"<<endl;
        cout<<"4.删除数据"<<endl;
        cout<<"5.全部数据"<<endl;
        cout<<"6.退出:"<<endl;
        cin>>select;
        while(cin.get()!='\n');
        switch(select)
        {
        case 1:
            AddData();
            break;
        case 2:
            UpdateData();
            break;
        case 3:
            SearchData();
            break;
        case 4:
            DeleteData();
            break;
        case 5:
            AllData();
            break;
        }
    }while(select!=6);
}
int main()
{
       int i;
        printf("                        --------------------------------------------                                \n");
        printf("                        |                                          |                                \n");
        printf("                        |           Welcome to MIS of Student      |                                \n");
        printf("                        |                       myth               |                                \n");
        printf("                        --------------------------------------------                                \n");

        printf("                         Please input a number(0..9) to continue!");
        scanf("%d",&i);
    system("title 简单学生管理系统-myth");
    system("color 84");
    system("cls");
    //system("mode con cols=80 lines=30&color 9f");
    Class sco;
    sco.Run();
    getch(); 
    printf("谢谢使用,祝您好运,再见!\n");
    return 0;
}

 

posted @ 2012-09-16 20:21  myth_HG  阅读(335)  评论(0编辑  收藏  举报