課程設計-學生資訊管理系統
阿新 • • 發佈:2019-02-16
#include <iostream> #include <stdio.h> #include <stdlib.h> #include<windows.h> #include<string.h> #include<string> #include<fstream> #include"conio.h" #include "2.h" using namespace std; int j=0,q=0;//j表示學生總數,q用來判斷學生學號重複 /**************************************/ /* 判斷重複 */ /**************************************/ void StuIm(Student *head,Student *p) { Student *Opinion=(Student *)malloc(sizeof(Student)); //用來判斷輸入節點中學生號是否有重複 Opinion=head->next; // cout<<"學生姓名:"; cin>>p->name; cout<<"學生學號:"; cin>>p->num; cout<<"學生性別:"; cin>>p->xingbie; cout<<"學生年齡:"; cin>>p->age; cout<<"學生出生年月:"; cin>>p->shengri; cout<<"學生地址:"; cin>>p->address; cout<<"學生電話:"; cin>>p->phone; if(Opinion!=NULL) { for(int b=1;b<=j;b++) { if(Opinion->num==p->num) //對比學號 { q=1; cout<<"該學生已錄入"; j--; Sleep(1000); break; } Opinion=Opinion->next; } } } /**************************************/ /* 輸入函式 */ /**************************************/ void StuIn(Student *head) { if(head->next!=NULL)//判斷頭結點是否為空 { cout<<"共有"<<j<<"名學生"<<endl; cout<<"系統已存在資訊。請轉入插入系統!"<<endl; Sleep(1000); return ; } // Student *Opinion=(Student *)malloc(sizeof(Student)); //用於判斷是否重複而定義 // Opinion=head->next; Student *point=(Student *)malloc(sizeof(Student)); // point->next=NULL; int s=1; while(s==1) { cout<<"共有"<<j<<"名學生"<<endl; Student *p=(Student *)malloc(sizeof(Student)); p->next=NULL; StuIm(head,p); if(q!=1) { if(head->next==NULL) { head->next=p; point=p; } else { point->next=p; point=p; } } j++; cout<<"是否繼續: 1 是,2 否 "<<endl; cin>>s; } } /**************************************/ /* 查詢函式 */ /**************************************/ void StuSelect(Student *head) { int s=1; //判斷函式是否繼續進行 int num; while(s!=2) { system("cls"); cout<<" 查詢學生資訊系統 "<<endl; cout<<" 【1】 全部學生資訊列表"<<endl; cout<<" 【2】 學號查詢學生資訊"<<endl; cout<<" 【3】 退出 "<<endl; cin>>num; switch(num) { case 1: StuFindErg(head); //遍歷函式 break; case 2: StuFindNum(head); //查詢函式 break; default: s=2; break; } } } /**************************************/ /* 遍歷函式 */ /**************************************/ void StuFindErg(Student *head) //遍歷函式,由查詢函式呼叫 { Student *p=(Student *)malloc(sizeof(Student)); p=head->next; int i=1; while(p!=NULL) { cout<<"第"<<i<<"位學生的資訊:"<<endl; StuOut(p); cout<<endl; p=p->next; i++; } cout<<"按任意鍵退出"<<endl; getch(); } /**************************************/ /* 查詢子函式 */ /**************************************/ void StuFindNum(Student *head) //查詢子函式,由查詢函式呼叫 { int num; cout<<"請輸入要查詢的學生的學號:"; cin>>num; Student *p=(Student *)malloc(sizeof(Student)); p=head->next; int i=1; while(p!=NULL) { if(num==p->num) { StuOut(p); cout<<endl; i++; } p=p->next; } if(i==1) cout<<"沒有該學生的資訊,請檢查輸入是否正確!"<<endl; cout<<"按任意鍵退出"<<endl; getch(); } /**************************************/ /* 輸出函式 */ /**************************************/ void StuOut(Student *p) //輸出函式,將連結串列某個節點的資訊輸出 { cout<<"學生姓名:"; cout<<p->name; cout<<"學生學號:"; cout<<p->num; cout<<"學生性別:"; cout<<p->xingbie; cout<<"學生年齡:"; cout<<p->age; cout<<"學生出生年月:"; cout<<p->shengri; cout<<"學生地址:"; cout<<p->address; cout<<"學生電話:"; cout<<p->phone; } /**************************************/ /* 修改函式 */ /**************************************/ void StuAlter(Student *head) //修改函式,由主函式呼叫 { int num; cout<<"輸入想要查詢學生的學生號:"; cin>>num; Student *p=(Student *)malloc(sizeof(Student)); p=head->next; int i=1; while(p!=NULL) { if(num==p->num) { StuOut(p); cout<<endl; printf("輸入修改資訊:\n"); cout<<"學生姓名:"; cin>>p->name; cout<<"學生學號:"; cout<<p->num<<endl; cout<<"學生性別:"; cin>>p->xingbie; cout<<"學生年齡:"; cin>>p->age; cout<<"學生出生年月:"; cin>>p->shengri; cout<<"學生地址:"; cin>>p->address; cout<<"學生電話:"; cin>>p->phone; printf("修改成功\n"); Sleep(1000); i++; } p=p->next; } if(i==1) cout<<"沒有該學生的資訊,請檢查輸入是否正確!"<<endl; cout<<"按任意鍵退出"<<endl; getch(); } /**************************************/ /* 插入函式 */ /**************************************/ void StuInsert(Student *head) //插入函式,由主函式呼叫 { Student *point=(Student *)malloc(sizeof(Student)); point=head->next; while(point->next!=NULL) point=point->next; int s=1; int num; while(s!=2) { cout<<" 插入學生資訊系統 "<<endl; cout<<" 【1】 從頭插入學生資訊 "<<endl; cout<<" 【2】 從後插入學生資訊 "<<endl; cout<<" 【3】 退出插入學生資訊系統"<<endl; cin>>num; Student *p=(Student *)malloc(sizeof(Student)); switch(num) { case 1: StuIm(head,p); //查重 if(q==1) { j++; cout<<"插入失敗"<<endl; break; } p->next=head->next; //頭插 head->next=p; cout<<"插入成功!"<<endl; Sleep(1000); break; case 2: StuIm(head,p); if(q==1) { j++; cout<<"插入失敗"<<endl; break; } point->next=p; //尾插 p->next=NULL; cout<<"插入成功!"<<endl; Sleep(1000); break; default: s=2; break; } } } /**************************************/ /* 刪除函式 */ /**************************************/ void StuDelect(Student *head) //刪除函式,由主函式呼叫 { int num; cout<<"輸入想要刪除學生的學生號:"; cin>>num; Student *p=(Student *)malloc(sizeof(Student)); p->next=head->next; int i=1; while(p->next!=NULL) { if(num==p->next->num) { StuOut(p->next); cout<<"是否刪除:1 是,2 否 "; cin>>i; if(num==head->next->num) //如果為頭結點 { head->next=head->next->next; } else { p->next=p->next->next; } i=2; j--; cout<<"刪除成功!"<<endl; Sleep(1000); break; } p=p->next; } if(i==1) cout<<"沒有該學生資訊"<<endl; Sleep(1000); } /**************************************/ /* 儲存函式 */ /**************************************/ void StuSave(Student *head) //儲存函式,由主函式呼叫 { FILE *fp; char filename[10]; cout<<"請輸入儲存的檔名(例如:C:\text.txt):"; cin>>filename; Student *p=(Student *)malloc(sizeof(Student)); p=head->next; if((fp=fopen(filename,"w"))==NULL) { cout<<"找不到該檔案!"<<endl; Sleep(1000); return; } cout<<"儲存成功!"<<endl; Sleep(1000); while(p!=NULL) { fwrite(p,sizeof(Student),1,fp); p=p->next; } ofstream out("d.txt",ios::out); out<<j<<endl; fclose(fp); out.close(); } /**************************************/ /* 檔案輸入函式 */ /**************************************/ void StuInFile(Student *head) //學生成績管理系統的檔案輸入函式,由輸入函式呼叫 { char filename[10]; if(head->next!=NULL) { cout<<"系統已存在資訊。請轉入插入系統!"<<endl; Sleep(1000); return ; } FILE *fp; cout<<"請輸入檔案路徑(例如C:\\text.txt)"; cin>>filename; if((fp=fopen(filename,"r"))==NULL) { cout<<"不能開啟檔案,請檢查是否正確!"<<endl; Sleep(1000); return; } Student *point=(Student *)malloc(sizeof(Student)); //while(!feof(fp)) ifstream in("d.txt",ios::in); in>>j; /*cout<<j; Sleep(2000);*/ int c=0; for(c=1;c<=j;c++) { Student *p=(Student *)malloc(sizeof(Student)); p->next=NULL; fread(p,sizeof(Student),1,fp); if(head->next==NULL) { head->next=p; point=p; } else { point->next=p; point=p; } } cout<<"傳輸成功!"<<endl; Sleep(1000); fclose(fp); in.close(); }
4.cpp