Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include "Data.h"
  2. class Contact; //to avoid an "undefined reference" error
  3.  
  4. struct ListMem{
  5. ListMem* next=NULL;
  6. Contact* cont=NULL;
  7. Data* dat=NULL;
  8. }; //this class can't have a destructor that calls "delete cont",
  9. //because of the order I declared my classes in
  10.  
  11. #include "Contact.h"
  12. #include "ListMem.h"
  13.  
  14. class Phonebook{
  15. size_t cont_num; //how many contacts are already stored
  16. ListMem* cont; //pointer to the first member of the linked list
  17. public:
  18. Phonebook(const char* filename){ //reads the Phonebook from file
  19. std::ifstream is;
  20. is.open(filename);
  21. size_t pb_size;
  22. string line;
  23. ListMem* tmp;
  24. is>>pb_size; getline(is, line);
  25. cont_num=0;
  26.  
  27. for(size_t i=0; i<pb_size; i++){
  28. getline(is, line);
  29. if(i==0){
  30. Contact* tmp_ct=new Contact(line);
  31. cont=add_cont(tmp_ct);
  32. tmp=cont;
  33. }
  34. else {
  35. Contact* tmp_ct=new Contact(line);
  36. tmp->next=add_cont(tmp_ct);
  37. tmp=tmp->next;
  38. }
  39. }
  40. }
  41.  
  42. ListMem* add_cont(Contact* new_ct){ //adds a new contact
  43. ListMem* tmp=new ListMem;
  44. tmp->cont=new Contact(new_ct);
  45. cont_num++;
  46. return tmp;
  47. }
  48.  
  49. ~Phonebook(){
  50. ListMem* tmp=cont;
  51. while(tmp!=NULL){
  52. ListMem* del=tmp;
  53. tmp=tmp->next;
  54. delete del->dat;
  55. delete del->cont;
  56. delete del;
  57. }
  58. }
  59. };
  60.  
  61. #include "Name.h"
  62. #include "ListMem.h"
  63.  
  64. class Contact{
  65. size_t data_num; //number of data stored
  66. ListMem* data; //pointer to the first data member
  67. public:
  68. Contact(string line){ //converts a "line" into a Contact
  69. ListMem* tmp;
  70. int dat_count=std::count(line.begin(), line.end(), ':');
  71. data_num=dat_count;
  72. string dat, tmp_dat, tmp_type;
  73. int pos_1=0, pos_2=line.find(';');
  74.  
  75. for(int i=0;i<dat_count;i++){
  76. dat=line.substr(pos_1, pos_2-pos_1);
  77. tmp_type=dat.at(0);
  78. tmp_dat=dat.substr(2, dat.size()-2);
  79. pos_1+=dat.size()+1;
  80. pos_2=line.find(';', pos_1);
  81. if(i==0){
  82. data=add_data(tmp_type, tmp_dat);
  83. tmp=data;
  84. }
  85. else {
  86. tmp->next=add_data(tmp_type, tmp_dat);
  87. tmp=tmp->next;
  88. }
  89. }
  90. }
  91.  
  92. ListMem* add_data(string type, string data){ //adds a new data member
  93. ListMem* tmp=new ListMem;
  94. if(type=="1") tmp->dat=new Name(data);
  95. //I have more, but it's irrelevant.
  96. data_num++;
  97. return tmp;
  98. }
  99.  
  100. ~Contact(){
  101. ListMem* tmp=data;
  102. while(tmp!=NULL){
  103. ListMem* del=tmp;
  104. tmp=tmp->next;
  105. delete del->dat;
  106. delete del->cont;
  107. delete del;
  108. }
  109. }
  110. };
  111.  
  112. class Data{
  113. string type;
  114. public:
  115. Data(){}
  116. void set_type(string t) {type=t;}
  117. virtual ~Data(){}
  118. };
  119.  
  120. #include "Data.h"
  121.  
  122. class Name: public Data{
  123. string name;
  124. public:
  125. Name(string n): name(n){ set_type("1");}
  126. ~Name(){}
  127. };
  128.  
  129. #include "Phonebook.h"
  130.  
  131. int main(){
  132. Phonebook* Test=new Phonebook("test.txt");
  133. delete Test;
  134.  
  135. return 0;
  136. }
  137.  
  138. 3
  139. 1:test_name_1;
  140. 1:test_name_2;
  141. 1:test_name_3;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement