From a6f62cf7786ad256aa88d4036e79d1c54aa18e2b Mon Sep 17 00:00:00 2001 From: winsdominoes Date: Wed, 18 Sep 2024 14:41:05 +0700 Subject: [PATCH] Modified structs to typedef, documented all functions --- vendorInfo | Bin 17848 -> 17808 bytes vendorInfo.c | 177 +++++++++++++++++++++++++++-------------------- vendorsmall.data | 4 +- 3 files changed, 104 insertions(+), 77 deletions(-) diff --git a/vendorInfo b/vendorInfo index a1d12393ae7188e9ac63ebfbf1142eab2099e32b..2131c0ba473fa87fdce22ca9ec86763a3ba4e454 100755 GIT binary patch delta 1604 zcmZux4@?_n6#u>}1-da>pzQh&cGtof2^A``g5c6la9zyYACU|ZGXjm84Vh33W}8eY zM#9`!9tO>(l5B~KF(%6;lVzsGnM|Ea)VL@ZHZfUZ7j+OL7M<*T-*;EW62IK{-n;jH z@Auxl@9rjs=?GpM=*Zbpk(#V&gfFYIm6lU^)UDl)x&z!u=|;^>8sTkW&RTK0^}uDv z_3cx2W%=Vz9;`pH>HAB{NyCk7fn+ML=DwF^jg3nI62E|Af@LQ5{;jG|B^H~;(g_)y zG)I%rF1o|F1wuk^3{Kus)!00OLqne@JP2io^Ym_PL62qVv3D?LKF9OS=Ozayd(H8G z5Rw>MRMnd=sH&=yGQLyrmFTcdW2NCIuB3|12DD-}SQ724t#Yc*R*-FZC0+z0@}oH< zLWUoa6+1e544-6$`!i^z07bmi7mW*hqo}o2nHt8&hN%>Xy{_>5bF5A zGWHC^?m}$rEf%;tu461vgMs0}3FH`96ngEkse3Vd875yVw1G{K6|lLH$B#lAdx&BF zSW?xI_#@~{WWrIg@|?U8VYOq?y1zRuzvXG`ZQxFnUQb#y%+hCih`@X8)&my2BF@viRfmr^*% z>y?Uo@L~xB#^eC}guBFDn7s_)K?u3Gu~(Kl4YfN^n{XfbT~$X>&)}%`q0Yj=9!7l; z^(Uz3aaJ>^x8Z63i~1C57rtjN;yYJ|y6v{AJ_QSIPwBJBupSYlKEq-`Re?K-J|pwt zD#Bfldme(G689a9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h@8Elow0IEMr%Z zge*e3z+K_`?@Dg0=%iGFOO+dG9NblF6w^M{6_97~%@^V5=4!=d@aD zU8=%Q8tDhFFrn!W{!jk~SGdP#8`H`1;vazsL%KY=25PGX-;=nuVQ)lKbq$*C7WA{d z84FbZ3|3%cJ5!;r@SYAJ$4VSzE+Fw(dg_L8?D(qIajQ*@@;ic*W}tq7p5!`TfX^>D zMzmUWxhj+24`iOj&^XZ|3zL{AEw5(=ABIqEw8n9$eSPQ zqVk2u8}&37e6jCAjJz4*?EJ diff --git a/vendorInfo.c b/vendorInfo.c index 968a7eb..27fa157 100644 --- a/vendorInfo.c +++ b/vendorInfo.c @@ -1,8 +1,8 @@ /* * vendorInfo.c * - * Program that - * + * SEN-102 Intro to Programming - Pre-Assessment + * Program that simulates a market containing vendors. * * Created by Win (Thanawin Pattanaphol), 28th August 2024 */ @@ -16,55 +16,58 @@ #define MAX_FILENAME_LENGTH 4096 /* STRUCTURES */ -struct vendor +typedef struct vendor { char vendor_name[20]; char merchandise[15]; int inventory_count; int item_price_baht; int revenue; - int duplicate; -} vendors[MAX_VENDORS]; + int duplicate; +} vendor; -struct search_r +// Return type for search function +typedef struct search_r { int status; int item_id; -}; +} search_r; -struct purchase_r +// Return type for purchase function +typedef struct purchase_r { int status; char vendor_name[20]; int price_owe_baht; -}; +} purchase_r; + +vendor vendors[MAX_VENDORS]; /* FUNCTION DECLARATION */ -void reset_output(); -int read_input(char str[], size_t n); +int getstr(char str[], size_t n); char *strnlower(char *s, size_t n); -struct search_r search +search_r search ( char query[], - struct vendor vendors[MAX_VENDORS], + vendor vendors[MAX_VENDORS], int total_vendors ); -struct purchase_r purchase +purchase_r purchase ( int item_id, int amount, - struct vendor vendors[MAX_VENDORS] + vendor vendors[MAX_VENDORS] ); -int check_vendors(struct vendor vendors[MAX_VENDORS], int total_vendors); +int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors); -int main() -{ +int main() +{ char file_name[MAX_FILENAME_LENGTH]; printf("Enter name of input file: "); - read_input(file_name, MAX_FILENAME_LENGTH); + getstr(file_name, MAX_FILENAME_LENGTH); FILE *fp; if ((fp = fopen(file_name, "r")) == NULL) @@ -73,15 +76,16 @@ int main() exit(EXIT_FAILURE); } - // calculate file size + // Calculate the file size fseek(fp, 0, SEEK_END); unsigned long file_size = ftell(fp); fseek(fp, 0, SEEK_SET); - // read file to buffer + // Dynamically allocate buffer for file content char *file_buffer = (char *) malloc(file_size); int index = 0; + // Reading the file with fgets and formatting them using strtok while ((fgets(file_buffer, 50, fp) != NULL) && (index <= MAX_VENDORS - 1)) { char *token = strtok(file_buffer, " "); @@ -102,12 +106,20 @@ int main() int total_vendors = index; printf("\n>>> Read %d vendors from file %s\n", total_vendors, file_name); - // print out each vendor's info + // Print out each vendor's info - excluding duplicates. for (int i = 0; i <= (total_vendors - 1); i++) - { + { + /* + This is similar to a linear search algorithm + i = the current item + j = the item next to i + + i is being compared to j: + - if i == j then mark both i and j as duplicates + - if not, move on to the next two items + */ for (int j = i + 1; j < total_vendors; j++) { - if ((strcmp(vendors[i].merchandise, vendors[j].merchandise)) == 0) { vendors[i].duplicate = 1; @@ -139,8 +151,7 @@ int main() } else { - printf("\033[0;31mDuplicate record detected!\n"); - reset_output(); + printf("\e[31m%s\e[m\n", "Duplicate record detected!"); } } @@ -148,7 +159,6 @@ int main() while (1) { - // CHECK IF ALL MERCHANDISE ARE EMPTY if(check_vendors(vendors, total_vendors) == 0) { break; @@ -156,9 +166,12 @@ int main() char user_query[100]; printf("What do you want to buy? (DONE to end) "); - read_input(user_query, 100); + getstr(user_query, 100); - if (strcmp(user_query, "DONE") == 0) + if ( + (strcmp(user_query, "DONE") == 0) || + (strcmp(user_query, "done") == 0) + ) { break; } @@ -179,12 +192,15 @@ int main() char input[10]; printf("How many do you want to buy? "); - read_input(input, 10); + getstr(input, 10); + // If a character of a string was sent, the return value would always be 0 + // If a negative number was sent, the return value will always be negative. + if(atoi(input) > 0) { int item_amount = atoi(input); - struct purchase_r response = purchase(item.item_id, item_amount, vendors); + purchase_r response = purchase(item.item_id, item_amount, vendors); if (response.status == 0) { @@ -195,28 +211,33 @@ int main() } else { - printf("\033[0;31mSorry -- %s does not have enough items available\n", - response.vendor_name + printf("\e[31m%s -- %s %s\e[m\n", + "Sorry", + response.vendor_name, + "does not have enough items available" ); - reset_output(); } } else { - printf("\033[0;31mYou must enter a positive whole number for the number of items\n"); - reset_output(); + printf("\e[31m%s\e[m\n", + "You must enter a positive whole number for the number of items" + ); } } else if (item.status == 2) { - printf("\033[0;31mThis item has duplicate records, please choose another item\n"); - reset_output(); + printf("\e[31m%s\e[m\n", + "This item has duplicate records, please choose another item" + ); } else { - printf("\033[0;31mNo vendor in this market sells %s\n", user_query); - reset_output(); + printf("\e[31m%s %s\e[m\n", + "No vendor in this market sells", + user_query + ); } } } @@ -248,22 +269,16 @@ int main() return 0; }; -/* - Output color reset -*/ -void reset_output() -{ - printf("\033[0m"); -}; - /* - read_input: Custom read input function - str = string itself - n = max amount of characters that the function is able to read into buffer + getstr: Custom read input function - reads one character at a time into buffer and adds a null terminator when finish reading + Parameters: + - str = string itself + - n = max amount of characters that the function is able to read into buffer + + Reads one character at a time into buffer and adds a null terminator when finish reading */ -int read_input(char str[], size_t n) +int getstr(char str[], size_t n) { int ch, i = 0; @@ -285,7 +300,9 @@ int read_input(char str[], size_t n) Parameters: - s: String - n: Length of string - + + Return values: + - (char *): Lowercased version of the string. */ char *strnlower(char *s, size_t n) { @@ -299,17 +316,18 @@ char *strnlower(char *s, size_t n) Search function Parameters: - - query: search query - - vendors: the vendor table - - total_vendors: the amount of vendors + - (string) query: search query + - (vendor) vendors: the vendor table + - (int) total_vendors: the amount of vendors Return values: - - i: Item Number - - 0: No results found + (search_r) response: + - status: 0 (Found item) + - status: 1 (No results) + - status: 2 (Found duplicate item) */ -/* TO-DO IMPLEMENT RESPONSE STRUCT */ -struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int total_vendors) +search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors) { int i; for (i = 0; i < total_vendors; i++) @@ -319,7 +337,7 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot { if (vendors[i].duplicate == 0) { - struct search_r response; + search_r response; response.status = 0; response.item_id = i; @@ -328,7 +346,7 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot } else { - struct search_r response; + search_r response; response.status = 2; return (response); } @@ -337,7 +355,7 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot if (i == total_vendors) { - struct search_r response; + search_r response; response.status = 1; response.item_id = 0; return (response); @@ -345,7 +363,7 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot }; /* - Purchase function + purchase: Purchase function Parameters: - item_id: The item id that the user is purchasing @@ -353,16 +371,19 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot - vendors: vendor table Return values: - - + (purchase_r) response: + - status: 0 (Purchase successful) + - status: 1 (Vendor does not have enough items) */ -struct purchase_r purchase(int item_id, int amount, struct vendor vendors[MAX_VENDORS]) + +purchase_r purchase(int item_id, int amount, vendor vendors[MAX_VENDORS]) { if(amount <= vendors[item_id].inventory_count) { // Decrease vendor inventory vendors[item_id].inventory_count = vendors[item_id].inventory_count - amount; - struct purchase_r response; + purchase_r response; response.status = 0; strcpy(response.vendor_name, vendors[item_id].vendor_name); @@ -370,11 +391,11 @@ struct purchase_r purchase(int item_id, int amount, struct vendor vendors[MAX_VE vendors[item_id].revenue = vendors[item_id].item_price_baht * amount; - return (response); + return response; } else { - struct purchase_r response; + purchase_r response; response.status = 1; strcpy(response.vendor_name, vendors[item_id].vendor_name); @@ -383,23 +404,29 @@ struct purchase_r purchase(int item_id, int amount, struct vendor vendors[MAX_VE }; /* - Check function + check_vendors: Custom function to check if all vendors are empty. Parameters: + - vendors[MAX_VENDORS]: (vendor) All of the vendors. + - total_vendors: (integer) Total vendors in market. + + Return values: + - (int) 0: All vendors are empty. + - (int) 1: There are still items left. */ -int check_vendors(struct vendor vendors[MAX_VENDORS], int total_vendors) + +int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors) { int i; for (i = 0; i < total_vendors; i++) - { - // If there are any items left + { + // Condition: Vendors that are not empty (Check if not marked as duplicates) if (vendors[i].inventory_count != 0 && vendors[i].duplicate == 0) { return 1; } } - // There aren't any if (i == total_vendors) { return 0; diff --git a/vendorsmall.data b/vendorsmall.data index c89a4b9..710411e 100644 --- a/vendorsmall.data +++ b/vendorsmall.data @@ -1,4 +1,4 @@ Win phone 1 50 -Win books 1 70 -Win books 5 100 +Peter books 1 70 +Amy teeshirts 5 100 Deez computers 5 80