Guest User

Untitled

a guest
Dec 14th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. struct bookinfo
  6. {
  7. char title[50],author[50];
  8. int isbn,price;
  9. };
  10.  
  11.  
  12.  
  13. struct Node
  14. {
  15.  
  16. struct bookinfo book;
  17. struct Node *next;
  18. }*Head;
  19.  
  20. struct bookinfo getBook()
  21. {
  22. char name[50],auth[50];
  23. int cost,sr;
  24. struct bookinfo tempbook;
  25. printf("Enter ISBN Number : ");
  26. scanf("%d",&sr);
  27. printf("Enter Book Name : ");
  28. scanf("%s", name);
  29. printf("Enter Book Author : ");
  30. scanf("%s",auth);
  31. printf("Enter Price : ");
  32. scanf("%d",&cost);
  33. tempbook.isbn = sr;
  34. strcpy(tempbook.title,name);
  35. strcpy(tempbook.author,auth);
  36. tempbook.price = cost;
  37.  
  38. return(tempbook);
  39. }
  40.  
  41. void addBook()
  42. {
  43. long int recsize;
  44. struct bookinfo newbook = getBook();
  45. struct Node *temp1, *temp2;
  46.  
  47. temp1=(struct Node *)malloc(sizeof(struct Node));
  48. temp1->book = newbook;
  49.  
  50. // Copying the Head location into another node.
  51. temp2=Head;
  52.  
  53. if(Head == NULL)
  54. {
  55. // If List is empty we create First Node.
  56. Head=temp1;
  57. Head->next=NULL;
  58. }
  59. else
  60. {
  61. // Traverse down to end of the list.
  62. while(temp2->next != NULL)
  63. temp2=temp2->next;
  64.  
  65. // Append at the end of the list.
  66. temp1->next=NULL;
  67. temp2->next=temp1;
  68. }
  69. system("clear");
  70. printf("\nBook Added Successfully\n");
  71.  
  72. FILE *fp,*fs;
  73. fp = fopen("bookrecord.txt","a");
  74. if (fp == NULL)
  75. {
  76. printf("Cannot open file");
  77. }
  78. fprintf ( fp, "\n %d \n %s \n %s \n %d\n", newbook.isbn, newbook.title, newbook.author, newbook.price ) ;
  79. fflush (stdin) ;
  80. fclose(fp);
  81.  
  82. fs = fopen( "BOOK.DAT", "rb+" ) ;
  83. if ( fs == NULL )
  84. {
  85. fs = fopen ( "BOOK.DAT", "wb+" ) ;
  86. if ( fs == NULL )
  87. {
  88. puts ( "Cannot open file" ) ;
  89. }
  90. }
  91. recsize = sizeof (newbook) ;
  92. fseek ( fs, 0 , SEEK_END ) ;
  93. fwrite ( &newbook, recsize, 1, fs ) ;
  94. fflush (stdin) ;
  95. fclose(fs);
  96. }
  97.  
  98.  
  99. void listFile()
  100. {
  101.  
  102. long int recsize;
  103. struct bookinfo b1;
  104. recsize = sizeof ( b1 ) ;
  105.  
  106. FILE *fp;
  107. fp = fopen("BOOK.DAT","rb+");
  108. if (fp == NULL)
  109. {
  110. printf("Cannot open file");
  111. }
  112.  
  113. rewind (fp) ;
  114. while ( fread ( &b1, recsize, 1, fp ) == 1 )
  115. {printf ( "\n ISBN:%d \n Title:%s \n Author:%s \n Price:%d\n", b1.isbn, b1.title, b1.author, b1.price ) ;}
  116. fclose(fp);
  117. }
  118.  
  119.  
  120.  
  121. void display()
  122. {
  123. struct Node *cur_ptr;
  124.  
  125. cur_ptr=Head;
  126.  
  127. if(cur_ptr==NULL)
  128. {
  129. system("clear");
  130. printf("\nThere are No BOOKS in the List\n\n");
  131. }
  132. else
  133. {
  134. system("clear");
  135. printf("\nBooks : \n\n");
  136. //traverse the entire linked list
  137. while(cur_ptr!=NULL)
  138. {
  139. printf("ISBN : %d \n",cur_ptr->book.isbn);
  140. printf("Title : %s \n",cur_ptr->book.title);
  141. printf("Author : %s \n",cur_ptr->book.author);
  142. printf("Price : %d \n",cur_ptr->book.price);
  143. cur_ptr=cur_ptr->next;
  144. printf("\n");
  145. }
  146. printf("\n");
  147. }
  148.  
  149. listFile();
  150.  
  151. }
  152.  
  153. int delBook(int num)
  154. {
  155.  
  156. int flag = 0,error = 440;
  157. struct Node *prev_ptr, *cur_ptr;
  158.  
  159. cur_ptr=Head;
  160.  
  161. while(cur_ptr != NULL)
  162. {
  163. if(cur_ptr->book.isbn == num)
  164. {
  165. if(cur_ptr==Head)
  166. {
  167. Head=cur_ptr->next;
  168. free(cur_ptr);
  169. system("clear");
  170. printf("\n Book Removed\n\n");
  171. flag = 1;
  172. return(1);
  173. break;
  174. }
  175. else
  176. {
  177. prev_ptr->next=cur_ptr->next;
  178. free(cur_ptr);
  179. system("clear");
  180. printf("\n Book Removed\n\n");
  181. flag = 1;
  182. return(1);
  183. break;
  184. }
  185.  
  186. }
  187. else
  188. {
  189. prev_ptr=cur_ptr;
  190. cur_ptr=cur_ptr->next;
  191.  
  192. }
  193. if(flag == 0){
  194. flag = flag + error;
  195. system("clear");
  196. printf("\nBook(ISBN %d) not found in the List\n\n", num);
  197. }
  198. }
  199. return(flag);
  200.  
  201. }
  202.  
  203.  
  204. int search(int num)
  205. {
  206.  
  207. int flag = 0,error=404;
  208. struct Node *prev_ptr, *cur_ptr;
  209.  
  210. cur_ptr=Head;
  211.  
  212. while(cur_ptr != NULL)
  213. {
  214. if(cur_ptr->book.isbn == num)
  215. {
  216. system("clear");
  217. printf("\nBook Found \n\n");
  218. printf("ISBN : %d \n",cur_ptr->book.isbn);
  219. printf("Title : %s \n",cur_ptr->book.title);
  220. printf("Author : %s \n",cur_ptr->book.author);
  221. printf("Price : %d \n",cur_ptr->book.price);
  222. cur_ptr=cur_ptr->next;
  223. printf("\n");
  224. flag = 1;
  225. return(1);
  226. break;
  227. }
  228. else
  229. {
  230. prev_ptr=cur_ptr;
  231. cur_ptr=cur_ptr->next;
  232.  
  233. }
  234. if(flag == 0){
  235. flag = flag + error;
  236. system("clear");
  237. printf("\nBook(ISBN %d) not found in the List\n\n", num);
  238. }
  239. }
  240. return(flag);
  241. }
  242.  
  243.  
  244.  
  245. void menu()
  246. {
  247. printf("\nMenu : ");
  248. printf("\n**********************************************************************");
  249. printf("\nEnter 1 to Add New Book");
  250. printf("\nEnter 2 to Remove Existing Book");
  251. printf("\nEnter 3 to List All Books");
  252. printf("\nEnter 4 to Search a Book");
  253. printf("\nEnter 0 to Quit");
  254. printf("\n************************************************************************");
  255. }
  256.  
  257. void main()
  258. {
  259. system("clear");
  260. int success;
  261. int found;
  262. int delISBN;
  263. int searchISBN;
  264. int choice;
  265.  
  266. do
  267. {
  268. menu();
  269. printf("\nEnter your choice : ");
  270. scanf("%d",&choice);
  271. switch(choice)
  272. {
  273. case 1:
  274. addBook();break;
  275. case 2:
  276. printf("Enter ISBN to Delete Book : ");
  277. scanf("%d",&delISBN);
  278. success = delBook(delISBN);
  279. if(success != 440 && success != 1){printf("\nBook Not Available\n");success = 9;}
  280. break;
  281. case 3:
  282. display();
  283. break;
  284. case 4:
  285. printf("Enter ISBN to Search Book : ");
  286. scanf("%d",&searchISBN);
  287. found = search(searchISBN);
  288. if(found != 404 && found != 1){printf("\nBook Not Available\n");found = 10;}
  289. break;
  290. case 0:
  291. printf("\nThank You for using our Service\n\n");
  292. break;
  293. default:
  294. printf("Please Enter a Valid Choice !");
  295. }
  296. }while(choice!=0);
  297.  
  298. }
Add Comment
Please, Sign In to add comment