detective1711

module.h

Apr 27th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.04 KB | None | 0 0
  1. /*
  2.     Date modified: 27/4/2013
  3.     Last Time save: 23:50
  4.     ||============Basic Information========================||
  5.     ||============Author Information=======================||
  6.     ||  Author Name: Trình Công Quang                    ||
  7.     ||  ID: 12520344                                       ||
  8.     ||  E-Mail:  [email protected]                     ||
  9.     ||============Module Information=======================||
  10.     ||                                                     ||
  11. */
  12. #include "stdafx.h"
  13.  
  14. #define max 100
  15. #define sgn '$'
  16.  
  17. using namespace std;
  18.  
  19. typedef struct dob//Save Date of birth
  20. {
  21.     string d;
  22.     string m;
  23.     string y;
  24. };//==================================
  25. typedef struct students//Save all information of a student
  26. {
  27.     string MSSV;
  28.     dob date;
  29.     string name;
  30.     string class_name;
  31.     string SDT;
  32.     string nick_name;
  33. };//=====================================================
  34. typedef struct node
  35. {
  36.     students rec;
  37.     node *next;
  38. };
  39. typedef struct list
  40. {
  41.     node *n_h;
  42.     node *n_t;
  43. };
  44. void init_list(list &lst)//Initializate list
  45. {
  46.     lst.n_h=lst.n_t=NULL;
  47. }//=========================================
  48. node *init_node(students sv)
  49. {
  50.     node *nd;
  51.     nd= new node;
  52.     if(nd==NULL)
  53.     {
  54.         exit(1);
  55.     }
  56.     nd->rec=sv;
  57.     nd->next=NULL;
  58. }
  59. node search(list lst, string mssv)//Search a node with variable is Student ID
  60. {
  61.     node *tmp_nd;
  62.     tmp_nd=lst.n_h;
  63.     while((tmp_nd!=NULL) && (tmp_nd->rec.MSSV!=mssv))
  64.     {
  65.         tmp_nd=tmp_nd->next;
  66.     }
  67.     return *tmp_nd;
  68. }//=========================================================================
  69. void add(list lst)//Add new student
  70. {
  71.     students sv;
  72.     node *nd;
  73.     nd=new node;
  74.     if (nd==NULL) exit(1);
  75.     cout<<"\nNhap thong tin sinh vien: ";
  76.     cout<<"\nMa so sinh vien: "; cin>> sv.MSSV;
  77.     cout<<"\nHo va ten: "; cin>> sv.name;
  78.     cout<<"\nNgay/Thang/Nam sinh: "; cin>> sv.date.d>> sv.date.m>>sv.date.y;
  79.     cout<<"\nLop: "; cin>> sv.class_name;
  80.     cout<<"\nNick name: "; cin>> sv.nick_name;
  81.     cout<<"\nSo dien thoai: "; cin>> sv.SDT;
  82.     nd->rec=sv;
  83.     nd->next=NULL;
  84.     if(lst.n_h==NULL)
  85.     {
  86.         lst.n_h=nd;
  87.         lst.n_t=nd;
  88.     }
  89.     else
  90.     {
  91.         lst.n_t->next=nd;
  92.         lst.n_t=nd;
  93.     }
  94. }//================================================================
  95. void add(list lst, students sv)//Add a student to list
  96. {
  97.     //students sv;
  98.     node *nd;
  99.     nd=new node;
  100.     if (nd==NULL) exit(1);
  101.     nd->rec=sv;
  102.     nd->next=NULL;
  103.     if(lst.n_h==NULL)
  104.     {
  105.         lst.n_h=nd;
  106.         lst.n_t=nd;
  107.     }
  108.     else
  109.     {
  110.         lst.n_t->next=nd;
  111.         lst.n_t=nd;
  112.     }
  113. }//================================================================
  114. void add(list lst, string mssv, string name, string day, string month, string year, string class_name, string nick_name, string sdt)//Temp===========
  115. {
  116.     node *nd;
  117.     students sv;
  118.     nd=new node;
  119.     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;
  120.     nd->rec=sv;
  121.     nd->next=NULL;
  122.     if(lst.n_h=NULL)
  123.     {
  124.         lst.n_h=nd;
  125.         lst.n_t=nd;
  126.     }
  127.     else
  128.     {
  129.         lst.n_t->next=nd;
  130.         lst.n_t=nd;
  131.     }
  132. }
  133. void del(list lst, students sv)//Delete a student at top of list
  134. {
  135.     node *nd;
  136.     if(lst.n_h!=NULL)
  137.     {
  138.         nd=lst.n_h;
  139.         sv=nd->rec;
  140.         lst.n_h=lst.n_h->next;
  141.         delete nd;
  142.         if(lst.n_h==NULL)
  143.         {
  144.             lst.n_t=NULL;
  145.         }
  146.     }
  147.     else
  148.         cout<<"\nDanh sach rong, khong the xoa.";
  149. }
  150. void del_pos(list lst, node *pos, students sv)//Delete a student at a position
  151. {
  152.     node *nd;
  153.     if(pos!=NULL)
  154.     {
  155.         nd=pos->next;
  156.         if(nd!=NULL)
  157.         {
  158.             if(nd==lst.n_t)
  159.             {
  160.                 lst.n_t=pos;
  161.             }
  162.             pos->next=nd->next;
  163.             sv=nd->rec;
  164.             delete nd;
  165.         }
  166.     }
  167.     else cout<<"\nKhong duoc de trong vi tri!";
  168. }
  169. void del(list lst)//Delete All data in a list
  170. {
  171.     node *nd;
  172.     while(lst.n_h!=NULL)
  173.     {
  174.         nd=lst.n_h;
  175.         lst.n_h=nd->next;
  176.         delete nd;
  177.     }
  178. }
  179. void del(list lst, students sv, string mssv)//Delete a student with existed ID
  180. {
  181.     node *nd=lst.n_h, *tmp=NULL;
  182.     if (nd==NULL)
  183.     {
  184.         cout<<"\nDanh sach rong, khong the xoa.";
  185.     }
  186.     else
  187.     {
  188.         if(&search(lst, mssv)==lst.n_h)
  189.         {
  190.             del(lst, sv);
  191.         }
  192.         else
  193.         {
  194.             del_pos(lst, &search(lst, mssv), sv);
  195.         }
  196.     }
  197. }
  198. //=====Update Information =========================
  199. void up_mssv(list lst, string mssv, string new_val)
  200. {
  201.     node *nd;
  202.     nd=&search(lst, mssv);
  203.     nd->rec.MSSV=new_val;
  204. }
  205. void up_name(list lst, string mssv, string new_val)
  206. {
  207.     node *nd;
  208.     nd=&search(lst, mssv);
  209.     nd->rec.name=new_val;
  210. }
  211. void up_date(list lst, string mssv, string day, string month, string year)
  212. {
  213.     node *nd;
  214.     nd=&search(lst, mssv);
  215.     nd->rec.date.d=day;
  216.     nd->rec.date.m=month;
  217.     nd->rec.date.y=year;
  218. }
  219. void up_class(list lst, string mssv, string new_val)
  220. {
  221.     node *nd;
  222.     nd=&search(lst, mssv);
  223.     nd->rec.class_name=new_val;
  224. }
  225. void up_nick(list lst, string mssv, string new_val)
  226. {
  227.     node *nd;
  228.     nd=&search(lst, mssv);
  229.     nd->rec.nick_name=new_val;
  230. }
  231. void up_sdt(list lst, string mssv, string new_val)
  232. {
  233.     node *nd;
  234.     nd=&search(lst, mssv);
  235.     nd->rec.SDT=new_val;
  236. }
  237. void update_info(list lst, string mssv)
  238. {
  239.     node *nd;
  240.     int m;
  241.     string new_val;
  242.     nd=&search(lst, mssv);
  243.     if(nd!=NULL)
  244.     {
  245.         cout<<"\nBan muon cap nhat thong tin gi?";
  246.         cout<<"\n1.Ma so sinh vien";
  247.         cout<<"\n2.Ho va ten";
  248.         cout<<"\n3.Ngay thang nam sinh";
  249.         cout<<"\n4.Lop";
  250.         cout<<"\n5.Nick name";
  251.         cout<<"\n6.So dien thoai";
  252.         cout<<"\nBan chon: "; cin>>m;
  253.         switch(m)
  254.         {
  255.         case 1:{
  256.                     cout<<"\nNhap vao ma so sinh vien moi: "; cin>>new_val;
  257.                     up_mssv(lst,mssv, new_val);
  258.                } break;
  259.         case 2:{
  260.                     cout<<"\nNhap Ten moi: "; cin>>new_val;
  261.                     up_name(lst, mssv, new_val);
  262.                } break;
  263.         case 3:{
  264.                     string m, y;
  265.                     cout<<"\nNhap ngay thang nam sinh moi: "; cin>>new_val>>m>>y;
  266.                     up_date(lst, mssv, new_val, m, y);
  267.                } break;
  268.         case 4:{
  269.                     cout<<"\nNhap lop moi: "; cin>>new_val;
  270.                     up_class(lst, mssv, new_val);
  271.                } break;
  272.         case 5:{
  273.                     cout<<"\nNhap nick name moi: "; cin>>new_val;
  274.                     up_nick(lst, mssv, new_val);
  275.                } break;
  276.         case 6:{
  277.                     cout<<"\nNhap So dien thoai moi: "; cin>>new_val;
  278.                     up_sdt(lst, mssv, new_val);
  279.                } break;
  280.         }
  281.     }
  282.     else
  283.         cout<<"\nKhong tim thay sinh vien nay.";
  284. }
  285. //================================================
  286.  
  287. void load(string file_name, students sv[])//Load data to a array
  288. {
  289.     fstream fs;
  290.     string tmp_str;
  291.     fs.open(file_name, ios::in);
  292.     if(fs.is_open())
  293.     {
  294.         int i=0;
  295.         while((fs.good())&& (!fs.eof()))
  296.         {
  297.             getline(fs, tmp_str);
  298.             istringstream tmp_info(tmp_str);
  299.             getline(tmp_info, sv[i].MSSV, sgn);
  300.             getline(tmp_info, sv[i].name, sgn);
  301.             getline(tmp_info, sv[i].date.d, sgn);
  302.             getline(tmp_info, sv[i].date.m, sgn);
  303.             getline(tmp_info, sv[i].date.y, sgn);
  304.             getline(tmp_info, sv[i].class_name, sgn);
  305.             getline(tmp_info, sv[i].nick_name, sgn);
  306.             getline(tmp_info, sv[i].SDT, sgn);
  307.             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";
  308.             i++;   
  309.         }
  310.         fs.close();
  311.     }
  312.     else cout <<"Cant Open File";
  313. }
  314. void load(string file_name, list lst, students sv)//Load data to list
  315. {
  316.     fstream fs;
  317.     string tmp_str;
  318.     fs.open(file_name, ios::in);
  319.     if(fs.is_open())
  320.     {
  321.         while((fs.good())&&(!fs.eof()))
  322.         {
  323.             getline(fs, tmp_str);
  324.             istringstream tmp_info(tmp_str);
  325.             getline(tmp_info, sv.MSSV, sgn);
  326.             getline(tmp_info, sv.name, sgn);
  327.             getline(tmp_info, sv.date.d, sgn);
  328.             getline(tmp_info, sv.date.m, sgn);
  329.             getline(tmp_info, sv.date.y, sgn);
  330.             getline(tmp_info, sv.class_name, sgn);
  331.             getline(tmp_info, sv.nick_name, sgn);
  332.             getline(tmp_info, sv.SDT, sgn);
  333.             add(lst, sv);
  334.         }
  335.         fs.close();
  336.     }
  337.     else
  338.         cout<<"\nCan't Open this File: "<<file_name;
  339. }
  340. int count_of(students sv[])//Cal count of array
  341. {
  342.     int cnt=0;
  343.     do{
  344.         ++cnt;
  345.     } while(sv[cnt].MSSV!="");
  346.     return cnt;
  347. }
  348. void save(string file_name, students sv[])//save data from array to file
  349. {
  350.     fstream fs(file_name, ios::out);
  351.     for(int i=0;i<count_of(sv);i++)
  352.     {
  353.         string tmp="";
  354.         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";
  355.         fs<<tmp;
  356.     }
  357.     fs.close();
  358. }
  359. void show(list lst)//Show a list
  360. {
  361.     node *nd;
  362.     nd=lst.n_h;
  363.     while(nd!=NULL)
  364.     {
  365.         cout<<"\n"<<nd->rec.MSSV<<"  "<<nd->rec.name;
  366.         nd=nd->next;
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment