PieInlaw

laba41 111

Apr 18th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1.  
  2. ///////////////////////////////////////////////////////
  3. // _________________________________________________ //
  4. //                                                   //
  5. //               Лабораторная работа №4.1            //
  6. // _________________________________________________ //
  7. //                                                   //
  8. ///////////////////////////////////////////////////////
  9.  
  10. #include<stdlib.h>
  11. #include <string.h>
  12. #include<iostream>
  13. #include<cstring>
  14. #include <stdio.h>
  15. #define lmax 200
  16. //пропуск символов до конца строки
  17. #define CLR while (getchar()!='\n')
  18.  
  19.  
  20. ///// Структура /////
  21. struct publishers
  22. {
  23.     char name[50], city[30];
  24.     int year;
  25. };
  26.  
  27. struct book
  28. {
  29.     char author[20], title[60], nazv[100];
  30.     int price, pages;
  31.     publishers cat2;
  32.    
  33. };
  34.  
  35.  
  36. ///// Ввод структуры /////
  37. void read_cat(int *kol, book cat[])
  38. { char ch, year[81], price[81], pages[81];;
  39.     *kol=0;
  40.     do
  41.     { printf("Книга # %d\n", ++(*kol));
  42.         do
  43.         {
  44.             printf("Автор..................."); gets(cat->author);
  45.         } while ( strcmp(cat->author, "")==0);
  46.        
  47.         do
  48.         {
  49.             printf("Название книги ........."); gets(cat->nazv);
  50.         } while ( strcmp(cat->nazv, "")==0);
  51.        
  52.         do
  53.         {
  54.             printf("Название издательства..."); gets(cat->cat2.name);
  55.         } while ( strcmp(cat->cat2.name, "")==0);
  56.        
  57.         do
  58.         {
  59.             printf("Город..................."); gets(cat->cat2.city);
  60.         } while ( strcmp(cat->cat2.city, "")==0);
  61.        
  62.         do
  63.         {
  64.             printf("Год издания............."); scanf("%s", &year);  cat->cat2.year=atoi(year);
  65.         } while(atoi(year)==NULL || atoi(year)<1800 || atoi(year)>2017);
  66.        
  67.         do
  68.         {
  69.             printf("Цена...................."); scanf("%s", &price);  cat->price=atoi(price);
  70.         } while(atoi(price)==NULL || atoi(price)<0);
  71.        
  72.         do
  73.         {
  74.             printf("Количество страниц......"); scanf("%s", &pages);  cat->pages=atoi(pages);
  75.         } while(atoi(pages)==NULL || atoi(pages)<0);
  76.        
  77.         CLR;
  78.         printf("Продолжить? (y/n) ");
  79.         ch=getchar();
  80.         CLR;
  81.         cat++;
  82.     }
  83.     while ((ch=='Y' || ch=='y')&&*kol<lmax);
  84.                 printf("\n");
  85. }
  86.  
  87.  
  88. ///// Функция поиска нужных книг /////
  89. void find_f(book cat[], book pod[],int kol, int *cheak)
  90. {
  91.     int i, S, X;
  92.     book *min=cat;
  93.     S=0;
  94.     *cheak = 0;
  95.     for (i=1; i<=kol; i++, min++)
  96.     {
  97.         S = S + min->pages;
  98.     }
  99.     X=S/kol;
  100.     printf("Среднее арифметическое: %d\n",X);
  101.     min = cat;
  102.     for (i=0; i<kol; i++)
  103.     {
  104.         if ( X > min->pages)
  105.         {
  106.             strcpy(min->nazv, cat[i].nazv);
  107.             *cheak++;
  108.         }
  109.         else strcpy(min->nazv, pod[i].nazv);
  110.         min++;
  111.     }
  112.    
  113. }
  114.  
  115.  
  116. ///// Вывод нужных книг /////
  117. void write_find_f(book cat[], int kol)
  118. {
  119.     int i;
  120.     char s[1]="";
  121.     printf("Подходящие книги:\n");
  122.     for (i=0; i<kol; i++)
  123.     {
  124.         if ( strcmp(cat[i].nazv, s) == 0)
  125.         {
  126.             i++;
  127.         }
  128.             else
  129.             {
  130.                 printf("Книга: %s\n", cat[i].nazv);
  131.             }
  132.     }
  133. }
  134.  
  135.  
  136. ///// Основная часть /////
  137. int main()
  138. {
  139.     int kol, count; book cat[lmax], pod[lmax],*min,*puck, cheak;
  140.         printf("\n");
  141.         printf("_____Лаборатораня работа №4_____\n");
  142.         printf("\n");
  143.     read_cat(&kol, cat);
  144.     find_f(cat, pod, kol, &cheak);
  145.         printf("\n");
  146.     if (kol==1) printf("Всего одна книга!\n");
  147.         else
  148.             write_find_f(cat, kol);
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment