C++课程设计

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<fstream>
#include<iomanip>
#include<windows.h>
using namespace std;
//基类 People
class People
{
public:
    People(int num , string na , string se , long sco ,string pho);
    People() {}
    ~People() {}
    virtual void Display() {}
protected:
    int id;
    string name;
    string sex;
    long score;
    string phone;
};
People::People(int num , string na , string se , long sco ,string pho)
{
    id = num;
    score = sco;
    sex = se;
    name = na;
    phone = pho;
}
bool TestOk_MessageBox()
{
    //调用API函数 MessageBox
    int nSelect = ::MessageBox(NULL, "Hello,此操作会改变信息,且不可恢复,是否执行?", "Hello!!!", MB_OKCANCEL);
    if(nSelect == IDOK)
        return true;
    else
        return false;
}
void ShowStudenInf()
{
    cout<<"           ***********学生的信息************\n\n";
    cout<<" ========    学号     姓名     性别     得分     电话   =======\n\n";
}
class Student:public People
{
public:
    Student(int num , string na , string se , long sco ,string pho):People(num , na , se , sco , pho) {}
    Student() {}
    //Student(Student& t);
    ~Student() {}
    Student *next;
    friend ostream& operator<<(ostream& , const Student&);
    friend istream& operator>>(istream& , Student&);
    friend class Control;
};
//操作符<< >> 的重载
ostream& operator<<(ostream& out , const Student& t1)
{
    out<<"               ";
    out<<setw(10)<<setiosflags(ios::left)<<setw(8)<<t1.id<<setw(8)<<t1.name<<setw(8)<<t1.sex<<setw(8)<<t1.score<<setw(8)<<t1.phone<<endl;
    return out;
}
istream& operator>>(istream& in , Student& t1)
{
    in>>t1.id>>t1.name>>t1.sex>>t1.score>>t1.phone;
    return in;
}
class Control
{
public:
    Control()
    {
        Sp=NULL;
        ReadFile();
    }
    ~Control();
    void StudentMenu();
    void Build();
    void Add();
    void Find();
    void Modify();
    void Del();
    void Display();
    void Save();
    void Sortt();
    void ReadFile();
private:
    Student *Sp;
};
void Control::Build()
{
    if(TestOk_MessageBox())
    {
        Sp=NULL;
        ShowStudenInf();
        Student *s = new Student;
        cin>>*s;
        s->next = Sp;
        Sp = s;
        Save();
    }
}
void Control::Add()
{
    ShowStudenInf();
    cout<<"请输入新建信息\n";
    Student *s = new Student;
    cin>>*s;
    s->next = Sp;
    Sp = s;
    Save();
}
void Control::Del()
{
    cout<<"          ************** 删除方式 **********\n";
    cout<<"          *             1 : 学号           *\n";
    cout<<"          *             2 : 姓名           *\n";
    cout<<"          *             3 : 退出           *\n";
    cout<<"          **********************************\n";
    cout<<"请选择!!1--3\n";
    if(!TestOk_MessageBox())
        return ;
    int alt,Endid,loop=0;
    string Endname;
    cin>>alt;
    while(alt<1||alt>3)
    {
        cout<<"输入错误,请重新输入!!\n";
        cin>>alt;
    }
    if(alt==3) return ;
    if(alt==1)
    {
        cout<<"请输入学号!\n";
        cin>>Endid;
    }
    if(alt==2)
    {
        cout<<"请输入姓名!\n";
        cin>>Endname;
    }
    Student *p1 = Sp;
    Student *p2 = p1;
    while(p1)
    {
        if(alt==1&&p1->id==Endid)
        {
            loop=1;
            break;
        }
        if(alt==2&&p1->name==Endname)
        {
            loop=1;
            break;
        }
        p2 = p1;
        p1 = p1->next;
    }
    if(loop==0)
    {
        cout<<"不存在!请重新删除\n\n";
        Del();
    }
    else
    {
        if(p1 == Sp)
        {
            Sp = Sp->next;
            delete p1;
        }
        else
        {
            p2->next = p1->next;
            delete p1;
        }
        cout<<"删除成功";
        Save();
    }
    system("pause\n");
}
void Control::Modify()
{
    cout<<"          ************** 修改方式 **********\n";
    cout<<"          *             1 : 学号           *\n";
    cout<<"          *             2 : 姓名           *\n";
    cout<<"          *             3 : 退出           *\n";
    cout<<"          **********************************\n";
    cout<<"请选择!!1--3\n";
    if(!TestOk_MessageBox())
        return ;
    int alt,Endid,loop=0;
    string Endname;
    cin>>alt;
    while(alt<1||alt>3)
    {
        cout<<"输入错误,请重新输入!!\n";
        cin>>alt;
    }
    if(alt==3) return ;
    if(alt==1)
    {
        cout<<"请输入学号!\n";
        cin>>Endid;
    }
    if(alt==2)
    {
        cout<<"请输入姓名!\n";
        cin>>Endname;
    }
    Student *p1 = Sp;
    while(p1)
    {
        if(alt==1&&p1->id==Endid)
        {
            loop=1;
            break;
        }
        if(alt==2&&p1->name==Endname)
        {
            loop=1;
            break;
        }
        p1 = p1->next;
    }
    if(loop==0)
    {
        cout<<"不存在!请重新修改\n\n";
        Modify();
    }
    else
    {
        ShowStudenInf();
        cout<<"请输入新的信息";
        cin>>*p1;
        Save();
    }
    system("pause\n");
}
void Control::Display()
{
    Student *p1 = Sp;
    ShowStudenInf();
    while(p1)
    {
        cout<<*p1;
        p1 = p1->next;
    }
    system("pause\n");
}
void Control::Find()
{
    cout<<"          ************** 查找方式 **********\n";
    cout<<"          *             1 : 学号           *\n";
    cout<<"          *             2 : 姓名           *\n";
    cout<<"          *             3 : 退出           *\n";
    cout<<"          **********************************\n";
    cout<<"请选择!!1--3\n";
    int alt,Endid,loop=0;
    string Endname;
    cin>>alt;
    while(alt<1||alt>3)
    {
        cout<<"输入错误,请重新输入!!\n";
        cin>>alt;
    }
    if(alt==3) return ;
    if(alt==1)
    {
        cout<<"请输入学号!\n";
        cin>>Endid;
    }
    if(alt==2)
    {
        cout<<"请输入姓名!\n";
        cin>>Endname;
    }
    Student *p1 = Sp;
    ShowStudenInf();
    while(p1)
    {
        if(alt==1&&p1->id==Endid)
        {
            cout<<*p1;
            loop=1;
            break;
        }
        if(alt==2&&p1->name==Endname)
        {
            cout<<*p1;
            loop=1;
            break;
        }
        p1 = p1->next;
    }
    if(loop==0)
    {
        cout<<"不存在!请重新查找\n\n";
        Find();
    }
    system("pause\n");
}
void Control::Sortt()
{
    cout<<"          ************** 排序方式 **********\n";
    cout<<"          *             1 : 学号           *\n";
    cout<<"          *             2 : 成绩           *\n";
    cout<<"          *             3 : 退出           *\n";
    cout<<"          **********************************\n";
    cout<<"请选择!!1--3\n";
    int alt;
    cin>>alt;
    while(alt<1||alt>3)
    {
        cout<<"输入错误,请重新输入!!\n";
        cin>>alt;
    }
    if(alt==3) return ;
    Student *p,*q;
    Student *loop;
    Student *Head = new Student;
    int i,j,n=0;
    for(loop = Sp ; loop ; loop = loop->next)
        n++;
    cout<<n<<endl;
    Head->next = Sp;
    for(i=1; i<n; i++)
    {
        loop = Head;
        for(j=1; j<=n-i; j++)
        {
            p = loop->next;
            q = p->next;
            if(alt==1 && p->id > q->id)
            {
                loop->next=q;
                p->next = q->next;
                q->next = p;
            }
            else if(alt==2 && p->score > q->score)
            {
                loop->next=q;
                p->next = q->next;
                q->next = p;
            }
            loop = loop->next;
        }
    }
    Sp=Head->next;
    delete Head;
    cout<<"             **********排序成功!!!***********\n";
    Save();
}
void Control::Save()
{
    Student *p1 = Sp;
    ofstream outfile("D:\\Student.txt",ios::out);
    while(p1)
    {
        cout<<"正在保存!!\n";
        outfile<<p1->id<<" ";
        outfile<<p1->name<<" ";
        outfile<<p1->sex<<" ";
        outfile<<p1->score<<" ";
        outfile<<p1->phone<<"\n";
        p1 = p1->next;
    }
    cout<<"保存文件成功\n";
    outfile.close();
    system("pause\n");
}
Control::~Control()
{
    Student *p1 = Sp;
    while(p1)
    {
        Sp = Sp->next;
        delete p1;
        p1 = Sp;
    }
}
void Control::ReadFile()
{
    Sp = NULL;
    ifstream infile("D:\\Student.txt",ios::in);
    int id;
    string name;
    string sex;
    long score;
    string phone;
    while(infile)
    {
        infile>>id>>name>>sex>>score>>phone;
        if(!infile) continue;
        Student *p = new Student(id,name,sex,score,phone);
        p->next = Sp;
        Sp = p;
        cout<<"             正在载入 …………!!\n";
    }
    infile.close();
    cout<<"             学生信息载入成功,欢迎使用!!  \n";
    system("pause\n");
}
void Control::StudentMenu()  //菜单函数
{
    system("cls");//清屏!!
    cout<<"               ★★★★★ C++课程设计 ★★★★★   \n";
    cout<<"          ※※※※※※※ 学生信息管理系统 ※※※※※※※※\n";
    cout<<"          ※             1 : 新建                       ※\n";
    cout<<"          ※             2 : 添加                       ※\n";
    cout<<"          ※             3 : 查找                       ※\n";
    cout<<"          ※             4 : 修改                       ※\n";
    cout<<"          ※             5 : 删除                       ※\n";
    cout<<"          ※             6 : 显示                       ※\n";
    cout<<"          ※             7 : 保存                       ※\n";
    cout<<"          ※             8 : 排序                       ※\n";
    cout<<"          ※             9 : 退出系统                   ※\n";
    cout<<"          ※※※※※※※※※※※※※※※※※※※※※※※※\n";
    cout<<"请选择:1-- 9\n";
    int alt;
    cin>>alt;
    switch(alt)
    {
    case 1:
        Build();
        StudentMenu();
        break;
    case 2:
        Add();
        StudentMenu();
        break;
    case 3:
        Find();
        StudentMenu();
        break;
    case 4:
        Modify();
        StudentMenu();
        break;
    case 5:
        Del();
        StudentMenu();
        break;
    case 6:
        Display();
        StudentMenu();
        break;
    case 7:
        Save();
        StudentMenu();
        break;
    case 8:
        Sortt();
        StudentMenu();
        break;
    case 9:
        exit(0);
    default:
        exit(0);
    }
}
int main()
{
    system("color 5E");
    Control a;
    a.StudentMenu();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值