學生信息管理系統(700行源碼,附註釋)【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++寫一個小項目,雖然很累,但是很有成就感~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章