Advertisement
apl-mhd

structure

Nov 15th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int infoSize;
  5.  
  6. struct library{
  7.  
  8.     char title[50];
  9.     char author[50];
  10.     int price;
  11.     int date;
  12. };
  13.  
  14.  struct library array[10];
  15.  
  16. void menu(){
  17. int menuNumber;
  18.    printf( "1.Add book information\n");
  19.    printf("2.Display book information. \n\n");
  20.    printf("3.Display all books of given author. \n\n");
  21.    printf("4.Display the highest price book information \n\n");
  22.  
  23.    scanf("%d",&menuNumber);
  24.  
  25.    if(menuNumber == 1){
  26.  
  27.          bookInfo(array);
  28.    }
  29.    else if(menuNumber == 2){
  30.  
  31.      displayBookInfo(array);
  32.  
  33.    }
  34.  
  35.     else if(menuNumber == 3){
  36.  
  37.     author(array);
  38.  
  39.    }
  40.  
  41. }
  42.  
  43. void bookInfo(struct library array[100]){
  44.  
  45.     int i, j;
  46.  
  47.     printf("Information size:\n");
  48.     scanf("%d",&infoSize);
  49.  
  50.     for(i=0;i<infoSize;i++){
  51.  
  52.         printf("Title:");
  53.         scanf("%s",&array[i].title);
  54.  
  55.         printf("author:");
  56.         scanf("%s",&array[i].author);
  57.  
  58.           printf("Price:");
  59.         scanf("%d",&array[i].price);
  60.  
  61.           printf("date:");
  62.         scanf("%d",&array[i].date);
  63.  
  64.           printf("------------------------------\n\n");
  65.  
  66.  
  67.  
  68.     }
  69. /*
  70.     printf("Title:%s\n",array[0].title );
  71.      printf("Author:%s\n",array[0].author);
  72.       printf("Price:%d\n",array[0].price);
  73.       printf("date:%d\n",array[0].date);
  74. */
  75.  
  76.   menu();
  77. }
  78.  
  79. void displayBookInfo(struct library array[100]){
  80.  
  81.     int i, j;
  82.  
  83.     printf("displayBookInfo\n");
  84.     for(i=0; i<infoSize; i++){
  85.  
  86.         printf("book %d:\n",i+1);
  87.         printf("Title:%s\n",array[i].title );
  88.      printf("Author:%s\n",array[i].author);
  89.       printf("Price:%d\n",array[i].price);
  90.       printf("date:%d\n",array[i].date);
  91.  
  92.     printf("------------------------------\n");
  93.  
  94.     }
  95.  
  96.  
  97.       menu();
  98. }
  99.  
  100.  
  101. void author(struct library array[100]){ /*autor*/
  102.  
  103.     char nam[50];
  104.  
  105.     int flag=0, add=0,size;
  106.  
  107.  
  108.     printf("author name:\n");
  109.     //gets(nam);
  110.     scanf("%s",nam);
  111.  
  112.         int i,j=0;
  113.  
  114.         for(i=0; i<infoSize; i++){
  115.  
  116.            // size = (strlen(array[i].name)>strlen(nam))? strlen(array[i].name:strlen(nam);
  117.            if(strlen(array[i].author)> strlen(nam)){
  118.  
  119.                 size = strlen(array[i].author);
  120.               }
  121.  
  122.               else{
  123.                 size = strlen(nam);
  124.               }
  125.  
  126.               flag =0;
  127.               for(j=0; j<size; j++){
  128.  
  129.  
  130.                 if(array[i].author[j] != nam[j] ){
  131.                     flag =-1;}
  132.               }
  133.  
  134.               if(flag = 0){
  135.                 add =i;
  136.               }
  137.         }
  138.  
  139.  
  140. printf("Author info:\n");
  141.         printf("Title:%s\n",array[add].title );
  142.      printf("Author:%s\n",array[add].author);
  143.       printf("Price:%d\n",array[add].price);
  144.       printf("date:%d\n",array[add].date);
  145.  
  146.     printf("------------------------------\n");
  147. menu();
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. int main()
  155. {
  156.  
  157.  
  158.    // struct library array[10];
  159.  
  160.     menu();
  161.  
  162.    // bookInfo(array);
  163.  
  164.  
  165.     //displayBookInfo(array);
  166.  
  167.  
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement