#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#define M 16        //M name_length
#define N 12        //N no_length
#define K 3            //K score_length

using namespace std;

class Student
{
    char num[N], name[M];
    int score[K];
    double gpa[K], agpa;
public:
    //there neednt be a constructor
    void setnum(char nu[]);        //set...
    void setname(char na[]);
    void setscore(int sc[]);
    void setgpa(double g[]);
    void setagpa(double ag);
    void computegpa();
    void computeagpa();

    char *getnum();            //get...
    char *getname();
    void getscore(int sc[]);
    void getgpa(double g[]);
    void getagpa(double &ag);

    void disp();        //display...
};

class UserDatabase
{
    int nElem, Maxu;
    Student *user;
public:
    UserDatabase(int n=30);
    ~UserDatabase();
    void add_record(char *nu,char *na, int sc[]);    //add record
    void modify_record(Student *p, int sc[]);        //modify record
    void delete_record(Student *p);                //delete record 不知道书上的第二个参数是干什么用的        
    int query(char *nu);                        //我这里把返回值改成了int,如果存在的话,返回下标,不存在的话返回-1,感觉这样比较简单
    Student *getStudent(int i);            //这个函数是我新加的用来解决上面那个函数修改的问题

    void sorta_num();        //sort by num ascend
    void sorta_name();        //name asc
    void sorta_agpa();        //agpa asc
    void sort_num();        //num descend
    void sort_name();        //name desc
    void sort_agpa();        //agpa desc

    void disp();    //display record
    void clear();    //delete all
};

void Student::setnum(char nu[])
{
    strcpy(num, nu);
}    
void Student::setname(char na[])
{
    strcpy(name, na);
}
void Student::setscore(int sc[])
{
    for (int i=0; i<K; i++)
        score[i] = sc[i];
}
void Student::setgpa(double g[])
{
    for (int i=0; i<K; i++)
        gpa[i] = g[i];
}
void Student::setagpa(double ag)
{
    agpa = ag;
}
void Student::computegpa()
{
    for (int i=0; i<K; i++)
        if (score[i]<60) gpa[i] = 0;
        else gpa[i] = score[i]/10.0-5;
}
void Student::computeagpa()
{
    double sum=0;
    for (int i=0; i<K; i++) sum += gpa[i];
    agpa = sum/K;
}
char *Student::getnum()
{
    return num;
}
char *Student::getname()
{
    return name;
}
void Student::getscore(int sc[])
{
    for (int i=0; i<K; i++)
        sc[i] = score[i];
}
void Student::getgpa(double g[])
{
    for (int i=0; i<K; i++)
        g[i] = gpa[i];
}
void Student::getagpa(double &ag)
{
    ag = agpa;
}
void Student::disp()
{
    int i;
    cout << setw(N-5) << left << num << setw(M-5) << left << name;
    for (i=0; i<K; i++)
        cout << setw(7) << left << score[i];
    for (i=0; i<K; i++)
        cout << setw(8) << left << fixed << setprecision(1) << gpa[i];
    cout << left << agpa << endl;
    cout.unsetf(ios::fixed);
}
UserDatabase::UserDatabase(int n)
{
    char nu[N], na[M];
    int sc[K], i;
    double g[K], ag;

    ifstream infile;
    infile.open("user.txt");
    if    (!infile)
    {
        cout << "user.txt文件不存在" << endl;
        exit(0);
    }
    nElem = 0;
    Maxu = n;
    if (n)
        user = new Student[n];
    else
        user = NULL;
    while (infile >> nu)
    {
        infile >> na;
        for (i=0; i<K; i++)
            infile >> sc[i];
        for (i=0; i<K; i++)
            infile >> g[i];
        infile >> ag;
        add_record(nu, na, sc);
    }
        infile.close();
}
UserDatabase::~UserDatabase()
{
    int i, j, sc[K];
    char nu[N], na[M];
    double g[K], ag;
    ofstream outfile;
    outfile.open("user.txt");
    for (i=0; i<nElem; i++)
    {
        strcpy(nu,user[i].getnum());
        strcpy(na,user[i].getname());
        user[i].getscore(sc);
        user[i].getgpa(g);
        user[i].getagpa(ag);
        outfile << nu << '\t' << na << '\t';
        for (j=0; j<K; j++)
            outfile << sc[j] << '\t';
        for (j=0; j<K; j++)
            outfile << g[j] << '\t';
        outfile << '\t' << ag << endl;
    }
    outfile.close();
}
void UserDatabase::add_record(char *nu,char *na, int sc[])
{
    int j;
    if (nElem == Maxu)
    {
        Student *newptr = new Student[Maxu +10];
        for(int i=0; i<nElem; i++)
            newptr[i] = user[i];
        delete []user;
        Maxu +=10;
        user = newptr;
    }
    for (j=nElem-1; j>=0&&(strcmp(user[j].getnum(),nu)==1); j--)
            user[j+1] = user[j];
    user[j+1].setnum(nu);
    user[j+1].setname(na);
    user[j+1].setscore(sc);
    user[j+1].computegpa();
    user[j+1].computeagpa();
    nElem++;
}
void UserDatabase::modify_record(Student *p, int sc[])
{
    p->setscore(sc);
    p->computegpa();
    p->computeagpa();
}
void UserDatabase::delete_record(Student *p)
{
    for (nElem--; p<user+nElem; p++)
        *p = *(p+1);
}
int UserDatabase::query(char *nu)
{
    for (int i=0; i<nElem; i++)
        if (strcmp(user[i].getnum(),nu)==0)
            return i;
    return -1;
}
Student *UserDatabase::getStudent(int i)
{
    return (user+i);
}
void UserDatabase::sorta_num()
{
    int min;
    Student temp;
    for (int i=0; i<nElem-1; i++)
    {
        min = i;
        for (int j=i+1; j<nElem; j++)
            if(strcmp(user[min].getnum(),user[j].getnum())==1)
                min = j;
        if (min!=i)
        {
            temp = user[min];
            user[min] = user[i];
            user[i] = temp;
        }
    }
}
void UserDatabase::sorta_name()
{
    int i, j;
    Student p;
    for (i=1; i<nElem; i++)
    {
        p = user[i];
        for (j=i-1; j>=0&&(strcmp(user[j].getname(),p.getname())==1); j--)
            user[j+1] = user[j];
        user[j+1] = p;
    }
}
void UserDatabase::sorta_agpa()
{
    double agpa1, agpa2;
    Student temp;
    for (int i=1; i<nElem; i++)
        for (int j=0; j<nElem-i; j++)
        {
            user[j].getagpa(agpa1);
            user[j+1].getagpa(agpa2);
            if (agpa1>agpa2)
            {
                temp = user[j];
                user[j] = user[j+1];
                user[j+1] = temp;
            }
        }    
}
void UserDatabase::sort_num()
{
    int max;
    Student temp;
    for (int i=0; i<nElem-1; i++)
    {
        max = i;
        for (int j=i+1; j<nElem; j++)
            if(strcmp(user[max].getnum(),user[j].getnum())==-1)
                max = j;
        if (max!=i)
        {
            temp = user[max];
            user[max] = user[i];
            user[i] = temp;
        }
    }
}
void UserDatabase::sort_name()
{
    Student temp;
    sorta_name();
    for (int i=0; i<nElem-i-1; i++)
    {
        temp = user[i];
        user[i] = user[nElem-i-1];
        user[nElem-i-1] = temp;
    }
}
void UserDatabase::sort_agpa()
{
    double agpa1, agpa2;
    Student temp;
    for (int i=1; i<nElem; i++)
        for (int j=0; j<nElem-i; j++)
        {
            user[j].getagpa(agpa1);
            user[j+1].getagpa(agpa2);
            if (agpa1<agpa2)
            {
                temp = user[j];
                user[j] = user[j+1];
                user[j+1] = temp;
            }
        }    
}
void show_head(int);        //声明一下这个函数
void UserDatabase::disp()
{
    int i, j;
    for (i=0; i<nElem; i++)
    {
        if (i%10==0)
        {
            if(i) system("pause");
            show_head(6);
            
            cout << setw(N-5) << left << "学号" << setw(M-5) << left << "姓名";
            cout << "成绩1  ";
            cout << "成绩2  ";
            cout << "成绩3  ";
            cout << "绩点1   ";
            cout << "绩点2   ";
            cout << "绩点3  ";
            cout << "平均绩点";
            cout << endl;
        }
        user[i].disp();
    }
        
}
void UserDatabase::clear()
{
    nElem = 0;
}

void show_head(int i)
{
    system("cls");
    cout << "=======================================================================" << endl;
    cout << "                             ";
    switch (i)
    {
        case 1:    cout << "增加记录" << endl;        break;
        case 2: cout << "修改记录" << endl;        break;
        case 3: cout << "删除记录" << endl;        break;
        case 4: cout << "     查找" << endl;        break;
        case 5: cout << "     排序" << endl;        break;
        case 6: cout << "显示记录" << endl;    break;
        case 7: cout << "全删" << endl;        break;
        case 0: cout << "退出" << endl;        break;
        case 998: cout << "学生成绩管理系统" << endl; break;
    }
    cout << "=======================================================================" << endl;
}
int menu_select(bool flag=true)    //传的值是一个bool值,true用来显示主菜单,false 显示排序的菜单
{
    char *menu_main[]={
        "     1  -   增加记录",
        "     2  -   修改记录",
        "     3  -   删除记录",
        "     4  -   查找",
        "     5  -   排序",
        "     6  -   显示记录",
        "     7  -   全删",
        "     0  -   退出                    www.rvfu.me",
        NULL
    };
    char *menu_sort[]={
        "1 - 按学号(升序)",
        "2 - 按姓名(升序)",
        "3 - 按平均绩点(升序)",
        "4 - 按学号(降序)",
        "5 - 按姓名(降序)",
        "6 - 按平均绩点(降序)",
        NULL
    };
    int i, choice;
    do{
        system("cls");
        if (!flag)
            show_head(5);
        else show_head(998);
            if(flag)
                for (i=0; menu_main[i]; i++)
                    cout << "\t\t\t" << menu_main[i] << endl;
            else 
                for (i=0; menu_sort[i]; i++)
                    cout << "\t\t\t" << menu_sort[i] << endl;
                cout << "=======================================================================" << endl;
        cout << "输入选择: ";
        cin >> choice;
    }while(   ( flag&&(choice<0||choice>7) )  ||  ( !flag&&(choice<1||choice>6) )    );
    return choice;
}
void add_record(UserDatabase &usrdb)
{
    char num[N], name[M];
    int score[K];

    cout << "输入学号: ";
    cin >> num;
    if (usrdb.query(num)!=-1)
        cout << endl << "数据重复,插入失败" << endl << endl;
    else
    {
        cout << "输入姓名: ";
        cin >> name;
        cout << "输入成绩: ";
        for (int i=0; i<K; i++)
            cin >> score[i];
        usrdb.add_record(num,name,score);
        cout << endl << "插入成功" << endl << endl;
    }
}
void modify_record(UserDatabase &usrdb)
{
    Student *stuptr;
    char num[N];
    int score[K], i;
    cout << "输入学号: ";
    cin >> num;
    if ((i=usrdb.query(num))==-1)
        cout << endl << "数据不存在" << endl << endl;
    else
    {
        cout << endl << "原始记录: ";
        stuptr = usrdb.getStudent(i);
        stuptr->disp();
        cout << endl << "输入新成绩: ";
        for (int i=0; i<K; i++)
            cin >> score[i];
        usrdb.modify_record(stuptr,score);
        cout << endl << "修改成功" << endl << endl;
    }
}
void delete_record(UserDatabase &usrdb)
{
    Student *stuptr;
    char num[N], flag;
    int i;
    cout << "输入学号: ";
    cin >> num;
    if ((i=usrdb.query(num))==-1)
        cout << endl << "数据不存在" << endl << endl;
    else
    {
        cout << endl << "该条数据为: ";
        stuptr = usrdb.getStudent(i);
        stuptr->disp();
        cout << endl << "确认删除吗? y/n ";
        cin >> flag;
        if (flag=='y'|| flag=='Y')
        {
            usrdb.delete_record(stuptr);
            cout << endl <<"删除了该条数据" << endl << endl;
        }
        else
            cout << endl <<"取消了删除操作" << endl << endl;
    }
}
void query(UserDatabase &usrdb)
{
    Student *stuptr;
    char num[N];
    int i;
    cout << "输入学号: ";
    cin >> num;
    if ((i=usrdb.query(num))==-1)
        cout << endl << "数据不存在" << endl << endl;
    else
    {
        cout << endl << "该条数据为: ";
        stuptr = usrdb.getStudent(i);
        stuptr->disp();
        cout << endl;
    }
}
void sort(UserDatabase &usrdb)
{
    switch (menu_select(false))
    {
        case 1:usrdb.sorta_num();    break;
        case 2:usrdb.sorta_name();    break;
        case 3:usrdb.sorta_agpa();    break;
        case 4:usrdb.sort_num();    break;
        case 5:usrdb.sort_name();    break;
        case 6:usrdb.sort_agpa();    break;
    }
    cout << endl << ":)已经按指定要求排序" << endl << endl;
}
void disp(UserDatabase &usrdb)
{
    usrdb.disp();
}
void clear(UserDatabase &usrdb)
{
    char flag;
    cout << "确认删除吗? y/n ";
    cin >> flag;
    if (flag=='y'|| flag=='Y')
    {
        usrdb.clear();
        cout << endl << "完成操作" << endl << endl;
    }
    else
        cout << endl << "取消了操作" << endl << endl;
}
int main()
{
    UserDatabase usrdb;
    int select;
    char flag;

    while("欢迎大家来关注我的个人公众号瑞辅笔记,我的网站主页是suruifu.com")
    {
        switch(select=menu_select())
        {
            
            case 1: show_head(1);    add_record(usrdb);        system("pause"); break;
            case 2: show_head(2);    modify_record(usrdb);    system("pause"); break;
            case 3: show_head(3);    delete_record(usrdb);    system("pause"); break;
            case 4: show_head(4);    query(usrdb);            system("pause"); break;
            case 5:                    sort(usrdb);            system("pause"); break;
            case 6: show_head(6);    disp(usrdb);            system("pause"); break;
            case 7: show_head(7);    clear(usrdb);            system("pause"); break;
            case 0: show_head(0);    cout << "Are you sure ? y/n : ";
                    cin >> flag;
                    if (flag!='y'&& flag!='Y') continue;
        }
        if (select==0) break;
    }
    return 0;

标签: none

评论已关闭