简易成绩信息管理系统

这周主要写了一个简易版的学生信息管理系统。用了两种办法分别实现,一个是单向链表,一个是vector模版。
下面贴出这两个代码,注意观察两者共同点!
一、单向链表
1. 类声明,h文件

#define STRING
#include<string>
using namespace std;
#endif
class Node
{
    string stu_id;
    string name;
    int math;
    int english;
    double average;
    Node *link;
public:
    Node();
    ~Node();
    Node(const Node &n);
    void operator=(const Node &n);
    friend class grade;
    friend void swap(Node &n1, Node &n2);
};

class grade
{
protected:
    Node *head;
    int amount;
public:
    grade();
    void create();
    void view();
    void add();
    void del();
    void change();
    void inquire();
    void read();
    void write();
    void insert();
    void sort();
};

void init(grade &g);
  1. 类实现,cpp文件
#include<fstream>
#include<cstring>
#include<iostream>
#include"grade.h"
using namespace std;

Node::Node()
{
}

Node::~Node()
{
    if(link != NULL)
    {
        link = NULL;
    }
}

Node::Node(const Node &n)
{
    this->stu_id = n.stu_id;
    this->name = n.name;
    this->math = n.math;
    this->english = n.english;
    this->average = n.average;
    this->link = n.link;
}

void Node::operator=(const Node &n)
{
    this->stu_id = n.stu_id;
    this->name = n.name;
    this->math = n.math;
    this->english = n.english;
    this->average = n.average;
}

grade::grade()
{
    head = NULL;
    amount = 0;
}

void grade::create()
{
    amount=0;
    int n;
    cout<<"请输入需要录入的学生数量:"<<endl;
    cin>>n;
    Node *p,*q;
    p = new Node;
    int i = 1;
    cout<<"以学号,姓名,数学成绩,英语成绩顺序输入"<<endl;
    cout<<"请输入第 "<<i<<" 个记录"<<endl;
    cin>>p->stu_id>>p->name>>p->math>>p->english;
    p->average = (p->math + p->english)/2.0;
    amount++;
    cin.clear();
    head = p;
    q = p;
    while(i < n)
    {
        i++;
        p = new Node;
        cout<<"请输入第 "<<i<<" 个记录"<<endl;
        cin>>p->stu_id>>p->name>>p->math>>p->english;
        p->average = (p->math + p->english)/2.0;
        amount++;
        cin.clear();
        q->link = p;
        q = p;
    }
    q->link = NULL;
}

void grade::view()
{
    if(head == NULL)
    {
        cout<<"没有记录!"<<endl;
        return;
    }
    Node *p = head;
    cout<<"\t学号\t姓名\t数学\t英语\t平均"<<endl;
    while(p)
    {
        cout<<'\t'<<p->stu_id<<'\t'<<p->name<<'\t'<<p->math
            <<'\t'<<p->english<<'\t'<<p->average<<endl;
        p = p->link;
    }
    cout<<"总人数: "<<amount<<endl;
}

void grade::add()
{
    if(head == NULL)
    {
        create();
        return;
    }
    int n;
    cout<<"请输入需要增添的学生数量:"<<endl;
    cin>>n;
    Node *q = head;
    while(q->link)
        q = q->link;
    cout<<"以学号,姓名,数学成绩,英语成绩顺序输入"<<endl;
    int i = 0;
    while(i<n)
    {
        i++;
        Node *p = new Node;
        cout<<"请输入第 "<<i<<" 个需要添加的记录"<<endl;
        p->average = (p->math + p->english)/2.0;
        cin>>p->stu_id>>p->name>>p->math>>p->english;
        amount++;
        q->link = p;
        q = p;
    }
    q->link = NULL;
    return;
}

void grade::del()
{
    if(head == NULL)
    {
        cout<<"\t没有记录!"<<endl;
        return;
    }
    string stu_id;
    cout<<"请输入需要删除的学生学号:"<<endl;
    cin>>stu_id;
    int flag = 0;
    Node *p,*q;
    p = head;
    while(!stu_id.compare(p->stu_id))
    {
        if(p->link == NULL)
        {
            delete p;
            amount--;
            p = NULL;
            head = NULL;
            cout<<"记录已全部删除!"<<endl;
            return;
        }
        q = p->link;
        delete p;
        p = q;
        flag = 1;
        amount--;
    }
    head = p;
    if(head->link == NULL)
    {
        return;
    }
    p = head;
    while(p->link != NULL)
    {
        q = p->link;
        if(!q->stu_id.compare(stu_id))
        {
            if(q->link == NULL)
            {
                delete q;
                amount--;
                p->link = NULL;
                return;
            }
            p->link = q->link;
            delete q;
            flag = 1;
            amount--;
        }
        p = p->link;
    }
    if(flag == 0)
    {
        cout<<"没有你删除的记录!"<<endl;
    }
    return;
}

void grade::change()
{
    if(head == NULL)
    {
        cout<<"\t没有记录!"<<endl;
        return;
    }
    string stu_id;
    cout<<"请输入需要修改的学生学号:"<<endl;
    cin>>stu_id;
    Node *p = head;
    while(p)
    {
        if(!stu_id.compare(p->stu_id))
        {
            string str;
            cout<<"请输入要改动的字段所对应编号。1,学号; 2,姓名; 3,数学成绩; 4,英语成绩;多选以逗号隔开"<<endl;
            cin>>str;
            char re;
            for(unsigned int i = 0; i < str.size(); i++)
            {
                re = str[i];
                if(re == '1')
                {
                    cout<<"请输入新的学号"<<endl;
                    cin>>p->stu_id;
                    continue;
                }
                if(re == '2')
                {
                    cout<<"请输入新的姓名"<<endl;
                    cin>>p->name;
                    continue;
                }
                if(re == '3')
                {
                    cout<<"请输入新的数学成绩"<<endl;
                    cin>>p->math;
                    continue;
                }
                if(re == '4')
                {
                    cout<<"请输入新的英语成绩"<<endl;
                    cin>>p->english;
                    continue;
                }
            }
            p->average = (p->math + p->english)/2.0;
            cout<<"该学生新的记录如下"<<endl;
            cout<<"\t学号\t姓名\t数学\t英语"<<endl;
            cout<<'\t'<<p->stu_id<<'\t'<<p->name<<'\t'<<p->math
                <<'\t'<<p->english<<p->average<<endl;
            break;
        }
        p = p->link;
    }

}

void grade::inquire()
{
    if(head == NULL)
    {
        cout<<"\t没有记录!"<<endl;
        return;
    }
    string stu_id;
    cout<<"请输入需要查询的学生学号:"<<endl;
    cin>>stu_id;
    Node *p = head;
    int flag = 0;
    while(p)
    {
        if(!stu_id.compare(p->stu_id))
        {
            if(flag == 0)
            {
                cout<<"\t学号\t姓名\t数学\t英语"<<endl;
            }
            cout<<'\t'<<p->stu_id<<'\t'<<p->name<<'\t'<<p->math
            <<'\t'<<p->english<<endl;
            flag = 1;
        }
        p = p->link;
    }
    if(flag == 0)
    {
        cout<<"\t没有找到该学生!"<<endl;
    }
}

void grade::read()
{
    string filename;
    cout<<"请输入需要有效的文件名(可指定路径,必须包含后缀名)"<<endl;
    cin >> filename;
    ifstream ins;
    ins.open(filename,ios::binary);
    ins.seekg(0,ios::end);
    long length = long(ins.tellg());
    ins.seekg(0,ios::beg);
    Node *p,*q;
    p = new Node;
    ins.read((char*)p,sizeof(Node));
    amount++;
    if(head == NULL)
    {
        cout << "\t正在重新读取..." << endl;
    }
    else
    {
        cout << "\t正在读取数据..." << endl;
    }
    head = p;
    q = p;
    while(ins.tellg() < length)
    {
        p = new Node;
        ins.read((char*)p,sizeof(Node));
        p->average = (p->math + p->english)/2.0;
        q->link = p;
        q = p;
        amount++;
    }
    q->link = NULL;
    ins.close();
    view();
}

void grade::write()
{
    string filename;
    cout<<"请输入需要有效的文件名(可指定路径,必须包含后缀名)"<<endl;
    cin >> filename;
    ofstream ous;
    ous.open(filename,ios::binary);
    if(head == NULL)
    {
        cout << "\t没有记录!" << endl;
        return;
    }
    cout<<"\t正在写入数据"<<endl;
    Node *p = head;
    while(p)
    {
        ous.write((char*)p,sizeof(Node));
        p = p->link;
    }
    ous.close();
}

void grade::sort()
{
    if(head==NULL)
    {
        cout<<"无数据!"<<endl;
    }
    if(head->link==NULL)
    {
        return;
    }
    Node *p,*q;
    p = head;
    while(p)
    {
        q = p->link;
        while(q)
        {
            if(p->average >= q->average)
            {
                swap(*p,*q);
            }
            q = q->link;
        }
        p = p->link;
    }
    view();
}

void grade::insert()
{
    char c;
    int flag = 0;
    if(head == NULL)
    {
        cout<<"没有记录!\n接下来为你创建记录"<<endl;
        create();
        cout<<"创建完毕,是否继续插入?输入1代表是,否则退出插入。"<<endl;
        cin>>c;
        if(c=='1'){}else{return;}
    }
    cout<<"是否需要在第一个记录之前插入?输入1代表是,否则跳过"<<endl;
    cin>>c;
    Node *p, *q;
    p = head;
    Node *a = new Node;
    if(c=='1')
    {
        cout<<"请输入需要插入的学生记录,按学号,姓名,数学成绩,英语成绩输入"<<endl;
        cin >> a->stu_id >> a->name >> a->math >> a->english;
        a->average = (a->math + a->english)/2.0;
        a->link = p;
        head = a;
        flag = 1;
        cout<<"插入完成!"<<endl;
        return;
    }
    string stu_id;
    cout<<"请输入学生学号,然后在该学生后面插入"<<endl;
    cin >> stu_id;
    cout<<"请输入需要插入的学生记录,按学号,姓名,数学成绩,英语成绩输入"<<endl;
    cin >> a->stu_id >> a->name >> a->math >> a->english;
    a->average = (a->math + a->english)/2.0;
    while(p)
    {
        if(p->link==NULL&&!stu_id.compare(p->stu_id))
        {
            p->link = a;
            a->link = NULL;
            flag = 1;
            return;
        }
        q = p->link;
        if(!stu_id.compare(p->stu_id))
        {
            p->link = a;
            a->link = q;
            cout<<"插入完成!"<<endl;
            flag = 1;
            return;
        }
        p = p->link;
    }
    if(flag == 0)
    {
        cout<<"找不到该学生学号!"<<endl;
    }
}

void swap(Node &n1, Node &n2)
{
    Node temp = n1;
    n1 = n2;
    n2 = temp;
}

void init(grade &g)
{
    char key;
    cout<<"----------------------------------------------"<<endl;
    cout<<"            欢迎进入学生信息管理系统               "<<endl<<endl;
    cout<<"            请输入编号进行相应的操作               "<<endl;
    cout<<"----------------------------------------------"<<endl;
    while(1)
    {
        cout<<"**********************************************"<<endl;
        cout<<"\t1,新建\t2,浏览\t3,保存并退出\t4,打开\n\n\t5,增加\t6,删除\t7,修改\t8,查询\n\n\t9,排序\t0,插入"<<endl;
        cout<<"**********************************************"<<endl;
        cin>>key;
        cin.clear();
        switch(key)
        {
        case '0':
            g.insert(); break;
        case '1':
            g.create();break;
        case '2':
            g.view();break;
        case '3':
            g.write();return;
        case '4':
            g.read();break;
        case '5':
            g.add();break;
        case '6':
            g.del();break;
        case '7':
            g.change();break;
        case '8':
            g.inquire();break;
        case '9':
            g.sort();break;
        default:
            cout<<"无效输入,请重新输入!"<<endl;
        }
    }
}

二、vector实现

  1. 类声明,h文件
#ifndef VECTOR
#define VECTOR
#include<vector>
using namespace std;
#endif

class student
{
    char stu_id[20];
    char name[10];
    char phone_number[15];
    char address[20];
    char sex[3];
    short age;
    float point_math;
    float point_english;
    float point_program;
    double average;
public:
    static void print_field();
    void input();
    void print();
    friend bool operator==(const student &,const student &);
    friend class students;
};

class students
{
    vector<student> mark;
public:
    void create();
    void view();
    void drop();
    void write();
    void read();
    void insert();
    void add();
    void del();
    void update();
    void inquire();
    void sort();

};

void init(students &);
  1. 类定义,cpp文件
#include"student.h"
#ifndef VECTOR
#include<vector>
using namespace std;
#endif
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstring>
#include<string>

void student::print_field()
{
    cout<<setw(14)<<"   学号   "
        <<setw(8)<<" 姓名 "
        <<setw(4)<<"性别"
        <<setw(4)<<"年龄"
        <<setw(14)<<"电话"
        <<setw(14)<<"地址"
        <<setw(6)<<" 数学 "
        <<setw(6)<<" 英语 "
        <<setw(6)<<" 程式 "
        <<setw(10)<<" 平均成绩 "
        <<endl;
}

void student::input()
{
    cin >> this->stu_id >> this->name
        >> this->sex >> this->age
        >> this->phone_number >> this->address
        >> this->point_math >> this->point_english
        >> this->point_program;
    this->average = (this->point_math + this->point_english
                     + this->point_program)/3.0;
}

void student::print()
{
    cout<<setw(14)<<setiosflags(ios::right)<<this->stu_id
        <<setw(8)<<setiosflags(ios::left)<<this->name
        <<setw(4)<<setiosflags(ios::left)<<this->sex
        <<setw(4)<<setiosflags(ios::right)<<this->age
        <<setw(14)<<setiosflags(ios::right)<<this->phone_number
        <<setw(14)<<setiosflags(ios::left)<<this->address
        <<setw(6)<<setiosflags(ios::right)<<this->point_math
        <<setw(6)<<setiosflags(ios::right)<<this->point_english
        <<setw(6)<<setiosflags(ios::right)<<this->point_program
        <<setw(10)<<setiosflags(ios::right)<<this->average
        <<endl;
}

bool operator==(const student &s1,const student &s2)
{
    if(s1.stu_id == s2.stu_id)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void students::create()
{
    int num = 0;
    int i = 0;
    student s;
    cout << "请输入学生数量: ";
    cin >> num;
    cout << "请以以下顺序输入信息(共9个),以空格或回车隔开" << endl;
    cout << "学号, 姓名, 性别, 年龄, 电话, 地址, 数学分, 英语分, 程式分" <<endl;
    while(i < num)
    {
        cout << "请输入第 " << (i+1) << "个学生信息:" << endl;
        s.input();
        mark.push_back(s);
        i++;
    }
    cout << "输入完毕,成功创建 " << num <<"个学生信息记录!" << endl;
}

void students::view()
{
    student::print_field();
    for(auto it = mark.begin(); it != mark.end(); it++)
    {
        it->print();
    }
}

void students::drop()
{
    mark.clear();
}

void students::write()
{
    string filename;
    cout<<"请输入需要有效的文件名(可指定路径,必须包含后缀名)"<<endl;
    cin >> filename;
    ofstream ous;
    ous.open(filename,ios::binary);
    for(auto it = mark.begin(); it!=mark.end(); ++it)
    {
        ous.write((char*)&(*it),sizeof(student));
    }
    ous.close();
    cout<<"保存成功!"<<endl;
}

void students::read()
{
    string filename;
    cout<<"请输入需要有效的文件名(可指定路径,必须包含后缀名)"<<endl;
    cin >> filename;
    ifstream ins;
    ins.open(filename,ios::binary);
    ins.seekg(0,ios::end);
    long len = ins.tellg();
    ins.seekg(0,ios::beg);
    student s;
    mark.clear();
    while(long(ins.tellg()) != len)
    {
        ins.read((char*)&s,sizeof(student));
        mark.push_back(s);
    }
    ins.close();
    cout<<"打开成功,文件包含 "<<mark.size()<<" 个学生信息记录!";
}

void students::insert()
{
    int i = 0;
    student s;
    cout << "请输入要插入的位置: ";
    cin >> i;
    while(i>(mark.size()+1)||i <= 0)
    {
        cout << "输入位置不合法,请重新输入!" << endl;
        cin >> i;
    }
    auto it = mark.begin() + (i-1);
    cout << "请以以下顺序输入信息(共9个),以空格或回车隔开" << endl;
    cout << "学号, 姓名, 性别, 年龄, 电话, 地址, 数学分, 英语分, 程式分" <<endl;
    s.input();
    mark.insert(it,s);
    cout << "插入成功!" <<endl;
}

void students::add()
{
    int num = 0;
    int i = 0;
    student s;
    cout << "请输入增加的学生数量: ";
    cin >> num;
    cout << "请以以下顺序输入信息(共9个),以空格或回车隔开" << endl;
    cout << "学号, 姓名, 性别, 年龄, 电话, 地址, 数学分, 英语分, 程式分" <<endl;
    while(i < num)
    {
        cout << "请输入第 " << (i+1) << "个学生信息:" << endl;
        s.input();
        mark.push_back(s);
        i++;
    }
    cout << "输入完毕,成功增加 " << num <<"个学生信息记录!" << endl;
}

void students::del()
{
    char key[20];
    cout << "请输入要删除的学生学号!" << endl;
    cin >> key;
    for(auto it = mark.begin(); it != mark.end(); it++)
    {
        if(!strcmp(key,it->stu_id))
        {
            mark.erase(it);
            cout << "成功删除!" << endl;
            return;
        }
    }
    cout << "未找到该学号,删除失败!" << endl;
}

void students::update()
{
    char key[20];
    cout << "请输入要更新的学生学号: ";
    cin >> key;
    for(auto it = mark.begin(); it != mark.end(); it++)
    {
        if(!strcmp(key,it->stu_id))
        {
            char str[20];
            char c;
            cout << "请输入需更新的项,多选以逗号隔开" << endl;
            cout << "1,学号,2,姓名,3,性别,4,年龄,5,电话,6,地址,7,数学分,8,英语分,9,程式分,0,全部" <<endl;
            cin >> str;
            for(int i = 0; i < strlen(str); i++)
            {
                c = str[i];
                switch(c)
                {
                case '1':
                    cout << "请输入学号: "; cin>>it->stu_id; break;
                case '2':
                    cout << "请输入姓名: "; cin>>it->name; break;
                case '3':
                    cout << "请输入性别: "; cin>>it->sex; break;
                case '4':
                    cout << "请输入年龄: "; cin>>it->age; break;
                case '5':
                    cout << "请输入电话: "; cin>>it->phone_number; break;
                case '6':
                    cout << "请输入地址: "; cin>>it->address; break;
                case '7':
                    cout << "请输入数学成绩: "; cin>>it->point_math; break;
                case '8':
                    cout << "请输入英语成绩: "; cin>>it->point_english; break;
                case '9':
                    cout << "请输入程式成绩: "; cin>>it->point_program; break;
                case '0':
                    cout << "请以以下顺序输入更新信息(共9个),以空格或回车隔开" << endl;
                    cout << "学号, 姓名, 性别, 年龄, 电话, 地址, 数学分, 英语分, 程式分" <<endl;
                    it->input(); break;
                }
                cout << "更新成功!" << endl;
            }
            return;
        }
    }
    cout << "未找到该学号,更新失败!" << endl;
}

void students::inquire()
{
    char key[20];
    cout << "请输入要查询的学生学号: ";
    cin >> key;
    for(auto it = mark.begin(); it != mark.end(); it++)
    {
        if(!strcmp(key,it->stu_id))
        {
            student::print_field();
            it->print();
            cout << "查询成功!";
            return;
        }
    }
    cout << "未找到该学号,查询失败!" << endl;
}

void students::sort()
{
    student temp;
    for(auto it = mark.begin(); it < mark.end(); it++)
    {
        for(auto is = it+1; is != mark.end(); is++)
        {
            if(it->average > is->average)
            {
                temp = (*it);
                (*it) = (*is);
                (*is) = temp;
            }
        }
    }
    cout << "根据平均成绩排序完成!" << endl;
}

void init(students &s)
{
    char key;
    cout<<"----------------------------------------------"<<endl;
    cout<<"            欢迎进入学生信息管理系统               "<<endl<<endl;
    cout<<"            请输入编号进行相应的操作               "<<endl;
    cout<<"----------------------------------------------"<<endl;
    while(1)
    {
        cout<<"**********************************************"<<endl;
        cout<<"\t1,新建\t2,浏览\t3,保存并退出\t4,打开\n\n\t5,增加\t6,删除\t7,修改\t8,查询\n\n\t9,排序\t0,插入"<<endl;
        cout<<"**********************************************"<<endl;
        cin>>key;
        switch(key)
        {
        case '0':
            s.insert(); break;
        case '1':
            s.create();break;
        case '2':
            s.view();break;
        case '3':
            s.write();return;
        case '4':
            s.read();break;
        case '5':
            s.add();break;
        case '6':
            s.del();break;
        case '7':
            s.update();break;
        case '8':
            s.inquire();break;
        case '9':
            s.sort();break;
        default:
            cout<<"无效输入,请重新输入!"<<endl;
        }
    }
}

别看代码挺长的,其实他们都很容易写出,完。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值