C++實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
非常簡(jiǎn)易,完成個(gè)作業(yè)夠用,僅供初學(xué)者參考,不喜勿噴。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
/*先用結(jié)構(gòu)體定義學(xué)生信息*/
struct stud?
{
? ? int ? num;?? ??? ??? ?//學(xué)號(hào)?
? ? char ?name[10];?? ??? ?//姓名?
? ? char ?sex[8];?? ??? ?//性別?
? ? int ? age;?? ??? ??? ?//年齡?
? ? char ?major[18]; ?? ?//專業(yè)?
? ? int ? grade;?? ??? ?//年級(jí)?
? ? int ? classes;?? ??? ?//班級(jí)?
? ? int ? building;?? ??? ?//樓號(hào)?
? ? int ? floor;?? ??? ?//樓層?
? ? int ? dormitary;?? ?//宿舍號(hào)?
? ? struct stud *next; /*next是指向本結(jié)構(gòu)體的類型的指針類型*/
};
struct stud *h; ? ? /*定義結(jié)構(gòu)體的指針變量*/
int N=sizeof(struct stud);//獲取結(jié)構(gòu)體總長(zhǎng)度,用于分配內(nèi)存空間?
/*輸入模塊: 建立鏈表*/
void input_message()
{ ? struct stud *p1,*p2;
? ? int i,k;
?? ?system("cls");
? ? printf("********** 您當(dāng)前正在進(jìn)行的操作是,輸入學(xué)生信息 **********\n\n");
? ? printf("請(qǐng)輸入本次注冊(cè)的生總數(shù):");scanf("%d",&k);
? ? h=NULL;
?? ?printf("\n您共有%d個(gè)學(xué)生信息需要錄入\n",k);
?? ??
? ? if(k>0)
? ? {?
?? ? ?h=p2=p1=(struct stud*)malloc(N); ? ? /*為head,p2,p1申請(qǐng)存儲(chǔ)空間*/
? ? ? for(i=1;i<=k;i++)
? ? ? {?
?? ??? ?printf("\n請(qǐng)輸入第%d名學(xué)生的信息:\n\n",i);
?? ??? ?p1=(struct stud*)malloc(N);
? ? ? ? printf("學(xué)號(hào):");
? ? ? ? scanf("%d",&p1->num);
? ? ? ? printf("姓名:");
? ? ? ? scanf("%s",p1->name);
? ? ? ? printf("性別(男或女):");
? ? ? ? scanf("%s",p1->sex);
? ? ? ? printf("年齡:");
? ? ? ? scanf("%d",&p1->age);
? ? ? ? printf("專業(yè):");
? ? ? ? scanf("%s",p1->major);
? ? ? ? printf("年級(jí):");
? ? ? ? scanf("%d",&p1->grade);
? ? ? ? printf("班級(jí):");
? ? ? ? scanf("%d",&p1->classes);
? ? ? ? printf("樓號(hào):");
? ? ? ? scanf("%d",&p1->building);
? ? ? ? printf("樓層:");
? ? ? ? scanf("%d",&p1->floor);
? ? ? ? printf("宿舍號(hào):");
? ? ? ? scanf("%d",&p1->dormitary);
? ? ? ? p2->next=p1; ? ? ? ? ? ? ? ? ? ? ? ? ? ?/*將新結(jié)點(diǎn)連到表尾*/
? ? ? ? p2=p1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/*p2指向新的表尾*/
? ? ? ?}
? ? ? ?p2->next=NULL;
? ? ? ?h=h->next;
? ? ? }
? ? ??
? ? ? getchar();//吸收掉上一行錄入時(shí)鍵入的回車符號(hào),否則卡不住?
? ? ? printf("\n%*s%s\n",10," ","輸入完成,按任意鍵進(jìn)入主菜單!!!!");
? ? ? getchar();
}
/*輸出模塊:顯示鏈表*/
void output_message()
{
?? ?char v;
?? ?struct stud *p=h;
?? ?system("cls");
?? ?printf("********** 您當(dāng)前正在進(jìn)行的操作是,顯示學(xué)生信息 **********\n\n");
?? ?
?? ?if(p==NULL)
?? ?{
?? ??? ?printf("%*s%s\n",10," ","當(dāng)前沒有找到任何學(xué)生信息,請(qǐng)您先輸入信息,再執(zhí)行本操作!\n");
?? ??? ?getchar();//吸收掉上一行錄入時(shí)鍵入的回車符號(hào),否則卡不住?
?? ??? ?printf("%*s%s\n",10," ","按任意鍵返回主菜單!!!!");
?? ??? ?getchar();
?? ??? ?return;?
?? ?}
?? ?printf(" ?學(xué)號(hào) ? ? ?姓名 ?性別 ?年齡 ? ? ?專業(yè) ?年級(jí) ?班級(jí) ?樓號(hào) ?樓層 ?宿舍號(hào)\n\n");
?? ?while(p!=NULL)
?? ?{
?? ??? ?printf("%6d%10s%6s%6d%10s%6d%6d%6d%6d%8d\n",p->num,p->name,p->sex,p->age,p->major,p->grade,p->classes,p->building,p->floor,p->dormitary);
?? ??? ?p=p->next;
?? ?}
?? ?
?? ?getchar();//吸收掉上一行錄入時(shí)鍵入的回車符號(hào),否則卡不住?
?? ?printf("\n%*s%s\n",10," ","輸出完成,按任意鍵返回主菜單!!!!");
?? ?getchar();
}
/*插入新學(xué)生信息:插入鏈表*/
void insert_message()
{ ?
?? ?struct stud *p0,*p1;
?? ?p1=(struct stud *)malloc(N);
?? ?system("cls");
?? ?printf("********** 您當(dāng)前正在進(jìn)行的操作是,插入學(xué)生信息 **********\n\n");
? ??? ?
? ??? ?printf("\n請(qǐng)輸入要插入學(xué)生的信息:\n\n");
?? ?p1=(struct stud*)malloc(N);
? ? printf("學(xué)號(hào):");
? ? scanf("%d",&p1->num);
? ? printf("姓名:");
? ? scanf("%s",p1->name);
? ? printf("性別(男或女):");
? ? scanf("%s",p1->sex);
? ? printf("年齡:");
? ? scanf("%d",&p1->age);
? ? printf("專業(yè):");
? ? scanf("%s",p1->major);
? ? printf("年級(jí):");
? ? scanf("%d",&p1->grade);
? ? printf("班級(jí):");
? ? scanf("%d",&p1->classes);
? ? printf("樓號(hào):");
? ? scanf("%d",&p1->building);
? ? printf("樓層:");
? ? scanf("%d",&p1->floor);
? ? printf("宿舍號(hào):");
? ? scanf("%d",&p1->dormitary);
?? ?p1->next=NULL;
? ?
?? ?if(h==NULL)//如果鏈表為空,插入的節(jié)點(diǎn)設(shè)為表頭
?? ?{ ?
? ??? ??? ?h=p1;
?? ??? ?getchar();//吸收掉上一行錄入時(shí)鍵入的回車符號(hào),否則卡不住?
?? ??? ?printf("\n%*s%s\n",10," ","插入成功! 按任意鍵返回主菜單!!!!");
?? ??? ?getchar();
?? ??? ?return;
? ? }
? ??
?? ?p0=h;
?? ?while((p0->next)!=NULL&&(p0->next->num)<(p1->num))
?? ?{
?? ??? ?p0=p0->next;
?? ?}
? ?
?? ?if(p0->next==NULL)
?? ?{
?? ??? ?p0->next=p1; //如果到尾部了加到尾部
?? ??? ?getchar();//吸收掉上一行錄入時(shí)鍵入的回車符號(hào),否則卡不住?
?? ??? ?printf("\n%*s%s\n",10," ","插入成功! 按任意鍵返回主菜單!!!!");
?? ??? ?getchar();?
?? ?}
? ? else if((p0->next->num)==(p1->num))
?? ?{
?? ??? ?free(p1);?? ?//如果已經(jīng)存在這個(gè)學(xué)號(hào),那就不能再插入了,釋放
?? ??? ?
?? ??? ?getchar();//吸收掉上一行錄入時(shí)鍵入的回車符號(hào),否則卡不住?
?? ??? ?printf("\n%*s%s\n",10," ","插入失敗,本學(xué)號(hào)信息已經(jīng)存在! 按任意鍵返回主菜單!!!!");
?? ??? ?getchar();
?? ?}
?? ?else
?? ?{
?? ??? ?//插入?
?? ??? ?p1->next=p0->next;
?? ??? ?p0->next=p1;?
?? ??? ?
?? ??? ?getchar();//吸收掉上一行錄入時(shí)鍵入的回車符號(hào),否則卡不住?
?? ??? ?printf("\n%*s%s\n",10," ","插入成功! 按任意鍵返回主菜單!!!!");
?? ??? ?getchar(); ? ? ? ??? ?
?? ?}
}
/*刪除學(xué)生信息*/
void del_message()
{ ?
?? ?int num;
?? ?bool delOk=false;
?? ?
?? ?struct stud *p0,*p1;
?? ?
?? ?system("cls");
?? ?printf("********** 您當(dāng)前正在進(jìn)行的操作是,刪除學(xué)生信息 **********\n\n");
?? ??? ?
?? ?if(h==NULL)
?? ?{
?? ??? ?printf("%*s%s\n",10," ","當(dāng)前沒有找到任何學(xué)生信息,請(qǐng)您先輸入信息,再執(zhí)行本操作!\n");
?? ??? ?getchar();//吸收掉上一行錄入時(shí)鍵入的回車符號(hào),否則卡不住?
?? ??? ?printf("%*s%s\n",10," ","按任意鍵返回主菜單!!!!");
?? ??? ?getchar();
?? ??? ?return;?
?? ?}
?? ?
?? ?printf("請(qǐng)輸入要?jiǎng)h除學(xué)生的學(xué)號(hào):");
?? ?scanf("%d",&num);
?? ?p0=h;
?? ?if(p0->num==num)
?? ?{
?? ??? ?h=p0->next;
?? ??? ?free(p0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/*若首結(jié)點(diǎn)是所要?jiǎng)h除的數(shù)據(jù),則釋放首結(jié)點(diǎn)*/
?? ??? ?delOk=true;
?? ?}
?? ?else
?? ?{
?? ??? ?p1=p0->next;
?? ??? ?while(p1!=NULL)
?? ??? ?{
?? ??? ??? ?if(p1->num==num)
?? ??? ??? ?{
?? ??? ??? ??? ?p0->next=p1->next;
?? ??? ??? ??? ?free(p1);
?? ??? ??? ??? ?delOk=true;
?? ??? ??? ??? ?break; //跳出循環(huán)?
?? ??? ??? ?}
?? ??? ??? ?p0=p1;
?? ??? ??? ?p1=p1->next;
?? ? ? }?? ??? ?
?? ? }?
?? ??
?? ?getchar();//吸收掉上一行錄入時(shí)鍵入的回車符號(hào),否則卡不住?
?? ? if(delOk)
?? ??? ?printf("%*s%s\n",10," ","刪除成功! 按任意鍵返回主菜單!!!!");
?? ? else
?? ??? ?printf("%*s%s\n",10," ","沒有找到匹配學(xué)號(hào),刪除失敗! 按任意鍵返回主菜單!!!!");
?? ??? ?
?? ?getchar();
}
//主函數(shù)?
int main(void){
? ? int choice;
? ??
? ? ? do{
?? ??? ?system("cls");
?? ??? ?printf("\n\n\n");
?? ??? ?printf("%*s%s",10," ","********** 學(xué) 生 宿 舍 管 理 系 統(tǒng) **********\n\n");
?? ??? ?printf("%*s%s",25," ","1.輸入學(xué)生信息\n\n");
?? ??? ?printf("%*s%s",25," ","2.輸出學(xué)生信息\n\n");
?? ? ? ?? ?printf("%*s%s",25," ","3.插入學(xué)生信息\n\n");
?? ? ? ?? ?printf("%*s%s",25," ","4.刪除學(xué)生信息\n\n");
?? ? ? ?? ?printf("%*s%s",25," ","0.退出系統(tǒng)\n\n");
?? ? ? ?? ?printf("%*s%s",10," ","*********************************************\n\n");
?? ? ? ?? ?printf("%*s%s",10," ","請(qǐng)選擇0-4之間任意整數(shù):");
?? ??? ?scanf("%d",&choice);
? ? ? ?
? ? ? ?switch(choice)
? ? ? ?{
? ? ? ? ?case 1:input_message();break;
? ? ? ? ?case 2:output_message();break;
? ? ? ? ?case 3:insert_message();break;
? ? ? ? ?case 4:del_message();break;
? ? ? ? ?case 0:break;
? ? ? ?}
? ? ?}while(choice!=0);
}
可以用 Dev-C++ 進(jìn)行編譯調(diào)試

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
linux c 獲取本機(jī)公網(wǎng)IP的實(shí)現(xiàn)方法
本篇文章是對(duì)在linux中使用c語言獲取本機(jī)公網(wǎng)IP的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++ 中動(dòng)態(tài)鏈接庫(kù)--導(dǎo)入和導(dǎo)出的實(shí)例詳解
這篇文章主要介紹了C++ 中動(dòng)態(tài)鏈接庫(kù)--導(dǎo)入和導(dǎo)出的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Qt重寫QTreeView自繪實(shí)現(xiàn)酷炫樣式
QTreeView,顧名思義,就是一種樹形的控件,在我們需要做類似于文件列表的視圖時(shí),是一個(gè)不錯(cuò)的選擇,下面我們就來看看qt如何重寫QTreeView實(shí)現(xiàn)酷炫樣式,感興趣的可以了解一下2023-08-08
深入理解C語言 static、extern與指針函數(shù)
這篇文章主要介紹了C語言 static、extern與指針函數(shù),有需要的朋友可以參考一下2013-12-12
C語言實(shí)現(xiàn)超市信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)超市信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

