学生信息管理系统(C++版)

    前排提醒,由于本人英语不好所以大部分命名基本以中文拼音为主。(不喜勿喷)

    首先来介绍一下编程实现的基本原理和所含的主要内容吧:

基本原理:利用结构体,动态链表来实现对学生数据的操作,并且包含文件的输入,输出(ASCII形式)等操作。

主要内容:实现链表的增、删、查、改、插入、排序、清空、以及文件的输入与输出功能。

#include<iostream>
#include<fstream>
#include<cstring>
#define Fname "E:/C++/上机作业/教师操作系统/教师操作系统1.0/data.txt"
using namespace std;
static int count=0;
bool mark = false;
 
struct student
{
    string name;
    int age;
    int chengJi;
    student* next;
};
 
void qingping()                              //清屏
{
    system("pause");
    system("cls");
}
 
void output_File(char* filename ,student*& head)       //写入文件
{
    if(head==NULL)     //判断链表中是否有数据
    {
        cerr<<"链表中没数据,无法写入......"<<endl;
        qingping();
        return;
    }
    ofstream outfile(filename,ios::app);   //打开文件
    if(!outfile)       //判断是否打开
    {
        cerr<<"没打开"<<endl;
        qingping();
        return;
    }
    student* pre=head;
    while(pre!=0)     //从链表输入到文件中
    {
        outfile<<pre->name<<"  "<<pre->age<<"  "<<pre->chengJi<<endl;
        pre=pre->next;
    }
    outfile.close();         //关闭文件
    cout<<"成功写入......"<<endl;
    qingping();
}

void input_File(char* filename,student*& head)               //读取文件
{
    ifstream infile(filename,ios::in);   //打开文件
    if(!infile)            //判断是否打开
    {
        cerr<<"没打开";
        qingping();      //清屏
        return;
    }
    if(infile.eof())         //文件判空         
    {
        cout<<"文件中没数据"<<endl;
        infile.close();
        qingping();
        return;
    }

    //遍历到链表尾部,方便插入数据
    student* pre = nullptr;
    if(head == nullptr){
        mark = true;
        pre = head = new student;
    } else {
        //遍历到链表尾部
        pre = head;
        while (pre->next != nullptr)
            pre = pre->next;
    }

    string data;   //读取一个数据
    while(!infile.eof())         //读取数据
    {
        string contents[3];
        for(int i = 0; i <3; i++)   //循环读取三个
        {
            infile>>data;
            contents[i] = data;
        }
        student* p=new student;
        p->name = contents[0];
        p->age = stoi(contents[1]);   //将字符串转换为int
        p->chengJi = stoi(contents[2]);
        pre->next=p;          //建立节点关系
        pre=p;
        p->next=NULL;
        count++;
    }
    infile.close();               //关闭文件
    cout<<"读取成功......"<<endl;
    qingping();                             
}
 
void zenJia(student*& head)                              //增加
{
    count++;
    student* p=new student;
    cout<<"输入学生信息(姓名,年龄,成绩)"<<endl;
    cin>>p->name>>p->age>>p->chengJi;
    if(head==NULL)
    {
        head=p;
        head->next=NULL;
        cout<<"添加成功........"<<endl;
        qingping();
        return;
    }
    student* pre=head;
    while(pre->next!=NULL)
        pre=pre->next;
    pre->next=p;
    p->next=NULL;
    cout<<"添加成功........"<<endl;
    qingping();
}
 
void xianShi(const student* pre)              //显示
{
    if(pre==nullptr)
    {
        cout<<"此为空表"<<endl;
        qingping();
        return;
    }
    if(mark)
    {
        pre = pre->next;
        while(pre->next!=NULL)
    {
        cout<<"name:"<<pre->name<<"   age:"<<pre->age<<"   scores:"<<pre->chengJi<<endl;
        pre=pre->next;
    }
    qingping();
    return;
    }

    while(pre!=NULL)
    {
        cout<<"name:"<<pre->name<<"   age:"<<pre->age<<"   scores:"<<pre->chengJi<<endl;
        pre=pre->next;
    }
    qingping();
}

void xiaoHui(student*& head)                 //销毁
{
    if(head==NULL)
    {
        cout<<"此为空表"<<endl;
        qingping();
        return;
    }
    student* pre;
    while(head!=NULL)
    {
        pre=head->next;
        delete head;
        head=pre;
    }
    count=0;
    cout<<"销毁成功......"<<endl;
    qingping();
}
 
void chaRu(student*& head)                 //插入
{
    int n;
    student* p=new student;
    cout<<"当前共有"<<count<<"个元素,输入插入位置: ";
    cin>>n;
    cout<<"请输入信息"<<endl;
    cin>>p->name>>p->age>>p->chengJi;
    count++;
    if(head==NULL)
    {
        head=p;
        head->next=NULL;
        qingping();
        return;
    }
    if(n==1)
    {
        p->next=head;
        head=p;
        qingping();
        return;
    }
    student* pre=head;
    student* temp=new student;
    for(int i=1;i<n-1;i++)
        pre=pre->next;
    temp=pre->next;
    pre->next=p;
    p->next=temp;
    qingping();
}
 
void shanChu(student*& head)                  //删除
{
    if(head==NULL)
    {
        cout<<"空表无法删除"<<endl;
        qingping();
        return;
    }
    int n;
    cout<<"当前共有"<<count<<"个元素,输入删除的位置";
    cin>>n;
    count--;
    if(n==1)
    {
        student* temp=head->next;
        delete head;
        head=temp;
        qingping();
        return;
    }
    student* pre=head;
    student* temp=new student;
    for(int i=1;i<n-1;i++)
        pre=pre->next;
    temp=pre->next;
    delete pre->next;
    pre->next=temp->next;
    qingping();
 
}
 
void chaZhao(student*& head)          //查找
{
    if(head==NULL)
    {
        cout<<"此为空表"<<endl;
        qingping();
        return;
    }
    string str;
    cout<<"输入查找的学生名字: "<<endl;
    cin>>str;
    student* pre=head;
    while(pre!=NULL)
    {
        if(pre->name==str)
        {
            cout<<"信息为:"<<endl;
            cout<<"name:"<<pre->name<<"   age:"<<pre->age<<"   scores:"<<pre->chengJi<<endl;
            qingping();
            return;
        }
        pre=pre->next;
    }
    cout<<"查无此人"<<endl;
    qingping();
}
 
void xouGai(student*& head)               //修改
{
    if(head==NULL)
    {
        cout<<"空表"<<endl;
        qingping();
        return;
    }
    int n;
    string str;
    cout<<"输入想修改的学生名字:  ";
    cin>>str;
    qingping();
    student* pre=head;
    while(pre!=NULL)
    {
        if(pre->name==str)
        {
            do
            {
                cout<<"当前学生的信息为:"<<endl;
                cout<<"name:"<<pre->name<<"   age:"<<pre->age<<"   scores:"<<pre->chengJi<<endl;
                cout<<"操作数字;"<<endl;
                cout<<"1.name  2.age  3.scores   0.quit"<<endl;
                cout<<"输入数字进行操作: ";
 
                cin>>n;
                if(n==1)
                    cin>>pre->name;
                else if (n==2)
                    cin>>pre->age;
                else if (n==3)
                    cin>>pre->chengJi;
                else if (n==0)
                {
                    qingping();
                    return;
                }
                else
                    cout<<"输入错误,请重新输入"; 
                system("cls");
            } while(n!=0);
        }
        pre=pre->next;
    }
    cout<<"查无此人"<<endl;
    qingping();
}
 
void paiXu(student*& head)                 //排序
{
    if(head==NULL)
    {
        cout<<"空表"<<endl;
        qingping();
        return;
    }
    if(count==1)
    {
        qingping();
        return;
    }
    student* pre=head;
    student St[count],temp;
    int i,j;
    for(i=0;i<count;i++)
    {
        St[i]=*pre;
        pre=pre->next;
    }
    for(i=0;i<count-1;i++)
    {
        for(j=0;j<count-i-1;j++)
            {
                if(St[j].name>St[j+1].name)
                {
                    temp=St[j];
                    St[j]=St[j+1];
                    St[j+1]=temp;
                }
            }
    }
    pre=head;
    for(i=0;i<count;i++)
    {
        pre->name=St[i].name;
        pre->age=St[i].age;
        pre->chengJi=St[i].chengJi;
        pre=pre->next;
    }
    qingping();
    
}
 
void caiDan(student*& head)                 //菜单
{
 
    do
    {
        cout<<"             菜单"<<endl;
        cout<<"1.增加     ";
        cout<<"2.显示     ";
        cout<<"3.插入"<<endl;
        cout<<"4.删除     ";
        cout<<"5.查找     ";
        cout<<"6.修改"<<endl;
        cout<<"7.排序     ";
        cout<<"8.销毁     ";
        cout<<"0.退出"<<endl;
        cout<<"11.将数据写入文件"<<endl;
        cout<<"12.从文件读取数据"<<endl;
        cout<<"输入数字进行操作: ";
        int n;
        cin>>n;
        switch(n)
    {
        case 1:
            zenJia(head);
            break;
        case 2:
            xianShi(head);
            break;       
        case 3:
            chaRu(head);
            break;
        case 4:
            shanChu(head);
            break;
        case 5:
            chaZhao(head);
            break;
        case 6:
            xouGai(head);
            break;
        case 7:
            paiXu(head);
            break;
        case 8:
            xiaoHui(head);
            break;
        case 11:
            output_File(Fname,head);
            break;
        case 12:
            input_File(Fname,head);
            break;
        case 0:
            cout<<"退出成功";
            exit(1);
            break;
        default:
            cout<<"输入错误"<<endl;
            qingping();
            break;
    }
    } while (1);
    
}
 
int main()
{
    student* head=NULL;
    caiDan(head);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值