Advertisement
epidzhx

Untitled

Dec 10th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6.  
  7. const int MAXNAMESIZE = 25; // 25 символов для имени и фирмы будет достаточно, чтобы не мучаться с динамическими массивами
  8. const int MAXFIRMSIZE = 25;
  9. const int MAXARRSIZE = 10000;
  10.  
  11.  
  12. struct accessories {
  13.     char name[MAXNAMESIZE];
  14.     char firm[MAXFIRMSIZE];
  15.     int cost, count;
  16. };
  17.  
  18.  
  19. void Append(struct accessories*, int);
  20.  
  21.  
  22. int menu() {
  23.     int option = 0;
  24.     int k = 0;
  25.     do
  26.     {
  27.         printf("\n|------|\n\n");
  28.         printf("Меню: \n");
  29.         printf("1.Создать массив структур. \n");
  30.         printf("2.Просмотреть. \n");
  31.         printf("3.Упорядочить. \n");
  32.         printf("4. Добавить. \n");
  33.         printf("5. Найти. \n");
  34.         printf("6. Удалить. \n");
  35.         printf("7. Удалить всё. \n");
  36.         printf("8.Выход \n");
  37.         scanf("%d",&option);
  38.         printf("\n");
  39.         if ((option>0) && (option < 9))
  40.             k=1;
  41.     }
  42.     while(!k);
  43.     return option;
  44. }
  45.  
  46.  
  47. void View(struct accessories *array, int FLAG) {
  48.    
  49.     if (FLAG == 0)
  50.         printf("Массив структур пуст!\n");
  51.     else {
  52.         printf ("----------------------------------------------------------------------\n");
  53.         printf ("Name                      Firm                      Cost       Count\n");
  54.         for (int i = 0; i < FLAG; i++) {
  55.             printf ("%-25s %-25s %-9d %-9d\n", array[i].name, array[i].firm, array[i].cost, array[i].count);
  56.         }
  57.         printf ("----------------------------------------------------------------------\n");
  58.     }
  59.    
  60. }
  61.  
  62.  
  63. void Sort(struct accessories *array, int FLAG) {
  64.     for (int i = 0; i < FLAG - 1; i++) {
  65.         for (int j = 0; j < FLAG - i - 1; j++) {
  66.             if (strcmp(array[j].name, array[j+1].name) > 0) {
  67.                 // тут меняем местами структуру
  68.                 int t = array[j].cost;
  69.                 array[j].cost = array[j+1].cost;
  70.                 array[j+1].cost = t;
  71.                
  72.                 t = array[j].count;
  73.                 array[j].count = array[j+1].count;
  74.                 array[j+1].count = t;
  75.                
  76.                 char tmp1[MAXNAMESIZE];
  77.                 strcpy(tmp1, array[j].name);
  78.                 strcpy(array[j].name, array[j+1].name);
  79.                 strcpy(array[j+1].name, tmp1);
  80.                
  81.                 char tmp2[MAXFIRMSIZE];
  82.                 strcpy(tmp2, array[j].firm);
  83.                 strcpy(array[j].firm, array[j+1].firm);
  84.                 strcpy(array[j+1].firm, tmp2);
  85.             }
  86.         }
  87.     }
  88. }
  89.  
  90.  
  91. void Append(struct accessories *array, int FLAG) {
  92.     printf("Введите название: ");
  93.     char *name = (char*)malloc(25);
  94.     scanf("%25s", name);
  95.    
  96.     printf("Введите фирму: ");
  97.     char *firm = (char*)malloc(25);
  98.     scanf("%25s", firm);
  99.    
  100.     printf("Введите стоимость: ");
  101.     int cost, count;
  102.     scanf("%d", &cost);
  103.     printf("Введите кол-во: ");
  104.     scanf("%d", &count);
  105.    
  106.     FLAG++;
  107.     for (int i = FLAG; i > 0; i--) // сдвигаем все элементы на 1
  108.         array[i] = array[i-1];
  109.    
  110.     strcpy(array[0].name, name); // заполняем первый(новый) элемент
  111.     strcpy(array[0].firm, firm);
  112.     array[0].cost = cost;
  113.     array[0].count = count;
  114.    
  115. }
  116.  
  117.  
  118. void Find(struct accessories *array, int FLAG) {
  119.     if (FLAG == 0) {
  120.         printf("Массив пуст!\n");
  121.         return;
  122.     }
  123.     printf("Как искать?\n");
  124.     printf("1. По названию\n");
  125.     printf("2. По диапазону стоимости\n");
  126.     int k = 0;
  127.     scanf("%d", &k);
  128.     if (k == 1) {
  129.         char *name = (char*)malloc(25);
  130.         printf("Введите название: ");
  131.         scanf("%s", name);
  132.         int count = 0;
  133.         printf ("----------------------------------------------------------------------\n");
  134.         printf ("Name                      Firm                      Cost       Count\n");
  135.         for (int i = 0; i < FLAG; i++){
  136.             if (strcmp(array[i].name, name) == 0) {
  137.                 count += 1;
  138.                 printf ("%-25s %-25s %-9d %-9d\n", array[i].name, array[i].firm, array[i].cost, array[i].count);
  139.             }
  140.         }
  141.         if (count == 0)
  142.             printf("Ничего не найдено! \n");
  143.         printf ("----------------------------------------------------------------------\n");
  144.     }
  145.     else if (k == 2) {
  146.         int l = 0, r = 0;
  147.        
  148.         printf("Введите начало диапазона: ");
  149.         scanf("%d", &l);
  150.        
  151.         printf("Введите конец диапазона: ");
  152.         scanf("%d", &r);
  153.        
  154.         int count = 0;
  155.         printf ("----------------------------------------------------------------------\n");
  156.         printf ("Name                      Firm                      Cost       Count\n");
  157.         for (int i = 0; i < FLAG; i++){
  158.             if ((array[i].cost >= l) && (array[i].cost <= r)) {
  159.                 count += 1;
  160.                 printf ("%-25s %-25s %-9d %-9d\n", array[i].name, array[i].firm, array[i].cost, array[i].count);
  161.             }
  162.         }
  163.         if (count == 0)
  164.             printf("Ничего не найдено! \n");
  165.         printf ("----------------------------------------------------------------------\n");
  166.     }
  167.     else {
  168.         printf ("Отмена поиска!\n");
  169.         return;
  170.     }
  171. }
  172.  
  173.  
  174. int Delete(struct accessories *array, int FLAG) {
  175.     int deletecount = 0;
  176.     if (FLAG == 0) {
  177.         printf("Массив пуст!\n");
  178.         return 0;
  179.     }
  180.     printf("Введите максимальную стоимость: ");
  181.     int maxcost = 0;
  182.     scanf("%d", &maxcost);
  183.     int i = 0;
  184.     while (i < FLAG) {
  185.         if (array[i].cost > maxcost) {
  186.             int j = i;
  187.             while (j < FLAG) {
  188.                 strcpy(array[j].name, array[j+1].name);
  189.                 strcpy(array[j].firm, array[j+1].firm);
  190.                 array[j].cost = array[j+1].cost;
  191.                 array[j].count = array[j+1].count;
  192.                 j++;
  193.             }
  194.             deletecount++;
  195.             i--;
  196.         }
  197.         i++;
  198.     }
  199.     if (deletecount == 0)
  200.         printf("Ничего не удалено!\n");
  201.     else
  202.         printf("Удалено элементов: %d", deletecount);
  203.     return deletecount;
  204. }
  205.  
  206.  
  207. void DeleteAll(struct accessories *array, int FLAG) {
  208.     FLAG = 0;
  209.     printf("Все записи удалены! \n");
  210. }
  211.  
  212.  
  213. int main() {
  214.     setlocale(LC_ALL,"rus");
  215.     struct accessories array[MAXARRSIZE];
  216.     int k = 0;
  217.     int FLAG = 0; // кол-во структур в массиве
  218.     while(!k)
  219.     {
  220.         switch (menu())
  221.         {
  222.             case 1:
  223.                 if (FLAG == 0) {
  224.                     printf("Введите размер массива: ");
  225.                     int n = 0;
  226.                     scanf("%d", &n);
  227.                     FLAG = 0;
  228.                     for (int i = 0; i < n;i++) {
  229.                         Append(array, n);
  230.                     }
  231.                     FLAG = n;
  232.                    
  233.                     printf("Массив создан! \n");
  234.                 }
  235.                 else
  236.                     printf("Массив уже создан!!! \n");
  237.                 break;
  238.             case 2:
  239.                 printf("Выводим массив:\n\n");
  240.                
  241.                 View(array, FLAG);
  242.                 break;
  243.             case 3:
  244.                 Sort(array, FLAG);
  245.                
  246.                 printf("Массив отсортирован!\n");
  247.                 break;
  248.             case 4:
  249.                
  250.                 Append(array, FLAG);
  251.                 FLAG++;
  252.                
  253.                 printf("Новый элемент добавлен!\n");
  254.                 break;
  255.             case 5:
  256.                 Find(array, FLAG);
  257.                 break;
  258.             case 6:
  259.                
  260.                 FLAG -= Delete(array, FLAG);
  261.                 break;
  262.             case 7:
  263.                 DeleteAll(array, FLAG);
  264.                 FLAG = 0;
  265.                 break;
  266.             case 8:
  267.                 printf("Bye!\n");
  268.                 k = 1;
  269.                 break;
  270.         }
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement