C++类再写一遍学生查分系统

学生类的.H文件: 学生属性声明

#ifndef __STUDENT_H_
#define __STUDENT_H_
#include<string>
using namespace std;

class Student
{
public:
    string name, ID;
    int score[5];
    Student *next;
};
#endif

链表类的.h文件: 通过操作学生对象实现链表的创建,增加,查找,修改,排序,退出。面向学生对象编程

#ifndef __LIST_H_
#define __LIST_H
#include<string>
#include"Student.h"

class List
{
public:
    List();                             //生成
    void print_list();                  //打印
    void add_list_node();               //增
    void remove_list_node(Student *p);  //删
    void seek_list_node();              //查
    void revise_list_node(Student *p);  //改
    void list_sort();                   //排序
    void  loading();                    //登陆
    void quit();                        //退出
private:
    Student *head,*stu[5];
    string
        acount = "1",
        password = "1",
        input_a,
        input_p;
    int 
    count = 0,
    chance = 5;
};
#endif 

链表类的.cpp文件:将操作链表的方法实现

# include "List.h"
#include<iostream>
#include<windows.h>
#include <iomanip>
using namespace std;


string
name[] = { "张三", "李四", "王五", "赵六", "田七" },
ID[] = { "1001", "1002", "1003", "1004", "1005" },
subject[] = { "姓名:", "学号:", "语文:", "数学:", "英语:", "理综:","总分:" };


//链表初始化
List::List()
{
    int i;      
    for (i = 0; i < 5; i++)
    {
        this->stu[i] = new Student;
        this->stu[i]->name = name[i];
        this->stu[i]->ID = ID[i];
        this->stu[i]->score[0] = rand() % 100 + 51;
        this->stu[i]->score[1] = rand() % 100 + 51;
        this->stu[i]->score[2] = rand() % 100 + 51;
        this->stu[i]->score[3] = rand() % 250 + 51;
        this->stu[i]->score[4] = this->stu[i]->score[0] + this->stu[i]->score[1] + this->stu[i]->score[2] + this->stu[i]->score[3];
        if (i>0)
        {
            this->stu[i - 1]->next = this->stu[i];
            this->stu[i]->next = NULL;
        }
        (this->count)++;
    }
    this->head = this->stu[0];
    this->print_list();
}


//登陆
void List::loading()
{
    while (this->count > 0)
    {
        cout << "账号: ";
        cin >> this->input_a;
        cout << "密码: ";
        cin >> this->input_p;
        if (this->input_a != this->acount || this->input_p != this->password)
        {
            (this->chance)--;
            if (this->chance == 0)
            {
                cout << "您已连续5次输错!!!" << endl;
                this->quit();
                break;
            }
            cout << "您输入的账号或密码有误,请重新输入! 剩余次数 :" << this->count << endl;
        }
        else
        {
            system("color 5e");
            cout << "\t\t登陆成功,欢迎使用!!!" << endl;
            break;
        }
    }
}

//增加
void List::add_list_node()
{
        Student *temp, *p = new Student;
        int i;
        for(i = 0; i < 6; i++)
        {
            cout << subject[i];
            if (i == 0)
            {
                cin >> p->name;
            }
            else if (i == 1)
            {
                cin >> p->ID;
            }
            else{
                cin >> p->score[i-2];
            }
            getchar();
        }
        p->score[4] = p->score[0] + p->score[1] + p->score[2] + p->score[3];
        if (this->head != NULL)
        {
            temp = this->head;
            this->head = p;
            p->next = temp;
        }
        else{
            head = p;
            p->next = NULL;
        }
        this->count++;
        cout << "添加成功" << endl;
        this->print_list();
}


//查找
void List::seek_list_node()
{
    Student *temp = this->head;
    string input;
    int choose;
    cout << "\n\n请输入您要查找的学生姓名或学号:";
    cin >> input; getchar();
    while (temp != NULL)
    {
        if (temp->ID == input || temp->name == input)
        {
            cout << "\n\t\t\t查找到学生信息如下:\n" << endl;
            printf("\n姓名\t学号\t语文\t数学\t英语\t理综\t总分\n");   
            cout << setw(8) << temp->name << setw(8) << temp->ID << setw(8) << temp->score[0] << setw(8) << temp->score[1] << setw(8) << temp->score[2] << setw(8) << temp->score[3] << setw(8) << temp->score[4] << endl;
            cout<<"\n\t1,修改 2,删除\n";
            cin >> choose;getchar();
            (choose - 1) ? remove_list_node(temp) : revise_list_node(temp);
            return;
        }
        temp = temp->next;
    }
    cout<<"\n找不到"<<endl;
}


//删除
void List::remove_list_node(Student *p)
{
    Student *p1, *p2;
    if (!count)
    {
        printf("\n当前人数为0,无法进行删除,修改等操作!");
        return;
    }
    if (this->head == p)
    {
        p1 = this->head;
        this->head = this->head->next;
        delete(p1);
    }
    else
    {
        p1 = this->head;
        while (this->head != NULL)
        {
            if (head->next == p)
            {
                p2 = this->head->next->next;
                delete(this->head->next);
                this->head->next = p2;
                break;
            }
            this->head = this->head->next;
        }
        this->head = p1;
    }
    this->count--;
    cout << "删除成功" << endl;
    this->print_list();
}

//修改
void List::revise_list_node(Student *p)
{
    int choose;
    P1:
    cout << "\n请选择修改项:1,姓名\t2,学号\t3,语文\t4,数学\t5,英语\t6,理综" << endl;
    cin >> choose; getchar();
    switch (choose)
    {
        cout << subject[choose - 1];
    case 1: 
        cin >> p->name;
        break;
    case 2:
        cin >> p->ID;
        break;
    case 3:
    case 4:
    case 5:
    case 6:
        cin >> p->score[choose - 3];
        break;
    default:
        cout << "输入有误,请重新输入!!" << endl;
        goto P1;
    }
    p->score[4] = p->score[0] + p->score[1] + p->score[2] + p->score[3];
    cout << "修改成功!!" << endl;
    this->print_list();
}


//排序
void List::list_sort()
{
    Student *temp = new Student, *temp1, *temp2, *start, *ps = this->head;
    int choose,n1,n2;
    P1:
    cout << "1,语文排名\t2,数学排名\t3,英语排名\t4,理综排名\t5,总分排名" << endl;
    cin >> choose; getchar();
    while (head != NULL)
    {
        start = head;
        while (start->next != NULL)
        {
            if (choose > 5 || choose < 1)
            {
                cout << "输入有误,请重新输入!!" << endl;
                goto P1;
            }
            else
            {
                n1 = head->score[choose - 1];
                n2 = start->next->score[choose - 1];
            }
            if (n1 < n2)
            {
                temp1 = head->next;
                temp2 = start->next->next;  //保存指向

                *temp = *(start->next);
                *(start->next) = *(this->head);   //交换对象值
                *(this->head) = *temp;

                this->head->next = temp1;
                start->next->next = temp2;   //恢复指向
            }
            start = start->next;
        }
        this->head = this->head->next;
    }
    this->head = ps;
    this->print_list();
}


//打印
void List::print_list()
{
    Student *pp = this->head;
    printf("\n姓名\t学号\t语文\t数学\t英语\t理综\t总分\n");
    while (pp != NULL)
    {
        cout.setf(ios::left); //左对齐
        cout << setw(8) << pp->name << setw(8) << pp->ID << setw(8) << pp->score[0] << setw(8) << pp->score[1] << setw(8) << pp->score[2] << setw(8) << pp->score[3] << setw(8) << pp->score[4] << endl;
        pp = pp->next;
    }
}


//退出
void List::quit()
{
    system("color 4e");
    Student *p = this->head;
        while (head != NULL)
        {
            p = head;
            head = head->next;
            delete(p);
        }
        system("color 4c");
        printf("\n系统将于3秒后自动退出\n");
        Sleep(3000);
}

主函数:链表操作界面

#include "List.h"
#include<iostream>

using namespace std;

List *list = NULL;

void main()
{
    int choose1;
    list = new List;
    list->loading();
MENU:
    cout << "\n\t\t主菜单:1,显示全部\t2,添加\t3,查找\t4,排名\t5,退出\n"<<endl;
    cin >> choose1; getchar();
    switch (choose1)
    {
    case 1:
        list->print_list();  //显示全部
        break;
    case 2:
        list->add_list_node();  //添加
        break;
    case 3:
        list->seek_list_node();  //查找
        break;
    case 4:
        list->list_sort();   //排名
        break;
    case 5:
        list->quit();   //退出
        break;
    default:
        cout << "您的输入有误,请重新选择!!" << endl;
        goto MENU;
    }
    cout << "按任意键返回主菜单" << endl;
    getchar();
    goto MENU;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值