Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. struct book{
  4. char title[100],author[100],subject[100];
  5. int id;
  6. };
  7. void input(struct book s[],int n){
  8. int i;
  9. for(i=0;i<n;i++){
  10. printf("Enter title:");
  11. gets(s[i].title);
  12. fflush(stdin);
  13. printf("Enter authors name:");
  14. gets(s[i].author);
  15. fflush(stdin);
  16. printf("Enter id:");
  17. scanf("%d",&s[i].id);
  18. fflush(stdin);
  19. printf("Enter subject:");
  20. gets(s[i].subject);
  21. fflush(stdin);
  22. printf("\n");
  23. }
  24. }
  25. void view(struct book s[],int n){
  26. int i;
  27. printf("Here is the list of books:\n");
  28.  
  29. for(i=0;i<n;i++){
  30. printf("Title:%s\n",s[i].title);
  31. printf("Author:%s\n",s[i].author);
  32. printf("Id:%d\n",s[i].id);
  33. printf("Subject:%s\n",s[i].subject);
  34. printf("\n");
  35. }
  36. }
  37. void search(struct book s[],int n){
  38. char find[100];
  39. int i;
  40. printf("\nEnter the title of the book to find it:");
  41. gets(find);
  42. printf("\n");
  43. for(i=0;i<n;i++){
  44. if(strcmp(find,s[i].title)==0){
  45. printf("Title:%s\n",s[i].title);
  46. printf("Author:%s\n",s[i].author);
  47. printf("Id:%d\n",s[i].id);
  48. printf("Subject:%s\n",s[i].subject);
  49.  
  50. }
  51. else{
  52. printf("Can't find it");
  53. }
  54.  
  55. }
  56.  
  57.  
  58. }
  59. void edit(struct book s[],int n){
  60. char edit[100],ch;
  61. int i;
  62. printf("\nEnter the title of the book to edit it:");
  63. gets(edit);
  64. printf("\n");
  65.  
  66. for(i=0;i<n;i++){
  67. if(strcmp(edit,s[i].title)==0){
  68. printf("Enter title:");
  69. gets(s[i].title);
  70. fflush(stdin);
  71. printf("Enter authors name:");
  72. gets(s[i].author);
  73. fflush(stdin);
  74. printf("Enter id:");
  75. scanf("%d",&s[i].id);
  76. fflush(stdin);
  77. printf("Enter subject:");
  78. gets(s[i].subject);
  79. fflush(stdin);
  80. printf("\n");
  81.  
  82. }
  83. }
  84. printf("Press s/S to see the updated list\n");
  85. ch=getch();
  86. if(ch=='s' || ch=='S'){
  87.  
  88. for(i=0;i<n;i++){
  89. printf("Title:%s\n",s[i].title);
  90. printf("Author:%s\n",s[i].author);
  91. printf("Id:%d\n",s[i].id);
  92. printf("Subject:%s\n",s[i].subject);
  93. printf("\n");
  94. }
  95.  
  96. }
  97.  
  98.  
  99. }
  100. void delet(struct book s[],int n){
  101. int del,k,i;
  102. char ch;
  103. printf("Enter the Id of the book you want to delete:");
  104. scanf("%d",&del);
  105. for(i=0;i<n;i++){
  106. if(del==s[i].id){
  107. for(k=i;k<n;k++){
  108. s[k]=s[k+1];
  109. }
  110. n--;
  111. }
  112. }
  113. printf("Press s/S to see the updated list\n");
  114. ch=getch();
  115. if(ch=='s' || ch=='S'){
  116.  
  117. for(i=0;i<n;i++){
  118. printf("Title:%s\n",s[i].title);
  119. printf("Author:%s\n",s[i].author);
  120. printf("Id:%d\n",s[i].id);
  121. printf("Subject:%s\n",s[i].subject);
  122. printf("\n");
  123. }
  124.  
  125. }
  126.  
  127. }
  128.  
  129. int main(){
  130. int n;
  131. char ch;
  132. printf("How many book you want to add:");
  133. scanf("%d",&n);
  134. fflush(stdin);
  135. struct book s[n];
  136. input(s,n);
  137. view(s,n);
  138. printf("Press 'S\s' for search a book\n");
  139. printf("Press 'e/E' for edit a book\n");
  140. printf("Press 'd/D' to delete any book\n");
  141. ch=getch();
  142. if(ch=='s' || ch=='S'){
  143. search(s,n);
  144. }
  145. else if(ch=='e' || ch=='E'){
  146. edit(s,n);
  147. }
  148. else if(ch=='d' || ch=='D')
  149.  
  150. {
  151. delet(s,n);
  152. }
  153.  
  154.  
  155.  
  156. return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement