Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- // 9_lab.cpp: определяет точку входа для консольного приложения.
- //
- #include <string.h>
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int const len_table = 4;
- struct sea
- {
- char name[64];
- int deep;
- char location[64];
- sea *last;
- sea *head;
- };
- sea read()
- {
- sea buf_elem;
- char name[32];
- char location[32];
- cin >> name >> buf_elem.deep >> location ;
- strcpy(buf_elem.name, name);
- strcpy(buf_elem.location,location);
- return buf_elem;
- }
- void show(sea seee, char sign = ' ')
- {
- if (sign != ' ')
- {
- cout <<
- sign <<
- seee.name << setw(10) << sign <<
- seee.deep << setw(10) << sign <<
- seee.location << setw(10) << sign <<
- endl;
- }
- else {
- cout <<
- sign <<
- seee.name << sign <<
- seee.deep << sign <<
- seee.location << sign <<
- endl;
- }
- }
- void add(sea *stack, sea elem)
- {
- sea *buf = new sea;
- strcpy(buf->name, elem.name);
- buf->deep = elem.deep;
- strcpy(buf->location ,elem.location);
- buf->last = stack->head; //тот элемент, который является головным, сейчас перемещается в предыдущий для текущего
- stack->head = buf; // текущий элемент становится головным
- }
- void delete_head_elem(sea *stack)
- {
- if (stack->head != NULL)
- {
- sea *ptr = stack->head->last;
- delete stack->head;
- stack->head = ptr;
- }
- else
- {
- delete stack->head;
- }
- }
- void clear_all_steck(sea *stack) {
- while (stack->head != NULL)
- {
- delete_head_elem(stack);
- }
- delete stack->head;
- delete stack;
- }
- void tr(sea *stack)
- {
- show(*stack->head);
- delete_head_elem(stack);
- }
- int main()
- {
- sea seas[len_table];
- for (int i = 0; i < len_table; i++)
- {
- seas[i] = read();
- }
- for (int i = len_table - 1; i >= 0; i--)
- {
- cout << "+--------------------+--------------------+--------------------+\n";
- show(seas[i], '|');
- }
- cout << "+--------------------+--------------------+--------------------+\n";
- sea *stack = new sea;
- stack->head = NULL;
- stack->last = NULL;
- for (int i = 0; i < len_table; i++)
- {
- add(stack, seas[i]);
- }
- char answer[16];
- cout << "Do you want to take head elements in stack? Enter \"no\" or \"yes\".\n";
- while (true) {
- cin >> answer;
- if (strcmp(answer, "no") != 0 & strcmp(answer, "yes") != 0)
- {
- cout << "Error. Enter again.\n";
- }
- else
- {
- if (strcmp(answer, "yes") == 0)
- {
- if (stack->head != NULL)
- {
- tr(stack);
- cout << "Do you want to take head elements in stack again ? Enter \"no\" or \"yes\".\n";
- }
- else
- {
- cout << "You can't take head element because stack is empty\n";
- break;
- }
- }
- if (strcmp(answer, "no") == 0) {
- break;
- }
- }
- }
- clear_all_steck(stack);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement