11// fishc
2+ #define _CRT_SECURE_NO_WARNINGS
23#include <stdio.h>
34#include <stdlib.h>
45#define MAX_SIZE 100
56
6- struct Date {
7- short year ;
8- short month ;
9- short day ;
7+ struct Date
8+ {
9+ short year ;
10+ short month ;
11+ short day ;
1012};
11- struct Book {
12- char title [128 ];
13- char author [40 ];
14- float price ;
15- struct Date date ;
16- char publisher [40 ];
13+ struct Book
14+ {
15+ char title [128 ];
16+ char author [40 ];
17+ float price ;
18+ struct Date date ;
19+ char publisher [40 ];
1720};
1821void getInput (struct Book * book );
1922void printBook (struct Book * book );
20- void InitLibrary (struct Book * Library []);
21- void printLibrary (struct Book * Library []);
22- void releaseLibrary (struct Book * Library []);
23- void getInput (struct Book * book ) {
24- printf ( "Book Title:\n" );
25- scanf ( "%s" , book -> title );
26- printf ( "Book Author:\n" );
27- scanf ( "%s" , book -> author );
28- printf ( "Book Price:\n" );
29- scanf ( "%f" , & ( book -> price ) );
30- printf ( "Book Publish Date(2000-12-1):\n" );
31- scanf ( "%hd-%hd-%hd" , & ( book -> date . year ), & ( book -> date . month ),
32- & ( book -> date .day ) );
33- printf ("Book Publisher:\n" );
34- scanf ("%s" , book -> publisher );
23+ void InitLibrary (struct Book * library []);
24+ void printLibrary (struct Book * library []);
25+ void releaseLibrary (struct Book * library []);
26+ void getInput (struct Book * book )
27+ {
28+ printf ( "Book Title:\n" );
29+ scanf ( "%s" , book -> title );
30+ printf ( "Book Author:\n" );
31+ scanf ( "%s" , book -> author );
32+ printf ( "Book Price:\n" );
33+ scanf ( "%f" , & book -> price );
34+ printf ( "Book Publish Date(2000-12-1):\n" );
35+ scanf ( "%hd-%hd-%hd" , & book -> date . year , & book -> date . month , & book -> date .day );
36+ printf ("Book Publisher:\n" );
37+ scanf ("%s" , book -> publisher );
3538}
36- void printBook (struct Book * book ) {
37- printf ("Book Title:\n%s\n" , book -> title );
38- printf ("Book Author:\n%s\n" , book -> author );
39- printf ("Book Price:\n%f\n" , book -> price );
40- printf ("Book Publish Date(2000-12-1):\n%hd-%hd-%hd\n" , book -> date .year ,
41- book -> date .month , book -> date .day );
42- printf ("Book Publisher:\n%s\n" , book -> publisher );
39+ void printBook (struct Book * book )
40+ {
41+ printf ("Book Title:\n%s\n" , book -> title );
42+ printf ("Book Author:\n%s\n" , book -> author );
43+ printf ("Book Price:\n%f\n" , book -> price );
44+ printf ("Book Publish Date:\n%hd-%hd-%hd\n" , book -> date .year ,
45+ book -> date .month , book -> date .day );
46+ printf ("Book Publisher:\n%s\n" , book -> publisher );
4347}
44- void InitLibrary (struct Book * Library []) {
45- for (int i = 0 ; i < MAX_SIZE ; i ++ ) {
46- Library [i ] = NULL ;
47- }
48+ void InitLibrary (struct Book * library [])
49+ {
50+ for (int i = 0 ; i < MAX_SIZE ; i ++ )
51+ {
52+ library [i ] = NULL ;
53+ }
4854}
49- void printLibrary (struct Book * Library []) {
50- for (int i = 0 ; i < MAX_SIZE ; i ++ ) {
51- if (Library [i ] != NULL ) {
52- printBook (Library [i ]);
53- putchar ('\n' );
55+ void printLibrary (struct Book * library [])
56+ {
57+ for (int i = 0 ; i < MAX_SIZE ; i ++ )
58+ {
59+ if (library [i ] != NULL )
60+ {
61+ printBook (library [i ]);
62+ putchar ('\n' );
63+ }
5464 }
55- }
5665}
57- void releaseLibrary (struct Book * Library []) {
58- for (int i = 0 ; i < MAX_SIZE ; i ++ ) {
59- if (Library [i ] != NULL ) {
60- free (Library [i ]);
66+ void releaseLibrary (struct Book * library [])
67+ {
68+ for (int i = 0 ; i < MAX_SIZE ; i ++ )
69+ {
70+ if (library [i ] != NULL )
71+ {
72+ free (library [i ]);
73+ }
6174 }
62- }
6375}
64- int main () {
65- struct Book * library [MAX_SIZE ];
66- struct Book * ptr = NULL ;
67- char ch ;
68- int index = 0 ;
69- InitLibrary (library );
70- while (1 ) {
71- printf ("Do you need to input information(Y/N):\n" );
72- do {
73- ch = getchar ();
74- // printf("You input %c\n",ch);
75- } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n' );
76+ int main ()
77+ {
78+ struct Book * library [MAX_SIZE ];
79+ struct Book * ptr = NULL ;
80+ char ch ;
81+ int index = 0 ;
82+ InitLibrary (library );
83+ while (1 )
84+ {
85+ printf ("Do you need to input information(Y/N):\n" );
86+ do
87+ {
88+ ch = getchar ();
89+ // printf("You input %c\n",ch);
90+ } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n' );
7691
77- if (ch == 'Y' || ch == 'y' ) {
78- if (index < MAX_SIZE ) {
79- ptr = (struct Book * )malloc (sizeof (struct Book ));
80- getInput (ptr );
81- library [index ] = ptr ;
82- index ++ ;
83- putchar ('\n' );
84- } else {
85- printf ("Library is full,exiting...\n" );
86- break ;
87- }
88- } else {
89- break ;
92+ if (ch == 'Y' || ch == 'y' )
93+ {
94+ if (index < MAX_SIZE )
95+ {
96+ ptr = (struct Book * )malloc (sizeof (struct Book ));
97+ getInput (ptr );
98+ library [index ] = ptr ;
99+ index ++ ;
100+ putchar ('\n' );
101+ }
102+ else
103+ {
104+ printf ("Library is full,exiting...\n" );
105+ break ;
106+ }
107+ }
108+ else
109+ {
110+ break ;
111+ }
90112 }
91- }
92- printf ("Printing Library......\n" );
93- printLibrary (library );
94- releaseLibrary (library );
113+ printf ("Printing Library......\n" );
114+ printLibrary (library );
115+ releaseLibrary (library );
95116}
0 commit comments