Advertisement
Guest User

HOMEWORK2

a guest
Nov 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. /**
  2. * File: HW2_Nikita_Mikhalchenko_185014MVEB.c
  3. * Author: Nikita Mikhalchenko
  4. * Created: 19.11.2019
  5. * Edited: 19.11.2019
  6. *
  7. * Description:
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #define LEN_MAX 30
  15.  
  16. typedef struct{
  17. int bookID;
  18. char title[LEN_MAX];
  19. char author[LEN_MAX];
  20. int year;
  21. float self_cost;
  22. float sell_price;
  23. }book;
  24.  
  25. typedef struct{
  26. int inventoryID;
  27. int bookID;
  28. int shelfN;
  29. int stock;
  30. }inventory;
  31.  
  32. book * ReadFile1(int *);
  33. inventory * ReadFile2(int *);
  34. void show(book *books, inventory *catalogue, int bNum);
  35. void edit1(book *books, inventory *catalogue, int bNum);
  36. void edit2(book *books, inventory *catalogue, int bNum);
  37. void find(book *books, inventory *catalogue, int bNum);
  38.  
  39. int main(void){
  40. int bNum = 0, cNum = 0;
  41. int ans;
  42.  
  43. book *books = ReadFile1(&bNum);
  44. inventory *catalogue = ReadFile2(&cNum);
  45.  
  46. do{
  47. printf("\nChose from following: 1 - show all, 2 - edit book");
  48. printf("title, 3 - edit shelf number, 4 - find books");
  49. printf("less than N, 0 - exit: ");
  50. scanf("%d", &ans);
  51. printf("\n");
  52. if(ans == 1){
  53. show(books, catalogue, bNum);
  54. }else if(ans == 2){
  55. edit1(books, catalogue, bNum);
  56. }else if(ans == 3){
  57. edit2(books, catalogue, bNum);
  58. }else if(ans == 4){
  59. find(books, catalogue, bNum);
  60. }
  61. }while(ans != 0);
  62.  
  63. free(books);
  64. free(catalogue);
  65. return 0;
  66. }
  67.  
  68. /**
  69. * Here the program is reading first input file
  70. */
  71. book * ReadFile1(int *bNum){
  72.  
  73. FILE *fp = fopen("HW2_Nikita_Mikhalchenko_185014MVEB_input1.txt",
  74. "r");
  75. book *file1;
  76.  
  77. if (fp == NULL){
  78. printf( "Error opening file1\n" );
  79. exit(2);
  80. }
  81.  
  82. if((file1 = (book *)malloc(sizeof(book))) == NULL)
  83. {
  84. printf("ERROR: malloc books\n");
  85. exit(1);
  86. }
  87. while(fscanf(fp, "%d %s %s %d %f %f", &file1[*bNum].bookID,
  88. file1[*bNum].title, file1[*bNum].author, &file1[*bNum].year,
  89. &file1[*bNum].self_cost, &file1[*bNum].sell_price) == 6 ){
  90. (*bNum)++;
  91. if((file1 = (book*)realloc(file1, sizeof(book)*((*bNum)+1))) == NULL)
  92. {
  93. printf("ERROR: realloc prod\n");
  94. exit(1);
  95. }
  96. }
  97. fclose(fp);
  98. return(file1);
  99. }
  100.  
  101. /**
  102. * Here the program is reading second input file
  103. */
  104. inventory * ReadFile2(int *cNum){
  105. FILE *fp = fopen("HW2_Nikita_Mikhalchenko_185014MVEB_input2.txt",
  106. "r");
  107. inventory *file2;
  108.  
  109. if (fp == NULL){
  110. printf( "Error opening file2\n" );
  111. exit(2);
  112. }
  113.  
  114. if((file2 = (inventory *)malloc(sizeof(inventory))) == NULL)
  115. {
  116. printf("ERROR: malloc books\n");
  117. exit(1);
  118. }
  119. while(fscanf(fp, "%d %d %d %d", &file2[*cNum].inventoryID,
  120. &file2[*cNum].bookID, &file2[*cNum].shelfN,
  121. &file2[*cNum].stock) == 4){
  122. (*cNum)++;
  123. if((file2 = (inventory*)realloc(file2, sizeof(inventory)*((*cNum)+1))) == NULL)
  124. {
  125. printf("ERROR: realloc prod\n");
  126. exit(1);
  127. }
  128. }
  129. fclose(fp);
  130. return(file2);
  131. }
  132.  
  133. void show(book *books, inventory *catalogue, int bNum)
  134. {
  135. FILE *outf = fopen("out.txt", "w");
  136. for(int i = 0; i < bNum ; i++){
  137. if(books[i].bookID == catalogue[i].bookID){
  138. printf("%6d %s %s %d %.2f %.2f %d %d %d\n", books[i].bookID,
  139. books[i].title, books[i].author, books[i].year,
  140. books[i].self_cost, books[i].sell_price,
  141. catalogue[i].inventoryID, catalogue[i].shelfN,
  142. catalogue[i].stock);
  143. fprintf(outf, "%d %s %s %d %.2f %.2f %d %d %d", books[i].bookID,
  144. books[i].title, books[i].author, books[i].year,
  145. books[i].self_cost, books[i].sell_price,
  146. catalogue[i].inventoryID, catalogue[i].shelfN,
  147. catalogue[i].stock);
  148. }
  149. }
  150. fclose(outf);
  151. }
  152.  
  153. void edit1(book *books, inventory *catalogue, int bNum){
  154. int ID;
  155. char bName[LEN_MAX];
  156. FILE *fp = fopen("HW2_Nikita_Mikhalchenko_185014MVEB_input1.txt",
  157. "r");
  158. printf("Enter the BookID to change book title: ");
  159. scanf("%d", &ID);
  160. printf("Enter the new name: ");
  161. scanf("%s", bName);
  162.  
  163. fclose(fp);
  164. }
  165.  
  166. void edit2(book *books, inventory *catalogue, int bNum){
  167. int ID;
  168. int sNum;
  169. FILE *fp = fopen("HW2_Nikita_Mikhalchenko_185014MVEB_input2.txt",
  170. "r");
  171. printf("Enter the inventoryID to change inventory shelf: ");
  172. scanf("%d", &ID);
  173. printf("Enter the new shelf number: ");
  174. scanf("%d" &sNum);
  175.  
  176. fclose(fp);
  177. }
  178.  
  179. void find(book *books, inventory *catalogue, int bNum){
  180. int N;
  181. FILE *outf = fopen("out2.txt", "w");
  182. printf("Enter the number to find books that have less pieces left: ");
  183. scanf("%d", &N);
  184. for(int i = 0; i < bNum ; i++){
  185. if(catalogue[i].stock < N){
  186. if(books[i].bookID == catalogue[i].bookID){
  187. printf("%6d %s %s %d %.2f %.2f %d %d %d\n", books[i].bookID,
  188. books[i].title, books[i].author, books[i].year,
  189. books[i].self_cost, books[i].sell_price,
  190. catalogue[i].inventoryID, catalogue[i].shelfN,
  191. catalogue[i].stock);
  192. fprintf(outf, "%d %s %s %d %.2f %.2f %d %d %d", books[i].bookID,
  193. books[i].title, books[i].author, books[i].year,
  194. books[i].self_cost, books[i].sell_price,
  195. catalogue[i].inventoryID, catalogue[i].shelfN,
  196. catalogue[i].stock);
  197. }
  198. }
  199. }
  200. fclose(outf);
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement