Facebook
From Abrupt Madrill, 4 Years ago, written in Plain Text.
This paste is a reply to Untitled from Subtle Guinea Pig - go back
Embed
Viewing differences between Untitled and Re: Untitled
#include 
#include 

using namespace std;

class Person
{
    char lastname[40];
    char name[20];
    int age;

public:
    Person *next_Person; *previous_Person;
    void save_Person_data(char add_lastname[], char add_name[], int add_age);

    char *display_lastname();
    char *display_name();
    int    display_age();

    char* get_name() { return name; }
    char* get_lastname() { return lastname; }
    int get_age() { return age; }
    Person* get_next() { return this->next_Person; }  //wsk this wskazuje na  obiekt dla którego została wywołana metoda
    void set_next(Person* next_person) { this->next_Person = next_Person; }
    bool less_than(Person* other) {
        return (this->name < other->get_name());
    }
};

class List
{

    Person *first_Person;
public:
    Person find_Person(char lastanme[]); //it means that function displays Person using it's lastname.
    void add_Person (Person *pointer_Person);//function take pointer at Person that should be added
    void add_person1 (Person *pointer_Person);
    void print_list();
};


int main()
{
//menu
    //cout << "Input 1 to add and save new person." << endl;
    //cout << "Input 2 to show list." << endl;
    //cout << "Input 3 to stop." << endl;

    int choice_number = 1;

    List *person_container = new List();

    int if_end = 0;
    while (if_end < 3) {
        cout << "Input 1 to add and save new person." << endl;
        cout << "Input 2 to show list." << endl;
        cout << "Input 3 to stop." << endl;
        cin >> choice_number;
        switch(choice_number)
        {
        case 1:
            {
                Person *tmp_pointer = new Person();
                char tmp_lastname[40];
                char tmp_name[20];
                int tmp_age;
                cout << "Input last name: " << endl;
                cin >> tmp_lastname;
                cout << "Input name: " << endl;
                cin >> tmp_name;
                cout << "Input age: " << endl;
                cin >> tmp_age;
                (*tmp_pointer).save_Person_data(tmp_lastname,tmp_name, tmp_age);
                person_container->add_Person(tmp_pointer);
                delete tmp_pointer;
                break;
            }
        case 2:
            {
                person_container -> print_list();
                break;
            }
        case 3:
            {
                if_end = 3;
                break;
            }
        }
        cout << "Type 3 to finish" << endl;
        cin >> if_end;
        cout << endl;
    }
    delete person_container;
    return 0;
}
//display last name
char *Person:: display_lastname()
{
return lastname;}
//save Person data
void Person:: save_Person_data(char add_lastname[], char add_name[], int add_age)
{
    strcpy(lastname, add_lastname);
    strcpy(name, add_name);
    age = add_age;
   // List:: add_Person();
}


//add Person to List
void List:: add_Person(Person *pointer_Person)
{
    Person *tmp_pointer = first_Person; //tymczasowy wskaznik na pierwsza osobe
    if(pointer_Person == NULL) return; //jesli wskzaznik nie wskzuje na nic to idz do nastpenej lini
    char tmp_lastname[40];
    strcpy(tmp_lastname, pointer_Person -> display_lastname());

    if(first_Person == NULL) //JESCCZE NIE MA PIERWSZEJ OSOBY
    {
        first_Person = pointer_Person; //WSKAZYWANA OSOBA STAJE SIĘ PIERWSZĄ OSOBĄ
        return; //PROGRAM PRZECHODZI DO NASTĘPNEJ LINI
    }
    if(strcmp(tmp_lastname, first_Person-> display_lastname())<0){ // jesli tymczasoe nazwisko jest więększe od pierwszego
        first_Person -> previous_Person = pointer_Person; //dodawana osoba staje sie poprzednia osoba i nadane jej jest nazwa first
        pointer_Person -> next_Person = first_Person;
        first_Person = pointer_Person;
        return;
    }
    //JEZELI DOSZLISMY DO TEGO MOMENTU DO pointer_Person MA BYC DOPISANA CONAJMNIEJ PO PIERWZYM ELEMENCIR
    while(tmp_pointer -> next_Person != NULL) //WYKONUJ DOPÓKI WSKAZNIK tmp_pointer WSKAZUJE NA NASTĘPNĄ OSOBĘ
    {
        if(strcmp(tmp_lastname, tmp_pointer -> next_Person -> display_lastname()) >0) //NOWĄ OSOBĘ DOPISAĆ W TYM MIEJSU

            {
                tmp_pointer -> next_Person -> previous_Person = pointer_Person;
                pointer_Person -> next_Person = tmp_pointer -> next_Person;
                pointer_Person -> previous_Person = tmp_pointer;
                tmp_pointer -> next_Person = pointer_Person;
                return;
            }
        tmp_pointer = tmp_pointer -> next_Person;

    }
    //JEZELI JESTESMY TU TO OSOBE NALEZAY DOPISAC NA KONIEC LISTY
    tmp_pointer -> next_Person = pointer_Person;
    pointer_Person -> previous_Person = tmp_pointer;
    return;

}

/*void List::add_person1(Person *head, Person *new_Person){
    if (head == NULL) {
        this->first_Person=new_Person;
    }
    else {
        if ((head->next_Person) == NULL) {
            head->next_Person=new_Person;
        }
        else {
            next_person = head->next_person;
        }
    }
}
*/
void List::print_list()
{
    //Person *temp = this -> first_Person;
    Person *temp = first_Person;
    //while(temp!=NULL)
    for(int i = 0; i<2; i++)
    {
      cout<get_name()<<"\t";
      cout<get_lastname()<< "\t";
      cout<get_age()<       temp = temp -> get_next();
    }
}
captcha