Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdafx.h"
- #include <iostream>
- #include <process.h>
- #include <iomanip>
- #include<fstream>
- using namespace std;
- struct list
- {
- int id;
- char* item;
- char* type;
- int count;
- int weight;
- list* next;
- };
- struct inventory
- {
- char key;
- list* first;
- inventory* prev;
- };
- inventory* add_kat(inventory* head, char c)
- {
- inventory* temp = new inventory;
- if (!temp)
- {
- cout << "Ошибка памяти!";
- exit(1);
- }
- temp->key = c;
- temp->first = NULL;
- temp->prev = head;
- return temp;
- }
- list* add_list(int id, char* item, char* type, int count, int weight, list* first)
- {
- list *temp;
- if (!first)
- {
- first = new list;
- temp = first;
- }
- else
- {
- list* new_list = new list;
- temp = first;
- while (temp->next) temp = temp->next;
- temp->next = new_list;
- temp = new_list;
- }
- temp->next = NULL;
- temp->id = id;
- temp->item = new char[strlen(item) + 1];
- strcpy(temp->item, item);
- temp->type = new char[strlen(type) + 1];
- strcpy(temp->type, type);
- temp->count = count;
- temp->weight = weight;
- return first;
- }
- inventory* add_element(inventory *head)
- {
- inventory *kat_ptr;
- list* ptr;
- int id;
- char item[255], type[255];
- int count, t,weight;
- cout << "\nВведите id детали: \t";
- cin >> id;
- cin.ignore(1);
- cout << "\nВведите название детали: \t";
- cin.getline(item, 200);
- cout << "\nВведите тип детали: \t";
- cin.getline(type, 200);
- cout << "\nВведите количество: \t";
- cin >> count;
- cout << "\nВведите вес детали: \t";
- cin >> weight;
- kat_ptr = head; ptr = NULL;
- while (kat_ptr)
- {
- if (kat_ptr->key == item[0]) { ptr = kat_ptr->first; break; }
- kat_ptr = kat_ptr->prev;
- }
- if (ptr) add_list(id, item, type, count, weight, ptr);
- else
- {
- head = add_kat(head, item[0]);
- head->first = add_list(id, item, type, count, weight, NULL);
- }
- cout << endl;
- return head;
- }
- inventory* delete_element(inventory *head, int id)
- {
- list *next, *prev = 0, *r;
- r = head->first;
- while (r)
- {
- next = r->next;
- if (r->id == id)
- {
- if (prev) prev->next = next;
- delete r;
- }
- else
- {
- prev = r;
- }
- r = next;
- }
- cout << "Элемент удалён" << endl;
- return head;
- }
- void show_all(inventory* head)
- {
- inventory* temp = head;
- list* ptr;
- ofstream out("lst.txt");
- cout << endl << setw(2) << "ID\t" << setw(10) << "Название" << setw(10) << "\t" << "Тип" << setw(10) << "\t" << "Кол-во" << setw(10) << "\t" << "Вес" << endl;
- out << endl << setw(2) << "ID\t" << setw(10) << "Название" << setw(10) << "\t" << "Тип" << setw(10) << "\t" << "Кол-во" << setw(10) << "\t" << "Вес" << endl;
- while (temp)
- {
- ptr = temp->first;
- while (ptr)
- {
- out << endl << setw(2) << ptr->id << setw(10) << "\t" << ptr->item << setw(10) << "\t" << ptr->type << setw(10) << "\t" << ptr->count << setw(10) << "\t" << ptr->weight << endl;
- cout << endl << setw(2) << ptr->id << setw(10) << "\t" << ptr->item << setw(10) << "\t" << ptr->type << setw(10) << "\t" << ptr->count << setw(10) << "\t" << ptr->weight << endl;
- ptr = ptr->next;
- }
- temp = temp->prev;
- }
- }
- inventory* search(inventory *head)
- {
- char ans;
- int _id;
- inventory *kat_ptr = head;
- cout << "\nВведите id товара: ";
- cin >> _id;
- cout << endl << setw(2) << "ID\t" << setw(10) << "Название" << setw(10) << "\t" << "Тип" << setw(3) << "\t" << "Кол-во" << setw(2) << "\t" << "Вес" << endl;
- if (head->first->id == _id)
- {
- cout << endl << setw(2) << head->first->id << setw(10) << "\t" << head->first->item << setw(10) << "\t" << head->first->type << setw(3) << "\t" << head->first->count << setw(2) << "\t" << head->first->weight << endl;
- }
- return head;
- }
- inventory* _delete(inventory *head)
- {
- int id,ans;
- inventory *kat_ptr = head;
- cout << "\nВведите id товара: ";
- cin >> id;
- cout << "\ny\\n: ";
- cin >> ans;
- if (ans == 'y' || ans == '1')
- return head = delete_element(head, id);
- else
- return head;
- }
- void main()
- {
- setlocale(LC_ALL, "rus");
- inventory *head = NULL;
- int input;
- start:
- cout << "1. Добавить элемент" << endl;
- cout << "2. Показать все элементы" << endl;
- cout << "3. Найти элемент" << endl;
- cout << "4. Удалить элемент" << endl;
- cout << "5. Очистить экран" << endl;
- cout << "6. Выход" << endl;
- cin >> input;
- switch (input)
- {
- case 1:
- head = add_element(head);
- goto start;
- break;
- case 2:
- show_all(head);
- goto start;
- break;
- case 3:
- search(head);
- goto start;
- break;
- case 4:
- _delete(head);
- goto start;
- break;
- case 5:
- system("Cls");
- cout << "Экран очищен!\n";
- goto start;
- break;
- case 6:
- cout << "Закрытие программы!\n";
- break;
- default:
- cout << "Ошибка ввода! Закрытие программы!\n";
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement