学生信息管理系统【C++实现】

课程设计要求:

1、课程设计的任务要求:
大数据时代来临,寻找东林最美味的美食,最值得读的书。
已知:现有3个文件的结构,分别记录学生学籍卡表、校园一卡通发行表、一卡通使用记录表。
2、程序功能要求:
2.1设计程序运行界面;
2.2编写程序按已知表样建立相应文件;
2.3编写程序分别向建立文件中录入测试样本数据;
2.4 编写程序实现各种数据的统计功能。
2.4.1统计东林最好吃的美食,给出最好吃的美食都是哪些同学常吃的。
2.4.2统计东林借阅最多的书,给出都是哪些同学借阅的。

bianliang.h(自定义头文件,功能是定义两个链表)

#ifndef BIANLIANG_H_INCLUDED
#define BIANLIANG_H_INCLUDED
using namespace std;
struct node_stu
{
    long long num,id;
    string grade,name,major,place;//卡号、学号、年级、姓名、专业、生源地
    struct node_stu *next;
};
struct node_bf
{
    long long id;
    string book,food;
    struct node_bf *next;
};
#endif // BIANLIANG_H_INCLUDED

hanshu.h(自定义头文件,功能是引用函数)

#ifndef HANSHU_H_INCLUDED
#define HANSHU_H_INCLUDED

extern void pr(char *p1);
extern void jingdutiao();
extern void menu();

extern struct node_stu *build_stu();
extern struct node_bf  *build_bf();

extern struct node_stu *add_stu(struct node_stu *head1);
extern struct node_bf  *add_bf(struct node_stu *head1,struct node_bf *head2);//加入书本和美食信息

extern struct node_stu *modify_stu(struct node_stu *head1);
extern struct node_bf  *modify_bf(struct node_bf *head2);

extern struct node_stu *del_stu(struct node_stu *head1);
extern struct node_bf  *del_bf(struct node_bf *head2);

extern struct node_stu *output_stu(struct node_stu *head1);
extern struct node_bf  *output_bf(struct node_bf *head2);

extern void exit_stu(struct node_stu *head1);
extern void exit_bf(struct node_bf *head2);

extern int get1();
extern int get2();

#endif // HANSHU_H_INCLUDED

new_build.cpp(写所有需要用到的函数,核心代码)

#include <bits/stdc++.h>
#include <windows.h>
#include "bianliang.h"
typedef long long ll;
using namespace std;
int cnt_stu,cnt_bf;
void pr(char *p1)//延迟打印效果
{
    Sleep(200);
    while(1)
    {
        if(*p1!=0)printf("%c",*p1++);
        else break;
        Sleep(35);
    }
}
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)//颜色
{
    HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));
}
void jingdutiao()//进度条
{
    pr("程序正在加载中,请稍后...\n\n");
    for(int i=1;i<=50;i++)
    {
        SetColor(0,14);
        printf(" ");
        printf("%d%%",2*i);
        Sleep(101-1*i);
        printf("\b\b\b");
    }
    SetColor(15,0);
    printf("\n");
    pr("\n\n加载完成!即将进入...");
    Sleep(2000);
}
void menu()
{
	system("cls");//清屏
    printf("\n\t\t\t\t\t——————欢迎使用 nefu_ljw 正版软件!——————\n\n");
    printf("\t\t\t\t\t当前已保存 %d 个学生的基本信息\n\n",cnt_stu);
    printf("\t\t\t\t\t当前已保存 %d 个借书和点菜信息\n\n",cnt_bf);
    printf("\t\t\t\t\t这里是主菜单页面,您可以输入数字来进行操作:\n\n");
    printf("\t\t\t\t\t|#1 新建学生信息系统(只需要新建一次即可)\n\n");
    printf("\t\t\t\t\t|#2 增加学生的基本信息\n\n");
    printf("\t\t\t\t\t|#3 修改学生的基本信息\n\n");
    printf("\t\t\t\t\t|#4 删除学生的基本信息\n\n");
    printf("\t\t\t\t\t|#5 查看学生的基本信息\n\n");
    printf("\t\t\t\t\t|#6 增加借书和点菜信息\n\n");
    printf("\t\t\t\t\t|#7 修改借书和点菜信息\n\n");
    printf("\t\t\t\t\t|#8 删除借书和点菜信息\n\n");
    printf("\t\t\t\t\t|#9 查看借书和点菜信息\n\n");
    printf("\t\t\t\t\t|#0 保存数据并退出系统\n\n");
    pr("请输入一个数字,选择您想要的功能:\n");
    //printf("请输入一个数字,选择您想要的功能:\n");
}
void insert_stu(struct node_stu *head1,ll num,string name,string major,string grade,string place,ll id)//链表中插入学生信息时按学号的从小到大排序
{
    struct node_stu *p,*newnode;
    p=head1;
    while(p!=NULL)
    {
        if(p->next==NULL)
        {
            p->next=new node_stu;//不能用malloc!
            p=p->next;
            p->num=num;
            p->name=name;
            p->major=major;
            p->grade=grade;
            p->place=place;
            p->id=id;
            p->next=NULL;
            break;
        }
        else
        {
            if( num > p->num && num < p->next->num )
            {
                newnode=new node_stu;//不能用malloc!
                newnode->num=num;
                newnode->name=name;
                newnode->major=major;
                newnode->grade=grade;
                newnode->place=place;
                newnode->id=id;
                newnode->next=p->next;
                p->next=newnode;
                break;
            }
        }
        p=p->next;
    }
}
void insert_bf(struct node_bf *head2,ll id,string book,string food)//链表中插入借书和点菜时按卡号的从小到大排序,如果卡号相同,则后输入的排在最后面
{
    struct node_bf *p,*newnode;
    p=head2;
    while(p!=NULL)
    {
        if(p->next==NULL)
        {
            //p->next=(struct node_bf*)malloc(sizeof(struct node_bf));
            p->next=new node_bf;//不能用malloc!
            p=p->next;
            p->id=id;
            p->book=book;
            p->food=food;
            p->next=NULL;
            break;
        }
        else
        {
            if( id >= p->id && id < p->next->id )
            {
                //newnode=(struct node_bf*)malloc(sizeof(struct node_bf));
                newnode=new node_bf;//不能用malloc!
                newnode->id=id;
                newnode->book=book;
                newnode->food=food;
                newnode->next=p->next;
                p->next=newnode;
                break;
            }
        }
        p=p->next;
    }
}
bool find_num(struct node_stu *head1,ll num)//查找第一个链表中是否已经有了num这个学号
{
    struct node_stu *p;
    p=head1->next;
    while(p!=NULL)
    {
        if(p->num==num)return 1;
        p=p->next;
    }
    return 0;
}
bool find_id(struct node_stu *head1,ll id)//查找第一个链表中是否已经有了id这个卡号
{
    struct node_stu *p;
    p=head1->next;
    while(p!=NULL)
    {
        if(p->id==id)return 1;
        p=p->next;
    }
    return 0;
}
struct node_stu *build_stu()//新建一个信息管理系统函数
{
    ll id,num;
    string grade,name,major,place;
    struct node_stu *H;
    //H=(struct node_stu*)malloc(sizeof(struct node_stu));//这是错的!!!
    H=new node_stu;//这里一定不能用malloc分配内存!!!
    H->num=-1;//赋值头节点为-1后可以特判插入的学号比第一个元素小的情况
    H->next=NULL;
    system("cls");
    printf("请依次输入每个学生的 学号 姓名 专业 年级 生源地 校园一卡通号码:\n");
    while(1)
    {
        cnt_stu++;
        printf("\n请输入学生的学号:");
        cin>>num;
        if(cnt_stu>1&&find_num(H,num)){printf("已经存在学号为 %lld 的学生,请重新输入!\n\n",num);cnt_stu--;continue;}
        printf("请输入学生的姓名:");
        cin>>name;
        printf("请输入学生的专业:");
        cin>>major;
        printf("请输入学生的年级:");
        cin>>grade;
        printf("请输入学生的生源地:");
        cin>>place;
        printf("请输入学生的校园一卡通号码:");
        cin>>id;
        insert_stu(H,num,name,major,grade,place,id);
        printf("\n是否继续录入学生信息?是:1 否:0\n");
        int f;cin>>f;
        if(f==0)break;
    }
    printf("\n输入的所有学生信息已录入成功!进入主菜单后输入数字5即可查看所有学生基本信息!\n\n");
    system("pause");
	return(H);
}
struct node_bf *build_bf()//新建一个借书和点菜信息管理系统
{
    cnt_bf=0;
    struct node_bf *H;
    //H=(struct node_bf*)malloc(sizeof(struct node_bf));//这是错的!
    H=new node_bf;//这里一定不能用malloc分配内存!!!
    H->id=-1;//赋值头节点为-1后可以特判插入的卡号比第一个元素小的情况
    H->next=NULL;
    return(H);
};
struct node_stu *add_stu(struct node_stu *head1)//增加学生信息
{
    if(cnt_stu==0){printf("\n请先新建学生信息系统后(输入数字1),再增加学生信息!\n\n");system("pause");return(head1);}
    printf("\n确定要增加学生信息吗?是:1 否:0\n");
    int f;cin>>f;
    if(f==0)return(head1);
    ll id,num;
    string grade,name,major,place;
    system("cls");
    printf("请依次输入要增加的学生的 学号 姓名 专业 年级 生源地 校园一卡通号码:\n");
    cnt_stu++;
    printf("\n请输入要增加的学生的学号:");
    cin>>num;
    if(cnt_stu>1&&find_num(head1,num)){printf("已经存在学号为 %lld 的学生,请重新输入!\n\n",num);cnt_stu--;system("pause");return(head1);}
    printf("请输入要增加的学生的姓名:");
    cin>>name;
    printf("请输入要增加的学生的专业:");
    cin>>major;
    printf("请输入要增加的学生的年级:");
    cin>>grade;
    printf("请输入要增加的学生的生源地:");
    cin>>place;
    printf("请输入要增加的学生的校园一卡通号码:");
    cin>>id;
    insert_stu(head1,num,name,major,grade,place,id);
    printf("\n录入成功!进入主菜单后输入数字5即可查看所有学生基本信息!\n\n");
    system("pause\n");
	return(head1);
};
struct node_bf *add_bf(struct node_stu *head1,struct node_bf *head2)//增加借书和点菜信息,head1是学生基本信息链表头,head2是保存借书和点菜信息的链表头
{
    if(cnt_stu==0){printf("\n当前还没有学生信息,请先增加学生信息!\n\n");system("pause");return(head2);}
    printf("\n确定要增加借书和点菜信息吗?是:1 否:0\n");
    int f;cin>>f;
    if(f==0)return(head2);
    ll id;
    string book,food;
    system("cls");
    printf("请依次输入要增加的 校园一卡通号码 该卡号借书记录 该卡号点菜记录:\n");
    while(1)
    {
        printf("\n请输入校园一卡通号码:");
        cin>>id;
        if(!find_id(head1,id)){printf("不存在卡号为 %lld 的学生,请先增加该卡号学生的信息!\n\n",id);system("pause");continue;}
        printf("请输入该卡号借书记录:");
        cin>>book;
        printf("请输入该卡号点菜记录:");
        cin>>food;
        insert_bf(head2,id,book,food);
        cnt_bf++;
        printf("\n是否继续录入借书和点菜信息?是:1 否:0\n");
        int flag;cin>>flag;
        if(flag==0)break;
    }
    printf("\n输入的所有信息已录入成功!进入主菜单后输入数字9即可查看所有借书和点菜信息!\n\n");
    system("pause\n");
    return(head2);
};
struct node_stu *modify_stu(struct node_stu *head1)//修改学生信息
{
    if(cnt_stu==0){printf("\n当前还没有学生信息,请先增加学生信息!\n\n");system("pause");return(head1);}
    printf("\n确定要修改学生信息吗?是:1 否:0\n");
    int f1;cin>>f1;
    if(f1==0)return(head1);
    system("cls");
    ll id,num;
    string grade,name,major,place;
    printf("请输入您要修改的学生的学号:");
    cin>>num;
    struct node_stu *p;
    p=head1->next;
    int flag=0;
    while(p!=NULL)
    {
        if(p->num==num){flag=1;break;}
        p=p->next;
    }
    if(flag==0){printf("\n没有找到您输入的学号,请重新输入!\n\n");system("pause");return(head1);}
    else
    {
        printf("\n您要修改的学生信息为:\n");
        printf("学号:%lld\t姓名:%s\t专业:%s\t年级:%s\t生源地:%s\t校园一卡通号码:%lld\n",p->num,p->name.c_str(),p->major.c_str(),p->grade.c_str(),p->place.c_str(),p->id);
        printf("\n确定要修改该学生的信息吗?(该学生的卡号对应的借书和点菜信息不会被修改)是:1 否:0\n");
        int f;cin>>f;
        if(f==1)
        {
            printf("请输入修改后的学号:");
            cin>>num;
            printf("请输入修改后的姓名:");
            cin>>name;
            printf("请输入修改后的专业:");
            cin>>major;
            printf("请输入修改后的年级:");
            cin>>grade;
            printf("请输入修改后的生源地:");
            cin>>place;
            printf("请输入修改后的校园一卡通号码:");
            cin>>id;
            p->num=num;
            p->name=name;
            p->major=major;
            p->grade=grade;
            p->place=place;
            p->id=id;
            printf("\n修改成功!进入主菜单后输入数字5即可查看修改后的所有学生基本信息!\n\n");
            system("pause");
        }
        return(head1);
    }
};
struct node_bf *modify_bf(struct node_bf *head2)//修改借书和点菜信息
{
    if(cnt_stu==0){printf("\n当前还没有学生信息,请先增加学生信息!\n\n");system("pause");return(head2);}
    if(cnt_bf==0){printf("\n当前还没有借书和点菜信息,请先增加借书和点菜信息!\n\n");system("pause");return(head2);}
    printf("\n确定要修改借书和点菜信息吗?是:1 否:0\n");
    int f1;cin>>f1;
    if(f1==0)return(head2);
    system("cls");
    ll id;
    string book,food;
    printf("请输入您要修改的借书和点菜信息对应的校园一卡通号码:");
    cin>>id;
    struct node_bf *p,*p0,*np;
    p=head2->next;
    int flag=0,sum=0;
    while(p!=NULL)
    {
        if(p->id==id){flag=1;p0=p;break;}
        p=p->next;
    }
    while(p!=NULL)
    {
        if(p->id==id)sum++;
        else break;
        p=p->next;
    }
    if(flag==0){printf("\n没有找到您输入的校园一卡通号码,请重新输入!\n\n");system("pause");return(head2);}
    else
    {
        printf("\n校园一卡通号码为 %lld 的借书和点菜记录一共有 %d 条:\n\n",id,sum);
        printf("校园一卡通号码\t\t\t借书记录\t\t\t点菜记录\t\t\t对应的数字编号\n");
        p=p0;
        for(int i=1;i<=sum;i++)
        {
            printf("%lld\t\t\t%s\t\t\t%s\t\t\t%d\n",p->id,p->book.c_str(),p->food.c_str(),i);
            p=p->next;
        }
        printf("\n确定要修改该卡号的借书和点菜信息吗?是:1 否:0\n");
        int f;cin>>f;
        if(f==1)
        {
            while(1)
            {
                int n;
                printf("\n请输入要修改的记录对应的数字编号:");
                cin>>n;
                if(n<1||n>sum){printf("数字编号错误!请重新输入!\n\n");continue;}
                printf("请输入修改后的书名:");
                cin>>book;
                printf("请输入修改后的菜名:");
                cin>>food;
                np=p0;
                for(int i=1;i<=n-1;i++)
                    np=np->next;
                np->book=book;
                np->food=food;
                printf("\n修改成功!进入主菜单后输入数字9即可查看修改后的所有借书和点菜信息!\n\n");
                printf("是否继续修改该卡号的借书和点菜信息?是:1 否:0\n");
                int f1;cin>>f1;
                if(f1==0)break;
            }
        }
        return(head2);
    }
};
struct node_stu *del_stu(struct node_stu *head1)//删除学生信息
{
    if(cnt_stu==0){printf("\n当前还没有学生信息,请先增加学生信息!\n\n");system("pause");return(head1);}
    printf("\n确定要删除学生信息吗?是:1 否:0\n");
    int f1;cin>>f1;
    if(f1==0)return(head1);
    system("cls");
    printf("请输入您要删除的学号:");
    ll num;cin>>num;
    struct node_stu *p,*np;
    p=head1;
    int flag=0;
    while(p->next!=NULL)
    {
        if(p->next->num==num){flag=1;break;}
        p=p->next;
    }
    if(flag==0){printf("\n没有找到您输入的学号,请重新输入!\n\n");system("pause");return(head1);}
    else
    {
        np=p->next;
        printf("\n您要删除的学生信息为:\n");
        printf("学号:%lld\t姓名:%s\t专业:%s\t年级:%s\t生源地:%s\t校园一卡通号码:%lld\n",np->num,np->name.c_str(),np->major.c_str(),np->grade.c_str(),np->place.c_str(),np->id);
        printf("\n确定要删除该学生的信息吗?(该学生的卡号对应的借书和点菜信息不会被删除)是:1 否:0\n");
        int f;cin>>f;
        if(f==1)
        {
            if(p->next->next==NULL)p->next=NULL;
            else p->next=p->next->next;
            cnt_stu--;
            printf("\n删除成功!进入主菜单后输入数字5即可查看删除后的所有学生基本信息!\n\n");
            system("pause");
        }
        return(head1);
    }
};
struct node_bf *del_bf(struct node_bf *head2)//删除借书和点菜信息
{
    if(cnt_stu==0){printf("\n当前还没有学生信息,请先增加学生信息!\n\n");system("pause");return(head2);}
    if(cnt_bf==0){printf("\n当前还没有借书和点菜信息,请先增加借书和点菜信息!\n\n");system("pause");return(head2);}
    printf("\n确定要删除借书和点菜信息吗?是:1 否:0\n");
    int f1;cin>>f1;
    if(f1==0)return(head2);
    system("cls");
    ll id;
    string book,food;
    printf("请输入您要删除的借书和点菜信息对应的校园一卡通号码:");
    cin>>id;
    struct node_bf *p,*p0;//p0为要删除的卡号在第二个链表中第一次出现时的前驱节点
    p=head2;
    int flag=0,sum=0;
    while(p->next!=NULL)
    {
        if(p->next->id==id){flag=1;p0=p;break;}
        p=p->next;
    }
    while(p->next!=NULL)
    {
        if(p->next->id==id)sum++;
        p=p->next;
    }
    if(flag==0){printf("\n没有找到您输入的校园一卡通号码,请重新输入!\n\n");system("pause");return(head2);}
    else
    {
        printf("\n校园一卡通号码为 %lld 的借书和点菜记录一共有 %d 条:\n\n",id,sum);
        printf("校园一卡通号码\t\t\t借书记录\t\t\t点菜记录\t\t\t对应的数字编号\n");
        p=p0;
        for(int i=1;i<=sum;i++)
        {
            p=p->next;
            printf("%lld\t\t\t%s\t\t\t%s\t\t\t%d\n",p->id,p->book.c_str(),p->food.c_str(),i);
        }
        printf("\n确定要删除该卡号的借书和点菜记录吗?(每次只能删除一条记录) 是:1 否:0\n");
        int f;cin>>f;
        if(f==1)
        {
            int n;
            while(1)
            {
                printf("\n请输入要删除的记录对应的数字编号:");
                cin>>n;
                if(n<1||n>sum)printf("数字编号错误!请重新输入!\n\n");
                else break;
            }
            p=p0;
            for(int i=1;i<=n-1;i++)
                p=p->next;
            if(p->next->next==NULL)p->next=NULL;
            else p->next=p->next->next;
            cnt_bf--;
            printf("\n删除成功!进入主菜单后输入数字9即可查看删除后的所有借书和点菜信息!\n\n");
            system("pause");
        }
        return(head2);
    }
};
struct node_stu *output_stu(struct node_stu *head1)//查看学生信息
{
    system("cls");
    printf("---学号---\t\t\t姓名\t\t\t专业\t\t\t年级\t\t\t生源地\t\t\t校园一卡通号码\n");
    if(cnt_stu==0){printf("\n当前还没有学生信息,请先增加学生信息!\n\n");system("pause");return(head1);}
    struct node_stu *p;
    p=head1->next;
    while(p!=NULL)
    {
        printf("%lld\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t%lld\n",p->num,p->name.c_str(),p->major.c_str(),p->grade.c_str(),p->place.c_str(),p->id);
        p=p->next;
    }
    printf("\n以上是当前已录入的所有学生的基本信息(已自动按学号由小到大排序)\n\n");
    system("pause");
    return(head1);
};
struct node_bf *output_bf(struct node_bf *head2)//查看借书和点菜信息
{
    system("cls");
    printf("校园一卡通号码\t\t\t借书记录\t\t\t点菜记录\n");
    if(cnt_stu==0){printf("\n当前还没有学生信息,请先增加学生信息,再增加借书和点菜信息!\n\n");system("pause");return(head2);}
    if(cnt_bf==0){printf("\n当前还没有借书和点菜信息,请先增加借书和点菜信息!\n\n");system("pause");return(head2);}
    map<string,int>vis1,vis2;
    int mx1=1,mx2=1;
    string book_mx_name=head2->next->book,food_mx_name=head2->next->food;
    struct node_bf *p;
    p=head2->next;
    while(p!=NULL)
    {
        vis1[p->book]++;
        vis2[p->food]++;
        if(vis1[p->book]>mx1){mx1=vis1[p->book];book_mx_name=p->book;}
        if(vis2[p->food]>mx2){mx2=vis2[p->food];food_mx_name=p->food;}
        printf("%lld\t\t\t%s\t\t\t%s\n",p->id,p->book.c_str(),p->food.c_str());
        p=p->next;
    }
    printf("\n以上是当前已录入的所有借书和点菜信息(已自动按校园一卡通号码由小到大排序)\n\n");
    p=head2->next;
    string ans1[110],ans2[110];
    map<string,int>flag1,flag2;
    int cnt1=0,cnt2=0;
    while(p!=NULL)
    {
        if(vis1[p->book]==mx1)
        {
            if(!flag1[p->book])ans1[++cnt1]=p->book;//cnt1表示最大的图书数量对应的图书名称有几种
            flag1[p->book]=1;
        }
        if(vis2[p->food]==mx2)
        {
            if(!flag2[p->food])ans2[++cnt2]=p->food;//cnt2表示最大的美食数量对应的美食名称有几种
            flag2[p->food]=1;
        }
        p=p->next;
    }
    printf("图书和美食的统计数据:\n\n");
    if(cnt1==1)printf("最受欢迎的图书名称:《%s》 它总共出现了%d次\n\n",book_mx_name.c_str(),mx1);
    else
    {
        printf("最受欢迎的图书名称:");
        for(int i=1;i<=cnt1;i++)
            printf("《%s》 ",ans1[i].c_str());
        printf("它均出现了%d次\n\n",mx1);
    }
    if(cnt2==1)printf("最受欢迎的美食名称:《%s》 它总共出现了%d次\n\n",food_mx_name.c_str(),mx2);
    else
    {
        printf("最受欢迎的美食名称:");
        for(int i=1;i<=cnt2;i++)
            printf("《%s》 ",ans2[i].c_str());
        printf("它们均出现了%d次\n\n",mx2);
    }
    system("pause");
    return(head2);
};
void exit_stu(struct node_stu *head1)//退出时保存学生信息
{
    FILE *fp;
    if((fp=fopen("student_data.txt","a+"))==NULL)
		fp=fopen("student_data.txt","a+");
    fprintf(fp,"---学号---\t\t姓名\t\t专业\t\t年级\t\t生源地\t\t校园一卡通号码\n");
    struct node_stu *p;
    p=head1->next;
	while(p!=NULL)
    {
        fprintf(fp,"%lld\t%s\t\t%s\t\t%s\t\t%s\t\t%lld\n",p->num,p->name.c_str(),p->major.c_str(),p->grade.c_str(),p->place.c_str(),p->id);
        p=p->next;
    }
    fclose(fp);
};
void exit_bf(struct node_bf *head2)//退出时保存借书和点菜信息
{
    FILE *fp;
    if((fp=fopen("book_and_food_data.txt","a+"))==NULL)
        fp=fopen("book_and_food_data.txt","a+");
    fprintf(fp,"校园一卡通号码\t\t\t借书记录\t\t\t点菜记录\n");
    struct node_bf *p;
    map<string,int>vis1,vis2;
    int mx1=1,mx2=1;
    string book_mx_name=head2->next->book,food_mx_name=head2->next->food;
    p=head2->next;
    while(p!=NULL)
    {
        vis1[p->book]++;
        vis2[p->food]++;
        if(vis1[p->book]>mx1){mx1=vis1[p->book];book_mx_name=p->book;}
        if(vis2[p->food]>mx2){mx2=vis2[p->food];food_mx_name=p->food;}
        fprintf(fp,"%lld\t\t\t%s\t\t\t%s\n",p->id,p->book.c_str(),p->food.c_str());
        p=p->next;
    }
    fprintf(fp,"\n以上是当前已录入的所有借书和点菜信息(已自动按校园一卡通号码由小到大排序)\n\n");
    p=head2->next;
    string ans1[110],ans2[110];
    map<string,int>flag1,flag2;
    int cnt1=0,cnt2=0;
    while(p!=NULL)
    {
        if(vis1[p->book]==mx1)
        {
            if(!flag1[p->book])ans1[++cnt1]=p->book;//cnt1表示最大的图书数量对应的图书名称有几种
            flag1[p->book]=1;
        }
        if(vis2[p->food]==mx2)
        {
            if(!flag2[p->food])ans2[++cnt2]=p->food;//cnt2表示最大的美食数量对应的美食名称有几种
            flag2[p->food]=1;
        }
        p=p->next;
    }
    fprintf(fp,"图书和美食的统计数据:\n\n");
    if(cnt1==1)fprintf(fp,"最受欢迎的图书名称:《%s》 它总共出现了%d次\n\n",book_mx_name.c_str(),mx1);
    else
    {
        fprintf(fp,"最受欢迎的图书名称:");
        for(int i=1;i<=cnt1;i++)
            fprintf(fp,"《%s》 ",ans1[i].c_str());
        fprintf(fp,"它们均出现了%d次\n\n",mx1);
    }
    if(cnt2==1)fprintf(fp,"最受欢迎的美食名称:《%s》 它总共出现了%d次\n\n",food_mx_name.c_str(),mx2);
    else
    {
        fprintf(fp,"最受欢迎的美食名称:");
        for(int i=1;i<=cnt2;i++)
            fprintf(fp,"《%s》 ",ans2[i].c_str());
        fprintf(fp,"它们均出现了%d次\n\n",mx2);
    }
	fclose(fp);
}
int get1(){return cnt_stu;}
int get2(){return cnt_bf;}

main.cpp(主函数)

#include <bits/stdc++.h>
#include "hanshu.h"
using namespace std;
int main()
{
    system("mode con cols=150");
    system("mode con lines=50");
    string s;
	int f,opt,cnt1,cnt2;
	struct node_stu *phead1;
	struct node_bf  *phead2;
	jingdutiao();//进度条
	system("color 0b");
	while(1)
	{
		menu();
		cin>>s;
		opt=s[0]-'0';
        if(s.length()>1||opt<0||opt>9)
        {
            printf("\n输入错误!请重新输入一个0~9以内的数字!\n\n");
            system("pause");
            continue;
        }
		if(opt==0)
        {
            printf("\n确定要退出程序吗?退出程序后您录入的全部数据将会被保存在文件中。  是:1 否:0\n");
            cin>>f;
            if(f==1)
            {
                cnt1=get1();cnt2=get2();
                if(cnt1!=0)exit_stu(phead1);
                if(cnt2!=0)exit_bf(phead2);
                printf("\n保存成功!所有学生的基本信息已保存于student_data.txt,所有借书和点菜信息已保存于book_and_food_data.txt!\n\n");
                printf("感谢您的使用,再见!\n\n");
                getchar();getchar();
                break;
            }
            else continue;
        }
		switch(opt)
		{
		     case 1:
            {
                printf("\n新建一个信息系统将会删除已保存的所有数据(包括学生基本信息以及借书和点菜记录),确定要新建吗? 是:1 否:0\n");
                cin>>f;
                if(f==1){phead1=build_stu();phead2=build_bf();}
                break;
            }
			 case 2:add_stu(phead1);break;
             case 3:modify_stu(phead1);break;
			 case 4:del_stu(phead1);break;
			 case 5:output_stu(phead1);break;
			 case 6:add_bf(phead1,phead2);break;
			 case 7:modify_bf(phead2);break;
			 case 8:del_bf(phead2);break;
			 case 9:output_bf(phead2);break;
		}
	}
}

总共七百行代码的样子,第一次用C++写一个小项目,虽然很累,但是很有成就感~

发布了72 篇原创文章 · 获赞 91 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/ljw_study_in_CSDN/article/details/96100850