Skip to content

Commit 3d1cd7a

Browse files
committed
Add rust demo and Book Library in c
1 parent 28a298b commit 3d1cd7a

File tree

20 files changed

+1804
-1681
lines changed

20 files changed

+1804
-1681
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

c/Library/main.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// fishc
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#define MAX_SIZE 100
5+
6+
struct Date {
7+
short year;
8+
short month;
9+
short day;
10+
};
11+
struct Book {
12+
char title[128];
13+
char author[40];
14+
float price;
15+
struct Date date;
16+
char publisher[40];
17+
};
18+
void getInput(struct Book *book);
19+
void 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);
35+
}
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);
43+
}
44+
void InitLibrary(struct Book *Library[]) {
45+
for (int i = 0; i < MAX_SIZE; i++) {
46+
Library[i] = NULL;
47+
}
48+
}
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');
54+
}
55+
}
56+
}
57+
void releaseLibrary(struct Book *Library[]) {
58+
for (int i = 0; i < MAX_SIZE; i++) {
59+
if (Library[i] != NULL) {
60+
free(Library[i]);
61+
}
62+
}
63+
}
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+
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;
90+
}
91+
}
92+
printf("Printing Library......\n");
93+
printLibrary(library);
94+
releaseLibrary(library);
95+
}

0 commit comments

Comments
 (0)