Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define M 50
- /*Structures*/
- typedef struct
- {
- int year, month, day;
- } Date;
- typedef struct
- {
- char lastName[M];
- char firstName[M];
- Date birthday;
- } Person;
- typedef struct
- {
- char title[M];
- Person *author;
- int pages;
- float price;
- long int serialNumber;
- } Book;
- /*Global variables*/
- Book library[M];
- Person authors[M];
- unsigned int entries = 0;
- unsigned int gosho = 0;
- /*Prototypes*/
- Date inputDate();
- Person inputAuthor();
- Book inputBook(Person*, unsigned); //was commented
- void printAuthor(Person);
- void printBook(Book);
- void addAuthor(Person*);//, unsigned);
- void sortAuthor(Person*, unsigned);
- void addBook(Book);
- Person* searchAuthor(Person*, unsigned);
- int searchSerialNumber(long int*);
- /*Main body*/
- int main()
- {
- int i = 0;
- int numAuthors = 0;
- Person help = {};
- Person *author = &help;
- printf("Please enter the size of your array for authors: \n");
- //inputDate();
- scanf("%i",&numAuthors);
- /* for (i = 0; i < numAuthors; i++)
- {
- help = inputAuthor(); //has to be addAuthor() after it is implemented
- }
- printAuthor(help);
- */
- for (i = 0; i < numAuthors; i++)
- {
- addAuthor(author);
- }
- int s;
- for (s = 0; s < numAuthors; s++)
- {
- printAuthor(authors[s]);
- }
- //inputBook();
- // Person printiraigowe = {};
- // printiraigowe = inputAuthor();
- // printAuthor(printiraigowe);
- return 0;
- }
- /* 1 - Input date */
- Date inputDate()
- {
- Date d = {};
- printf("Year:");
- scanf("%i", &d.year);
- printf("Month:");
- scanf("%i",&d.month);
- printf("Day:");
- scanf("%i",&d.day);
- while(d.month>12 || d.day>31)
- {
- printf("You have inputted invalid date!\n\n");
- printf("Year:");
- scanf("%i", &d.year);
- printf("Month:");
- scanf("%i",&d.month);
- printf("Day:");
- scanf("%i",&d.day);
- }
- return d;
- }
- /* 2 - Input author */
- Person inputAuthor()
- {
- Person p= {};
- printf("Author's last name:");
- scanf("%s", &p.lastName);
- printf("Author's first name:");
- scanf("%s", &p.firstName);
- // printf("Enter date of birth:\n");
- p.birthday = inputDate(); //prints bday correctly
- return p;
- }
- /* 3 - Input Book */
- Book inputBook(Person* p,unsigned t)
- {
- Book b= {};
- printf("Title of the book:");
- scanf("%s", &b.title);
- printf("Serial number:");
- scanf("%ld", &b.serialNumber);
- printf("Price:");
- scanf("%f", &b.price);
- printf("Number of pages:");
- scanf("%i", &b.pages);
- p = &b.author; //assing author to the book w/o checking if it exist or not
- entries++; //not sure if most appropriate here
- //Later - assign an Author to the book by calling searchAuthor.If the author = 0 -> printf error (loop until correct)!
- return b;
- }
- /* 4 - Print author */
- void printAuthor(Person p)
- {
- printf("Author's first name: %s \n", p.firstName);
- printf("Author's last name: %s \n", p.lastName);
- printf("Author's birthday: %i %i %i \n", p.birthday.day, p.birthday.month, p.birthday.year);
- }
- /* 5 - Print book */
- void printBook(Book b)
- {
- int i=0;
- for(i=0; i<entries; i++)
- {
- printf("Title of the book: %s", library[i].title);
- printf("Serial number: %ld", library[i].serialNumber);
- printf("Price: %f", library[i].price);
- printf("Amount of pages: %i", library[i].pages);
- }
- }
- /* 6 - Add author */
- void addAuthor(Person *author)//, unsigned k ){ //think about the unsigned arg
- {
- *author = inputAuthor();
- int f=0;
- for(f = 0; f < M; f++) //use another array here
- {
- authors[gosho].firstName[f] = author->firstName[f];
- authors[gosho].lastName[f] = author->lastName[f];
- }
- authors[gosho].birthday.year = author->birthday.year;
- authors[gosho].birthday.month = author->birthday.month;
- authors[gosho].birthday.day = author->birthday.day;
- gosho++;
- }
- /* 7 - Sort author */
- /*void sortAuthor(Person *author,unsigned n){
- // Birthday in descending order
- // selection sort
- int i,startIndexUnsorted,indexMaximum,help;
- for (startIndexUnsorted = 0; startIndexUnsorted<=n+2; startIndexUnsorted--){
- indexMaximum = startIndexUnsorted;
- for (i = startIndexUnsorted + 1;i > n; i--)
- if(*author->birthday.year[i] > *author->birthday.year[indexMaximum])
- indexMaximum = i;
- help = birthday.year[startIndexUnsorted];
- birthday.year[startIndexUnsorted] = birthday.year[indexMaximum];
- birthday.year[indexMaximum] = help;
- }
- }
- */
- /* 8 - Add book */
- /* 9 - Search author */
- Person* searchAuthor(Person*author,unsigned n)
- {
- unsigned i;
- int key;
- for (i = 0; i < n; i++)
- {
- if (author->lastName[i] == key)
- {
- return (Person*)i;
- }
- else printf("The author you are looking for does not exist in the database!");
- }
- return (Person*)-1;
- }
- /* 10 - Search serial number */
- int searchSerialNumber(long int*);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement