學生選題資訊管理系統

回憶釀的甜發表於2018-11-17
//
//本人大一寫東忙西忙寫的,感覺很一般,鞏固基礎麻,沒有別的大神寫的那麼酷炫,但是還是基本任務完成了。
//需先建立student.txt檔案


#include<fstream> #include<cstring> #include<stdio.h> #include<iostream> #include<stdlib.h> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -1 #define List_INIT_SPACE 100 //儲存空間初始分配量 #define List_INC_SPACE 10 //儲存空間分配增量 int node = 1; typedef struct Student { int age; //年齡 int student_id; //學號 int course_id; //題目編號 char student_name[40]; //姓名 char sex[10]; //性別 char Class[40]; //班級 char specialty[40]; //專業 char course_name[40]; //題目名稱 char keyword[40]; //關鍵詞 char technology[40]; }Student; typedef struct STent_list { struct Student *stu; //儲存空間基址 int length; //當前長度 int listsize; //當前分配的儲存容量(以sizeof(Student)為單位 }STent_list; int STent_listInit(STent_list &S) { //在記憶體中分配空間 S.stu = (Student*)malloc(List_INC_SPACE*sizeof(Student)); if(!S.stu) exit(OVERFLOW); //儲存空間分配失敗 //構造一個空的線性表 S.length = 0; S.listsize = List_INC_SPACE; //初始儲存容量 return OK; }//函式STent_listInit結束 void write(STent_list &S, int n) { fstream myfile; myfile.open("student.txt",ios::out|ios::binary); //fstream myfile("student.txt",ios::out|ios::binary); if(! myfile) { cout<<"該檔案不能開啟!"<<endl; abort(); //異常終止函式 } int count = n; myfile<<count<<endl<<endl; for(int i = 0; i <= count; i++) { myfile<<(S.stu[i]).student_id<<" "<<(S.stu[i]).student_name<<" "<<(S.stu[i]).sex<<" "<<(S.stu[i]).age<<" "<<(S.stu[i]).Class<<" "<<(S.stu[i]).specialty<<" "<<S.stu[i].course_id<<" "<<S.stu[i].course_name<<" "<<S.stu[i].keyword<<" "<<S.stu[i].technology<<" "<<endl; } myfile.close(); } /**************************************/ //函式名: read(STent_list &S) //引數: (傳入) STent_list S順序表 //返回值: int型,返回1表示建立成功,0表示失敗 //功能: 讀取順序表中的內容 /*************************************/ int read(STent_list &S) { fstream myfile; myfile.open("student.txt",ios::in|ios::binary); //fstream myfile("student.txt",ios::out|ios::binary); if(! myfile) { cout<<"該檔案不能開啟"<<endl; abort(); } int count; myfile.seekg(0); myfile>>count; for(int i = 0; i <= count; i++) { myfile>>S.stu[i].student_id>>S.stu[i].student_name>>S.stu[i].sex>>S.stu[i].age>>S.stu[i].Class>>S.stu[i].specialty>>S.stu[i].course_id>>S.stu[i].course_name>>S.stu[i].keyword>>S.stu[i].technology; } myfile.close(); return count; } /*****************************************/ //函式名:add(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 新增學生資訊 /*****************************************/ void add(STent_list &S) { int n = read(S); int i = 0; char sign = `F`; cout<<endl<<"請輸入增加的學生的相關資訊:"<<endl; while(sign != `N`) { loop: cout<<"學號:"; cin>>S.stu[i].student_id; cout<<endl; int c = 0; while(c < i) { c++; if(S.stu[i].student_id == S.stu[i-c].student_id) { cout<<"你輸入的學號已經存在!請重新輸入" <<endl; goto loop; } } cout<<"姓名:"; cin>>S.stu[i].student_name; cout<<endl; cout<<"性別:"; cin>>S.stu[i].sex; cout<<endl; cout<<"年齡:"; cin>>S.stu[i].age; cout<<endl; cout<<"班級:"; cin>>S.stu[i].Class; cout<<endl; cout<<"專業:"; cin>>S.stu[i].specialty; cout<<endl; cout<<"題目編號:"; cin>>S.stu[i].course_id; cout<<endl; cout<<"題目名稱:"; cin>>S.stu[i].course_name; cout<<endl; cout<<"關鍵詞:"; cin>>S.stu[i].keyword; cout<<endl; cout<<"實現技術:"; cin>>S.stu[i].technology; cout<<endl; cout<<"提示:是否繼續寫入學生資訊?(Y/N):"; cin>>sign; i++; } write(S, i); } /*****************************************************/ //函式名: search_student_id(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 根據學號查詢學生資訊 /*****************************************************/ void search_student_id(STent_list &S) { int n = read(S); int s; int i = 0; cout<<endl<<"查詢學生資訊:"<<endl; cout<<"請輸入需要查詢學生的學號:"<<endl; cin>>s; while((S.stu[i].student_id - s) != 0 && i < n) i++; if(i == n) { cout<<"提示: 對不起,無法找到該學生的資訊! "<<endl; } else { cout<<"***************************"<<endl; cout<<"學號:"<<S.stu[i].student_id<<endl; cout<<"姓名:"<<S.stu[i].student_name<<endl; cout<<"性別: "<<S.stu[i].sex<<endl; cout<<"年齡:"<<S.stu[i].age<<endl; cout<<"班級:"<<S.stu[i].Class<<endl; cout<<"專業:"<<S.stu[i].specialty<<endl; cout<<"題目編號:"<<S.stu[i].course_id<<endl; cout<<"題目名稱:"<<S.stu[i].course_name<<endl; cout<<"關鍵詞:"<<S.stu[i].keyword<<endl; cout<<"實現技術:"<<S.stu[i].technology<<endl; } } /*******************************************************/ //函式名: search_student_name(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 根據學生姓名查詢學生資訊 /*******************************************************/ void search_student_name(STent_list &S) { int n = read(S); char a[100]; cout<<"請輸入需要查詢的姓名:"<<endl; cin>>a; for(int i = 0; i < n; i++) if(strcmp(S.stu[i].student_name, a) == 0) { cout<<"****************************"<<endl; cout<<"學號:"<<S.stu[i].student_id<<endl; cout<<"姓名:"<<S.stu[i].student_name<<endl; cout<<"性別: "<<S.stu[i].sex<<endl; cout<<"年齡:"<<S.stu[i].age<<endl; cout<<"班級:"<<S.stu[i].Class<<endl; cout<<"專業:"<<S.stu[i].specialty<<endl; cout<<"題目編號:"<<S.stu[i].course_id<<endl; cout<<"題目名稱:"<<S.stu[i].course_name<<endl; cout<<"關鍵詞:"<<S.stu[i].keyword<<endl; cout<<"實現技術:"<<S.stu[i].technology<<endl; } } /*******************************************************/ //函式名: search_course_id(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 根據學生課程設計的編號查詢 /*******************************************************/ void search_course_id(STent_list &S) { int n = read(S); int b; int i = 0; cout<<"請輸入需要查詢的題目編號:"<<endl; cin>>b; while((S.stu[i].course_id - b) != 0 && i < n) i++; if(i == n) { cout<<"提示:對不起,無法找到該資訊!"<<endl; } else { for(i = 0; i < n; i++) if(S.stu[i].course_id - b == 0) { cout<<"******************************"<<endl; cout<<"學號:"<<S.stu[i].student_id<<endl; cout<<"姓名:"<<S.stu[i].student_name<<endl; cout<<"性別: "<<S.stu[i].sex<<endl; cout<<"年齡:"<<S.stu[i].age<<endl; cout<<"班級:"<<S.stu[i].Class<<endl; cout<<"專業:"<<S.stu[i].specialty<<endl; cout<<"題目編號:"<<S.stu[i].course_id<<endl; cout<<"題目名稱:"<<S.stu[i].course_name<<endl; cout<<"關鍵詞:"<<S.stu[i].keyword<<endl; cout<<"實現技術:"<<S.stu[i].technology<<endl; } } } /****************************************************/ //函式名: search_course_name(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 根據課程設計名稱查詢 /***************************************************/ void search_course_name(STent_list &S) { int n = read(S); char c[100]; cout<<"請輸入需要查詢的題目名稱:"<<endl; cin>>c; for(int i = 0; i < n; i++) if(strcmp(S.stu[i].course_name, c) == 0) { cout<<"******************************"<<endl; cout<<"學號:"<<S.stu[i].student_id<<endl; cout<<"姓名:"<<S.stu[i].student_name<<endl; cout<<"性別: "<<S.stu[i].sex<<endl; cout<<"年齡:"<<S.stu[i].age<<endl; cout<<"班級:"<<S.stu[i].Class<<endl; cout<<"專業:"<<S.stu[i].specialty<<endl; cout<<"題目編號:"<<S.stu[i].course_id<<endl; cout<<"題目名稱:"<<S.stu[i].course_name<<endl; cout<<"關鍵詞:"<<S.stu[i].keyword<<endl; cout<<"實現技術:"<<S.stu[i].technology<<endl; } } /******************************************************/ //函式名: search(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 選擇查詢關鍵詞 /******************************************************/ void search(STent_list &S) { int n = read(S); cout<<"**(1)根據學號查詢 **"<<endl; cout<<"**(2)根據姓名查詢 **"<<endl; cout<<"**(3)根據編號查詢 **"<<endl; cout<<"**(4)根據名稱查詢 **"<<endl; cout<<endl; int c; cout<<"請輸入選擇: "; cin>>c; switch(c) { case 1: search_student_id(S);break; case 2: search_student_name(S);break; case 3: search_course_id(S);break; case 4: search_course_name(S);break; default: cout<<"輸入錯誤,請重新輸入!"<<endl; } write(S, n); } /*******************************************/ //函式名: student_alter(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 修改學生資訊 /******************************************/ void student_alter(STent_list &S) { int n = read(S); int s; int i = 0; cout<<endl<<"修改學生資訊:"<<endl; cout<<"請輸入需要修改學生的學號:"<<endl; cin>>s; while((S.stu[i].student_id - s) != 0 && i < n) i++; if(i == n) { cout<<"提示:對不起,無該學生的資訊!!!"<<endl; } else { cout<<"該學生的資訊:"<<endl; cout<<"學號:"<<S.stu[i].student_id<<endl; cout<<"姓名:"<<S.stu[i].student_name<<endl; cout<<"性別: "<<S.stu[i].sex<<endl; cout<<"年齡:"<<S.stu[i].age<<endl; cout<<"班級:"<<S.stu[i].Class<<endl; cout<<"專業:"<<S.stu[i].specialty<<endl; cout<<"題目編號:"<<S.stu[i].course_id<<endl; cout<<"題目名稱:"<<S.stu[i].course_name<<endl; cout<<"關鍵詞:"<<S.stu[i].keyword<<endl; cout<<"實現技術:"<<S.stu[i].technology<<endl; cout<<"請重新輸入該學生的資訊"<<endl; cout<<"學號:"; cin>>S.stu[i].student_id; cout<<endl; cout<<"姓名:"; cin>>S.stu[i].student_name; cout<<endl; cout<<"性別:"; cin>>S.stu[i].sex; cout<<endl; cout<<"年齡:"; cin>>S.stu[i].age; cout<<endl; cout<<"班級:"; cin>>S.stu[i].Class; cout<<endl; cout<<"專業:"; cin>>S.stu[i].specialty; cout<<endl; cout<<"題目編號:"; cin>>S.stu[i].course_id; cout<<endl; cout<<"題目名稱:"; cin>>S.stu[i].course_name; cout<<endl; cout<<"關鍵詞:"; cin>>S.stu[i].keyword; cout<<endl; cout<<"實現技術:"; cin>>S.stu[i].technology; cout<<endl; char c; cout<<"是否儲存資料?(y/n)"<<endl; cin>>c; if(c == `y`) cout<<"修改成功!"<<endl; write(S, n); } } /**************************************/ //函式名: student_delete(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 刪除學生資訊 /*************************************/ void student_delete(STent_list &S) { int n = read(S); int s; int i = 0, j; cout<<endl<<"刪除學生資訊:"<<endl; cout<<"請輸入需要刪除學生的學號:"<<endl; cin>>s; while((S.stu[i].student_id - s) != 0 && i < n) i++; if(i == n) { cout<<"提示:記錄為空!!!"<<endl; } else { cout<<"提示:已成功刪除!"<<endl; cout<<"你要刪除的資訊如下:"<<endl; cout<<"學號:"<<S.stu[i].student_id<<endl; cout<<"姓名:"<<S.stu[i].student_name<<endl; cout<<"性別: "<<S.stu[i].sex<<endl; cout<<"年齡:"<<S.stu[i].age<<endl; cout<<"班級:"<<S.stu[i].Class<<endl; cout<<"專業:"<<S.stu[i].specialty<<endl; cout<<"題目編號:"<<S.stu[i].course_id<<endl; cout<<"題目名稱:"<<S.stu[i].course_name<<endl; cout<<"關鍵詞:"<<S.stu[i].keyword<<endl; cout<<"實現技術:"<<S.stu[i].technology<<endl; for(j = i; j < n-1; j++) { S.stu[j].student_id = S.stu[j+1].student_id; strcpy(S.stu[j].student_name,S.stu[j+1].student_name); strcpy(S.stu[j].sex,S.stu[j+1].sex); S.stu[j].age = S.stu[j+1].age; strcpy(S.stu[j].Class,S.stu[j+1].Class); strcpy(S.stu[j].specialty,S.stu[j+1].specialty); S.stu[j].course_id = S.stu[j+1].course_id; strcpy(S.stu[j].course_name,S.stu[j+1].course_name); strcpy(S.stu[j].keyword,S.stu[j+1].keyword); strcpy(S.stu[j].technology,S.stu[j+1].technology); } } write(S, n-1); } /******************************************/ //函式名: total(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 統計學生資訊 /*****************************************/ void total(STent_list &S) { int n = read(S); char c[100]; int ok = 0; cout<<"請輸入需要查詢的題目名稱:"<<endl; cin>>c; for(int i = 0; i < n; i++) if(strcmp(S.stu[i].course_name, c) == 0) { cout<<"你要統計的資訊如下:"<<endl; cout<<"學號:"<<S.stu[i].student_id<<endl; cout<<"姓名:"<<S.stu[i].student_name<<endl; cout<<"性別: "<<S.stu[i].sex<<endl; cout<<"年齡:"<<S.stu[i].age<<endl; cout<<"班級:"<<S.stu[i].Class<<endl; cout<<"專業:"<<S.stu[i].specialty<<endl; cout<<"題目編號:"<<S.stu[i].course_id<<endl; cout<<"題目名稱:"<<S.stu[i].course_name<<endl; cout<<"關鍵詞:"<<S.stu[i].keyword<<endl; cout<<"實現技術:"<<S.stu[i].technology<<endl; ok = 1; } if(ok == 0) { cout<<"沒有此條記錄!"<<endl; } } /********************************************/ //函式名: display(STent_list &S) //引數: (傳入)STent_list S,順序表 //返回值: 空 //功能: 輸出所有學生的全部資訊 /********************************************/ void display(STent_list &S) { int n = read(S); cout<<endl<<"顯示全部學生資訊:"<<endl; if(! S.stu) { cout<<"沒有記錄"<<endl; } else { for(int i = 0; i < n; i++) { cout<<"學號:"<<S.stu[i].student_id<<endl; cout<<"姓名:"<<S.stu[i].student_name<<endl; cout<<"性別: "<<S.stu[i].sex<<endl; cout<<"年齡:"<<S.stu[i].age<<endl; cout<<"班級:"<<S.stu[i].Class<<endl; cout<<"專業:"<<S.stu[i].specialty<<endl; cout<<"題目編號:"<<S.stu[i].course_id<<endl; cout<<"題目名稱:"<<S.stu[i].course_name<<endl; cout<<"關鍵詞:"<<S.stu[i].keyword<<endl; cout<<"實現技術:"<<S.stu[i].technology<<endl; } } } int main() { char choice; cout<<endl<<endl<<" "<<" **歡迎使用課程設計選題管理系統**" <<endl<<endl; cout<<" "<<"1.*********新增新的記錄**********"<<endl; cout<<" "<<"2.*********查詢記錄資訊**********"<<endl; cout<<" "<<"3.*********修改學生資訊**********"<<endl; cout<<" "<<"4.*********刪除學生資訊**********"<<endl; cout<<" "<<"5.*********統計所有記錄**********"<<endl; cout<<" "<<"6.*********顯示所有記錄**********"<<endl; cout<<" "<<"0.********* 退出系統 **********"<<endl; STent_list S; STent_listInit(S); cout<<" "<<"請輸入您的選擇:"; cin>>choice; if(choice == `0`) { cout<<endl<<" "<<" "<<" "<<"謝謝使用本系統! "<<endl<<endl; exit(0); } else if(choice == `1`) { add(S); system("pause"); main(); } else if(choice == `2`) { search(S); system("pause"); main(); } else if(choice == `3`) { student_alter(S); system("pause"); main(); } else if(choice == `4`) { student_delete(S); system("pause"); main(); } else if(choice == `5`) { total(S); system("pause"); main(); } else if(choice == `6`) { display(S); system("pause"); main(); } else { cout<<" "<<"輸入錯誤,請重新輸入你的選擇:"; main(); } return 0; }

  

相關文章