Modified structs to typedef, documented all functions
This commit is contained in:
parent
a119eac168
commit
a6f62cf778
BIN
vendorInfo
BIN
vendorInfo
Binary file not shown.
163
vendorInfo.c
163
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,7 +16,7 @@
|
|||
#define MAX_FILENAME_LENGTH 4096
|
||||
|
||||
/* STRUCTURES */
|
||||
struct vendor
|
||||
typedef struct vendor
|
||||
{
|
||||
char vendor_name[20];
|
||||
char merchandise[15];
|
||||
|
@ -24,47 +24,50 @@ struct vendor
|
|||
int item_price_baht;
|
||||
int revenue;
|
||||
int duplicate;
|
||||
} vendors[MAX_VENDORS];
|
||||
} 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()
|
||||
{
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -249,21 +270,15 @@ int main()
|
|||
};
|
||||
|
||||
/*
|
||||
Output color reset
|
||||
*/
|
||||
void reset_output()
|
||||
{
|
||||
printf("\033[0m");
|
||||
};
|
||||
getstr: Custom read input function
|
||||
|
||||
/*
|
||||
read_input: Custom read input function
|
||||
str = string itself
|
||||
n = max amount of characters that the function is able to read into buffer
|
||||
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
|
||||
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;
|
||||
|
||||
|
@ -286,6 +301,8 @@ int read_input(char str[], size_t n)
|
|||
- 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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue