Facebook
From Abrupt Madrill, 4 Years ago, written in Plain Text.
This paste is a reply to Untitled from Subtle Guinea Pig - view diff
Embed
Download Paste or View Raw
Hits: 197
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. class Person
  7. {
  8.     char lastname[40];
  9.     char name[20];
  10.     int age;
  11.  
  12. public:
  13.     Person *next_Person; *previous_Person;
  14.     void save_Person_data(char add_lastname[], char add_name[], int add_age);
  15.  
  16.     char *display_lastname();
  17.     char *display_name();
  18.     int    display_age();
  19.  
  20.     char* get_name() { return name; }
  21.     char* get_lastname() { return lastname; }
  22.     int get_age() { return age; }
  23.     Person* get_next() { return this->next_Person; }  //wsk this wskazuje na  obiekt dla którego została wywołana metoda
  24.     void set_next(Person* next_person) { this->next_Person = next_Person; }
  25.     bool less_than(Person* other) {
  26.         return (this->name < other->get_name());
  27.     }
  28. };
  29.  
  30. class List
  31. {
  32.  
  33.     Person *first_Person;
  34. public:
  35.     Person find_Person(char lastanme[]); //it means that function displays Person using it's lastname.
  36.     void add_Person (Person *pointer_Person);//function take pointer at Person that should be added
  37.     void add_person1 (Person *pointer_Person);
  38.     void print_list();
  39. };
  40.  
  41.  
  42. int main()
  43. {
  44. //menu
  45.     //cout << "Input 1 to add and save new person." << endl;
  46.     //cout << "Input 2 to show list." << endl;
  47.     //cout << "Input 3 to stop." << endl;
  48.  
  49.     int choice_number = 1;
  50.  
  51.     List *person_container = new List();
  52.  
  53.     int if_end = 0;
  54.     while (if_end < 3) {
  55.         cout << "Input 1 to add and save new person." << endl;
  56.         cout << "Input 2 to show list." << endl;
  57.         cout << "Input 3 to stop." << endl;
  58.         cin >> choice_number;
  59.         switch(choice_number)
  60.         {
  61.         case 1:
  62.             {
  63.                 Person *tmp_pointer = new Person();
  64.                 char tmp_lastname[40];
  65.                 char tmp_name[20];
  66.                 int tmp_age;
  67.                 cout << "Input last name: " << endl;
  68.                 cin >> tmp_lastname;
  69.                 cout << "Input name: " << endl;
  70.                 cin >> tmp_name;
  71.                 cout << "Input age: " << endl;
  72.                 cin >> tmp_age;
  73.                 (*tmp_pointer).save_Person_data(tmp_lastname,tmp_name, tmp_age);
  74.                 person_container->add_Person(tmp_pointer);
  75.                 delete tmp_pointer;
  76.                 break;
  77.             }
  78.         case 2:
  79.             {
  80.                 person_container -> print_list();
  81.                 break;
  82.             }
  83.         case 3:
  84.             {
  85.                 if_end = 3;
  86.                 break;
  87.             }
  88.         }
  89.         cout << "Type 3 to finish" << endl;
  90.         cin >> if_end;
  91.         cout << endl;
  92.     }
  93.     delete person_container;
  94.     return 0;
  95. }
  96. //display last name
  97. char *Person:: display_lastname()
  98. {
  99. return lastname;}
  100. //save Person data
  101. void Person:: save_Person_data(char add_lastname[], char add_name[], int add_age)
  102. {
  103.     strcpy(lastname, add_lastname);
  104.     strcpy(name, add_name);
  105.     age = add_age;
  106.    // List:: add_Person();
  107. }
  108.  
  109.  
  110. //add Person to List
  111. void List:: add_Person(Person *pointer_Person)
  112. {
  113.     Person *tmp_pointer = first_Person; //tymczasowy wskaznik na pierwsza osobe
  114.     if(pointer_Person == NULL) return; //jesli wskzaznik nie wskzuje na nic to idz do nastpenej lini
  115.     char tmp_lastname[40];
  116.     strcpy(tmp_lastname, pointer_Person -> display_lastname());
  117.  
  118.     if(first_Person == NULL) //JESCCZE NIE MA PIERWSZEJ OSOBY
  119.     {
  120.         first_Person = pointer_Person; //WSKAZYWANA OSOBA STAJE SIĘ PIERWSZĄ OSOBĄ
  121.         return; //PROGRAM PRZECHODZI DO NASTĘPNEJ LINI
  122.     }
  123.     if(strcmp(tmp_lastname, first_Person-> display_lastname())<0){ // jesli tymczasoe nazwisko jest więększe od pierwszego
  124.         first_Person -> previous_Person = pointer_Person; //dodawana osoba staje sie poprzednia osoba i nadane jej jest nazwa first
  125.         pointer_Person -> next_Person = first_Person;
  126.         first_Person = pointer_Person;
  127.         return;
  128.     }
  129.     //JEZELI DOSZLISMY DO TEGO MOMENTU DO pointer_Person MA BYC DOPISANA CONAJMNIEJ PO PIERWZYM ELEMENCIR
  130.     while(tmp_pointer -> next_Person != NULL) //WYKONUJ DOPÓKI WSKAZNIK tmp_pointer WSKAZUJE NA NASTĘPNĄ OSOBĘ
  131.     {
  132.         if(strcmp(tmp_lastname, tmp_pointer -> next_Person -> display_lastname()) >0) //NOWĄ OSOBĘ DOPISAĆ W TYM MIEJSU
  133.  
  134.             {
  135.                 tmp_pointer -> next_Person -> previous_Person = pointer_Person;
  136.                 pointer_Person -> next_Person = tmp_pointer -> next_Person;
  137.                 pointer_Person -> previous_Person = tmp_pointer;
  138.                 tmp_pointer -> next_Person = pointer_Person;
  139.                 return;
  140.             }
  141.         tmp_pointer = tmp_pointer -> next_Person;
  142.  
  143.     }
  144.     //JEZELI JESTESMY TU TO OSOBE NALEZAY DOPISAC NA KONIEC LISTY
  145.     tmp_pointer -> next_Person = pointer_Person;
  146.     pointer_Person -> previous_Person = tmp_pointer;
  147.     return;
  148.  
  149. }
  150.  
  151. /*void List::add_person1(Person *head, Person *new_Person){
  152.     if (head == NULL) {
  153.         this->first_Person=new_Person;
  154.     }
  155.     else {
  156.         if ((head->next_Person) == NULL) {
  157.             head->next_Person=new_Person;
  158.         }
  159.         else {
  160.             next_person = head->next_person;
  161.         }
  162.     }
  163. }
  164. */
  165. void List::print_list()
  166. {
  167.     //Person *temp = this -> first_Person;
  168.     Person *temp = first_Person;
  169.     //while(temp!=NULL)
  170.     for(int i = 0; i<2; i++)
  171.     {
  172.       cout<<temp->get_name()<<"\t";
  173.       cout<<temp->get_lastname()<< "\t";
  174.       cout<<temp->get_age()<<endl;
  175.       temp = temp -> get_next();
  176.     }
  177. }
  178.  
captcha