Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Date modified: 27/4/2013
- Last Time save: 23:50
- ||============Basic Information========================||
- ||============Author Information=======================||
- || Author Name: Trình Công Quang ||
- || ID: 12520344 ||
- || E-Mail: [email protected] ||
- ||============Module Information=======================||
- || ||
- */
- #include "stdafx.h"
- #define max 100
- #define sgn '$'
- using namespace std;
- typedef struct dob//Save Date of birth
- {
- string d;
- string m;
- string y;
- };//==================================
- typedef struct students//Save all information of a student
- {
- string MSSV;
- dob date;
- string name;
- string class_name;
- string SDT;
- string nick_name;
- };//=====================================================
- typedef struct node
- {
- students rec;
- node *next;
- };
- typedef struct list
- {
- node *n_h;
- node *n_t;
- };
- void init_list(list &lst)//Initializate list
- {
- lst.n_h=lst.n_t=NULL;
- }//=========================================
- node *init_node(students sv)
- {
- node *nd;
- nd= new node;
- if(nd==NULL)
- {
- exit(1);
- }
- nd->rec=sv;
- nd->next=NULL;
- }
- node search(list lst, string mssv)//Search a node with variable is Student ID
- {
- node *tmp_nd;
- tmp_nd=lst.n_h;
- while((tmp_nd!=NULL) && (tmp_nd->rec.MSSV!=mssv))
- {
- tmp_nd=tmp_nd->next;
- }
- return *tmp_nd;
- }//=========================================================================
- void add(list lst)//Add new student
- {
- students sv;
- node *nd;
- nd=new node;
- if (nd==NULL) exit(1);
- cout<<"\nNhap thong tin sinh vien: ";
- cout<<"\nMa so sinh vien: "; cin>> sv.MSSV;
- cout<<"\nHo va ten: "; cin>> sv.name;
- cout<<"\nNgay/Thang/Nam sinh: "; cin>> sv.date.d>> sv.date.m>>sv.date.y;
- cout<<"\nLop: "; cin>> sv.class_name;
- cout<<"\nNick name: "; cin>> sv.nick_name;
- cout<<"\nSo dien thoai: "; cin>> sv.SDT;
- nd->rec=sv;
- nd->next=NULL;
- if(lst.n_h==NULL)
- {
- lst.n_h=nd;
- lst.n_t=nd;
- }
- else
- {
- lst.n_t->next=nd;
- lst.n_t=nd;
- }
- }//================================================================
- void add(list lst, students sv)//Add a student to list
- {
- //students sv;
- node *nd;
- nd=new node;
- if (nd==NULL) exit(1);
- nd->rec=sv;
- nd->next=NULL;
- if(lst.n_h==NULL)
- {
- lst.n_h=nd;
- lst.n_t=nd;
- }
- else
- {
- lst.n_t->next=nd;
- lst.n_t=nd;
- }
- }//================================================================
- void add(list lst, string mssv, string name, string day, string month, string year, string class_name, string nick_name, string sdt)//Temp===========
- {
- node *nd;
- students sv;
- nd=new node;
- sv.MSSV=mssv; sv.date.d=day; sv.date.m=month; sv.date.y=year; sv.class_name=class_name; sv.nick_name=nick_name; sv.SDT=sdt;
- nd->rec=sv;
- nd->next=NULL;
- if(lst.n_h=NULL)
- {
- lst.n_h=nd;
- lst.n_t=nd;
- }
- else
- {
- lst.n_t->next=nd;
- lst.n_t=nd;
- }
- }
- void del(list lst, students sv)//Delete a student at top of list
- {
- node *nd;
- if(lst.n_h!=NULL)
- {
- nd=lst.n_h;
- sv=nd->rec;
- lst.n_h=lst.n_h->next;
- delete nd;
- if(lst.n_h==NULL)
- {
- lst.n_t=NULL;
- }
- }
- else
- cout<<"\nDanh sach rong, khong the xoa.";
- }
- void del_pos(list lst, node *pos, students sv)//Delete a student at a position
- {
- node *nd;
- if(pos!=NULL)
- {
- nd=pos->next;
- if(nd!=NULL)
- {
- if(nd==lst.n_t)
- {
- lst.n_t=pos;
- }
- pos->next=nd->next;
- sv=nd->rec;
- delete nd;
- }
- }
- else cout<<"\nKhong duoc de trong vi tri!";
- }
- void del(list lst)//Delete All data in a list
- {
- node *nd;
- while(lst.n_h!=NULL)
- {
- nd=lst.n_h;
- lst.n_h=nd->next;
- delete nd;
- }
- }
- void del(list lst, students sv, string mssv)//Delete a student with existed ID
- {
- node *nd=lst.n_h, *tmp=NULL;
- if (nd==NULL)
- {
- cout<<"\nDanh sach rong, khong the xoa.";
- }
- else
- {
- if(&search(lst, mssv)==lst.n_h)
- {
- del(lst, sv);
- }
- else
- {
- del_pos(lst, &search(lst, mssv), sv);
- }
- }
- }
- //=====Update Information =========================
- void up_mssv(list lst, string mssv, string new_val)
- {
- node *nd;
- nd=&search(lst, mssv);
- nd->rec.MSSV=new_val;
- }
- void up_name(list lst, string mssv, string new_val)
- {
- node *nd;
- nd=&search(lst, mssv);
- nd->rec.name=new_val;
- }
- void up_date(list lst, string mssv, string day, string month, string year)
- {
- node *nd;
- nd=&search(lst, mssv);
- nd->rec.date.d=day;
- nd->rec.date.m=month;
- nd->rec.date.y=year;
- }
- void up_class(list lst, string mssv, string new_val)
- {
- node *nd;
- nd=&search(lst, mssv);
- nd->rec.class_name=new_val;
- }
- void up_nick(list lst, string mssv, string new_val)
- {
- node *nd;
- nd=&search(lst, mssv);
- nd->rec.nick_name=new_val;
- }
- void up_sdt(list lst, string mssv, string new_val)
- {
- node *nd;
- nd=&search(lst, mssv);
- nd->rec.SDT=new_val;
- }
- void update_info(list lst, string mssv)
- {
- node *nd;
- int m;
- string new_val;
- nd=&search(lst, mssv);
- if(nd!=NULL)
- {
- cout<<"\nBan muon cap nhat thong tin gi?";
- cout<<"\n1.Ma so sinh vien";
- cout<<"\n2.Ho va ten";
- cout<<"\n3.Ngay thang nam sinh";
- cout<<"\n4.Lop";
- cout<<"\n5.Nick name";
- cout<<"\n6.So dien thoai";
- cout<<"\nBan chon: "; cin>>m;
- switch(m)
- {
- case 1:{
- cout<<"\nNhap vao ma so sinh vien moi: "; cin>>new_val;
- up_mssv(lst,mssv, new_val);
- } break;
- case 2:{
- cout<<"\nNhap Ten moi: "; cin>>new_val;
- up_name(lst, mssv, new_val);
- } break;
- case 3:{
- string m, y;
- cout<<"\nNhap ngay thang nam sinh moi: "; cin>>new_val>>m>>y;
- up_date(lst, mssv, new_val, m, y);
- } break;
- case 4:{
- cout<<"\nNhap lop moi: "; cin>>new_val;
- up_class(lst, mssv, new_val);
- } break;
- case 5:{
- cout<<"\nNhap nick name moi: "; cin>>new_val;
- up_nick(lst, mssv, new_val);
- } break;
- case 6:{
- cout<<"\nNhap So dien thoai moi: "; cin>>new_val;
- up_sdt(lst, mssv, new_val);
- } break;
- }
- }
- else
- cout<<"\nKhong tim thay sinh vien nay.";
- }
- //================================================
- void load(string file_name, students sv[])//Load data to a array
- {
- fstream fs;
- string tmp_str;
- fs.open(file_name, ios::in);
- if(fs.is_open())
- {
- int i=0;
- while((fs.good())&& (!fs.eof()))
- {
- getline(fs, tmp_str);
- istringstream tmp_info(tmp_str);
- getline(tmp_info, sv[i].MSSV, sgn);
- getline(tmp_info, sv[i].name, sgn);
- getline(tmp_info, sv[i].date.d, sgn);
- getline(tmp_info, sv[i].date.m, sgn);
- getline(tmp_info, sv[i].date.y, sgn);
- getline(tmp_info, sv[i].class_name, sgn);
- getline(tmp_info, sv[i].nick_name, sgn);
- getline(tmp_info, sv[i].SDT, sgn);
- cout<<"\n"<<sv[i].MSSV<<"\n"<<sv[i].name<<"\n"<<sv[i].date.d<<"/"<<sv[i].date.m<<"/"<<sv[i].date.y<<"\n"<<sv[i].class_name<<"\n"<<sv[i].nick_name<<"\n"<<sv[i].SDT<<"\n\n";
- i++;
- }
- fs.close();
- }
- else cout <<"Cant Open File";
- }
- void load(string file_name, list lst, students sv)//Load data to list
- {
- fstream fs;
- string tmp_str;
- fs.open(file_name, ios::in);
- if(fs.is_open())
- {
- while((fs.good())&&(!fs.eof()))
- {
- getline(fs, tmp_str);
- istringstream tmp_info(tmp_str);
- getline(tmp_info, sv.MSSV, sgn);
- getline(tmp_info, sv.name, sgn);
- getline(tmp_info, sv.date.d, sgn);
- getline(tmp_info, sv.date.m, sgn);
- getline(tmp_info, sv.date.y, sgn);
- getline(tmp_info, sv.class_name, sgn);
- getline(tmp_info, sv.nick_name, sgn);
- getline(tmp_info, sv.SDT, sgn);
- add(lst, sv);
- }
- fs.close();
- }
- else
- cout<<"\nCan't Open this File: "<<file_name;
- }
- int count_of(students sv[])//Cal count of array
- {
- int cnt=0;
- do{
- ++cnt;
- } while(sv[cnt].MSSV!="");
- return cnt;
- }
- void save(string file_name, students sv[])//save data from array to file
- {
- fstream fs(file_name, ios::out);
- for(int i=0;i<count_of(sv);i++)
- {
- string tmp="";
- tmp=tmp+ sv[i].MSSV+ "$"+sv[i].name+"$"+sv[i].date.d+"$"+sv[i].date.m+"$"+sv[i].date.y+"$"+sv[i].class_name+"$"+sv[i].nick_name+"$"+sv[i].SDT+"\n";
- fs<<tmp;
- }
- fs.close();
- }
- void show(list lst)//Show a list
- {
- node *nd;
- nd=lst.n_h;
- while(nd!=NULL)
- {
- cout<<"\n"<<nd->rec.MSSV<<" "<<nd->rec.name;
- nd=nd->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment