多文件组成的c++课程设计学生管理系统(单链表)

来源:互联网 发布:泰格软件 编辑:程序博客网 时间:2024/06/06 07:29

//stu.h

#ifndef STUD_H_INCLUDED
#define STUD_H_INCLUDED
#include<iostream>
using namespace std;
class stud
{
public:
    stud();
    ~stud();
    struct student *p1,*p2,*head;
    char name[15];
    char id[20];
    char cla[50];
    char sex[20];
    double c1;
    double c2;
    double c3;
};
#endif  STUD_H_INCLUDED

//stud.cpp

#include"stud.h"
struct student
{
    char name[15];//名字
    char id[20];//学号
    char cla[50];//班级
    char sex[20];//性别
    double c1;
    double c2;
    double c3;
    struct student *next;
};
stud::stud()
{
    p1=NULL;
    p2=NULL;
    head=NULL;
}
stud::~stud()
{
    while(!head)
    {
        p1=head->next;
        delete head;
        p1=head;
    }
}

//function1.h

#ifndef FUNCTION1_H_INCLUDED
#define FUNCTION1_H_INCLUDED
#include<iostream>
#include"function1.h"
#include"stud.h"
#include<iostream>
#include<iomanip>
#include <conio.h>
#include <fstream>
#include <time.h>
#include<windows.h>
using namespace std;
struct student
{
    char name[15];//名字
    char id[20];//学号
    char cla[50];//班级
    char sex[20];//性别
    double c1;
    double c2;
    double c3;
    struct student *next;
};
class function1:public stud
{
//    student *p1,*p2,*head;
//    char name[15];
//    char id[20];
//    char cla[50];
//    char sex[20];
//    double c1;
//    double c2;
//    double c3;
public:
    void gettime();//得到系统时间的
    void mainmenu(); //主菜单
    void findmenu();//查找菜单
    void clear(); //清空学生
    void inputs(student *p);
    student* input();//插入学生
    void output(student *head);//输出全部学生
    void infile(student *head);//存入文件
    student* del(student *head, char*p);///删除记录/
    void modify(student *head,char *p);//按学号修改学生信息
    void outfile(); //将文件导出
    student* findx(student *head,char *p); // 按学号查找学生信息记录
    student* findn(student *head,char *p,int &n); // 按姓名查找学生信息记录(可查找多个同名学生)
};
#endif  FUNCTION1_H_INCLUDED


//function1.cpp


#ifndef FUNCTION1_CPP_INCLUDED
#define FUNCTION1_CPP_INCLUDED
#include"stud.h"
#include"function1.h"
int n,k;
struct student *head=NULL,*pd=NULL;
void function1::outfile()//将文件导出
{
    p1=new student;//开辟空间
    ifstream fin("d:\student.txt",ios::in);//输入文件
    cout.flags(ios::left); //设置左对齐
    if(!fin)
        cerr<<"Cannot open !"<<endl;
    else
    {
        cout << "-------------------------------------------------------------------------------\n";
        cout << "姓名      性别       班级        学号      英语成绩      语文成绩      数学成绩\n";
        cout << "-------------------------------------------------------------------------------\n";
        while(!fin.eof()) //统计文本文件(fin)中含有多少个字符串(parola)
        {
            fin>>setw(11)>> p1->name>>setw(11)>> p1->sex >> setw(11)>> p1->cla>>setw(12)>>p1->id>>setw(13)>>p1->c1>>setw(14)>>p1->c2>>setw(15)>> p1->c3 ;
            cout <<setw(11)<< p1->name<<setw(11)<< p1->sex << setw(11)<< p1->cla<< setw(12)<<p1->id<<setw(13)<<p1->c1<<setw(14)<<p1->c2<<setw(15)<< p1->c3<<endl;
        }
    }
    system("pause");//当没这个语句或类似语句,你点开生成的.exe文件,运行界面只会闪一下,而有这个语句,就不会了!


    fin.close();//向p1中增加数据
}
void function1::infile(student *head)//存入文件
{
    p1=head;
    ofstream fout("d:\student.txt",ios::app);//创建文件,存放联系人信息//以输入方式打开文件,写的数据添加到文件尾部
    if(!fout)
        cerr<<"Cannot open ! "<<endl;
    else
    {
        while(p1)
        {
            fout <<setw(11)<< p1->name<<setw(11)<< p1->sex << setw(11)<< p1->cla<< setw(12)<<p1->id<<setw(13)<<p1->c1<<setw(14)<<p1->c2<<setw(15)<< p1->c3;
            p1=p1->next;
        }
        cout << "保存成功!"<< endl;
        system("pause");
    }
    fout.close();
}
void function1::modify(student *head,char *p)
{
    findx(head,p);// //按学号修改学生信息
    cout << "1:姓名    2:学号  3:性别  4:班级  5:英语成绩  6:语文成绩  7:数学成绩"<<endl;
    cout << "请输入要修改的项目:"<< endl;
    cin >> k;
    while (k>7&&k<1)
    {
        cout << "输入有误!请重新输入:" << endl;
        cin >> k;
    }
    switch(k)
    {
    case 1:
    {
        cout << "新的姓名:"<< endl;
        cin >> name;
        strcpy(p1->name,name);
    };
    break;
    case 2:
    {
        cout << "新的学号:"<< endl;
        cin>>id;
        strcpy(p1->id,id);
    };
    break;
    case 3:
    {
        cout << "新的性别:"<< endl;
        cin >> sex;
        strcpy(p1->sex,sex);
    };
    break;
    case 4:
    {
        cout << "新的班级:"<< endl;
        cin >> cla;
        strcpy(p1->cla,cla);
    };
    break;
    case 5:
    {
        cout << "新的英语成绩:"<< endl;
        cin >> c1;
        p1->c1=c1;
    };
    break;
    case 6:
    {
        cout << "新的语文成绩:"<< endl;
        cin >> c2;
        p1->c2=c2;
    };
    break;
    case 7:
    {
        cout << "新的数学成绩:"<< endl;
        cin >> c3;
        p1->c3=c3;
    };
    break;
    }
}
student* function1::findx(student *head,char *p)  // 按学号查找学生信息记录
{
    p1=head;
    cout.flags(ios::left);//设置左对齐
    while(strcmp(p1->id,p)&&p1->next!=NULL)
    {
        p1=p1->next;
    }


    if(strcmp(p1->id,p)==0)
    {
        cout << "-------------------------------------------------------------------------------\n";


        cout << "姓名      性别       班级       学号      英语成绩      语文成绩       数学成绩\n";


        cout << "-------------------------------------------------------------------------------\n";
        cout << setw(11)<<p1->name<<setw(11)<< p1->sex << setw(11)<< p1->cla<< setw(12)<<p1->id<<setw(13)<<p1->c1<<setw(14)<<p1->c2<<setw(15)<< p1->c3<<endl;
    }
    else
    {
        cout << "没有此学号的学生!"<< endl;
        system("pause");
        mainmenu();///////
    }
    system("pause");
    //getch();
    return p1;


}
void function1::inputs(student *p)
{
    cout << "请输入:姓名  性别  班级  学号  英语成绩  语文成绩  数学成绩"<<endl;


    cin >> p->name >>p->sex >>p->cla >>p->id >> p->c1 >> p->c2 >> p->c3;


    if(p2!=NULL)   //判断学号是否已存在
    {
        while(strcmp(p2->id,p->id)&&p2->next!=NULL)
        {
            p2=p2->next;
        }
        if(strcmp(p2->id,p->id)==0)
        {
            cout << "学号已存在!请从新输入:" << endl;
            Sleep(900);
            system("cls");
            input();
        }
    }
}
void function1::clear()
{
    while(head)
    {
        p1=head->next;
        delete head;
        head = p1;
    }
}
void function1::output(student *head)
{
    p1=head;
    cout.flags(ios::left);
    cout << "-------------------------------------------------------------------------------\n";
    cout << "姓名      性别       班级        学号       英语成绩     语文成绩      数学成绩\n";
    cout << "-------------------------------------------------------------------------------\n";
    while(p1)
    {


        cout <<setw(11)<< p1->name<<setw(11)<< p1->sex << setw(11)<< p1->cla<< setw(12)<<p1->id<<setw(13)<<p1->c1<<setw(14)<<p1->c2<<setw(15)<< p1->c3<<endl;
        p1=p1->next;
    }
}
student* function1::input()
{


    p2=head;
    p1=new student;
    inputs(p1);
    if(head==NULL)
    {
        head = p1;
        p1->next=NULL;
        return head;
    }
    else
    {
        while(p2->next!=NULL)
        {
            p2=p2->next;
        }
        p2->next=p1;
        p1->next=NULL;
        return head;
    }
}
student* function1::findn(student *head,char *p,int &n)//按姓名查找学生信息记录(可查找多个同名学生)


{
    p1=head;
    cout.flags(ios::left);
    while(strcmp(p1->name,p)&&p1->next!=NULL)
    {
        p1=p1->next;
    }


    if(strcmp(p1->name,p)==0)
    {


        cout <<setw(11)<< p1->name<<setw(11)<< p1->sex << setw(11)<< p1->cla<< setw(12)<<p1->id<<setw(13)<<p1->c1<<setw(14)<<p1->c2<<setw(15)<< p1->c3<<endl;
        n++;
    }
    return p1;
}


student* function1::del(student *head, char*p)//删除记录
{
    p1=head;
    p2=NULL;
    while(strcmp(p1->name,p)&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(!strcmp(p1->name,p))
    {
        if(p1==head)
        {
            head=p1->next;
        }
        else
            p2->next=p1->next;
        cout << "删除成功!"<< endl;
        delete p1;
    }
    else
        cout << "没有要找的姓名是:"<<p<<"的学生"<<endl;
    system("pause");
    return head;
    remove("d:\student.txt");
}
void function1::findmenu()//查找菜单
{
    while(1)
    {
        system("cls");
        cout << "1:按姓名查找学生      2:按学号查找学生        3:显示全部学生      4:返回"<< endl;
        cin >> k;
        switch (k)
        {
        case 1:
        {
            system("cls");
            n=0;
            cout << "请输入要查询的学生姓名:"<< endl;
            cin >> name;
            system("cls");
            cout << "-------------------------------------------------------------------------------\n";
            cout << "姓名      性别       班级      学号       英语成绩      语文成绩       数学成绩\n";
            cout << "-------------------------------------------------------------------------------\n";
            pd=findn(head,name,n);//按姓名查找
            while(pd->next!=NULL)
            {
                pd=findn(pd->next,name,n);
            }
            if(n==0)
                cout << "没有此同学!"<< endl;
            system("pause");
        }
        break;
        case 2:
        {
            system("cls");
            cout << "请输入要查询的学生学号:"<< endl;
            cin >> id;
            system("cls");
            findx(head,id);///按学号查找
        }
        break;
        case 3:
            system("cls");
            output(head);//显示信息
            system("pause");
            break;
        case 4:
            mainmenu();//返回主菜单
            break;
        }
    }
    system("pause");
}
void function1::gettime()//获得系统时间
{
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );//本地时间
    cout<<"当前时间是:";
    cout<< asctime (timeinfo)<<endl;
}
void function1::mainmenu()//主菜单
{
    char n;
    while (1)
    {
        system("cls");//清屏
        system("color 8e");
        cout<<" ==============================================="<<endl;
        cout<<" *         * 学 生 信 息 管 理 系 统 *         *"<<endl;
        cout<<" ==============================================="<<endl;
        cout<<" *        1:添加学生        2:删除学生         *"<<endl;
        cout<<" *                                             *"<<endl;
        cout<<" *        3:查找学生        4:修改学生信息     *"<<endl;
        cout<<" *                                             *"<<endl;
        cout<<" *        5:清空所以数据    6:保存学生信息     *"<<endl;
        cout<<" *                                             *"<<endl;
        cout<<" *        7:输出文件数据    8:安全退出         *"<<endl;
        cout<<" *                                             *"<<endl;
        cout<<" *                                             *"<<endl;
        cout<<" ================================================"<<endl;
        cout<<" 请输入您的选择(0--8):";
        cout<<endl;
        cin >> n;
        // switch (n)
        // {
        // case '1':
        if(n=='1')
        {
            system("cls");
            input();
            cout << "插入成功!"<< endl;
            system("pause");
           // break;
        }
        //case '2':
        else if(n=='2')
        {
            cout << "请输入要删除学生的姓名:"<<endl;
            cin >> name;
            head=del(head,name);
            //break;
        }


        //case '3':
        else if(n=='3')


        {
            findmenu();
           // break;
        }
        //case '4':
        else if(n=='4')
        {
            system("cls");
            cout << "请输入要修改的学生的学号:"<<endl;
            cin >> id;
            modify(head,id);
            cout << "信息修改成功!"<< endl;
            system("pause");
            //break;
        }
        else if(n=='5')
            // case '5':
        {
            clear();
            cout << "已清空!"<< endl;
            Sleep(800);
           // break;
        }
        //case '6
        else if(n=='6')
        {
            infile(head);
            break;
        }
        else if(n=='7')
            // case '7':
        {
            system("cls");
            outfile();
           // break;


        }
        // case '8':
        else if(n=='5')
        {
            exit(0);
            //break;
        }
        else
        {
            cout<<"输入有误,请重新输入"<<endl;
            system("pause");
            //break;
        }
    }
    system("pause");
}
#endif  FUNCTION1_CPP_INCLUDED


//自身感觉写的很烂,,,,,,,,

0 0
原创粉丝点击