Advertisement
193030

Kursova added unemploted list

Oct 17th, 2020
1,985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. // Трите имена
  5. // Домашни и служебни адреси(индикатор за различаване)
  6. // телефони на съответните адреси
  7.  
  8. // -създаване на указателя, като информацията се чете
  9. // клавиатурата
  10. // -извежда информация за броя на работещите(лицето е
  11. // е работещо, ако има поне един служебен адрес
  12.  
  13. string firstName;
  14. string secondName;
  15. string lastName;
  16. string adress1;
  17. string adress2;
  18. string adress3;
  19. string phoneNumber1;
  20. string phoneNumber2;
  21. string phoneNumber3;
  22.  
  23.  
  24.  
  25. struct Node
  26. {
  27.     string data;
  28.     Node *next;
  29.     Node *child;
  30. };
  31.  
  32.  
  33.  
  34. Node *firstChild = NULL;
  35. Node *secondChild = NULL;
  36.  
  37. void enterPerson(); // Read input from the console
  38. // third name, address1, address2, address3, Phone number 1,2,3.
  39. struct Node* addNode(); // Create a new node
  40. void traverseNodes(struct Node*); // Traverse and print the multilevel linked list
  41. void traverseNodesWithoutJobPlace(struct Node*); // Print only the people without job
  42.  
  43. int currentNode = 1;
  44. int currentNodeUnmployedList = 1;
  45. struct Node* tempFirst;
  46. int printFlag = 0;
  47.  
  48.  
  49. int main()
  50. {
  51.  
  52.     typedef struct Node* *testDynamicArray;
  53.     struct Node *Array[5];
  54.     struct Node **DynamicArray;
  55.     struct Node **temp;
  56.     int dynamicArraySize = 1;
  57.     DynamicArray = new struct Node*[dynamicArraySize];
  58.  
  59.     int numberOfPeople = 0;
  60.     int loop = 1;
  61.     while(loop)
  62.     {
  63.         //dynamicArraySize = sizeof(DynamicArray)/sizeof(DynamicArray[0]);
  64.         cout << "Menu: " << endl;
  65.         cout << "1. Press 1 to add person" << endl;
  66.         cout << "2. Press 2 to show the list" << endl;
  67.         cout << "3. Press 3 to show unemployed people" << endl;
  68.         cout << "4. Press 0 to exit" << endl;
  69.         int inputCommand;
  70.         cin >> inputCommand;
  71.         switch(inputCommand)
  72.         {
  73.         case 1:
  74.             enterPerson();
  75.             if(numberOfPeople == 0)
  76.             {
  77.                  // Saving pointer in the first address of the dynamic array
  78.                 DynamicArray = new struct Node*[numberOfPeople];
  79.                 DynamicArray[0] = addNode();
  80.                 cout << "dynamic array[0] " << DynamicArray[0]->data<< endl;
  81.  
  82.             }
  83.             else
  84.             {
  85.                 // Temporary array [numberOfPeople+1]
  86.                 temp = new struct Node*[numberOfPeople+1];
  87.  
  88.                 // Copy data to temp array
  89.                 for(int i =0; i<numberOfPeople;i++)
  90.                 {
  91.                     temp[i] = DynamicArray[i];
  92.                 }
  93.  
  94.                 // Remove dynamic array
  95.                 delete[] DynamicArray;
  96.                 // temp = nullptr;
  97.  
  98.  
  99.                 DynamicArray = temp;
  100.                 // Add Node to the dynamic array
  101.                 DynamicArray[numberOfPeople] = addNode();
  102.  
  103.             }
  104.             numberOfPeople++;
  105.             break;
  106.         case 2:
  107.             for(int i =0; i<numberOfPeople;i++)
  108.             {
  109.                 traverseNodes(DynamicArray[i]);
  110.             }
  111.  
  112.             break;
  113.         case 3:
  114.             for(int i =0; i<numberOfPeople;i++)
  115.             {
  116.                 traverseNodesWithoutJobPlace(DynamicArray[i]);
  117.             }
  118.             break;
  119.         case 0:
  120.             loop = 0;
  121.             break;
  122.         default:
  123.  
  124.             break;
  125.  
  126.         }
  127.     }
  128. }
  129.  
  130.  
  131.  
  132. struct Node* addNode()
  133. {
  134.    struct Node* first;
  135.    first = new Node;
  136.    first->data = firstName;
  137.    Node* secN = new Node;
  138.    secN->data = secondName;
  139.    secN->child = NULL;
  140.    first->next =secN;
  141.  
  142.  
  143.    Node* lastN = new Node;
  144.    lastN ->data = lastName;
  145.    secN->next = lastN;
  146.    lastN->child = NULL;
  147.  
  148.    Node* adress1Node = new Node;
  149.    adress1Node->data = adress1;
  150.    adress1Node->child = NULL;
  151.  
  152.    Node* adress2Node = new Node;
  153.    adress2Node->data = adress2;
  154.    adress1Node->next = adress2Node;
  155.    adress2Node->child = NULL;
  156.  
  157.  
  158.    Node* adress3Node = new Node;
  159.    adress3Node->data = adress3;
  160.    adress2Node->next = adress3Node;
  161.    adress3Node->child = NULL;
  162.  
  163.    first->child = adress1Node;
  164.  
  165.    Node* firstNumberNode = new Node;
  166.    firstNumberNode->data = phoneNumber1;
  167.    firstNumberNode->next = NULL;
  168.    firstNumberNode->child = NULL;
  169.  
  170.    Node* secondNumberNode = new Node;
  171.    secondNumberNode->data = phoneNumber2;
  172.    secondNumberNode->next = NULL;
  173.    secondNumberNode->child = NULL;
  174.    firstNumberNode->next = secondNumberNode;
  175.  
  176.    Node* thirdNumberNode = new Node;
  177.    thirdNumberNode->data = phoneNumber3;
  178.    thirdNumberNode->next = NULL;
  179.    thirdNumberNode->child = NULL;
  180.  
  181.    adress1Node->child = firstNumberNode;
  182.    adress3Node->child = thirdNumberNode;
  183.     return first;
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190. void traverseNodesWithoutJobPlace(struct Node* p)
  191. {
  192.     if(currentNodeUnmployedList>9 && p)
  193.     {
  194.         currentNodeUnmployedList = 1;
  195.         printFlag = 0;
  196.         cout << "_______" << endl;
  197.     }
  198.     if(currentNodeUnmployedList==1 && p)
  199.     {
  200.         cout << " stava 1 " << endl;
  201.         tempFirst = p;
  202.  
  203.     }
  204.    // if(tempFirst->child->next->data.empty() && tempFirst->child->next->next->data.empty())
  205.    if(tempFirst!=NULL && currentNodeUnmployedList == 1 && tempFirst->child->next->data.empty() && tempFirst->child->next->next->data.empty())
  206.    {
  207.      printFlag = 1;
  208.    }
  209.  
  210.  
  211.     if(p==secondChild)
  212.        secondChild=NULL;
  213.     if(p==firstChild)
  214.         firstChild = NULL;
  215.     while(p)
  216.     {
  217.  
  218.       if(p->child)
  219.       {
  220.          if(secondChild == NULL && firstChild!=NULL)
  221.          {
  222.             secondChild = p->child;
  223.          }
  224.  
  225.          if(firstChild == NULL)
  226.             firstChild = p->child;
  227.  
  228.       }
  229.         if(printFlag)
  230.         {
  231.             cout << " trqbva da printira ";
  232.             cout << p->data << " ";
  233.  
  234.         }
  235.         p = p->next;
  236.         currentNodeUnmployedList++;
  237.     }
  238.     if(firstChild !=NULL || secondChild != NULL)
  239.     {
  240.  
  241.         if(firstChild)
  242.         {
  243.             traverseNodesWithoutJobPlace(firstChild);
  244.  
  245.         }
  246.         if(secondChild)
  247.         {
  248.             traverseNodesWithoutJobPlace(secondChild);
  249.  
  250.         }
  251.  
  252.     }
  253. }
  254.  
  255.  
  256.  
  257. void traverseNodes(struct Node* p)
  258. {
  259.     if(p==secondChild)
  260.        secondChild=NULL;
  261.     if(p==firstChild)
  262.         firstChild = NULL;
  263.     while(p)
  264.     {
  265.  
  266.       if(p->child)
  267.       {
  268.          if(secondChild == NULL && firstChild!=NULL)
  269.          {
  270.             secondChild = p->child;
  271.          }
  272.  
  273.          if(firstChild == NULL)
  274.             firstChild = p->child;
  275.  
  276.       }
  277.         cout << p->data << " ";
  278.         p = p->next;
  279.     }
  280.  
  281.     if(firstChild !=NULL || secondChild != NULL)
  282.     {
  283.  
  284.         if(firstChild)
  285.         {
  286.             traverseNodes(firstChild);
  287.  
  288.         }
  289.         if(secondChild)
  290.         {
  291.             //cout << " v ifa second child data " << secondChild->data << endl;
  292.             traverseNodes(secondChild);
  293.            // cout << " second child " << endl;
  294.  
  295.         }
  296.  
  297.         //enqueue(lastChild);
  298.     }
  299. }
  300.  
  301. void enterPerson()
  302. {
  303.     cin.ignore();
  304.     cout << "Enter first name: " << endl;
  305.     cin >> firstName;
  306.     cout << "Enter second name: " << endl;
  307.     cin >> secondName;
  308.     cout << "Enter last name name: " << endl;
  309.     cin >> lastName;
  310.     cout << "Enter first address: " << endl;
  311.     cin >> adress1;
  312.     cout << "Enter second address: " << endl;
  313.     cin.ignore();
  314.     getline(cin,adress2);
  315.     cout << "Enter third address: " << endl;
  316.     getline(cin,adress3);
  317.     cout << "Enter first phone number: " << endl;
  318.     cin >> phoneNumber1;
  319.     cout << "Enter second phone number: " << endl;
  320.     cin >> phoneNumber2;
  321.     cout << "Enter third phone number: " << endl;
  322.     cin >> phoneNumber3;
  323.  
  324. }
  325.  
  326.  
  327.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement