Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define M 50
  4.  
  5. /*Structures*/
  6. typedef struct
  7. {
  8.  
  9.     int year, month, day;
  10.  
  11. } Date;
  12.  
  13. typedef struct
  14. {
  15.  
  16.     char lastName[M];
  17.     char firstName[M];
  18.     Date birthday;
  19.  
  20. } Person;
  21.  
  22.  
  23.  
  24. typedef struct
  25. {
  26.  
  27.     char title[M];
  28.     Person *author;
  29.     int pages;
  30.     float price;
  31.     long int serialNumber;
  32.  
  33. } Book;
  34.  
  35. /*Global variables*/
  36. Book library[M];
  37. Person authors[M];
  38. unsigned int entries = 0;
  39. unsigned int gosho = 0;
  40.  
  41.  
  42. /*Prototypes*/
  43. Date inputDate();
  44. Person inputAuthor();
  45. Book inputBook(Person*, unsigned); //was commented
  46. void printAuthor(Person);
  47. void printBook(Book);
  48. void addAuthor(Person*);//, unsigned);
  49. void sortAuthor(Person*, unsigned);
  50. void addBook(Book);
  51. Person* searchAuthor(Person*, unsigned);
  52. int searchSerialNumber(long int*);
  53.  
  54.  
  55. /*Main body*/
  56. int main()
  57. {
  58.     int i = 0;
  59.     int numAuthors = 0;
  60.     Person help = {};
  61.     Person *author = &help;
  62.     printf("Please enter the size of your array for authors: \n");
  63.     //inputDate();
  64.     scanf("%i",&numAuthors);
  65.     /* for (i = 0; i < numAuthors; i++)
  66.      {
  67.          help = inputAuthor(); //has to be addAuthor() after it is implemented
  68.      }
  69.      printAuthor(help);
  70.      */
  71.     for (i = 0; i < numAuthors; i++)
  72.     {
  73.         addAuthor(author);
  74.     }
  75. int s;
  76.     for (s = 0; s < numAuthors; s++)
  77.     {
  78.         printAuthor(authors[s]);
  79.     }
  80.  
  81.     //inputBook();
  82.     // Person printiraigowe = {};
  83.     //  printiraigowe = inputAuthor();
  84.     // printAuthor(printiraigowe);
  85.  
  86.     return 0;
  87. }
  88.  
  89. /* 1 - Input date */
  90. Date inputDate()
  91. {
  92.     Date d = {};
  93.  
  94.     printf("Year:");
  95.     scanf("%i", &d.year);
  96.  
  97.  
  98.     printf("Month:");
  99.     scanf("%i",&d.month);
  100.  
  101.  
  102.     printf("Day:");
  103.     scanf("%i",&d.day);
  104.  
  105.  
  106.     while(d.month>12 || d.day>31)
  107.     {
  108.         printf("You have inputted invalid date!\n\n");
  109.  
  110.         printf("Year:");
  111.         scanf("%i", &d.year);
  112.  
  113.         printf("Month:");
  114.         scanf("%i",&d.month);
  115.  
  116.         printf("Day:");
  117.         scanf("%i",&d.day);
  118.     }
  119.  
  120.     return d;
  121. }
  122.  
  123. /* 2 - Input author */
  124. Person inputAuthor()
  125. {
  126.  
  127.     Person p= {};
  128.  
  129.     printf("Author's last name:");
  130.     scanf("%s", &p.lastName);
  131.     printf("Author's first name:");
  132.     scanf("%s", &p.firstName);
  133.     // printf("Enter date of birth:\n");
  134.     p.birthday = inputDate(); //prints bday correctly
  135.  
  136.  
  137.     return p;
  138. }
  139.  
  140. /* 3 - Input Book */
  141. Book inputBook(Person* p,unsigned t)
  142. {
  143.  
  144.     Book b= {};
  145.  
  146.     printf("Title of the book:");
  147.     scanf("%s", &b.title);
  148.     printf("Serial number:");
  149.     scanf("%ld", &b.serialNumber);
  150.     printf("Price:");
  151.     scanf("%f", &b.price);
  152.     printf("Number of pages:");
  153.     scanf("%i", &b.pages);
  154.     p = &b.author; //assing author to the book w/o checking if it exist or not
  155.     entries++; //not sure if most appropriate here
  156.     //Later - assign an Author to the  book by calling searchAuthor.If the author = 0 -> printf error (loop until correct)!
  157.  
  158.     return b;
  159. }
  160.  
  161. /* 4 - Print author */
  162. void printAuthor(Person p)
  163. {
  164.     printf("Author's first name: %s \n", p.firstName);
  165.     printf("Author's last name: %s \n", p.lastName);
  166.     printf("Author's birthday: %i %i %i \n", p.birthday.day, p.birthday.month, p.birthday.year);
  167. }
  168.  
  169. /* 5 - Print book */
  170. void printBook(Book b)
  171. {
  172.  
  173.     int i=0;
  174.  
  175.     for(i=0; i<entries; i++)
  176.     {
  177.         printf("Title of the book: %s", library[i].title);
  178.         printf("Serial number: %ld", library[i].serialNumber);
  179.         printf("Price: %f", library[i].price);
  180.         printf("Amount of pages: %i", library[i].pages);
  181.     }
  182.  
  183.  
  184.  
  185. }
  186.  
  187. /* 6 - Add author */
  188.  
  189. void addAuthor(Person *author)//, unsigned k ){ //think about the unsigned arg
  190. {
  191.     *author = inputAuthor();
  192.     int f=0;
  193.     for(f = 0; f < M; f++) //use another array here
  194.     {
  195.         authors[gosho].firstName[f] = author->firstName[f];
  196.         authors[gosho].lastName[f] = author->lastName[f];
  197.     }
  198.     authors[gosho].birthday.year = author->birthday.year;
  199.     authors[gosho].birthday.month = author->birthday.month;
  200.     authors[gosho].birthday.day = author->birthday.day;
  201.     gosho++;
  202. }
  203.  
  204.  
  205. /* 7 - Sort author */
  206.  
  207. /*void sortAuthor(Person *author,unsigned n){
  208.     // Birthday in descending order
  209.     // selection sort
  210.  
  211.  
  212.     int i,startIndexUnsorted,indexMaximum,help;
  213.  
  214.     for (startIndexUnsorted = 0; startIndexUnsorted<=n+2; startIndexUnsorted--){
  215.  
  216.         indexMaximum = startIndexUnsorted;
  217.             for (i = startIndexUnsorted + 1;i > n; i--)
  218.                 if(*author->birthday.year[i] > *author->birthday.year[indexMaximum])
  219.                     indexMaximum = i;
  220.             help = birthday.year[startIndexUnsorted];
  221.                         birthday.year[startIndexUnsorted] = birthday.year[indexMaximum];
  222.                         birthday.year[indexMaximum] = help;
  223.     }
  224.  
  225.  
  226.  
  227. }
  228. */
  229. /* 8 - Add book */
  230.  
  231.  
  232.  
  233. /* 9 - Search author */
  234.  
  235. Person* searchAuthor(Person*author,unsigned n)
  236. {
  237.  
  238.     unsigned i;
  239.     int key;
  240.  
  241.     for (i = 0; i < n; i++)
  242.     {
  243.         if (author->lastName[i] == key)
  244.         {
  245.             return (Person*)i;
  246.         }
  247.  
  248.         else printf("The author you are looking for does not exist in the database!");
  249.     }
  250.  
  251.  
  252.  
  253.  
  254.     return (Person*)-1;
  255. }
  256.  
  257. /* 10 - Search serial number */
  258.  
  259. int searchSerialNumber(long int*);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement