Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct COFFEE
- {
- string articul;
- int count;
- float price;
- COFFEE* next;
- };
- // Добавяне на елемент в началото
- COFFEE* prependNode(COFFEE* head )
- {
- COFFEE* newNode = new COFFEE;
- cin >> newNode->articul;
- cin >> newNode->count;
- cin >> newNode->price;
- newNode->next = head;
- head = newNode;
- return head;
- }
- // Визуализиране на елементите
- void displayNodes(COFFEE* head)
- {
- COFFEE* list = head;
- while (list != NULL)
- {
- cout << list->articul << " " << list->count << " " << list->price << endl;
- list = list->next;
- }
- }
- // Изчисляване на дължима сума по артикули (брой по единична цена)
- void calculatePrice(COFFEE* head)
- {
- if (head == NULL)
- {
- return;
- }
- else
- {
- cout << head->articul<<" "<<(head->count)*(head->price) << endl;
- calculatePrice(head->next);
- }
- }
- // Обща дължима сума
- float totalPrice(COFFEE* head, float S)
- {
- if (head == NULL)
- {
- return 0;
- }
- else
- {
- S = (head->count) * (head->price);
- return S + totalPrice(head->next, S);
- }
- }
- int main()
- {
- COFFEE* head = NULL;
- for (size_t i = 0; i < 3; i++)
- {
- head = prependNode(head);
- }
- cout << endl;
- displayNodes(head);
- cout << endl;
- calculatePrice(head);
- cout << endl;
- cout << "Total price: " << totalPrice(head, 0);
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment