免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7850 | 回复: 7
打印 上一主题 下一主题

[C++] 使用自己编写的链表类实现的学生信息管理系统 [复制链接]

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-08-12 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-04-29 12:13 |只看该作者 |倒序浏览
本帖最后由 framily 于 2015-04-29 12:18 编辑

===================================================
List.h文件,该文件用来设计自己的链表类,并对部分运算符进行了重载
===================================================
#pragma once

typedef struct _STUDENT
{
        int number;
        char name[20];
        float math;
}DATA;

struct _NODE
{
        DATA data;
        _NODE *prev, *next;
};

class List
{
        _NODE *head_, *tail_;
        int count_;
public:
        class iterator
        {
                _NODE *p_;
        public:
                iterator(_NODE *p)
                {
                        this->p_ = p;
                }

                DATA & operator*()
                {
                        return p_->data;
                }

                DATA *operator->()
                {
                        return &(p_->data);
                }

                operator _NODE*()
                {
                        return p_;
                }

                iterator & operator++()
                {
                        p_ = p_->next;
                        return *this;
                }

                iterator operator++(int)
                {
                        iterator iter(p_);
                        p_ = p_->next;
                        return iter;
                }

                iterator operator--()
                {
                        p_ = p_->prev;
                        return *this;
                }

                iterator operator--(int)
                {
                        iterator iter(p_);
                        p_ = p_->prev;
                        return iter;
                }

                iterator()
                {

                }

                ~iterator()
                {

                }
        };

        int size() const
        {
                return count_;
        }

        iterator begin() const
        {
                return head_;
        }

        iterator end() const
        {
                return tail_;
        }

        void push_back(DATA data);
        void push_front(DATA data);
        void erase(iterator iter);
        void clear();
        List();
        ~List();
};

=============================================================
List.cpp 该文件用来具体实现List.h文件中声明的函数
=============================================================
#include "List.h"



void List::push_back(DATA data)
{
        _NODE *p = new _NODE;

        p->data = data;

        if (tail_)
                tail_->next = p;
        else
                head_ = p;

        p->prev = tail_;
        p->next = nullptr;
        tail_ = p;
        ++count_;
}

void List::push_front(DATA data)
{
        _NODE *p = new _NODE;

        p->data = data;

        if (head_)
                head_->prev = p;
        else
                tail_ = p;

        p->next = head_;
        p->prev = nullptr;
        head_ = p;
        ++count_;
}

void List::erase(iterator iter)
{
        _NODE  *p = (_NODE*)iter;

        if (p == head_)
                head_ = p->next;
        else
                p->prev->next = p->next;

        if (p == tail_)
                tail_ = p->prev;
        else
                p->next->prev = p->prev;

        delete p;
        --count_;
}

void List::clear()
{
        _NODE *p = head_, *q = nullptr;

        while (p)
        {
                q = p;
                p = p->next;
                delete q;
        }

        head_ = tail_ = nullptr;
        count_ = 0;
}

List::List()
{
        head_ = tail_ = nullptr;
        count_ = 0;
}


List::~List()
{
        clear();
}

==============================================================
Student.h 文件  用来编写学生信息管理类
==============================================================
#pragma once

#define _CRT_SECURE_NO_WARNINGS

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

typedef bool(*BY_FUNC)(DATA &q, DATA &m);
typedef List::iterator ITER;

class CStudent
{
        List list_;

        static bool ByNumb(DATA &q, DATA &m)
        {
                return q.number < m.number;
        }

        static bool ByName(DATA &q, DATA &m)
        {
                return strcmp(q.name, m.name) < 0;
        }

        static bool ByMath(DATA &q, DATA &m)
        {
                return q.math > m.math;
        }

        void Welcome();
        int Menu();
        int Input();
        bool Check(int number);
        bool Del();
        bool Mod();
        int Find();
        void FindByID();
        void FindByName();
        void FindByScore();
        int SortMenu();
        void Sort(BY_FUNC);
        void Print();
        void PrintPS(ITER *);
        void ColorSet();
        void Save();
        void Load();
        void PrintFind(List &cl);
public:
        CStudent();
        ~CStudent();
        void Start();
};

=====================================================
Student.cpp
=====================================================
#include "Student.h"
#include <conio.h>
#include <iostream>

using namespace std;

CStudent::CStudent()
{
}


CStudent::~CStudent()
{
        list_.clear();
}


void CStudent::Start()
{
        Welcome();
        Load();
        while (Menu())
                ;
}

void CStudent::Welcome()
{
        system("color 5F";
        printf("\n\n";
        printf("  \t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
        printf("  \t┃ *********************************************************┃\n";
        printf("  \t┃***┏━━━━━━━━━━━━━━━━━━━━━━━━┓***┃\n";
        printf("  \t┃***┃************************************************┃***┃\n";
        printf("  \t┃***┃***                                         ****┃***┃\n";
        printf("  \t┃***┃***         欢迎使用学生信息管理系统        ****┃***┃\n";
        printf("  \t┃***┃***                                         ****┃***┃\n";
        printf("  \t┃***┃***                                         ****┃***┃\n";
        printf("  \t┃***┃***                ANONYMOUS                 **** |***┃\n");
        printf("  \t┃***┃***                                         ****┃***┃\n");
        printf("  \t┃***┃***                2015.04.29               ****┃***┃\n");
        printf("  \t┃***┃***                                         ****┃***┃\n");
        printf("  \t┃***┃************************************************┃***┃\n");
        printf("  \t┃***┗━━━━━━━━━━━━━━━━━━━━━━━━┛***┃\n");
        printf("  \t┃ *********************************************************┃\n");
        printf("  \t┗━━━━━━━━━━━按任意键继续━━━━━━━━━━━━┛\n");
        _getch();
}

int CStudent::Menu()
{
        system("cls");
        cout << "==================" << endl;
        puts("1、浏览所有信息");
        puts("2、添加信息");
        puts("3、删除信息");
        puts("4、修改信息");
        puts("5、查找信息");
        puts("6、颜色设置");
        puts("0、退出");
        cout << "==================" << endl;
        printf("请选择:");

        int i = 0;

        scanf_s("%d", &i);

        switch (i)
        {
        case 1:
                while (SortMenu())
                        ;
                break;
        case 2:
                while (Input())
                        ;
                break;
        case 3:
                while (Del())
                        ;
                break;
        case 4:
                while (Mod())
                        ;
                break;
        case 5:
                while (Find())
                        ;
                break;
        case 6:
                ColorSet();
                break;
        }

        return i;
}

int CStudent::Input()
{
        system("cls");
        cout << "请输入学号:";

        DATA data;

        while (true)
        {
                cin >> data.number;

                if (Check(data.number))
                        cout << "号码已经存在,请重新输入:";
                else
                        break;
        }

        cout << "请输入姓名和数学成绩(中间用空格分开):";
        cin >> data.name >> data.math;

        list_.push_back(data);
        Save();
        Print();
        cout << "继续添加(y/n):";

        char c = _getch();
       
        return (c == 'y' || c == 'Y');
}

bool CStudent::Check(int number)
{
        ITER iter = list_.begin();

        while (iter)
        {
                if (iter->number == number)
                        return true;

                ++iter;
        }

        return false;
}

bool CStudent:el()
{
        system("cls");
        Print();

        int number;

        cout << "请输入要删除的学号:";
        cin >> number;

        ITER iter = list_.begin();

        while (iter)
        {
                if (iter->number == number)
                {
                        list_.erase(iter);
                        Print();
                        cout << "删除成功!" << endl;
                        puts("");
                        system("pause");
                        Save();
                        return false;
                }

                ++iter;
        }

        cout << "你输入的学号不存在,是否继续?(y/n):";

        char c = _getch();

        return ('y' == c || 'Y' == c);
}

bool CStudent::Mod()
{
        system("cls");
        Print();

        int n;

        cout << "请输入你要修改的学号:";
        cin >> n;

        ITER iter = list_.begin();

        while (iter)
        {
                if (iter->number == n)
                {
                        cout << "请输入新的姓名和数学成绩(中间用空格分开):" << endl;
                        cin >> iter->name >> iter->math;
                        Print();
                        cout << "修改成功!" << endl;
                        Save();
                        puts("");
                        system("pause");
                        return false;
                }

                ++iter;
        }

        cout << "你要修改的学号不存在!" << endl;
        cout << "继续修改?(y/n):";

        char c = _getch();

        return (c == 'y' || c == 'Y');
}

int CStudent::Find()
{
        int n;

        system("cls");
        cout << "=================" << endl;
        cout << "1、按学号查找" << endl;
        cout << "2、按姓名查找" << endl;
        cout << "3、按分数段查找" << endl;
        cout << "0、返回主菜单" << endl;
        cout << "=================" << endl;
        cout << "请选择:";
        cin.clear();
        cin.sync();
        cin >> n;

        switch (n)
        {
        case 1:
                FindByID();
                break;
        case 2:
                FindByName();
                break;
        case 3:
                FindByScore();
                break;
        default:
                break;
        }

        return n;
}

void CStudent::FindByID()
{
        int n;

        system("cls");
        cout << "请输入您要查找的学号:";
        fflush(stdin);
        cin >> n;

        ITER iter = list_.begin();

        while (iter)
        {
                if (iter->number == n)
                {
                        cout << "===========================" << endl;
                        cout << "学号\t" << "姓名\t" << "数学成绩" << endl;
                        cout << "===========================" << endl;
                        cout << iter->number << "\t" << iter->name
                                << "\t" << iter->math << endl;
                        puts("");
                        system("pause");
                        return;
                }
               
                ++iter;
        }

        cout << "您所查找的信息不存在!" << endl;

        puts("");
        system("pause");
}

void CStudent::FindByName()
{
        char name[20];
        List list;

        system("cls");
        cout << "请输入您要查找的姓名:";
        cin.clear();
        cin.sync();
        cin >> name;

        ITER iter = list_.begin();

        while (iter)
        {
                if (strstr(iter->name, name))
                        list.push_back(*iter);

                ++iter;
        }

        if (list.size())
        {
                PrintFind(list);
                list.clear();
        }
        else
                cout << "没有找到符合条件的学号" << endl;

        puts("");
        system("pause");
}

void CStudent::FindByScore()
{
        float score1;
        float score2;
        List list;

        system("cls");
        printf("请输入要查询的分数区间(两个分数之间用空格分开):");
        cin.clear();
        cin.sync();
        cin >> score1 >> score2;

        if(score1>score2)
                swap(score1, score2);

        ITER iter = list_.begin();

        while (iter!)
        {
                if (iter->math > score1 && iter->math < score2)
                        list.push_back(*iter);

                ++iter;
        }

        if (list.size())
        {
                PrintFind(list);
                list.clear();
        }
        else
                cout << "没有找到符合条件的数据" << endl;

        puts("");
        system("pause");
}

int CStudent::SortMenu()
{
        system("cls");
        cout << "====================" << endl;
        puts("1、按学号排序:");
        puts("2、按姓名排序:");
        puts("3、按数学成绩排序:");
        puts("4、不排序直接浏览:");
        puts("0、返回主菜单:");
        cout << "====================" << endl;
        cout << "请选择:";

        int i;

        cin >> i;

        BY_FUNC ps[] = { ByNumb,ByName,ByMath };

        switch (i)
        {
        case 1:
        case 2:
        case 3:
                Sort(ps[i - 1]);
                break;
        case 4:
                Print();
                system("pause");
                break;
        }

        return i;
}

void CStudent::Sort(BY_FUNC func)
{
        system("cls");

        int n = list_.size(), i = 0;

        ITER *iters = new ITER[n + 1];
        ITER iter = list_.begin();

        while (iters = iter)
        {               
                ++i;
                ++iter;
        }

        i = 0;

        while (i < n - 1)
        {
                int j = i + 1;
                int m = i;

                while (j < n)
                {
                        if (func(*iters[j], *iters[m]))
                                m = j;

                        ++j;
                }

                if (m != i)
                {
                        ITER t = iters;
                        iters = iters[m];
                        iters[m] = t;
                }

                ++i;
        }

        PrintPS(iters);
        delete[]iters;
}

void CStudent:rint()
{
        system("cls");

        ITER iter = list_.begin();

        cout << "===========================" << endl;
        cout << "学号\t" << "姓名\t" << "数学成绩" << endl;
        cout << "===========================" << endl;
        while (iter)
        {
                cout << iter->number << "\t" << iter->name
                        << "\t" << iter->math << endl;
                ++iter;
        }
}

void CStudent:rintPS(ITER *iters)
{
        cout << "===========================" << endl;
        cout << "学号\t" << "姓名\t" << "数学成绩" << endl;
        cout << "===========================" << endl;

        int i = 0;

        while (iters)
        {
                cout << iters->number << "\t" << iters->name << "\t"
                        << iters->math << endl;
                ++i;
        }

        puts("");
        system("pause");
}

void CStudent::ColorSet()
{
        char str[9] = "color ";

        system("cls");
        cout << "==============================" << endl;
        cout << "0 = 黑色       8 = 灰色" << endl;
        cout << "1 = 蓝色       9 = 淡蓝色" << endl;
        cout << "2 = 绿色       A = 淡绿色" << endl;
        cout << "3 = 浅绿色     B = 淡浅绿色" << endl;
        cout << "4 = 红色       C = 淡红色" << endl;
        cout << "5 = 紫色       D = 淡紫色" << endl;
        cout << "6 = 黄色       E = 淡黄色" << endl;
        cout << "7 = 白色       F = 亮白色" << endl;
        cout << "==============================" << endl;
        cout << "请选择背景色(0-F):";
        cin >> str[6];
        cout << "请选择前景色(0-F):";
        cin >> str[7];
        system(str);
}

void CStudent::Save()
{
        FILE *pf = fopen("stu.dat", "wb");

        if (!pf)
                return;

        DATA d;
        ITER iter = list_.begin();

        while (iter != list_.end())
        {
                d = *iter;
                fwrite(&d, 1, sizeof(d), pf);
                iter++;
        }

        fclose(pf);
}

void CStudent::Load()
{
        FILE *pf = fopen("stu.dat", "rb");

        if (!pf)
                return;

        DATA d;

        while (fread(&d, 1, sizeof(d), pf) == sizeof(d))
                list_.push_back(d);

        fclose(pf);
}

void CStudent:rintFind(List & list)
{
        system("cls");

        int i = 0;
        ITER iter = list.begin();

        cout << "===========================" << endl;
        cout << "学号\t" << "姓名\t" << "数学成绩" << endl;
        cout << "===========================" << endl;

        while (iter)
        {
                cout << iter->number << "\t" << iter->name << "\t"
                        << iter->math << endl;
                ++iter;
        }
}


=====================================================
Source.cpp 定义函数入口
=====================================================
#include "Student.h"

int main()
{
        CStudent st;

        st.Start();

        return 0;
}

论坛徽章:
6
技术图书徽章
日期:2013-11-13 11:11:27子鼠
日期:2014-02-20 17:54:13处女座
日期:2014-06-16 17:43:33午马
日期:2014-08-08 09:11:17未羊
日期:2014-08-10 11:57:072015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2015-04-29 14:11 |只看该作者
本帖最后由 littledick 于 2015-04-29 14:13 编辑

构造函数、成员变量初始化都是很重要的,不要偷懒不写,尤其是指针初始化。
指针拿来用,要注意有效性。必要时,用引用代替指针。

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-08-12 06:20:00
3 [报告]
发表于 2015-04-29 14:17 |只看该作者
大哥说得好,赞回复 2# littledick


   

论坛徽章:
12
2015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之同曦
日期:2017-03-17 19:13:162016科比退役纪念章
日期:2016-11-07 08:28:12luobin
日期:2016-06-17 17:46:36wusuopu
日期:2016-06-17 17:43:4515-16赛季CBA联赛之福建
日期:2016-01-14 12:49:22程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:002015年亚洲杯之科威特
日期:2015-03-24 14:21:272015年迎新春徽章
日期:2015-03-04 09:57:092016科比退役纪念章
日期:2018-04-10 16:20:18
4 [报告]
发表于 2015-04-29 14:31 |只看该作者
另外,代码建议打包传附件,不然,,,,帖子太长了,

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
5 [报告]
发表于 2015-04-29 14:39 |只看该作者
VIP_fuck 发表于 2015-04-29 14:31
另外,代码建议打包传附件,不然,,,,帖子太长了,



传附件,还会有人看?

还是以代码方式贴,免得表情符号

论坛徽章:
36
子鼠
日期:2013-08-28 22:23:29黄金圣斗士
日期:2015-12-01 11:37:51程序设计版块每日发帖之星
日期:2015-12-14 06:20:00CU十四周年纪念徽章
日期:2015-12-22 16:50:40IT运维版块每日发帖之星
日期:2016-01-25 06:20:0015-16赛季CBA联赛之深圳
日期:2016-01-27 10:31:172016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之福建
日期:2016-04-07 11:25:2215-16赛季CBA联赛之青岛
日期:2016-04-29 18:02:5915-16赛季CBA联赛之北控
日期:2016-06-20 17:38:50技术图书徽章
日期:2016-07-19 13:54:03程序设计版块每日发帖之星
日期:2016-08-21 06:20:00
6 [报告]
发表于 2015-04-29 15:00 |只看该作者
如果没看到楼主的回帖还以为是机器人发帖,差点就给举报了

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-08-12 06:20:00
7 [报告]
发表于 2015-08-09 01:16 |只看该作者
  1. // main.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include "Student.h"

  5. int _tmain(int argc, _TCHAR* argv[])
  6. {
  7.         Student st;

  8.         st.Start();

  9.         return 0;
  10. }

复制代码
  1. //student.h

  2. #pragma once

  3. #include <afxtempl.h>

  4. typedef struct User
  5. {
  6.         int number;
  7.         char name[20];
  8.         float math;
  9. } Data;

  10. typedef bool(*Func)(Data &q, Data &m);

  11. class Student
  12. {
  13.         CList<Data> list;

  14.         int Menu();
  15.         int Input();
  16.         bool Del();
  17.         bool Mod();
  18.         void Welcome();
  19.         void Print();
  20.         void ColorSet();
  21.         int Find();
  22.         void SearchByID();
  23.         void SearchByName();
  24.         void SearchByScore();
  25.         void Load();
  26.         void Save();
  27.         bool Check(int number);
  28.         int SortMenu();
  29.         void Sort(Func func);
  30. public:
  31.         Student();
  32.         ~Student();
  33.         void Start();
  34.         void Print1(POSITION *ps);
  35. };

复制代码
  1. //student.cpp

  2. #include "stdafx.h"
  3. #include "Student.h"
  4. #include <iostream>
  5. #include <conio.h>
  6. #include <string>

  7. using namespace std;


  8. int Student::Menu()
  9. {
  10.         system("cls");//clear screen
  11.         puts("\n\t\t1、浏览所有信息");
  12.         puts("\t\t2、添加信息");
  13.         puts("\t\t3、删除信息");
  14.         puts("\t\t4、修改信息");
  15.         puts("\t\t5、查找信息");
  16.         puts("\t\t6、颜色设置");
  17.         puts("\t\t0、退出");
  18.         printf("\t\t请选择:");

  19.         int i = 0;

  20.         scanf_s("%d", &i);

  21.         switch (i)
  22.         {
  23.         case 1:
  24.                 while (SortMenu())
  25.                         ;
  26.                 break;
  27.         case 2:
  28.                 while (Input())
  29.                         ;
  30.                 break;
  31.         case 3:
  32.                 while (Del())
  33.                         ;
  34.                 break;
  35.         case 4:
  36.                 while (Mod())
  37.                         ;
  38.                 break;
  39.         case 5:
  40.                 while (Find())
  41.                         ;
  42.                 break;
  43.         case 6:
  44.                 ColorSet();
  45.                 break;
  46.         }

  47.         return i;
  48. }

  49. int Student::Input()
  50. {
  51.         cout << "请输入一个数字:";

  52.         Data data;

  53.         while (true)
  54.         {
  55.                 cin >> data.number;
  56.                
  57.                 if (Check(data.number))
  58.                         cout << "号码已经存在,请重新输入:";
  59.                 else
  60.                         break;
  61.         }

  62.         cout << "请输入姓名和数学成绩(中间用空格分开):";
  63.         cin >> data.name >> data.math;

  64.         list.AddTail(data);
  65.         Save();
  66.         Print();
  67.         cout << "继续添加(y/n):";

  68.         char c = _getch();
  69.         putchar(c);
  70.         puts("");
  71.         system("pause");
  72.         return c == 'y' || c== 'Y' ;
  73. }

  74. int Student::Find()
  75. {
  76.         int n;

  77.         system("cls");
  78.         cout << "=================" << endl;
  79.         cout << "1、按学号查找" << endl;
  80.         cout << "2、按姓名查找" << endl;
  81.         cout << "3、按分数段查找" << endl;
  82.         cout << "0、返回主菜单" << endl;
  83.         cout << "=================" << endl;
  84.         cout << "请选择:";
  85.         cin.clear();
  86.         cin.sync();
  87.         cin >> n;

  88.         switch (n)
  89.         {
  90.         case 1:
  91.                 SearchByID();
  92.                 break;
  93.         case 2:
  94.                 SearchByName();
  95.                 break;
  96.         case 3:
  97.                 SearchByScore();
  98.                 break;
  99.         default:
  100.                 break;
  101.         }

  102.         return n;
  103. }

  104. void Student::SearchByID()
  105. {
  106.         int n;
  107.         Data data;

  108.         cout << "请输入您要查找的学号:";
  109.         cin.clear();
  110.         cin.sync();
  111.         cin >> n;

  112.         POSITION pos = list.GetHeadPosition();

  113.         while (pos)
  114.         {
  115.                 if (list.GetAt(pos).number == n)
  116.                 {
  117.                         data = list.GetAt(pos);

  118.                         cout << data.number << "\t" << data.name << "\t" << data.math << endl;
  119.                         system("pause");
  120.                         return;
  121.                 }

  122.                 list.GetNext(pos);
  123.         }

  124.         if (!pos)
  125.                 cout << "您所查找的信息不存在!";

  126.         system("pause");
  127. }

  128. void Student::SearchByName()
  129. {
  130.         char name[20];
  131.         Data data;

  132.         cout << "请输入您要查找的姓名:";
  133.         cin.clear();
  134.         cin.sync();
  135.         cin >> name;

  136.         POSITION pos = list.GetHeadPosition();

  137.         while (pos)
  138.         {
  139.                 if (!strcmp(list.GetAt(pos).name,name))
  140.                 {
  141.                         data = list.GetAt(pos);

  142.                         cout << data.number << "\t" << data.name << "\t" << data.math << endl;
  143.                         system("pause");
  144.                         return;
  145.                 }

  146.                 list.GetNext(pos);
  147.         }

  148.         if (!pos)
  149.                 cout << "您查找的信息不存在!" << endl;

  150.         system("pause");
  151. }

  152. void Student::SearchByScore()
  153. {
  154.         float score1;
  155.         float score2;
  156.         Data data;
  157.         char flag = false;

  158.         printf("请输入要查询的分数区间(两个分数之间用空格分开):");
  159.         cin.clear();
  160.         cin.sync();
  161.         cin >> score1 >> score2;

  162.         if (score1 > score2)
  163.         {
  164.                 float t;
  165.                 t = score1;
  166.                 score1 = score2;
  167.                 score2 = t;
  168.         }

  169.         POSITION pos = list.GetHeadPosition();

  170.         while (pos)
  171.         {
  172.                 if (list.GetAt(pos).math>score1&&list.GetAt(pos).math<score2)
  173.                 {
  174.                         data = list.GetAt(pos);
  175.                         flag = true;
  176.                         cout << data.number << "\t" << data.name << "\t" << data.math << endl;
  177.                 }

  178.                 list.GetNext(pos);
  179.         }

  180.         if (!flag)
  181.                 cout << "您要查找的信息不存在!" << endl;

  182.         system("pause");
  183. }

  184. bool Student::Mod()
  185. {
  186.         int n;
  187.         Data data;

  188.         cout << "请输入你要修改的学号:";
  189.         cin >> n;

  190.         POSITION pos = list.GetHeadPosition();

  191.         while (pos)
  192.         {
  193.                 if (list.GetAt(pos).number == n)
  194.                 {
  195.                         data = list.GetAt(pos);

  196.                         cout << "请输入新的姓名和数序成绩(中间用空格分开):" << endl;
  197.                         cin >> data.name >> data.math;
  198.                         list.SetAt(pos, data);
  199.                         Print();
  200.                         cout << "修改成功!" << endl;
  201.                         Save();
  202.                         system("pause");
  203.                         return false;
  204.                 }

  205.                 list.GetNext(pos);
  206.         }
  207.        
  208.         cout << "你要修改的学号不存在!" << endl;
  209.         cout << "继续修改?(y/n):";

  210.         char c = _getch();

  211.         putchar(c);
  212.         puts("");
  213.         system("pause");
  214.         return c == 'y' || c == 'Y';
  215. }

  216. bool Student::Del()
  217. {
  218.         int number;

  219.         cout << "请输入要删除的学号:";
  220.         cin >> number;

  221.         POSITION pos = list.GetHeadPosition();

  222.         while (pos)
  223.         {
  224.                 if (list.GetAt(pos).number == number)
  225.                 {
  226.                         list.RemoveAt(pos);
  227.                         Print();
  228.                         cout << "删除成功!" << endl;
  229.                         system("pause");
  230.                         Save();
  231.                         return false;
  232.                 }

  233.                 list.GetNext(pos);
  234.         }

  235.         cout << "你输入的学号不存在,是否继续?(y/n):";

  236.         char c = _getch();

  237.         putchar(c);
  238.         cout << "\n";
  239.         return 'y' == c || 'Y' == c;
  240. }

  241. void Student::Welcome()
  242. {
  243.         system("color 5F");
  244.         printf("\n\n");
  245.         printf("  \t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
  246.         printf("  \t┃ *********************************************************┃\n");
  247.         printf("  \t┃***┏━━━━━━━━━━━━━━━━━━━━━━━━┓***┃\n");
  248.         printf("  \t┃***┃************************************************┃***┃\n");
  249.         printf("  \t┃***┃***                                         ****┃***┃\n");
  250.         printf("  \t┃***┃***         欢迎使用学生信息管理系统        ****┃***┃\n");
  251.         printf("  \t┃***┃***                                         ****┃***┃\n");
  252.         printf("  \t┃***┃***                                         ****┃***┃\n");
  253.         printf("  \t┃***┃***                  framily                 **** |***┃\n");
  254.         printf("  \t┃***┃***                                         ****┃***┃\n");
  255.         printf("  \t┃***┃***                2015.04.23               ****┃***┃\n");
  256.         printf("  \t┃***┃***                                         ****┃***┃\n");
  257.         printf("  \t┃***┃************************************************┃***┃\n");
  258.         printf("  \t┃***┗━━━━━━━━━━━━━━━━━━━━━━━━┛***┃\n");
  259.         printf("  \t┃ *********************************************************┃\n");
  260.         printf("  \t┗━━━━━━━━━━━按任意键继续━━━━━━━━━━━━┛\n");
  261.         _getch();
  262. }

  263. void Student::Print()
  264. {
  265.         POSITION pos = list.GetHeadPosition();

  266.         while (pos)
  267.         {
  268.                 Data data = list.GetAt(pos);
  269.                
  270.                 cout << data.number << "\t" << data.name << "\t" << data.math << endl;
  271.                 list.GetNext(pos);
  272.         }
  273. }

  274. void Student::Print1(POSITION * ps)
  275. {
  276.         int i = 0;

  277.         cout << "学号\t" << "姓名\t" << "数学成绩" << endl;

  278.         while (ps[i])
  279.         {
  280.                 Data data = list.GetAt(ps[i]);

  281.                 cout << data.number << "\t" << data.name << "\t" << data.math << endl;
  282.                 i++;
  283.         }

  284.         system("pause");
  285. }

  286. void Student::ColorSet()
  287. {
  288.         char str[9] = "color ";

  289.         system("cls");
  290.         cout << "\n\n\n" << endl;
  291.         cout << "\t\t        0 = 黑色       8 = 灰色" << endl;
  292.         cout << "\t\t        1 = 蓝色       9 = 淡蓝色" << endl;
  293.         cout << "\t\t        2 = 绿色       A = 淡绿色" << endl;
  294.         cout << "\t\t        3 = 浅绿色     B = 淡浅绿色" << endl;
  295.         cout << "\t\t        4 = 红色       C = 淡红色" << endl;
  296.         cout << "\t\t        5 = 紫色       D = 淡紫色" << endl;
  297.         cout << "\t\t        6 = 黄色       E = 淡黄色" << endl;
  298.         cout << "\t\t        7 = 白色       F = 亮白色" << endl;
  299.         cout << "\t\t\t请选择背景色(0-F):";
  300.         cin >> str[6];
  301.         cout << "\t\t\t请选择前景色(0-F):";
  302.         cin >> str[7];
  303.         system(str);
  304. }

  305. Student::Student()
  306. {
  307. }


  308. Student::~Student()
  309. {
  310.         list.RemoveAll();
  311. }

  312. void Student::Start()
  313. {
  314.         Welcome();
  315.         Load();
  316.         while (Menu())
  317.                 ;
  318. }

  319. void Student::Load()
  320. {
  321.         FILE *file = fopen("stu.dat", "rb");

  322.         if (!file)
  323.                 return;

  324.         Data data;

  325.         while (fread(&data, 1, sizeof(data), file)== sizeof(data))
  326.                 list.AddTail(data);

  327.         fclose(file);
  328. }

  329. void Student::Save()
  330. {
  331.         FILE *file = fopen("stu.dat", "wb");

  332.         if (!file)
  333.         {
  334.                 cout << "文件保存失败!" << endl;
  335.                 _getch();
  336.                 return;
  337.         }

  338.         POSITION pos = list.GetHeadPosition();

  339.         while (pos)
  340.         {
  341.                 Data data = list.GetAt(pos);

  342.                 fwrite(&data, 1, sizeof(data), file);
  343.                 list.GetNext(pos);
  344.         }

  345.         fclose(file);
  346. }

  347. bool Student::Check(int number)
  348. {
  349.         POSITION pos = list.GetHeadPosition();

  350.         while (pos)
  351.         {
  352.                 Data data = list.GetAt(pos);

  353.                 if (data.number == number)
  354.                         return true;

  355.                 list.GetNext(pos);
  356.         }

  357.         return false;
  358. }

  359. bool ByNumber(Data &q, Data &m)
  360. {
  361.         return q.number < m.number;
  362. }

  363. bool ByName(Data &q, Data &m)
  364. {
  365.         return strcmp(q.name, m.name) < 0;
  366. }

  367. bool ByMath(Data &q, Data &m)
  368. {
  369.         return q.math > m.math;
  370. }

  371. int Student::SortMenu()
  372. {
  373.         system("cls");
  374.         cout << "1、按学号排序" << endl;
  375.         cout << "2、按姓名排序" << endl;
  376.         cout << "3、按数学成绩排序" << endl;
  377.         cout << "4、不排序" << endl;
  378.         cout << "0、返回主菜单" << endl;

  379.         int i;

  380.         cin >> i;

  381.         Func ps[] = { ByNumber,ByName,ByMath };

  382.         switch (i)
  383.         {
  384.         case 1:
  385.         case 2:
  386.         case 3:
  387.                 Sort(ps[i - 1]);
  388.                 break;
  389.         case 4:
  390.                 Print();
  391.                 system("pause");
  392.                 break;
  393.         }

  394.         return i;
  395. }

  396. void Student::Sort(Func func)
  397. {
  398.         int n = list.GetCount();
  399.         POSITION *ps = new POSITION[n + 1];
  400.         POSITION pos = list.GetHeadPosition();
  401.         int i = 0, m = 0, j = 0;

  402.         while (ps[i++]=pos)
  403.         {
  404.                 list.GetNext(pos);
  405.         }
  406.        
  407.         i = 0;

  408.         while (i < n - 1)
  409.         {
  410.                 m = i;
  411.                 j = i + 1;

  412.                 while (j < n)
  413.                 {
  414.                         if (func(list.GetAt(ps[j]), list.GetAt(ps[m])))
  415.                                 m = j;

  416.                         j++;
  417.                 }

  418.                 if (m != i)
  419.                 {
  420.                         POSITION t = ps[i];
  421.                         ps[i] = ps[m];
  422.                         ps[m] = t;
  423.                 }

  424.                 i++;
  425.         }

  426.         Print1(ps);
  427.         delete[]ps;
  428. }
复制代码

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-08-12 06:20:00
8 [报告]
发表于 2015-08-09 01:21 |只看该作者
  1. //List.h

  2. #pragma once

  3. typedef int Data;

  4. struct Node
  5. {
  6.         Data data;
  7.         Node *prev, *next;
  8. };

  9. typedef void* Position;

  10. class List
  11. {
  12.         Node *head_, *tail_;
  13.         int count_;

  14. public:
  15.         List();
  16.         ~List();

  17.         int GetCount() const
  18.         {
  19.                 return count_;
  20.         }

  21.         Position GetHeadPosition() const
  22.         {
  23.                 return head_;
  24.         }

  25.         Position GetTailPosition() const
  26.         {
  27.                 return tail_;
  28.         }

  29.         Data & GetAt(Position pos) const
  30.         {
  31.                 return ((Node *)pos)->data;
  32.         }

  33.         Data GetPrev(Position &pos) const;
  34.         Data GetNext(Position &pos) const;
  35.         void AddHead(Data data);
  36.         void AddTail(Data data);

  37.         void SetAt(Position pos, Data data) const
  38.         {
  39.                 ((Node *)pos)->data=data;
  40.         }

  41.         void RemoveAt(Position pos);
  42.         void RemoveAll();
  43. };

复制代码
  1. // List.cpp

  2. #include "List.h"



  3. List::List()
  4. {
  5.         head_ = tail_ = nullptr;
  6.         count_ = 0;
  7. }


  8. List::~List()
  9. {
  10.         RemoveAll();
  11. }

  12. Data List::GetPrev(Position & pos) const
  13. {
  14.         Data data = ((Node *)pos)->data;

  15.         pos = ((Node *)pos)->prev;
  16.         return data;
  17. }

  18. Data List::GetNext(Position & pos) const
  19. {
  20.         Data data = ((Node *)pos)->data;

  21.         pos = ((Node *)pos)->next;
  22.         return data;
  23. }

  24. void List::AddHead(Data data)
  25. {
  26.         Node *p = new Node;

  27.         p->data = data;

  28.         if (head_)
  29.                 head_->prev = p;
  30.         else
  31.                 tail_ = p;

  32.         p->next = head_;
  33.         p->prev = nullptr;
  34.         head_ = p;
  35.         count_++;
  36. }

  37. void List::AddTail(Data data)
  38. {
  39.         Node *p = new Node;

  40.         p->data = data;

  41.         if (tail_)
  42.                 tail_->next = p;
  43.         else
  44.                 head_ = p;

  45.         p->prev = tail_;
  46.         p->next = nullptr;
  47.         tail_ = p;
  48.         count_++;
  49. }

  50. void List::RemoveAt(Position pos)
  51. {
  52.         Node *p = (Node *)pos;

  53.         if (p == head_)
  54.                 head_ = p->next;
  55.         else
  56.                 p->prev->next = p->next;

  57.         if (p == tail_)
  58.                 tail_ = p->prev;
  59.         else
  60.                 p->next->prev = p->prev;

  61.         delete p;
  62.         --count_;
  63. }

  64. void List::RemoveAll()
  65. {
  66.         Node *p = head_, *q = nullptr;

  67.         while (p)
  68.         {
  69.                 q = p;
  70.                 p = p->next;
  71.                 delete q;
  72.         }

  73.         head_ = tail_ = nullptr;
  74.         count_ = 0;
  75. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP