Advertisement
Yu-Wei

圖書館檢索系統_計概期末專題

Jan 27th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5. struct library{
  6. int number;
  7. char name[60];
  8. int lent_amount;
  9. int remain_amount;
  10. };
  11. struct borrower{
  12. int id;
  13. char name[20];
  14. int borrow;
  15. char bookname[50][50];
  16. };
  17. void register_books(void);
  18. void lend_out(void);
  19. void return_books(void);
  20. void list(void);
  21. void add_borrower(void);
  22. void inquire_borrower(void);
  23. void search(void);
  24.  
  25. int function,i,j,k,l;
  26. char * divide;
  27. char pre_books_divide[300][20][20],pre_books[300][100];
  28. struct library books[300];
  29.  
  30. int m,n,o,p;
  31. char pre_name_divide[100][20][20],pre_name[100][20];
  32. char * dive;
  33. struct borrower borrow[100];
  34.  
  35. int main(void)
  36. {
  37.  
  38. //================================================================================
  39. FILE* ofptr;
  40. ofptr=fopen("books.dat","r");
  41. i=0;
  42. char use[100];
  43. while(!feof(ofptr)){
  44. fgets(use,100,ofptr);
  45. if(*use!='\n'){
  46. strcpy(pre_books[i],use);
  47. i++;
  48. }
  49. }
  50. if(isdigit(pre_books[i][0])!=1){
  51. i--;
  52. }
  53.  
  54. for(j=0;j<i;j++){
  55. divide=strtok(pre_books[j]," ");
  56. strcpy(pre_books_divide[j][0],divide);
  57. books[j].number=atoi(pre_books_divide[j][0]);
  58. //printf("%d\t",books[j].number);
  59. k=1;
  60. while(1){
  61. divide=strtok(NULL," ");
  62. if(divide==NULL){
  63. break;
  64. }
  65. strcpy(pre_books_divide[j][k],divide);
  66. k++;
  67. }
  68. for(l=1;l<k-2;l++){
  69. strcat(books[j].name,pre_books_divide[j][l]);
  70. if(l+1!=k-2){
  71. strcat(books[j].name," ");
  72. }
  73. }
  74. //printf("%s\t",books[j].name);
  75.  
  76. books[j].lent_amount=atoi(pre_books_divide[j][k-2]);
  77. books[j].remain_amount=atoi(pre_books_divide[j][k-1]);
  78. //printf("%d\t%d\n",books[j].lent_amount,books[j].remain_amount);
  79.  
  80. }
  81. fclose(ofptr);
  82. //==================================================================================
  83. FILE* pfptr;
  84. pfptr=fopen("borrower.dat","r");
  85. m=0;
  86. char pre[100],hello[100];
  87.  
  88. while(!feof(pfptr)){
  89. fgets(pre,100,ofptr);
  90. if(*pre!='\n'){
  91. strcpy(pre_name[m],pre);
  92. }
  93. dive=strtok(pre_name[m]," ");
  94. strcpy(pre_name_divide[m][0],dive);
  95. borrow[m].id=atoi(pre_name_divide[m][0]);
  96. //printf("%d\t",books[j].number);
  97. o=1;
  98. while(1){
  99. dive=strtok(NULL," ");
  100. if(dive==NULL){
  101. break;
  102. }
  103. strcpy(pre_name_divide[m][o],dive);
  104. o++;
  105. }
  106. for(p=1;p<o-1;p++){
  107. strcat(borrow[m].name,pre_name_divide[m][p]);
  108. if(p+1!=o-1){
  109. strcat(borrow[m].name," ");
  110. }
  111. }
  112. //printf("%s\t",books[j].name);
  113.  
  114. //books[j].lent_amount=atoi(pre_books_divide[j][k-2]);
  115. borrow[m].borrow=atoi(pre_name_divide[m][o-1]);
  116. //printf("%d\t%d\n",books[j].lent_amount,books[j].remain_amount);
  117. for(n=0;n<borrow[m].borrow;n++){
  118. char * ch;
  119. fgets(hello,100,pfptr);
  120. ch=strtok(hello,"\n");
  121. strcpy(borrow[m].bookname[n],ch);
  122. }
  123. fgets(use,100,ofptr);
  124.  
  125. m++;
  126.  
  127.  
  128. }
  129. fclose(pfptr);
  130.  
  131. //=========================================================================
  132.  
  133.  
  134.  
  135.  
  136. while(1){
  137. printf("Welcome to uduck 3000 Library System\n"
  138. "1. Register new books\n"
  139. "2. Lend out books\n"
  140. "3. Return back books\n"
  141. "4. List all the books in the library\n"
  142. "5. Add new borrower\n"
  143. "6. Inquire borrower state\n"
  144. "7. Search\n"
  145. "8. Quit\n");
  146. scanf("%d",&function);
  147. while(getchar() != '\n')
  148. ;
  149. if(function==8){
  150. break;
  151. }
  152. switch(function){
  153. case 1:
  154. register_books();
  155. break;
  156. case 2:
  157. lend_out();
  158. break;
  159. case 3:
  160. return_books();
  161. break;
  162. case 4:
  163. list();
  164. break;
  165. case 5:
  166. add_borrower();
  167. break;
  168. case 6:
  169. inquire_borrower();
  170. break;
  171. case 7:
  172. search();
  173. break;
  174. }
  175. }
  176.  
  177. }
  178.  
  179. void register_books(void){
  180. char n[100],new_book[100];
  181. char * newbook;
  182. char amount_s[10];
  183. int amount,a,b;
  184. int det1=0,det2=0,det3=0;
  185. printf("Please enter the new book's name: ");
  186. fgets(n,100,stdin);
  187. newbook=strtok(n,"\n");
  188. strcpy(new_book,newbook);
  189. while(1){
  190.  
  191. printf("Please enter book amount:");
  192. scanf("%s",&amount_s);
  193. if(isdigit(amount_s[0])==1){
  194. amount=atoi(amount_s);
  195. while(getchar() != '\n')
  196. ;
  197. a=0;
  198. while(a<i){
  199. if(strcmp(new_book,books[a].name)==0){
  200. books[a].remain_amount+=amount;
  201. det1+=1;
  202. det2=a;
  203. }
  204. a++;
  205. }
  206. if(det1!=0){
  207. FILE* rfptr;
  208. rfptr=fopen("books.dat","w");
  209. for(a=0;a<i;a++){
  210. fprintf(rfptr,"%d %s %d %d\n",books[a].number,books[a].name,books[a].lent_amount,books[a].remain_amount);
  211. }
  212. fclose(rfptr);
  213. }else{
  214. FILE* rfptr;
  215. rfptr=fopen("books.dat","a");
  216. books[i].number=i+1;
  217. det2=i+1;
  218. strcpy(books[i].name,new_book);
  219. books[i].lent_amount=0;
  220. books[i].remain_amount=amount;
  221. fprintf(rfptr,"%d %s %d %d\n",books[a].number,books[a].name,books[a].lent_amount,books[a].remain_amount);
  222. det2=i;
  223. i++;
  224. fclose(rfptr);
  225. }
  226. break;
  227. }else{
  228. printf("Book amount should be a digit!!\n");
  229. det3+=1;
  230. }
  231. }
  232. if(det3==0){
  233. printf("Register complete!\n");
  234. }
  235. printf("There are %d \"%s\" in the library now!!\n\n",books[det2].remain_amount,new_book);
  236. printf("Press \"Enter\" for return to main menu.");
  237. while(getchar() != '\n')
  238. ;
  239. }
  240. void lend_out(void){
  241. int id,book_id,a,b,c;
  242. char bookk[10];
  243. printf("Borrower id: ");
  244. while(1){
  245.  
  246. scanf("%d",&id);
  247. if(id>m){
  248. printf("Sorry, no borrower belongs to this id.\n");
  249. }else{
  250. break;
  251. }
  252. printf("Please enter borrower id again: ");
  253.  
  254. }
  255. printf("Please enter the book id you want to borrow (0 for back): ");
  256. while(1){
  257.  
  258. scanf("%s",&bookk);
  259. getchar();
  260. book_id=atoi(bookk);
  261. if(isdigit(bookk[0])==1&&book_id>0&&book_id<i){
  262. break;
  263. }else{
  264. printf("There is no book with this id!!\n\n");
  265. }
  266. printf("Please enter again (0 for back): ");
  267. }
  268. if(books[book_id-1].remain_amount==0){
  269. printf("Sorry, the book you want to borrow is insufficient.\n\n");
  270. }else{
  271. printf("%s borrows \"%s\" successfully!!\n\n",borrow[id-1].name,books[book_id-1].name);
  272. books[book_id-1].remain_amount-=1;
  273. books[book_id-1].lent_amount+=1;
  274. strcpy(borrow[id-1].bookname[borrow[id-1].borrow],books[book_id-1].name);
  275. borrow[id-1].borrow+=1;
  276.  
  277. FILE* rfptr;
  278. rfptr=fopen("books.dat","w");
  279. for(a=0;a<i;a++){
  280. fprintf(rfptr,"%d %s %d %d\n",books[a].number,books[a].name,books[a].lent_amount,books[a].remain_amount);
  281. }
  282. fclose(rfptr);
  283.  
  284. FILE* qfptr;
  285. qfptr=fopen("borrower.dat","w");
  286. for(b=0;b<m;b++){
  287. fprintf(qfptr,"%d %s %d\n",borrow[b].id,borrow[b].name,borrow[b].borrow);
  288. for(c=0;c<borrow[b].borrow;c++){
  289. fprintf(qfptr,"%s\n",borrow[b].bookname[c]);
  290. }
  291. fprintf(qfptr,"\n");
  292. }
  293. fclose(qfptr);
  294.  
  295. }
  296. printf("Press \"Enter\" for return to main menu.\n");
  297. getchar();
  298.  
  299.  
  300. }
  301. void return_books(void){
  302. }
  303. void list(void){
  304. int rows,a,pages=0,det2=0;
  305. char det;
  306. printf("Rows per page: ");
  307. scanf("%d",&rows);
  308. while(getchar() != '\n')
  309. ;
  310. while(1){
  311. printf(" Id Name \t\t\t\t\t Lend Out Remaining\n");
  312. for(a=0;a<rows;a++){
  313. if(books[a+pages*rows].number!=0)
  314. printf("%3d %-50s%3d\t %3d\n",books[a+pages*rows].number,books[a+pages*rows].name,books[a+pages*rows].lent_amount,books[a+pages*rows].remain_amount);
  315. }
  316. printf("\n\"1\" to view previous page, \"2\" to view next page\n"
  317. "Press \"Enter\" for return\n");
  318. det=getchar();
  319. if(det=='\n')
  320. break;
  321. int zz;
  322. zz=getchar();
  323. det2=0;
  324. switch(det){
  325. case '1':
  326. if(pages!=0)
  327. pages-=1;
  328.  
  329. break;
  330. case '2':
  331. if(i%rows==0){
  332. if(pages!=((i/rows)-1))
  333. pages+=1;
  334. }else{
  335. if(pages!=((i/rows)))
  336. pages+=1;
  337. }
  338.  
  339. break;
  340. // case 13:
  341. // case '\n':
  342. // det2+=1;
  343. // break;
  344.  
  345. }
  346. }
  347.  
  348.  
  349. }
  350. void add_borrower(void){
  351. int a,dett;
  352. char * ll;
  353. char n[25],new_user[25];
  354. printf("Please enter the new borrowers' name: ");
  355. while(1){
  356. dett=0;
  357. fgets(n,25,stdin);
  358. ll=strtok(n,"\n");
  359. strcpy(new_user,ll);
  360. for(a=0;a<m;a++){
  361. if(strcmp(new_user,borrow[a].name)==0){
  362. printf("Sorry, %s has been registered.\n"
  363. "Please type again: ",new_user);
  364. dett+=1;
  365. }
  366. }
  367. if(dett==0)
  368. break;
  369. }
  370. FILE* lptr;
  371. lptr=fopen("borrower.dat","a");
  372. borrow[m].id=m+1;
  373. strcpy(borrow[m].name,new_user);
  374. borrow[m].borrow=0;
  375. fprintf(lptr,"\n%d %s %d\n",borrow[m].id,borrow[m].name,borrow[m].borrow);
  376. printf("Welcome %s!!\n"
  377. "Your id is %d\n\n"
  378. "Press \"Enter\" for return to main menu.\n",borrow[m].name,borrow[m].id);
  379. m++;
  380. fclose(lptr);
  381. getchar();
  382. }
  383. void inquire_borrower(void){
  384. char i[20],inquire[20];
  385. char * in;
  386. int inq,x,y,z,get=-1;
  387. printf("Please enter the user borrower name or id:");
  388. while(1){
  389.  
  390. fgets(i,20,stdin);
  391. in=strtok(i,"\n");
  392. strcpy(inquire,in);
  393. if(isdigit(inquire[0])==1){
  394. inq=atoi(inquire);
  395. if(inq>m)
  396. get=-2;
  397. else
  398. get=inq-1;
  399. }else{
  400. for(x=0;x<m;x++){
  401. if(strcmp(inquire,borrow[x].name)==0){
  402. get=x;
  403. }
  404. }
  405. }
  406. if(get==-1){
  407. printf("Sorry, no one names %s in this system.\n"
  408. "Please type again:",inquire);
  409. continue;
  410. }else if(get==-2){
  411. printf("Sorry, no one's id is %d in this system.\n"
  412. "Please type again:",inq);
  413. continue;
  414. }else{
  415. printf("%d %s borrows %d books\n",borrow[get].id,borrow[get].name,borrow[get].borrow);
  416. for(z=0;z<borrow[get].borrow;z++){
  417. printf("%s\n",borrow[get].bookname[z]);
  418. }
  419. printf("\n");
  420. break;
  421. }
  422. }
  423. printf("Press \"Enter\" for return to main menu.\n");
  424. getchar();
  425. }
  426. void search(void){
  427. int a,b,count=0,num[200]={0},lent[200],remain[200],pages=0,det2;
  428. char name[200][60],search[25],det;
  429. printf("Please enter the search keyword:");
  430. scanf("%s",&search);
  431. getchar();
  432. for(a=0;a<i;a++){
  433. if(strstr(books[a].name,search)!=NULL){
  434. num[count]=books[a].number;
  435. strcpy(name[count],books[a].name);
  436. lent[count]=books[a].lent_amount;
  437. remain[count]=books[a].remain_amount;
  438. count++;
  439. }
  440. }
  441. if(count==0)
  442. printf("No book matches the pattern \"%s\"\n\n"
  443. "Press \"Enter\" for return.\n",search);
  444. else
  445. printf("There is %d book(s) match \"%s\"\n",count,search);
  446. while(1){
  447. det2=0;
  448. if(count!=0){
  449.  
  450. printf(" Id Name \t\t\t\t\t Lend Out Remaining\n");
  451. for(b=0;b<10;b++){
  452. if(num[b+pages*10]!=0)
  453. printf("%3d %-50s%3d\t %3d\n",num[b+pages*10],name[b+pages*10],lent[b+pages*10],remain[b+pages*10]);
  454. }
  455. printf("\n\"1\" to view previous page, \"2\" to view next page\n"
  456. "Press \"Enter\" for return.\n");
  457.  
  458. }
  459.  
  460.  
  461. det=getchar();
  462. if(det=='\n')
  463. break;
  464. int zz;
  465. zz=getchar();
  466. switch(det){
  467. case '1':
  468. if(pages!=0)
  469. pages-=1;
  470. break;
  471. case '2':
  472. if(count%10==0){
  473. if(pages!=((count/10)-1))
  474. pages+=1;
  475. }else{
  476. if(pages!=((count/10)))
  477. pages+=1;
  478. }
  479. break;
  480. // case 13:
  481. // det2+=1;
  482. // break;
  483. }
  484.  
  485. }
  486. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement