dstamatova

LincedList-Cofe

Mar 27th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct COFFEE
  6. {
  7.     string articul;
  8.     int count;
  9.     float price;
  10.     COFFEE* next;
  11. };
  12.  
  13. // Добавяне на елемент в началото
  14. COFFEE* prependNode(COFFEE* head )
  15. {
  16.     COFFEE* newNode = new COFFEE;
  17.  
  18.     cin >> newNode->articul;
  19.     cin >> newNode->count;
  20.     cin >> newNode->price;
  21.     newNode->next = head;
  22.     head = newNode;
  23.  
  24.     return head;
  25. }
  26.  
  27. // Визуализиране на елементите
  28. void displayNodes(COFFEE* head)
  29. {
  30.     COFFEE* list = head;
  31.    
  32.     while (list != NULL)
  33.     {
  34.         cout << list->articul << " " << list->count << " " << list->price << endl;
  35.         list = list->next;
  36.     }
  37. }
  38.  
  39. // Изчисляване на дължима сума по артикули (брой по единична цена)
  40. void calculatePrice(COFFEE* head)
  41. {
  42.     if (head == NULL)
  43.     {
  44.         return;
  45.     }
  46.     else
  47.     {
  48.         cout << head->articul<<" "<<(head->count)*(head->price) << endl;
  49.         calculatePrice(head->next);
  50.     }
  51. }
  52.  
  53. // Обща дължима сума
  54. float totalPrice(COFFEE* head, float S)
  55. {
  56.     if (head == NULL)
  57.     {
  58.         return 0;
  59.     }
  60.     else
  61.     {
  62.         S = (head->count) * (head->price);
  63.         return S + totalPrice(head->next, S);
  64.     }
  65. }
  66.  
  67. int main()
  68. {
  69.     COFFEE* head = NULL;
  70.    
  71.     for (size_t i = 0; i < 3; i++)
  72.     {
  73.         head = prependNode(head);
  74.     }
  75.     cout << endl;
  76.  
  77.     displayNodes(head);
  78.     cout << endl;
  79.  
  80.     calculatePrice(head);
  81.     cout << endl;
  82.  
  83.     cout << "Total price: " << totalPrice(head, 0);
  84.     cout << endl;
  85. }
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment