c++课程设计(新生入学管理系统)

这个学期快要结束了,学校布置的课程设计也要完成并检验,因为之前自学c语言的原因因此我还是比较快的完成了课程设计,虽然其中也有一些bug,而其中c++的关键还是在类与对象的使用上,这个新生入学管理系统中,完成增删改查文件的保存与读取
代码中建立两个类一个是存储学生信息,一个用于存储各种功能
1,创建链表
先开辟一个空间基本套路使其不断循环将内容存于空间中,且当输入姓名为0的时候退出
2,添加
首先先开辟一片空间,将内容输入后再将其添加在已存在的链表的后面
3.删除
一种是头结点:直接让头指针移向下一位,再把原结点free掉。其他结点,令p1指针先向后移动一位,若不是要删除的,就每次向后位移。直到出现要删除结点,把该指针的上一个next搭到它的下一个,把它隔过去,再free掉。
4.搜索
通过对链表的遍历,当名字相同时打印出信息
5.修改
首先重新定义学生信息的几个内容,通过遍历和判断找到需要修改的学生信息,在函数重新定义的学生信息中输入的内容,通过copy将原有的信息更换
6.排序
我所用的单链表的选择排序,通过找出最小值,通过memcpy,strcpy,"="将内容排序1
7.统计
通过性别的统计,定义两个字符串,与链表中已有的性别进行比较,当相同时对定义的两个不同的整数加一
8.读文件
首先创建一个新链表,头结点为空,将文件中的内容逐个输入到该链表中
9.存文件
把头赋给p1后,只要p1存值,就把它写入到out所新建的那个文件里,再让p1移动下一个

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstring>
#include<stdlib.h>
using namespace std;
int n=0;
char stop[12]= {"0"};
char name[12];
class Student
{
public:
    char name[12];
    char sex[3];
    char study[12];
    char addr[12];
    int birth[3];
    int engli;
    Student *next;
};
class information
{
private:
    Student *head,*p1,*p2,*p3;
public:
    information () {};
    Student *creat();
    Student *add(Student *head);
    Student *delet(Student *head);
    void *search(Student *head);
    Student *modify(Student *head);
    Student *sort(Student*head);
    Student *statistics(Student*head);
    void display(Student *head);
    void displaynode(Student *head);
    void write_file(Student *head);
    Student * read_file();
    ~information () {};
};
class manage
{
public:
    char menu();
    int choose();
};
information s;
Student *information::creat()
{
    p1=p2=new Student;
    head=NULL;
    char sex1[3];
    char sex2[3];
    strcpy(sex1, "男");
    strcpy(sex2, "女");
    cout<<"请输入学生的基本信息:以姓名为0结束。\n";
    while(1)
    {
        cout<<"姓名:  \t";
        cin>>p1->name;
        if( strcmp(stop,p1->name)==0 )
        {
            break;
        }
        cout<<"性别:  \t";
        cin>>p1->sex;
        while(strcmp(sex1,p1->sex)!=0 &&strcmp(sex2,p1->sex)!=0 )
        {
            cout<<"请输入格式  男/女,请重新输入!"<<endl;
            cout<<"性别:  \t";
            cin>>p1->sex;
        }
        cout<<"专业:  \t";
        cin>>p1->study;
        cout<<"家庭住址:  \t";
        cin>>p1->addr;
        cout<<"出生年:";
        cin>>p1->birth[0];
        while(p1->birth[1]<1 || p1->birth[1]>12)
        {
            cout<<"出生月:";
            cin>> p1->birth[1];
            if(p1->birth[1]<1 || p1->birth[1]>12)
            {
                cout<<"月份应为1-12月,请重新输入!"<<endl;
            }
        }
         cout<<"出生日:";
        cin>>p1->birth[2];
        while(p1->birth[2]<1 || p1->birth[2]>31)
        {
            cout<<"每月应为1-31天,请重新输入!"<<endl;
            cout<<"出生日:";
            cin>>p1->birth[2];
        }
        cout<<"英语:";
        cin>>p1->engli;
        while(p1->engli<0 || p1->engli>100)
        {
                cout<<"英语成绩必须在0-100之间,请重新输入!"<<endl;
                cout<<"英语:";
                cin>>p1->engli;
        }
        system("cls");
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=new Student;
    }
    p2->next=NULL;
    return head;
}
Student *information::statistics(Student*head)
{
    p1=new Student;
    p1=head;
    int x=0,y=0;
    char sex1[3];
    char sex2[3];
    strcpy(sex1, "男");
    strcpy(sex2, "女");
    for(p1=head; p1!=NULL; p1=p1->next)
    {
        if(strcmp(sex1,p1->sex)==0 )
        {
            x=x+1;
        }
        if(strcmp(sex2,p1->sex)==0)
        {
            y=y+1;
        }
    }
    cout<<"男:      "<<x<<endl;
    cout<<"女:      "<<y<<endl;
    return head;
}

Student *information::add(Student *head)
{
    p1=p3=new Student;
    p1=head;
    char sex1[3];
    char sex2[3];
    strcpy(sex1, "男");
    strcpy(sex2, "女");
    if(p1==NULL)
    {
        cout<<"在添加前,先新建!"<<endl;
        return 0;
    }
    else
    {
        cout<<"请输入要添加学生的信息!\n";
        cout<<"姓名:  \t";
        cin>>p3->name;
        cout<<"性别:  \t";
        cin>>p3->sex;
        while(strcmp(sex1,p3->sex)!=0 &&strcmp(sex2,p3->sex)!=0 )
        {
            cout<<"请输入格式  男/女,请重新输入!"<<endl;
            cout<<"性别:  \t";
            cin>>p3->sex;
        }
        cout<<"专业:  \t";
        cin>>p3->study;
        cout<<"家庭住址:  \t";
        cin>>p3->addr;
        cout<<"出生年:";
        cin>>p3->birth[0];
        while(p3->birth[1]<1 || p3->birth[1]>12)
        {
            cout<<"出生月:";
            cin>> p3->birth[1];
            if(p3->birth[1]<1 || p3->birth[1]>12)
            {
                cout<<"月份应为1-12月,请重新输入!"<<endl;
            }
        }
        cout<<"出生日:";
        cin>>p3->birth[2];
        while(p3->birth[2]<1 || p3->birth[2]>31)
        {
            cout<<"每月应为1-31天,请重新输入!"<<endl;
            cout<<"出生日:";
            cin>>p3->birth[2];
        }
        cout<<"英语:";
        cin>>p3->engli;
        while(p3->engli<0 || p3->engli>100)
        {
                cout<<"英语成绩必须在0-100之间,请重新输入!"<<endl;
                cout<<"英语:";
                cin>>p3->engli;
        }
        cout<<"\n";
        p3->next=p1->next;
        p1->next=p3;
        cout<<"添加成功!\n";
    }
    return head;
}

Student *information::delet(Student *head)
{
    p2=p1=new Student;
    if(head==NULL)
    {
        cout<<"没有数据,无法删除";
        return 0;
    }
    cout<<"请输入要删除学生的名字:\n";
    cin>>name;
    p2=p1=head;
    int j=0;
    if( ( strcmp(name,head->name)==0 ) && (head!=NULL))
    {
        head=head->next;
        free(p1);
        j=1;
    }
    else
    {
        p1=head->next;
        while(p1!=NULL)
        {
            if(strcmp(name,p1->name)==0)
            {
                p2->next=p1->next;
                free(p1);
                j=1;
                break;
            }
            else
            {
                p2=p1;
                p1=p2->next;
            }
        }
    }
    if(j==0)
        cout<<"此学生不存在,删除失败!\n";
    else
        cout<<"删除成功!\n";
    return head;
}

void *information::search(Student *head)
{
    char name[20];
    p1=new Student;
    cout<<"请输入要查找学生的姓名:\n";
    cin>>name;
    p1=head;
    for(p1=head; p1!=NULL; p1=p1->next)
    {
        if(strcmp(name,p1->name)==0 )
        {
            cout<<"姓名:"<<p1->name<<" 性别:"<<p1->sex<<" 专业:"<<p1->study;
            cout<<" 家庭住址:"<<p1->addr<<endl;
            cout<<p1->birth[0]<<"年";
            cout<<p1->birth[1]<<"月:"<<p1->birth[2]<<"日 出生:";
            cout<<"年龄:"<<2017-p1->birth[0]<<endl;
            cout<<"英语成绩:"<<p1->engli<<"\n\n";
        }
    }
    return head;
};

Student *information::modify(Student *head)
{

    if(head==NULL)
    {
        cout<<"没有数据,先新建吧";
        return 0;
    }
    char name1[12];
    char sex[3];
    char study[12];
    char addr[12];
    int birth[3];
    int engli;
    p1=new Student;
    int j=0;
    p1=head;
    cout<<"请输入你要更改学生的姓名:\n";
    cin>>name;

    if(strcmp( name, head->name)==0)
    {
        cout<<"显示要修改学生的信息:\n";
        s.displaynode(p1);
        cout<<"请输入要更改学生的信息:\n";
        cout<<"姓名:  \t";
        cin>>name1;
        cout<<"性别:  \t";
        cin>>sex;
        cout<<"专业:  \t";
        cin>>study;
        cout<<"家庭住址:  \t";
        cin>>addr;
        cout<<"出生年:";
        cin>>birth[0];
        cout<<"出生月:";
        cin>>birth[1];
        cout<<"出生日:";
        cin>>birth[2];
        cout<<"英语:";
        cin>>engli;
        strcpy(head->name,name1);
        strcpy(head->sex,sex);
        strcpy(head->study,study);
        strcpy(head->addr,addr);
        head->birth[0]=birth[0];
        head->birth[1]=birth[1];
        head->birth[2]=birth[2];
        head->engli=engli;
        j=1;
    }
    else
    {
        p1=head->next;
        while(p1!=NULL)
        {
            if(strcmp(p1->name,name)==0)
                cout<<"显示要修改学生的信息:\n";
            s.displaynode(p1);
            cout<<"请输入要更改学生的信息:\n";
            cout<<"姓名:  \t";
            cin>>name1;
            cout<<"性别:  \t";
            cin>>sex;
            cout<<"专业:  \t";
            cin>>study;
            cout<<"家庭住址:  \t";
            cin>>addr;
            cout<<"出生年:";
            cin>>birth[0];
            cout<<"出生月:";
            cin>>birth[1];
            cout<<"出生日:";
            cin>>birth[2];
            cout<<"英语:";
            cin>>engli;
            strcpy(p1->name,name1);
            strcpy(p1->sex,sex);
            strcpy(p1->study,study);
            strcpy(p1->addr,addr);
            p1->birth[0]=birth[0];
            p1->birth[1]=birth[1];
            p1->birth[2]=birth[2];
            p1->engli=engli;
            j=1;
            break;
        }
    }
    if(j==0)
        cout<<"没有找到你要更改的学生,更改失败!\n";
    else
        cout<<"更改成功!\n";
    return head;
}

void information::display(Student *head)
{
    p1=head;
    if(p1==NULL)
        cout<<"这是一个空表!请先输入学生信息。"<<endl;
    else
    {
        while(p1!=NULL)
        {
            s.displaynode(p1);
            p1=p1->next;
        }
    }
}
void information::displaynode(Student *head)
{
    p1=head;
    if(p1==NULL)
        cout<<"这是一个空表!请先输入学生信息。"<<endl;
    else
    {
        cout<<"姓名:"<<p1->name<<" 性别:"<<p1->sex<<" 专业:"<<p1->study;
        cout<<" 家庭住址:"<<p1->addr<<endl;
        cout<<"生日: "<<p1->birth[0]<<"年";
        cout<<p1->birth[1]<<"月"<<p1->birth[2]<<"日"<<endl;
        cout<<"年龄:"<<2017-p1->birth[0]<<endl;
        cout<<"英语成绩:"<<p1->engli<<"\n\n";
    }
}


Student *information::read_file()
{
    int i=0;
    char name2[12];
    char study[12];
    char sex[3];
    char addr[12];
    int birth[3];
    int engli;
    p1=p2=new Student;
    head=NULL;
    ifstream in;
    in.open("myC++work.txt");
    if(!in)
    {
        cout<<"打开文件失败!"<<endl;
    }
    else
    {
        while(in>>name2>>sex>>study>>addr>>birth[0]>>birth[1]>>birth[2]>>engli)
        {
            strcpy(p1->name,name2);
            strcpy(p1->sex,sex);
            strcpy(p1->study,study);
            strcpy(p1->addr,addr);
            p1->birth[0]=birth[0];
            p1->birth[1]=birth[1];
            p1->birth[2]=birth[2];
            p1->engli=engli;
            i++;
            if(i==1)
            {
                head=p2=p1;
            }
            else
            {
                p2->next=p1;
            }
            p2=p1;
            p1=new Student;
            p1->next=NULL;
        }
        cout<<"成功读取文件!内容如下:"<<endl;
    }
    return head;
}

void information::write_file(Student *head)
{
    ofstream out;
    out.open("myC++work.txt");
    if(!out)
    {
        cout<<"打开文件失败!"<<endl;
    }
    p1=NULL;
    p1=head;
    while(p1)
    {
        out<<p1->name<<setw(5)<<p1->sex<<setw(5)<<p1->study<<setw(5)<<p1->addr<<setw(5)<<p1->birth[0]<<setw(5)<<p1->birth[1]<<setw(5)<<p1->birth[2]<<setw(5)<<p1->engli<<endl;
        p1=p1->next;
    }
    out.close();
    cout<<"写入完毕。";
}

Student *information::sort(Student *head)
{
    Student *p,*q,*small;
    char name[12];
    char sex[3];
    char study[12];
    char addr[12];
    int birth[3];
    int engli;
    p=head;
    for(; p->next!=NULL; p=p->next)
    {
        small=p;
        for(q=p->next; q; q=q->next)
        {
            if(q->engli<small->engli)
            {
                small=q;
            }
        }
        if(small!= p)
        {
            engli=p->engli;
            p->engli=small->engli;
            small->engli=engli;
            strcpy(name,p->name);
            strcpy(p->name,small->name);
            strcpy(small->name,name);
            strcpy(study,p->study);
            strcpy(p->study,small->study);
            strcpy(small->study,study);
            memset(birth,0,sizeof(birth));
            memcpy(birth,p->birth,sizeof(birth));
            memcpy(p->birth,small->birth,sizeof(birth));
            memcpy(small->birth,birth,sizeof(birth));
            strcpy(addr,p->addr);
            strcpy(p->addr,small->addr);
            strcpy(small->addr,addr);
            strcpy(sex,p->sex);
            strcpy(p->sex,small->sex);
            strcpy(small->sex,sex);
        }
    }
    return head;
}
char  manage::menu()
{
    char ch;
    cout<<endl;
    cout<<endl;
    cout<<"                                        ***   ***     "<<endl;
    cout<<"                                       ***** *****    "<<endl;
    cout<<"         *****************学生信息管理****************"<<endl;
    cout<<"                        1.新增学生信息  *********     "<<endl;
    cout<<"                        2.插入学生信息    *****       "<<endl;
    cout<<"                        3.删除学生信息     ***        "<<endl;
    cout<<"                        4.学生信息搜索                "<<endl;
    cout<<"                        5.修改学生信息                "<<endl;
    cout<<"                        6.按照(英语)成绩排序          "<<endl;
    cout<<"                        7.统计男/女人数                  "<<endl;
    cout<<"                        8.读取学生信息                "<<endl;
    cout<<"                        9.保存信息退出                "<<endl;
    cout<<"         ********************WPSEC********************"<<endl;
    cin>>ch;
    return ch;
}
int manage::choose()
{
    manage m;
    information s;
    Student *head;
    head=NULL;
    int n=0;
    char c;
    while(1)
        switch (m.menu())
        {
        case'1':
                head=s.creat();
            system("pause");
            system("cls");
            break;
        case'2':
                head=s.add(head);
            system("pause");
            system("cls");
            break;
        case'3':
                head=s.delet(head);
            system("pause");
            system("cls");
            break;
        case'4':
                s.search(head);
            system("pause");
            system("cls");
            break;
        case'5':
                head=s.modify(head);
            system("pause");
            system("cls");
            break;
        case'6':
                s.sort(head);
            s.display(head);
            system("pause");
            system("cls");
            break;
        case'7':
                s.statistics(head);
            system("pause");
            system("cls");
            break;
        case'8':
                head=s.read_file();
            s.display(head);
            system("pause");
            system("cls");
            break;
        case'9':
                s.write_file(head);
            cout<<"谢谢使用!再见!\n";
            system("pause");
            system("cls");
            return 0;
        default:
            cout<<"选择有错,请重新选择\n";
        }
}
int main ()
{
    manage m;
    m.choose();
    return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353