Advertisement
evgenko

max_borejko_coursework

Jun 2nd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4.  
  5. #define count_of_capibilitie 2
  6. char capibilities[count_of_capibilitie][50] = {"NFC", "Поліфонія"};
  7.  
  8. struct Model {
  9.     char model[50];
  10.     char company[50];
  11.     char place[50];
  12.     int year;
  13.     int capibilitie;
  14.     float price;
  15.     struct Model *prev;
  16. } *base = NULL;
  17.  
  18. void print_menu();
  19. void add_element();
  20. void print_elements();
  21. void find_expensive();
  22. void find_elements_from_range();
  23. void sort_by_price();
  24. void sort_by_year();
  25. void find_model_by_capabiltie();
  26. void find_range_by_company();
  27. void export_to_file();
  28. void import_from_file();
  29. void remove_new_line(char *a);
  30. int main() {
  31.     while(1) {
  32.         system("cls");
  33.         print_menu();
  34.         printf("Зробіть свій вибір: ");
  35.         int act;
  36.         scanf("%d", &act);
  37.         switch (act) {
  38.             case 1:
  39.                 add_element();
  40.                 break;
  41.             case 2:
  42.                 print_elements();
  43.                 break;
  44.             case 3:
  45.                 find_expensive();
  46.                 break;
  47.             case 4:
  48.                 find_elements_from_range();
  49.                 break;
  50.             case 5:
  51.                 sort_by_year();
  52.                 break;
  53.             case 6:
  54.                 sort_by_price();
  55.                 break;
  56.             case 7:
  57.                 find_model_by_capabiltie();
  58.                 break;
  59.             case 8:
  60.                 find_range_by_company();
  61.                 break;
  62.             case 9:
  63.                 export_to_file();
  64.                 break;
  65.             case 10:
  66.                 import_from_file();
  67.                 break;
  68.             case 11:
  69.                 exit(0);
  70.             default:
  71.                 printf("ERROR");
  72.                 exit(1);
  73.         }
  74.     }
  75. }
  76.  
  77. void print_menu(){
  78.     puts("Введіть 1  щоб додати модель");
  79.     puts("Введіть 2  щоб вивести всі моделі");
  80.     puts("Введіть 3  щоб знайти моедль самого дорогого телефону");
  81.     puts("Введіть 4  щоб знайти моеделі по заданому діапазону цін");
  82.     puts("Введіть 5  для сортування за роком випуску");
  83.     puts("Введіть 6  для сортування за ціною");
  84.     puts("Введіть 7  для виведення моделі по заданії додатковій можливості");
  85.     puts("Введіть 8  щоб знайти діапазон цін у компанії");
  86.     puts("Введіть 9  щоб експортувати данні");
  87.     puts("Введіть 10 щоб імпортувати данні");
  88.     puts("Введіть 11 щоб закінчити виконання програми");
  89. }
  90.  
  91. void print_element(struct Model element){
  92.     printf("Модель: %s\n", element.model);
  93.     printf("Компанія: %s\n", element.company);
  94.     printf("Місце виробництва: %s\n", element.place);
  95.     printf("Рік виробництва: %d\n", element.year);
  96.     printf("Додаткова можливість: %s\n", capibilities[element.capibilitie-1]);
  97.     printf("Ціна: %.2f\n", element.price);
  98. }
  99. void add_element(){
  100.     struct Model *new_element = malloc(sizeof(struct Model));
  101.     while(1){
  102.         system("cls");
  103.         getchar();
  104.         printf("Введіть модель: ");
  105.         fgets(new_element->model, 50, stdin);
  106.         printf("Введіть назву компанії: ");
  107.         remove_new_line(new_element->model);
  108.         fgets(new_element->company, 50, stdin);
  109.         printf("Введіть місце виробництва: ");
  110.         remove_new_line(new_element->company);
  111.         fgets(new_element->place, 50, stdin);
  112.         remove_new_line(new_element->place);
  113.         printf("Введіть рік виробництва: ");
  114.         scanf("%d", &new_element->year);
  115.         printf("Виберіть додаткову можливість: \n");
  116.         for (int i=0;i<count_of_capibilitie; i++){
  117.             printf("\t%d - %s\n", i+1, capibilities[i]);
  118.         }
  119.         printf("Введіть номер можливості: ");
  120.         scanf("%d", &new_element->capibilitie);
  121.         printf("Введіть ціну: ");
  122.         scanf("%f", &new_element->price);
  123.         system("cls");
  124.         print_element(*new_element);
  125.         printf("Введіть 1 для пітвердження, 2 для вводу данних ще раз\nчи 3 для того щоб повернутись у головне меню");
  126.         int act;
  127.         scanf("%d", &act);
  128.         switch(act) {
  129.             case 1:
  130.                 new_element->prev = base;
  131.                 base = new_element;
  132.                 return;
  133.             case 2:
  134.                 break;
  135.             case 3:
  136.                 return;
  137.             default:
  138.                 printf("ERROR");
  139.                 exit(1);
  140.         }
  141.     }
  142. }
  143. void print_elements(){
  144.     system("cls");
  145.     struct Model *element = base;
  146.     while(element!=NULL){
  147.         print_element(*element);
  148.         puts("******************************************************");
  149.         element = element->prev;
  150.     }
  151.     system("pause");
  152. }
  153. void find_expensive(){
  154.     system("cls");
  155.     struct Model *element = base;
  156.     struct Model *expensive_model = base;
  157.     if (expensive_model == NULL){
  158.         printf("Моделей ще не має в программі");
  159.         return;
  160.     }
  161.     while(element!=NULL){
  162.         if (element->price > expensive_model->price){
  163.             expensive_model = element;
  164.         }
  165.         element = element->prev;
  166.     }
  167.     element = base;
  168.     while(element!=NULL){
  169.         if (element->price == expensive_model->price) {
  170.             print_element(*element);
  171.             puts("******************************************************");
  172.         }
  173.         element = element->prev;
  174.     }
  175.     system("pause");
  176. }
  177. void find_elements_from_range(){
  178.     system("cls");
  179.     printf("Введіть границю: ");
  180.     float left, right;
  181.     scanf("%f", &left);
  182.     printf("Введіть границю: ");
  183.     scanf("%f", &right);
  184.     if (left>right){
  185.         float tmp = left;
  186.         left = right;
  187.         right = tmp;
  188.     }
  189.     struct Model *element = base;
  190.     while(element!=NULL){
  191.         if (left <= element->price && element->price <= right) {
  192.             print_element(*element);
  193.             puts("******************************************************");
  194.         }
  195.         element = element->prev;
  196.     }
  197.     system("pause");
  198. }
  199. void swap_data(struct Model *a, struct Model *b){
  200.     struct Model tmp;
  201.     strcpy(tmp.model, a->model);
  202.     strcpy(tmp.company, a->company);
  203.     strcpy(tmp.place, a->place);
  204.     tmp.year = a->year;
  205.     tmp.capibilitie = a->capibilitie;
  206.     tmp.price = a->price;
  207.     strcpy(a->model, b->model);
  208.     strcpy(a->company, b->company);
  209.     strcpy(a->place, b->place);
  210.     a->year = b->year;
  211.     a->capibilitie = b->capibilitie;
  212.     a->price = b->price;
  213.     strcpy(b->model, tmp.company);
  214.     strcpy(b->company, tmp.company);
  215.     strcpy(b->place, tmp.place);
  216.     b->year = tmp.year;
  217.     b->capibilitie = tmp.capibilitie;
  218.     b->price = tmp.price;
  219. }
  220. void sort_by_year(){
  221.     struct Model *i = base;
  222.     while(i!=NULL){
  223.         struct Model *j = base;
  224.         while(j!=NULL) {
  225.             if (i->year > j->year){
  226.                 swap_data(i, j);
  227.             }
  228.             j = j->prev;
  229.         }
  230.         i = i->prev;
  231.     }
  232.     puts("Відсортированно");
  233. }
  234. void sort_by_price(){
  235.     struct Model *i = base;
  236.     while(i!=NULL){
  237.         struct Model *j = base;
  238.         while(j!=NULL) {
  239.             if (i->price > j->price){
  240.                 swap_data(i, j);
  241.             }
  242.             j = j->prev;
  243.         }
  244.         i = i->prev;
  245.     }
  246.     puts("Відсортированно");
  247. }
  248. void find_model_by_capabiltie(){
  249.     printf("Виберіть додаткову можливість: \n");
  250.     for (int i=0;i<count_of_capibilitie; i++){
  251.         printf("\t%d - %s\n", i+1, capibilities[i]);
  252.     }
  253.     printf("Введіть номер можливості: ");
  254.     int cap;
  255.     scanf("%d", &cap);
  256.     system("cls");
  257.     struct Model *element = base;
  258.     while(element!=NULL){
  259.         if (element->capibilitie == cap) {
  260.             print_element(*element);
  261.             puts("******************************************************");
  262.         }
  263.         element = element->prev;
  264.     }
  265.     system("pause");
  266. }
  267. void find_range_by_company(){
  268.     system("cls");
  269.     printf("Введіть назву компанії: ");
  270.     struct Model *element = base;
  271.     if (element == NULL){
  272.         printf("Елементів не знайдено");
  273.         return;
  274.     }
  275.     float min,max;
  276.     while(element!=NULL){
  277.         min = element->price<min?element->price:min;
  278.         max = element->price>max?element->price:max;
  279.         element = element->prev;
  280.     }
  281.     printf("Діапазон цін: %.2f -- %.2f", min, max);
  282.     system("pause");
  283. }
  284. void export_to_file(){
  285.     FILE *file;
  286.     getchar();
  287.     char path[100];
  288.     printf("Введіть шлях до файлу: ");
  289.     fgets(path, 100, stdin);
  290.     remove_new_line(path);
  291.     file = fopen(path, "wb");
  292.     if (file==NULL){
  293.         puts("Помилка запису");
  294.     }
  295.     struct Model *element = base;
  296.     while(element!=NULL){
  297.         fwrite(element, sizeof(struct Model), 1, file);
  298.         element = element->prev;
  299.     }
  300.     puts("Записано");
  301. }
  302. void import_from_file(){
  303.     FILE *file;
  304.     getchar();
  305.     char path[100];
  306.     printf("Введіть шлях до файлу: ");
  307.     fgets(path, 100, stdin);
  308.     remove_new_line(path);
  309.     file = fopen(path, "rb");
  310.     if (file==NULL){
  311.         puts("Помилка зчитування");
  312.     }
  313.     struct Model *element = malloc(sizeof(struct Model));
  314.     while(fread(element ,sizeof(struct Model), 1 ,file) == 1){
  315.         element->prev = base;
  316.         base = element;
  317.     }
  318.     puts("Зчитанно");
  319. }
  320.  
  321. void remove_new_line(char *a){
  322.     if (a[strlen(a)-1] == '\n') a[strlen(a)-1] = '\0';
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement