您的位置:首页 > 编程语言 > C语言/C++

c++等级考试管理系统

2015-07-10 18:29 495 查看
学习了一段时间了,学校要求的课设,就用链表写了这个,其中有些不足,欢迎大家批评指正

//等级考试报名系统

#include<iostream>

#include<windows.h>//引用一些系统函数

#include<stdlib.h>

#include<fstream>//定义文件

#include<iomanip>//输入输出格式

#include<time.h>//引用时间头文件

using namespace std;

int Person=0;//用来记录总人数  

class R_Examination{    

    public:

        R_Examination(){};

        ~R_Examination(){};

        char Name[10];

        int Num;

        int IDcard;

        char Grade;

        void Tongji(R_Examination*head);

        void Find_IDcard(R_Examination*head,int num);

        void Find_Num(R_Examination*head,int num);

        void Find_Grade(R_Examination*head,char num);

        R_Examination*creat();

        void Show(R_Examination *head);

        void Find_Name(R_Examination*head,char s[]);

        void Insert(R_Examination*head,int num,char *name,int idcard,char grade);

        void Delete(R_Examination*head,int num);

        void Derive(R_Examination*head);        

        R_Examination * next;

};

R_Examination* head=NULL;//定义全局头指针

R_Examination*R_Examination::creat()//该函数用于创建链表

{

    R_Examination *p1,*p2;//声明 R_Examination型指针变量

    p1=new R_Examination;//利用new运算符开辟一段新的内存空间

    head=p1;//令头指针指向p1

    p2=p1;//令p2=p1

    cout<<"请输入学生学号,输入0结束"<<endl;

    cin>>p1->Num;//输入学生学号

    if(p1->Num!=0)//判断输入是否为0,若不为0,则执行代码块中的语句

    {

        cout<<"请输入学生姓名"<<endl;

        cin>>p1->Name;//输入学生姓名

        cout<<"请输入学生身份证号"<<endl;

        cin>>p1->IDcard;//输入学生身份证号

        cout<<"请输入学生报考等级"<<endl;

        cin>>p1->Grade; //输入学生报考等级,分为A,B

        Person++;

    }

    else//如果输入的学号为0

    {

        delete p1;//删除p1

        head=NULL;//将头指针赋空

        p2=NULL;//将p赋空

        return head;//返回头指针

    }

    while(p1->Num!=0)

    {

        p2=p1;//令p2指向p1

        p1=new R_Examination;//

        cout<<"请输入学生学号,输入0结束"<<endl;

        cin>>p1->Num;

        if(p1->Num!=0)

        {

            cout<<"请输入学生姓名"<<endl;

            cin>>p1->Name;

            cout<<"请输入学生身份证号"<<endl;

            cin>>p1->IDcard;

            cout<<"请输入学生报考等级"<<endl;

            cin>>p1->Grade;

        }

        p2->next=p1;//使旧指针的next指向下一个节点

        Person++;        

    }

    delete p1;//删除p1

    p2->next=NULL;//将最后一个节点的next赋空

    return head;//返回头指针

}

void R_Examination::Tongji(R_Examination*head)//统计总人数

{

    int num=0,k=0,m=0;

    while(head)

    {

        num++;

        if(head->Grade=='A')k++;

        if(head->Grade=='B')m++;

        head=head->next;

    }

    cout<<"一共有:"<<num<<"人参加等级考试"<<endl;

    cout<<"其中A等级的有"<<k<<"人"<<endl;

    cout<<"其中B等级的有"<<m<<"人"<<endl;

    return;

}

void R_Examination::Find_IDcard(R_Examination*head,int num)//根据身份证号查找学生

{

    cout<<setw(20)<<"学号"; //setw()控制输出格式,20个字宽

    cout<<setw(20)<<"姓名";

    cout<<setw(20)<<"身份证号";

    cout<<setw(20)<<"报考等级"<<endl;

    int n=0,k=0;

    while(head){//当头指针不为空的情况下

        n++;

        if(head->IDcard==num){

            cout<<"该考生在"<<n<<"号,信息如下:"<<endl;

            cout<<setw(20)<<head->Num;

            cout<<setw(20)<<head->Name;

            cout<<setw(20)<<head->IDcard;

            cout<<setw(20)<<head->Grade<<endl;

            k++;

            break;

        }

        head=head->next;//指向下一个节点

    }

    if(k!=0){

        cout<<"操作成功"<<endl;

    }

    else{

        cout<<"未找到你要查询的学生"<<endl;

    }

}

void R_Examination::Find_Num(R_Examination*head,int num)//根据学好查找学生

{

    int n=0,k=0;

    cout<<setw(20)<<"学号";

    cout<<setw(20)<<"姓名";

    cout<<setw(20)<<"身份证号";

    cout<<setw(20)<<"报考等级"<<endl;

    while(head){

        n++;

        if(head->Num==num){

            cout<<"该考生在"<<n<<"号,信息如下:"<<endl;

            cout<<setw(20)<<head->Num;

            cout<<setw(20)<<head->Name;

            cout<<setw(20)<<head->IDcard;

            cout<<setw(20)<<head->Grade<<endl;

            k++;

            break;

        }

        head=head->next;

    }

    if(k!=0){

        cout<<"操作成功"<<endl;

    }

    else{

        cout<<"未找到你要查询的学生"<<endl;

    }

}

void R_Examination::Find_Grade(R_Examination*head,char num)//根据等级查找学生

{

    int n=0;

    if(num=='A')

    cout<<"报考A等级的学生如下:"<<endl;

    else if(num=='B')

    cout<<"报考B等级的学生如下:"<<endl;

    else{

        cout<<"你输入的等级有误,请重新输入"<<endl;

        return;

    }    

    while(head){

        if(head->Grade==num){

            cout<<"学号:"<<head->Num<<endl;

            cout<<"姓名:"<<head->Name<<endl;

            cout<<"身份证号:"<<head->IDcard<<endl;

            cout<<"报考等级:"<<head->Grade<<endl;

        }

        head=head->next;

    }

}

void R_Examination::Find_Name(R_Examination*head,char s[])//根据姓名查找学
4000


{

    int k=0;

    cout<<setw(20)<<"学号";

    cout<<setw(20)<<"姓名";

    cout<<setw(20)<<"身份证号";

    cout<<setw(20)<<"报考等级"<<endl;

    while(head)

    {

        if(strcmp(head->Name,s)==0)

        {

            cout<<"找到该学生,信息如下:"<<endl;

            cout<<setw(20)<<head->Num;

            cout<<setw(20)<<head->Name;

            cout<<setw(20)<<head->IDcard;

            cout<<setw(20)<<head->Grade<<endl;

            k++;

            break;

        }

        head=head->next;

    }

    if(k!=0)

    cout<<"操作成功"<<endl;

    else

    cout<<"未找到该学生!"<<endl;

}

void R_Examination::Show(R_Examination *head)//学生的显示

{

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED |FOREGROUND_INTENSITY);//设置控制台字体颜色

    cout<<setw(20)<<"学号";

    cout<<setw(20)<<"姓名";

    cout<<setw(20)<<"身份证号";

    cout<<setw(20)<<"报考等级"<<endl;

    cout<<"------------------------------------------------------------------------------------------"<<endl;

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN |FOREGROUND_INTENSITY);

    while(head)

    {

        cout<<setw(20)<<head->Num;

        cout<<setw(20)<<head->Name;

        cout<<setw(20)<<head->IDcard;

        cout<<setw(20)<<head->Grade<<endl;

        cout<<"------------------------------------------------------------------------------------------"<<endl;

        head=head->next;

    }

}

void R_Examination::Insert(R_Examination*head,int num,char *name,int idcard,char grade)//学生插入

{

    R_Examination*pause=new R_Examination;

    //将学要插入的数据赋给新的指针

    pause->Num=num;

    strcpy(pause->Name,name);

    pause->IDcard=idcard;

    pause->Grade=grade;

    //如果要插入的学号比第一个学号小,就在此插入

    if(num<head->Num)

    {

        pause->next=head;//以新节点作为头节点,

        ::head=pause;//将头指针变为插入的节点

        Person++;

        return;

    }

    //以上条件不满足

    R_Examination *temp=NULL;

    while((num>head->Num)&&(head->next!=NULL))//寻找满足插入的地方

    {

        temp=head;

        head=head->next;

    }

    //判断进行插入

    if(num>head->Num)

    {

        head->next=pause;

        pause->next=NULL;

        Person++;

    }

    else

    {

        temp->next=pause;

        pause->next=head;

        Person++;

    }

}

void R_Examination::Delete(R_Examination*head,int num)//学生的删除

{

    R_Examination *temp;

    if(head->Num==num)

    {

        temp=head;

        head=head->next;

        ::head=head;

        delete temp;

        cout<<"操作成功"<<endl;

        Person--;

        return;

    }

    while(head)

    {

        if(head->next==NULL)

        {

            cout<<"找不到该同学"<<endl;

            return;

        }

        if(head->next->Num==num)

        {

            temp=head->next;

            head->next=temp->next;

            delete temp;

            cout<<"操作成功"<<endl;

            Person--;

            return;

        }

        head=head->next;

    }

}

void R_Examination::Derive(R_Examination*head)//导出到文件

{

    int lastTime = 1;

    int i=0;     

    char name[30];

    cout<<"请输入你要保存的文件名"<<endl;

    cin>>name;

    fstream file(name,ios::in|ios::out|ios::trunc);//定义文件,打开的同时清空数据

    if(!file.fail())//如果打开成功

    {

        /*file<<"等级考试报名资料库"<<endl;

        file<<setw(20)<<"学号";

        file<<setw(20)<<"姓名";

        file<<setw(20)<<"身份证号";

        file<<setw(20)<<"报考等级"<<endl; */

        while(head)

        {

            int now=clock();

    

            if( now - lastTime >=0 )

            {        

             lastTime = now;

             cout<<"正在写入....";

            }

            file<<setw(20)<<head->Num;//写入文件

            file<<setw(20)<<head->Name;

            file<<setw(20)<<head->IDcard;

            file<<setw(20)<<head->Grade<<endl;

            head=head->next;

            i++;

        }

    }

    else

    cout<<"文件打开错误"<<endl;

}

R_Examination*WRITE_FROM_FILE()//从文件读入

{

        char name[30];

        cout<<"请输入你要打开的文件名"<<endl;

        cin>>name;

        fstream fin;   //fin an ifstream object

        char Name[10];

        int Num;

        int IDcard;

        char Grade;

        R_Examination *p3,*p4;

        ::head=p3=new R_Examination;

        fin.open(name);

        if(!fin)//如果打开失败

        {

            cerr<<"open error!"<<endl;

            abort();//异常终止一个进程

        }

        if(!fin.eof())

        {

            //依次从文件读去学号,姓名,身份证号,报考等级

            fin>>Num;

            fin>>Name;

            fin>>IDcard;

            fin>>Grade;

            //将读入的数据赋给指针

            p3->Num=Num;        

            strcpy(p3->Name,Name);    

            p3->IDcard=IDcard;

            p3->Grade=Grade;

            Person++;

        }

        //依次读入

        while(!fin.eof())

        {

            p4=p3;

            p3=new R_Examination;    

            fin>>Num;

            fin>>Name;

            fin>>IDcard;

            fin>>Grade;

            p3->Num=Num;

            strcpy(p3->Name,Name);                

            p3->IDcard=IDcard;

            p3->Grade=Grade;    

            p4->next=p3;

            Person++;            

        }

        delete p3;

        p4->next=NULL;

        return head;

}

void BLUE(){

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE |FOREGROUND_INTENSITY);

}

void GREEN(){

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN |FOREGROUND_INTENSITY);

}

void RED(){

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN |FOREGROUND_INTENSITY|COMMON_LVB_REVERSE_VIDEO );

}

void Display(){

    RED();

    system("Date/T");//显示系统日期

    system("TIME/T");//显示系统时间

    GREEN();

    system("title 等级考试报名系统");//输出程序名称

}

void Menu()//打印菜单

{

    GREEN();

    cout<<"■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"<<endl;   

    cout<<" |﹊﹊﹊﹊﹊﹊﹊﹊﹊﹊﹊﹊﹊|"<<endl;

    cout<<"   欢迎使用等级考试报名系统  "<<endl;

    cout<<" |﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍|"<<endl;

    GREEN();

    cout<<"■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(20)<<"1.添加信息"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(20)<<"2.删除信息"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(20)<<"3.插入信息"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(20)<<"4.显示信息"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(20)<<"5.信息查询"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(20)<<"6.信息统计"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(20)<<"7.信息导出"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(16)<<"8.设置"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(22)<<"9.从文件打开"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<setw(20)<<"Q.退出程序"<<endl;

    BLUE();

    cout<<" |                          |"<<endl;

    GREEN();

    cout<<"■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"<<endl;

    BLUE();

    

}

void Error()//如果链表为空,则执行该函数

{

            cout<<"你还未录入学生,请先录入"<<endl;

            cout<<"按回车键返回主程序"<<endl;

            cin.get();

            cin.get();

            system("cls");//清屏函数

}

void Clean_S()//清屏函数

{

    cin.get();

    cin.get();

    system("cls");

}

int main(void){

    R_Examination data;//定义对象

    Display(); //输出菜单

    char str[10];

    int num,m=3;

    string password;//定义系统密码

    string login;//定义用户登陆密码

    Fail://如果密码输入错误,调到此处

    GREEN();

    cout<<"■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"<<endl;

    BLUE();

    cout<<"       等级考试报名系统  "<<endl;

    GREEN();

    cout<<"■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"<<endl;   

    cout<<"请输入密码!默认密码为123456"<<endl;

    cin>>login;//用户自定义输入密码

    fstream file("F:\\password.txt",ios::in|ios::out);//打开密码存放的文件

    while(!file.eof())

    {

        getline(file,password); //读取密码

    }

    if(password==login) //判断用户输入密码和系统密码是否相同

    {

    system("cls");

    cout<<"登陆成功"<<endl;

    file.close();//关闭文件

    }

    else//如果密码输入错误

    {

        m--;

        cout<<"密码错误!!!你还有"<<m<<"次机会"<<endl;

        if(m==0)

        {

            cout<<"你输入密码错误已达上限"<<endl;//如果输错三次,退出程序

            exit(0);

        }

        goto Fail;         

    }

    begin:

    Menu();//输入菜单

    GREEN();

    cout<<"请输入进行操作"<<endl;

    cin>>str;    

    if(str[0]=='1')

    {

        ::head=data.creat();

        cout<<"创建成功,按回车键返回菜单"<<endl;

        Clean_S();

        goto begin;

    }

    else if(str[0]=='2')

    {

        if(head==NULL)

        {

            Error();

            goto begin;

        }

        cout<<"请输入你要删除的学生学号"<<endl;

        cin>>num;

        data.Delete(head,num);

        cout<<"操作成功"<<endl;

        cout<<"按回车键返回主程序"<<endl;

        Clean_S();

        goto begin;

    }

    else if(str[0]=='4')

    {

        if(head==NULL)

        {

            Error();

            goto begin;

        }

        data.Show(head);

        cout<<"操作成功,按回车键返回菜单"<<endl;

        Clean_S();

        goto begin;

    }

    else if(str[0]=='5')

    {

        char s[10];

        char s1;

        int num;

        cout<<"*********************"<<endl;

        cout<<"1.学号查询"<<endl;

        cout<<"2.身份证查询"<<endl;

        cout<<"3.等级查询"<<endl;

        cout<<"4.姓名查询"<<endl;

        cout<<"*********************"<<endl;

        cout<<"请选择操作"<<endl;

        cin>>s;

        if(s[0]=='1'){

            if(head==NULL)

            {

            Error();

            goto begin;

            }

            cout<<"请输入你要查询的学生学号"<<endl;

            cin>>num;

            data.Find_Num(head,num);

            cout<<"按回车键返回主程序"<<endl;

            Clean_S();

            goto begin;    

        }

        else if(s[0]=='2'){            

            if(head==NULL)

            {

            Error();

            goto begin;

            }

            cout<<"请输入你要查询的学生身份证号"<<endl;

            cin>>num;

            data.Find_IDcard(head,num);

            cout<<"按回车键返回主程序"<<endl;

            Clean_S();

            goto begin;    

        }

        else if(s[0]=='3'){

            if(head==NULL)

            {

            Error();

            goto begin;

            }

            cout<<"请输入你要查询的等级"<<endl;

            cin>>s1;

            data.Find_Grade(head,s1);

            cout<<"按回车键返回主程序"<<endl;

            Clean_S();

            goto begin;    

        }

        else if(s[0]=='4'){

            if(head==NULL)

            {

            Error();

            goto begin;

            }

            cout<<"请输入你要查询的学生姓名"<<endl;

            cin>>s;

            data.Find_Name(head,s);

            cout<<"按回车键返回主程序"<<endl;

            Clean_S();

            goto begin;    

        }

    }

    else if(str[0]=='6')

    {

        data.Tongji(head);

        cout<<"操作成功,按回车键返回菜单"<<endl;

        Clean_S();

        goto begin;

    }

    else if(str[0]=='3')

    {

        int num,idcard;

        char name[10];

        char grade;

        if(head==NULL)

        {

            Error();

            goto begin;

        }

        cout<<"请输入你要插入的学生信息:"<<endl;

        cout<<"学号:"<<endl;

        cin>>num;

        cout<<"姓名:"<<endl;

        cin>>name;

        cout<<"身份证号:"<<endl;

        cin>>idcard;

        cout<<"报考等级:"<<endl;

        cin>>grade;

        data.Insert(head,num,name,idcard,grade);

        cout<<"操作成功,按回车键返回菜单"<<endl;

        Clean_S();

        goto begin;

    }

    else if(str[0]=='7')

    {

        if(head==NULL)

        {

            Error();

            goto begin;

        }

        cout<<"文件导出开始执行"<<endl;

        data.Derive(head);

        cout<<"操作成功,按回车键返回菜单"<<endl;

        //Clean_S();

        goto begin;

    }

    else if(str[0]=='8')

    {

        char s1;

        cout<<"1.设置大小"<<endl;

        cout<<"2.设置密码"<<endl;

        cout<<"请选择"<<endl;

        cin>>s1;

        if(s1=='1')

        {

            char s;

        cout<<setw(20)<<"请选择"<<endl;

        cout<<setw(18)<<"1.小"<<endl;

        cout<<setw(18)<<"2.大"<<endl;

        cout<<"请输入选择"<<endl;

        cin>>s;

        if(s=='1')

        {

            system("mode con: cols=50 lines=50");//调用系统函数设置命令行窗口的大小

            Error();

            goto begin;

        }

        if(s=='2')

        {

            system("mode con: cols=150 lines=150");

            Error();

            goto begin;

        }

        }

        if(s1=='2')

        {

            string oldpassword;

            cout<<"请输入旧密码";

            cin>>oldpassword;

            if(oldpassword==password)

            {

                cout<<"请输入新密码"<<endl;

                cin>>password;

                fstream fin("F:\\password.txt",ios::in|ios::out|ios::trunc);//将新密码保存到密码文件中去

                if(!fin.eof())

                fin<<password;

                fin.close();

                goto Fail;

            }

            else

            cout<<"旧密码错误!!!请重新登录"<<endl;

            system("cls");

            goto Fail;

        }

    }

    else if(str[0]=='9')

    {

        ::head=WRITE_FROM_FILE();

        cout<<"操作成功,按回车键返回菜单"<<endl;

        Clean_S();

        goto begin;

    }

    else

    {

        if(str[0]!='q'&&str[0]!='Q')

        {

            cout<<"操作错误"<<endl;

            cout<<"按回车键返回主程序"<<endl;

            Clean_S();

            goto begin;

        }        

    }

    if(str[0]=='q'||str[0]=='Q')//按q和Q离开程序

    exit(0);        

    

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表 c c++ 文件