Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. struct groceryList{
  7. int ID;
  8. float amount;
  9. char grocery[100];
  10. char unit[10];
  11. };
  12.  
  13. /*funktion som kollar om användaren skriver in chars i "amount".*/
  14. void errorHandling(struct groceryList *pgroceries, int listCounter)
  15. {
  16. int i, scanString = 0, count;
  17. char arr[20];
  18. do{
  19. fflush(stdin);
  20. gets(arr);
  21. scanString = strlen(arr);
  22. count = 0;
  23.  
  24. for(i = 0; i < scanString;)
  25. {
  26. if(isdigit(arr[i]) != 0 || arr[i] == '.') //Om arr:s index inte är alfabetiskt eller är en punkt. Hoppa till nästa index
  27. {
  28. i++;
  29. }
  30. else
  31. {
  32. printf("Wrong input. Try again!");
  33. count++;
  34. break;
  35. }
  36. }
  37.  
  38. }while(count > 0);
  39. pgroceries[listCounter].amount = atof(arr); //ger amount arr:s numeriska symboler.
  40. }
  41.  
  42. /* funktion som listar alla varor och mängden av varan.*/
  43. void listingGroceries(struct groceryList *pgroceries, int listCounter)
  44. {
  45.  
  46. fflush(stdin);
  47. pgroceries[listCounter].ID = listCounter + 1; //listar varorna
  48. printf("What grocery do you wanna put in the list? ");
  49. gets(pgroceries[listCounter].grocery);
  50. printf("What's the amount? ");
  51. errorHandling(pgroceries, listCounter);
  52. printf("What's the unit? ");
  53. gets(pgroceries[listCounter].unit);
  54. }
  55.  
  56. /*Skriver ut alla varor med formaterad text.*/
  57. void printGroceries(struct groceryList *pgroceries, int listCounter)
  58. {
  59. int j;
  60. if(pgroceries == 0)
  61. {
  62. printf("There are no groceries to print.\n");
  63. }
  64. else
  65. {
  66.  
  67. for(j = 0; j < listCounter; j++)
  68. {
  69. printf("Grocery %2d:", pgroceries[j].ID); //Skriver ut varorna.
  70. printf("%-15s:", pgroceries[j].grocery);
  71. printf("%10.2f:", pgroceries[j].amount);
  72. printf("%-8s\n", pgroceries[j].unit);
  73.  
  74. }
  75. }
  76. }
  77. int writeToFile(struct groceryList **pgroceries, int listCounter)
  78. {
  79. char userInput[50];
  80. FILE *fp = 0;
  81. printf("What would you like to call the file?\n"); //Funktion som läser in varorna till en fil. Felhantering finns.
  82. scanf("%s", userInput);
  83. fp = fopen(userInput,"wb");
  84. if(fp == NULL)
  85. {
  86. printf("Could not write to file, sorry!\n");
  87. return 0;
  88. }
  89. fwrite(*pgroceries, sizeof(struct groceryList), listCounter, fp);
  90. printf("%d groceries where saved to the file!\n", listCounter);
  91.  
  92. fclose(fp);
  93.  
  94. fflush(stdin);
  95. return 0;
  96. }
  97.  
  98. int readFromFile(struct groceryList **pgroceries, int listCounter)
  99. {
  100. char userInput[50];
  101. int byteSize = 0;
  102. FILE *fp = 0;
  103. printf("What file would you like to open?\n");
  104. scanf("%s", userInput);
  105. fp = fopen(userInput,"rb"); //funktion som läser in från en fil.
  106. if(fp == NULL)
  107. {
  108. printf("Could not read from file, sorry!\n");
  109. return 0;
  110. }
  111. else
  112. {
  113. fseek(fp, 0, SEEK_END); //letar efter filens slut.
  114. byteSize = ftell(fp); //Get bytesize filpekarens antal minnesplatser som en siffra.
  115. rewind(fp); //Sätter filpekaren i början av filen igen.
  116. *pgroceries = (struct groceryList*)realloc(*pgroceries, sizeof(struct groceryList) * (byteSize/sizeof(struct groceryList))); //allokerar minne
  117. listCounter = fread(*pgroceries, sizeof(struct groceryList), byteSize/sizeof(struct groceryList), fp); // läser in varorna och ger listcounter
  118. printf("%d groceries where read from the file!\n", listCounter); //antalet varor som läses in.
  119.  
  120. fclose(fp);
  121. }
  122. fflush(stdin);
  123. return listCounter;
  124. }
  125. //funktion som modiferar en vara i listan.
  126. int modifyGrocery(struct groceryList **pgroceries, int listCounter)
  127. {
  128. printf("Type in the ID of the grocery you would like to modify. ");
  129. scanf("%d", &listCounter);
  130. if(*pgroceries == NULL)
  131. {
  132. printf("No groceries to modify. Add groceries to the list first!\n");
  133. fflush(stdin);
  134. return 0;
  135. }
  136. else
  137. {
  138. listingGroceries(*pgroceries,listCounter-1);
  139. printf("ID %d has been modified.", listCounter);
  140. }
  141. fflush(stdin);
  142. return listCounter;
  143. }
  144.  
  145. void setMemory(struct groceryList **pgroceries, int listCounter)
  146. {
  147. struct groceryList *tp = (struct groceryList*) realloc(*pgroceries,listCounter*sizeof(struct groceryList)); // Allokerar minne varje gång man lägger till en vara.
  148.  
  149. if(tp != NULL)
  150. {
  151. *pgroceries = tp;
  152. }
  153. else
  154. printf("Something went wrong");
  155.  
  156. }
  157.  
  158.  
  159. int removeGrocery(struct groceryList **pgroceries, int listCounter)
  160. {
  161. int pickedElement;
  162.  
  163.  
  164. printf("What grocery would you like to remove? ");
  165. scanf("%d", &pickedElement);
  166. if(*pgroceries == NULL)
  167. {
  168. printf("No groceries to remove. Add groceries first!\n");
  169. fflush(stdin);
  170. return 0;
  171. }
  172. else if(pickedElement == 0 || pickedElement > listCounter)
  173. {
  174. printf("No grocery with that number.\n");
  175. fflush(stdin);
  176. return 0;
  177. }
  178. else
  179. {
  180.  
  181. pickedElement--;
  182. strcpy((*pgroceries)[pickedElement].grocery, (*pgroceries)[listCounter-1].grocery); // tar bort ett element, och lägger sista elementet på dess plats.
  183. (*pgroceries)[pickedElement].amount = (*pgroceries)[listCounter-1].amount;
  184. strcpy((*pgroceries)[pickedElement].unit, (*pgroceries)[listCounter-1].unit);
  185. listCounter--;
  186. setMemory(pgroceries, listCounter);
  187. fflush(stdin);
  188. }
  189.  
  190. printf("Grocery %d is annihilated.\n", pickedElement+1);
  191. fflush(stdin);
  192. return listCounter;
  193. }
  194.  
  195. int* menu(struct groceryList **pgroceries)
  196. {
  197. char menuBrowser[2];
  198. int listCounter = 0;
  199.  
  200. do{
  201.  
  202. printf("Menu\nWhat would you like to do?\n");
  203. printf("\n-----------------------------------\n");
  204. printf("1. Add grocery\n");
  205. printf("2. Print your list\n");
  206. printf("3. Add groceries to file\n");
  207. printf("4. Read groceries from file\n");
  208. printf("5. Modify grocery\n");
  209. printf("6. Remove grocery\n");
  210. printf("7. Exit the program\n");
  211. printf("\n-----------------------------------\n");
  212. gets(menuBrowser);
  213.  
  214.  
  215.  
  216. switch(menuBrowser[0]){
  217.  
  218. case '1':
  219. {
  220. listCounter++;
  221. setMemory(pgroceries, listCounter);
  222. listingGroceries(*pgroceries, listCounter-1);
  223. break;
  224. }
  225. case '2':
  226. {
  227. printGroceries(*pgroceries, listCounter);
  228. break;
  229. }
  230. case '3':
  231. {
  232. writeToFile(pgroceries, listCounter);
  233. break;
  234. }
  235. case '4':
  236. {
  237. listCounter = readFromFile(pgroceries, listCounter);
  238. break;
  239. }
  240. case '5':
  241. {
  242.  
  243. modifyGrocery(pgroceries, listCounter);
  244. break;
  245. }
  246.  
  247. case '6':
  248. {
  249. listCounter = removeGrocery(pgroceries, listCounter);
  250. break;
  251. }
  252. case '7':
  253. {
  254. free(pgroceries);
  255. return 0;
  256. }
  257. }
  258.  
  259. }while(1);
  260. }
  261.  
  262. int main()
  263. {
  264.  
  265. char loop = 'y';
  266. struct groceryList *groceries = NULL;
  267.  
  268. do{
  269.  
  270. menu(&groceries);
  271.  
  272.  
  273. printf("\nDo you want to play the program again? Y/N ");
  274. do
  275. {
  276. scanf("%c", &loop);
  277. }
  278. while(loop != 'y' && loop != 'n');
  279. fflush(stdin);
  280. }while(loop == 'y');
  281. return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement