Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define ROW 3
  5. #define COL 7
  6. #define MENUSIZE 7
  7.  
  8. typedef struct{
  9. int row;
  10. int col;
  11. }TableDetails;
  12.  
  13. typedef struct{
  14. char mealCode[5];
  15. char mealName[65];
  16. float price;
  17. }MenuItem;
  18.  
  19. typedef struct{
  20. MenuItem meal[MENUSIZE];
  21. int count; /* contains the actual number of meals on the menu */
  22. }MenuArrList;
  23.  
  24. typedef struct{
  25. int tableNum[ROW][COL]; /* Multidimensional Array representation of the tables in a restaurant
  26. Values of each index are as follows:
  27. 0 - if table is unoccupied.
  28. 1 - if table is occupied and is still waiting for their order.
  29. 2 - if table is occupied and has already received their order.
  30. */
  31. int numAvailTables; /* contains the number of unoccupied tables. */
  32. }Seating;
  33.  
  34. typedef struct OrderNode{
  35. MenuItem meal;
  36. TableDetails tableNum;
  37. struct OrderNode* nextOrd;
  38. }OrderNode, *OrderList;
  39.  
  40. typedef struct{
  41. MenuArrList menu;
  42. Seating table;
  43. OrderList orders;
  44. float sales; /* contains the amount of money earned by the restaurant. Starts at 0.*/
  45. }Restaurant;
  46.  
  47. /* Completed Functions */
  48. void pause(void);
  49. void generateMenuList(void);
  50. void generateSales(void);
  51.  
  52. /* Problem 1 */
  53. MenuItem* readMenuFromFile(void);
  54. void populateMenu(MenuArrList* ML, MenuItem* M);
  55. void initSeating(Seating* S);
  56. void initResto(Restaurant* R);
  57. void dispSeating(Seating S);
  58. void dispRestaurant(Restaurant R);
  59.  
  60. /* Problem 2 */
  61. int isVacant(Restaurant R, TableDetails tNum);
  62. void purchaseMeal(Restaurant* R, char mealCode[], TableDetails tNum);
  63.  
  64. /* Problem 3 */
  65. void deliverMeal(Restaurant* R);
  66.  
  67. /* Problem 4 */
  68. void cleanTables(Restaurant* R);
  69.  
  70. /* Problem 5 */
  71. void closeRestaurant(Restaurant* R);
  72.  
  73.  
  74. int main(void)
  75. {
  76. Restaurant resto;
  77. TableDetails tables[22] = {{0,0}, {0,1}, {0,2}, {0,3}, {0,4}, {0,5}, {0,6},
  78. {1,0}, {1,1}, {1,2}, {1,3}, {1,4}, {1,5}, {1,6},
  79. {2,0}, {2,1}, {2,2}, {2,3}, {2,4}, {2,5}, {2,6},
  80. {9,9}
  81. };
  82. char meal[][5] = {"BRGA", "CSPS", "FRRL", "HRDB", "PFCB", "RRPL", "SBFW", "AAAA"};
  83.  
  84. generateMenuList();
  85. generateSales();
  86.  
  87. /* Problem 1 */
  88. /* Instructions:
  89. 1) Initialize variable resto by calling the initResto() function.
  90. 2) Call the dispRestaurant() function.
  91. 3) Call the pause() function.
  92. */
  93. printf("Problem 1\n");
  94. initResto(&resto);
  95. dispRestaurant(resto);
  96. pause();
  97.  
  98. /* Problem 2 */
  99. /* Instructions:
  100. 1) Call the purchaseMeal() function, passing meal[0] and tables[4] as the parameters.
  101. 2) Call the purchaseMeal() function, passing meal[3] and tables[9] as the parameters.
  102. 3) Call the purchaseMeal() function, passing meal[2] and tables[3] as the parameters.
  103. 4) Call the purchaseMeal() function, passing meal[5] and tables[15] as the parameters.
  104. 5) Call the purchaseMeal() function, passing meal[4] and tables[7] as the parameters.
  105. 6) Call the purchaseMeal() function, passing meal[3] and tables[14] as the parameters.
  106. 7) Call the purchaseMeal() function, passing meal[6] and tables[17] as the parameters.
  107. 8) Call the purchaseMeal() function, passing meal[1] and tables[13] as the parameters.
  108. 9) Call the purchaseMeal() function, passing meal[4] and tables[6] as the parameters.
  109. 10) Call the purchaseMeal() function, passing meal[2] and tables[6] as the parameters.
  110. 11) Call the purchaseMeal() function, passing meal[7] and tables[2] as the parameters.
  111. 12) Call the purchaseMeal() function, passing meal[5] and tables[21] as the parameters.
  112. 13) Call the dispRestaurant() function.
  113. 14) Call the pause() function.
  114. */
  115. printf("Problem 2\n");
  116.  
  117. /* Problem 3 */
  118. /* Instructions:
  119. 1) Call the deliverMeal() function 5 times.
  120. 2) Call the dispRestaurant() function.
  121. 3) Call the pause() function.
  122. */
  123. printf("Problem 3\n");
  124.  
  125. /* Problem 4 */
  126. /* Instructions:
  127. 1) Call the cleanTables() function.
  128. 2) Call the dispRestaurant() function.
  129. 3) Call the pause() function.
  130. */
  131. printf("Problem 4\n");
  132.  
  133. /* Problem 5 */
  134. /* Instructions:
  135. 1) Call the closeRestaurant() function.
  136. 2) Call the dispRestaurant() function.
  137. 3) Call the pause() function.
  138. */
  139. printf("Problem 5\n");
  140.  
  141. return 0;
  142. }
  143.  
  144. void pause(void)
  145. {
  146. printf("\n\nPress any key to continue...");
  147. getch();
  148. system("CLS");
  149. }
  150.  
  151. void generateMenuList(void)
  152. {
  153. MenuItem meals[MENUSIZE] = {{"BRGA", "Beef Ribeye with Grilled Artichokes", 35.95},
  154. {"CSPS", "Crispy Salt and Pepper Squid", 18.95},
  155. {"FRRL", "Fennel Rubbed Rack of Lamb", 49.95},
  156. {"HRDB", "Honey Roast Duck with Green Beans", 33.95},
  157. {"PFCB", "Pan Fried Chicken Breast with Morel Sauce", 26.95},
  158. {"RRPL", "Roasted Rolled Pork Loin with Lemon and Sage", 22.95},
  159. {"SBFW", "Signature Beef Wellington", 58.95}
  160. };
  161.  
  162. FILE* fp = fopen("menu.bin", "wb");
  163. if(fp != NULL){
  164. fwrite(meals, sizeof(MenuItem), MENUSIZE, fp);
  165. fclose(fp);
  166. }
  167. }
  168.  
  169. void generateSales(void)
  170. {
  171. float initialSales = 606.15;
  172. generateMenuList();
  173. FILE* fp = fopen("sales.txt", "w");
  174. if(fp != NULL){
  175. fwrite(&initialSales, sizeof(float), 1, fp);
  176. fclose(fp);
  177. }
  178. }
  179.  
  180. /****************************************************************************************************************************************
  181. * readMenuFromFile() - This function will access a file called "menu.bin" and read ALL of the MenuItem records in the file. The records *
  182. * are then stored into a dynamically allocated array of MenuItems and returned to the calling function. *
  183. ****************************************************************************************************************************************/
  184. MenuItem* readMenuFromFile(void)
  185. {
  186. FILE *fp = fopen("menu.bin", "rb");
  187. MenuItem *items = (MenuItem *) malloc(sizeof(MenuItem) * MENUSIZE);
  188.  
  189. if(fp != NULL) {
  190. fread(items, sizeof(MenuItem), MENUSIZE, fp);
  191. fclose(fp);
  192. }
  193.  
  194. return items;
  195. }
  196.  
  197. /***************************************************************************************************************************************
  198. * populateMenu() - The function will populate a MenuArrList given an array of MenuItems. *
  199. ***************************************************************************************************************************************/
  200. void populateMenu(MenuArrList* ML, MenuItem* M)
  201. {
  202. memcpy(ML->meal, M, sizeof(MenuItem)*MENUSIZE);
  203. ML->count = MENUSIZE;
  204. }
  205.  
  206. void populateMenuListFromFile(MenuArrList* ML)
  207. {
  208. MenuItem *temp = (MenuItem *) malloc(sizeof(MenuItem)*50);
  209. MenuItem item;
  210. int count = 0;
  211. FILE *fp = fopen("menu.bin", "rb");
  212.  
  213. if(fp != NULL) {
  214. while(fread(&item, sizeof(MenuItem), 1, fp)) {
  215. temp[count++] = item;
  216. }
  217. fclose(fp);
  218. memcpy(ML->meal, temp, sizeof(MenuItem)*count);
  219. ML->count = count;
  220. }
  221.  
  222. }
  223. /*
  224. MenuArrList populateMenuListFromFile()
  225. {
  226. MenuArrList ML;
  227. MenuItem *temp = (MenuItem *) malloc(sizeof(MenuItem)*50);
  228. MenuItem item;
  229. int count = 0;
  230. FILE *fp = fopen("menu.bin", "rb");
  231.  
  232. if(fp != NULL) {
  233. while(fread(&item, sizeof(MenuItem), 1, fp)) {
  234. temp[count++] = item;
  235. }
  236. fclose(fp);
  237. memcpy(ML.meal, temp, sizeof(MenuItem)*count);
  238. ML.count = count;
  239. }
  240. return ML;
  241. }
  242. */
  243. /***************************************************************************************************************************************
  244. * initSeating() - The function will initialize the tableNum and numAvailTires of the passed parameter. *
  245. ***************************************************************************************************************************************/
  246. void initSeating(Seating* S)
  247. {
  248. int x, y;
  249. int *temp;
  250.  
  251. for(y=0; y < ROW; ++y) {
  252. for(x=0; x < COL; ++x) {
  253. S->tableNum[y][x] = 0;
  254. }
  255. }
  256.  
  257. // temp = calloc(sizeof(int), ROW * COL);
  258. // memcpy(S->tableNum, temp, ROW * COL);
  259.  
  260. S->numAvailTables = ROW * COL;
  261. }
  262.  
  263. /***************************************************************************************************************************************
  264. * initResto() - The function will initialize ALL of the fields inside the Restaurant. This function will call the 3 functions *
  265. * previously defined (readMenufromFile(), populateMenu(), initSeating()). *
  266. ***************************************************************************************************************************************/
  267. void initResto(Restaurant* R)
  268. {
  269. populateMenu(&(R->menu), readMenuFromFile());
  270. initSeating(&(R->table));
  271. R->orders = NULL;
  272. R->sales = 0;
  273. }
  274.  
  275. /***************************************************************************************************************************************
  276. * dispSeating() - The function will display by row and column, the current status of the seating arrangements in the restaurant. It *
  277. * will also display the remaining available tables. *
  278. ***************************************************************************************************************************************/
  279. void dispSeating(Seating S)
  280. {
  281. int x, y;
  282.  
  283. for(y=0; y < ROW; ++y) {
  284. for(x=0; x < COL; ++x) {
  285. printf("[%d]", S.tableNum[y][x]);
  286. }
  287. printf("\n");
  288. }
  289. printf("Available: %d", S.numAvailTables);
  290. }
  291.  
  292. /***************************************************************************************************************************************
  293. * dispRestaurant() - Partial Code is provided. The function will display all of the details of the restaurant. Specifically, it will *
  294. * display the menu, the seating arrangement, and the current total sales for the day. * *
  295. ***************************************************************************************************************************************/
  296. void dispRestaurant(Restaurant R)
  297. {
  298. int i;
  299. int x, y;
  300. printf("\n*******************************************************************\n\n\t <-- %s Menu -->\n\n",
  301. "Gordon Ramsay's Hell's Kitchen");
  302. printf("*******************************************************************\n\n");
  303. printf("%-10s", "Code");
  304. printf("%-50s", "Name");
  305. printf("%-25s", "Price");
  306.  
  307. /* insert code here */
  308. printf("\n\n*******************************************************************\n");
  309. for(i = 0; i < R.menu.count; ++i){
  310. printf("%-10s%-10s%25.2f", R.menu.meal[i].mealCode, R.menu.meal[i].mealName, R.menu.meal[i].price);
  311. printf("\n");
  312. }
  313.  
  314.  
  315. printf("\n\t\t\t\t=========TABLES==========");
  316. for(x = 0; x < ROW; ++x){
  317. printf("\n\t\t\t\t");
  318. printf("||");
  319. for(y = 0; y < COL; ++y){
  320. printf("[%d]", R.table.tableNum[x][y]);
  321. }
  322. printf("||");
  323. printf("\n");
  324. printf("\t\t\t\t=========================");
  325. }
  326.  
  327. /* insert code here */
  328. printf("\nCurrent Total Sales: %.2f\n", R.sales);
  329. }
  330.  
  331. /***************************************************************************************************************************************
  332. * isVacant() - The function will first check if the passed table number is a valid table number. If it is, the function will then *
  333. * check if the table is unoccupied. If table is unnoccupied, then return 1. Else, return 0. *
  334. ***************************************************************************************************************************************/
  335. int isVacant(Restaurant R, TableDetails tNum)
  336. {
  337. return R.table.tableNum[tNum.row][tNum.col] == 0?1:0;
  338. }
  339.  
  340. /***************************************************************************************************************************************
  341. * purchaseMeal() - The function allows the user to purchase a meal with the passed mealCode, and stay at the passed table number. *
  342. * The function first checks if there are still any available tables in the restaurant. If there are, it then *
  343. * checks if the meal being ordered is on the menu. If the meal is found, the function calls isVacant() to check *
  344. * if the table the user wants to book is unoccupied. If it is, insert a new node after the LAST node of the OrderList.*
  345. * Update the necessary values (tableNum, availTables, sales). *
  346. ***************************************************************************************************************************************/
  347. void purchaseMeal(Restaurant* R, char mealCode[], TableDetails tNum)
  348. {
  349. int i;
  350. OrderList *trav, temp;
  351. if(isVacant(*R, tNum)) {
  352. for(i=0; i<R->menu.count && strcmp(R->menu.meal[i].mealCode, mealCode)!=0; ++i) {}
  353. if(R->menu.count > i) {
  354. for(trav = &(R->orders);
  355. *trav !=NULL ;
  356. trav = &(*trav)->nextOrd) {}
  357. temp = (OrderList) malloc(sizeof(OrderNode));
  358. temp->meal = R->menu.meal[i];
  359. temp->tableNum = tNum;
  360. temp->nextOrd = NULL;
  361. (*trav)->nextOrd = temp;
  362.  
  363. R->sales += R->menu.meal[i].price;
  364. R->table.tableNum[tNum.row][tNum.col] = 1;
  365. R->table.numAvailTables -= 1;
  366. }
  367. }
  368. }
  369.  
  370. /***************************************************************************************************************************************
  371. * deliverMeal() - The function will check if there are any orders in the restaurant's orderlist. If there are, it will delete the *
  372. * FIRST node on the list, and set the tableNum of that order to be 2 (delivered). Also display a message containing *
  373. * the food that has been delivered, and to what tableNumber the food has been delivered to. *
  374. ***************************************************************************************************************************************/
  375. void deliverMeal(Restaurant* R)
  376. {
  377.  
  378. }
  379.  
  380. /***************************************************************************************************************************************
  381. * cleanTables() - The function will check Restaurant's Seating for any tableNum that has a status of 2. *
  382. * The function will then set that table's status back to zero, thus making the table available for occupancy once more.*
  383. * Finally, display a message that the table has been cleaned. Do not forget to update the necessary fields. *
  384. ***************************************************************************************************************************************/
  385. void cleanTables(Restaurant* R)
  386. {
  387.  
  388. }
  389.  
  390. /***************************************************************************************************************************************
  391. * recordSales() - The function will open a file called "sales.txt", which simply contains the previous sales of the restaurant. *
  392. * It will then read that value, and add it to the current sales. The sum is then written back into the file, making *
  393. * sure to OVERWRITE the previous amount on the file. Finally, set the current sales back to 0. *
  394. ***************************************************************************************************************************************/
  395. void recordSales(Restaurant* R)
  396. {
  397.  
  398. }
  399.  
  400. /***************************************************************************************************************************************
  401. * closeRestaurant() - The function will keep calling deliverMeal() until all of the orders have been successfully delivered. *
  402. * After which it will call cleanTables(), and recordSales() to prepare the restaurant for another new day. *
  403. ***************************************************************************************************************************************/
  404. void closeRestaurant(Restaurant* R)
  405. {
  406.  
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement