wuiyang

Example of how a module works (simple)

Dec 22nd, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.64 KB | None | 0 0
  1. //updated 10:48pm 22/12 added few comments
  2. //updated 4:28pm 23/12 comment of how to use save load
  3. //updated 4:54pm added function declaration and function with parameters (with and without pointers)
  4.  
  5. #include <stdio.h>
  6.  
  7. typedef struct{
  8. char orderId[6], foodId[6], memberId[6];
  9. int quantity;
  10. } Order;
  11.  
  12. //Global variable (you can change it to local, but need to pass by reference to each function)
  13. //Structure list (for faster read/write)
  14. Order orderList[100];
  15. //How many structure inside the list?
  16. int orderCount=0;
  17.  
  18. //function declaration
  19. void del();
  20. void search();
  21. void modify();
  22. void add()
  23. void display();
  24. int userIsConfirm(char *print);
  25. void load();
  26. void save();
  27. void getUserInputOrderWithPointers(int isModify, Order *order);
  28. Order getUserInputOrder(int isModify);
  29.  
  30. void main(){
  31. int choice;
  32. load(); //read data once enough, then no need read again, only write it back
  33. //load data to orderlist, then we modify the list, and save it back
  34. //so no need to read it as the data is stored in the list
  35. do{
  36. printf("\n\n1. Add\n2. Display\n3. Search\n4. Modify\n5. Delete\n6. Quit\nEnter: ");
  37. rewind(stdin);
  38. scanf("%d", &choice);
  39. if(choice == 1){
  40. add();
  41. }else if(choice == 2){
  42. display();
  43. }else if(choice == 3){
  44. search();
  45. }else if(choice == 4){
  46. modify();
  47. }else if(choice == 5){
  48. del();
  49. }
  50. }while(choice != 6);
  51. }
  52.  
  53. void del(){
  54. //i use order to store data, so dont need to declare many string
  55. Order orderTemp;
  56. char userConfirm;
  57. int orderIndex, isFound, i;
  58.  
  59. //initialize
  60. isFound=0; //0 = false, rest = true
  61.  
  62. //display to user so they know which one to delete
  63. display();
  64.  
  65. //get user input for which order they want to delete
  66. printf("Enter record of order ID to delete : ");
  67. rewind(stdin);
  68. gets(orderTemp.orderId);
  69.  
  70. //find data from list
  71. for(i=0; i<orderCount; i++){
  72. //if order ID match
  73. if(strcmp(orderList[i].orderId, orderTemp.orderId) == 0){
  74. //save current index to orderIndex, for further reference
  75. orderIndex = i;
  76. //isFound = 1 or "true"
  77. isFound = 1;
  78. //this one is optional, but for long array (>10000) it is better to do this
  79. break;
  80. }
  81. }
  82.  
  83. //found it! Get user input to update data
  84. if(isFound){
  85. if(userIsConfirm("Confirm to delete (Y/N)? ")){
  86. //reduced 1 structure
  87. orderCount--;
  88. for(i=orderIndex; i<orderCount; i++){
  89. //moving data one step forward, eg: 1 2 3 4 5, remove 2, become 1 3 4 5
  90. orderList[i] = orderList[i+1];
  91. }
  92. }
  93. }else{
  94. printf("No data found :(\n");
  95. }
  96. }
  97.  
  98. void search(){
  99. Order orderTemp;
  100. int i;
  101. //give user option to which column they want to find
  102. //optional: use if/switch statement to get which column they want to find
  103. printf("Leave blank to skip\n");
  104. printf("Enter record of order ID to search: ");
  105. rewind(stdin);
  106. gets(orderTemp.orderId);
  107. printf("Enter record of food ID to search: ");
  108. rewind(stdin);
  109. gets(orderTemp.foodId);
  110. printf("Enter record of member ID to search: ");
  111. rewind(stdin);
  112. gets(orderTemp.memberId);
  113.  
  114. //find data from list
  115. printf("ORDER ID | FOOD ID | MEMBER ID | QUANTITY\n");
  116. printf("+++++++++|+++++++++|+++++++++++|+++++++++\n");
  117. for(i=0; i<orderCount;i++){
  118. //check if it is not empty, if it is not empty, then check memberId is same with what user want
  119. //if it is empty, then the first character in the string is '\0'
  120. if((orderTemp.orderId[0] != '\0' && strcmp(orderList[i].orderId, orderTemp.orderId) == 0) ||
  121. (orderTemp.foodId[0] != '\0' && strcmp(orderList[i].foodId, orderTemp.foodId) == 0) ||
  122. (orderTemp.memberId[0] != '\0' && strcmp(orderList[i].memberId, orderTemp.memberId) == 0)){
  123. printf(" %-7s | %-7s | %-9s | %d\n", orderList[i].orderId, orderList[i].foodId, orderList[i].memberId, orderList[i].quantity);
  124. }
  125. }
  126. }
  127.  
  128. void modify(){
  129. //i use order to store data, so dont need to declare many string
  130. Order orderTemp;
  131. char userConfirm;
  132. int orderIndex, isFound, i;
  133.  
  134. //initialize
  135. isFound=0; //0 = false, rest = true
  136.  
  137. //display to user so they know which one to modify
  138. display();
  139.  
  140. //get user input for which order they want to modify
  141. printf("Enter record of order ID to modify : ");
  142. rewind(stdin);
  143. gets(orderTemp.orderId);
  144.  
  145. //find data from list
  146. for(i=0; i<orderCount; i++){
  147. //if order ID is match
  148. if(strcmp(orderList[i].orderId, orderTemp.orderId) == 0){
  149. //save current index to orderIndex, for further reference
  150. orderIndex = i;
  151. //isFound = 1 or "true"
  152. isFound = 1;
  153. //this one is optional, but for long array (>10000) it is better to do this
  154. break;
  155. }
  156. }
  157.  
  158. //found it! Get user input to update data
  159. if(isFound){
  160. //get all data into temporary variable (here is orderTemp)
  161. //we use pointer to edit as the orderId is in ordertemp
  162. getUserInputOrderWithPointers(1, &orderTemp);
  163.  
  164. //get confirmation to change
  165. if(userIsConfirm("Confirm to modify (Y/N)? ")){
  166. //structure can use =
  167. orderList[orderIndex] = orderTemp;
  168. save();//save after updated
  169. printf("Successfully updated list!\n");
  170. }
  171. }else{
  172. printf("No data found :(\n");
  173. }
  174. }
  175.  
  176. void add(){
  177. Order order;
  178.  
  179. //using function with parameter to get user input
  180. order = getUserInputOrder(0);
  181.  
  182. //put in the order to order list
  183. orderList[orderCount] = order;
  184.  
  185. //how above line works:
  186. //imagine currently has 0 order, so orderCount is 0
  187. //if you want to store something, you would store it at the first index (0), as there is no data
  188. //which we store it at orderList[0] = order;
  189. //and we can directly do orderList[orderCount] = order;
  190.  
  191. //add 1 to total count, eg: now has 0, added, then become 1
  192. orderCount++;
  193. //now has 1 order, and orderCount is 1
  194. //when next time you want to store a data, you would store it at the second index (1), as there is no data
  195. //which we store it at orderList[1] = order;
  196. //and we can directly do orderList[orderCount] = order;
  197.  
  198. //or a shorter code, combing both line become orderList[orderCount++] = order;
  199.  
  200. save();//save after updated
  201. }
  202.  
  203. void getUserInputOrderWithPointers(int isModify, Order *order){
  204. //using pointers to directly modify the order
  205. //if is not modify, then it is new, so we get user input for order
  206. if(isModify == 0){
  207. printf("Enter order ID: ");
  208. rewind(stdin);
  209. gets(order->orderId); //or gets((*order).orderId);
  210. }
  211. printf("Enter %sfood ID: ", isModify ? "new " : "");
  212. rewind(stdin);
  213. gets(order->foodId); //or gets((*order).foodId);
  214. printf("Enter %smember ID: ", isModify ? "new " : "");
  215. rewind(stdin);
  216. gets(order->memberId); //or gets((*order).memberId);
  217. printf("Enter %squantity: ", isModify ? "new " : "");
  218. scanf("%d", &(order->quantity)); //or scanf("%d", &(*order).quantity);
  219. }
  220.  
  221. Order getUserInputOrder(int isModify){
  222. //return a new order back to the line where it call
  223. Order order;
  224. //if is not modify, then it is new, so we get user input for order
  225. if(isModify == 0){
  226. printf("Enter order ID: ");
  227. rewind(stdin);
  228. gets(order.orderId);
  229. }
  230. //the isModify ? "new " : "" is same as if(isModify){ "new" } else { "" } in a simplier form
  231. printf("Enter %sfood ID: ", isModify ? "new " : "");
  232. rewind(stdin);
  233. gets(order.foodId);
  234. printf("Enter %smember ID: ", isModify ? "new " : "");
  235. rewind(stdin);
  236. gets(order.memberId);
  237. printf("Enter %squantity: ", isModify ? "new " : "");
  238. scanf("%d", &order.quantity);
  239. return order;
  240. }
  241.  
  242. void display(){
  243. int i;
  244. printf("ORDER ID | FOOD ID | MEMBER ID | QUANTITY\n");
  245. printf("+++++++++|+++++++++|+++++++++++|+++++++++\n");
  246. for(i=0;i<orderCount;i++){
  247. printf(" %-7s | %-7s | %-9s | %d\n", orderList[i].orderId, orderList[i].foodId, orderList[i].memberId, orderList[i].quantity);
  248. }
  249. }
  250.  
  251. void save(){
  252. FILE *fptr=fopen("order.txt", "w");
  253. int i;
  254. if(fptr==NULL){
  255. printf("Error! Unable open file!\n");
  256. return;
  257. }
  258. //write data from index 0 to orderCount-1 (which is from first to last)
  259. for(i=0; i<orderCount;i++){
  260. fprintf(fptr, "%s|%s|%s|%d\n", orderList[i].orderId, orderList[i].foodId, orderList[i].memberId, orderList[i].quantity);
  261. }
  262. fclose(fptr);
  263. }
  264.  
  265. void load(){
  266. FILE *fptr=fopen("order.txt", "r");
  267. if(fptr==NULL){
  268. printf("Error! Unable open file!\n");
  269. return;
  270. }
  271. orderCount = 0;
  272. //read data
  273. while(fscanf(fptr, "%[^|]|%[^|]|%[^|]|%d\n", orderList[orderCount].orderId, orderList[orderCount].foodId, orderList[orderCount].memberId, &orderList[orderCount].quantity)!=EOF){
  274. //orderCount increase by one with each data has been written to orderList
  275. orderCount++;
  276. }
  277. fclose(fptr);
  278. }
  279.  
  280. int userIsConfirm(char *print){
  281. char userConfirm;
  282. do{
  283. printf("%s", print);
  284. rewind(stdin);
  285. userConfirm = tolower(getchar());
  286. if(userConfirm != 'y' && userConfirm != 'n')
  287. printf("Error! Invalid input! Either Y or N!\n");
  288. }while(userConfirm != 'y' && userConfirm != 'n');
  289. return userConfirm == 'y';
  290. }
Advertisement
Add Comment
Please, Sign In to add comment