Modified structs to typedef, documented all functions
This commit is contained in:
parent
a119eac168
commit
a6f62cf778
BIN
vendorInfo
BIN
vendorInfo
Binary file not shown.
177
vendorInfo.c
177
vendorInfo.c
|
@ -1,8 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* vendorInfo.c
|
* 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
|
* Created by Win (Thanawin Pattanaphol), 28th August 2024
|
||||||
*/
|
*/
|
||||||
|
@ -16,55 +16,58 @@
|
||||||
#define MAX_FILENAME_LENGTH 4096
|
#define MAX_FILENAME_LENGTH 4096
|
||||||
|
|
||||||
/* STRUCTURES */
|
/* STRUCTURES */
|
||||||
struct vendor
|
typedef struct vendor
|
||||||
{
|
{
|
||||||
char vendor_name[20];
|
char vendor_name[20];
|
||||||
char merchandise[15];
|
char merchandise[15];
|
||||||
int inventory_count;
|
int inventory_count;
|
||||||
int item_price_baht;
|
int item_price_baht;
|
||||||
int revenue;
|
int revenue;
|
||||||
int duplicate;
|
int duplicate;
|
||||||
} vendors[MAX_VENDORS];
|
} vendor;
|
||||||
|
|
||||||
struct search_r
|
// Return type for search function
|
||||||
|
typedef struct search_r
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
int item_id;
|
int item_id;
|
||||||
};
|
} search_r;
|
||||||
|
|
||||||
struct purchase_r
|
// Return type for purchase function
|
||||||
|
typedef struct purchase_r
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
char vendor_name[20];
|
char vendor_name[20];
|
||||||
int price_owe_baht;
|
int price_owe_baht;
|
||||||
};
|
} purchase_r;
|
||||||
|
|
||||||
|
vendor vendors[MAX_VENDORS];
|
||||||
|
|
||||||
/* FUNCTION DECLARATION */
|
/* FUNCTION DECLARATION */
|
||||||
void reset_output();
|
int getstr(char str[], size_t n);
|
||||||
int read_input(char str[], size_t n);
|
|
||||||
char *strnlower(char *s, size_t n);
|
char *strnlower(char *s, size_t n);
|
||||||
|
|
||||||
struct search_r search
|
search_r search
|
||||||
(
|
(
|
||||||
char query[],
|
char query[],
|
||||||
struct vendor vendors[MAX_VENDORS],
|
vendor vendors[MAX_VENDORS],
|
||||||
int total_vendors
|
int total_vendors
|
||||||
);
|
);
|
||||||
|
|
||||||
struct purchase_r purchase
|
purchase_r purchase
|
||||||
(
|
(
|
||||||
int item_id,
|
int item_id,
|
||||||
int amount,
|
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];
|
char file_name[MAX_FILENAME_LENGTH];
|
||||||
printf("Enter name of input file: ");
|
printf("Enter name of input file: ");
|
||||||
read_input(file_name, MAX_FILENAME_LENGTH);
|
getstr(file_name, MAX_FILENAME_LENGTH);
|
||||||
|
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
if ((fp = fopen(file_name, "r")) == NULL)
|
if ((fp = fopen(file_name, "r")) == NULL)
|
||||||
|
@ -73,15 +76,16 @@ int main()
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculate file size
|
// Calculate the file size
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, 0, SEEK_END);
|
||||||
unsigned long file_size = ftell(fp);
|
unsigned long file_size = ftell(fp);
|
||||||
fseek(fp, 0, SEEK_SET);
|
fseek(fp, 0, SEEK_SET);
|
||||||
|
|
||||||
// read file to buffer
|
// Dynamically allocate buffer for file content
|
||||||
char *file_buffer = (char *) malloc(file_size);
|
char *file_buffer = (char *) malloc(file_size);
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
|
// Reading the file with fgets and formatting them using strtok
|
||||||
while ((fgets(file_buffer, 50, fp) != NULL) && (index <= MAX_VENDORS - 1))
|
while ((fgets(file_buffer, 50, fp) != NULL) && (index <= MAX_VENDORS - 1))
|
||||||
{
|
{
|
||||||
char *token = strtok(file_buffer, " ");
|
char *token = strtok(file_buffer, " ");
|
||||||
|
@ -102,12 +106,20 @@ int main()
|
||||||
int total_vendors = index;
|
int total_vendors = index;
|
||||||
printf("\n>>> Read %d vendors from file %s\n", total_vendors, file_name);
|
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++)
|
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++)
|
for (int j = i + 1; j < total_vendors; j++)
|
||||||
{
|
{
|
||||||
|
|
||||||
if ((strcmp(vendors[i].merchandise, vendors[j].merchandise)) == 0)
|
if ((strcmp(vendors[i].merchandise, vendors[j].merchandise)) == 0)
|
||||||
{
|
{
|
||||||
vendors[i].duplicate = 1;
|
vendors[i].duplicate = 1;
|
||||||
|
@ -139,8 +151,7 @@ int main()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("\033[0;31mDuplicate record detected!\n");
|
printf("\e[31m%s\e[m\n", "Duplicate record detected!");
|
||||||
reset_output();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +159,6 @@ int main()
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
// CHECK IF ALL MERCHANDISE ARE EMPTY
|
|
||||||
if(check_vendors(vendors, total_vendors) == 0)
|
if(check_vendors(vendors, total_vendors) == 0)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
|
@ -156,9 +166,12 @@ int main()
|
||||||
|
|
||||||
char user_query[100];
|
char user_query[100];
|
||||||
printf("What do you want to buy? (DONE to end) ");
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -179,12 +192,15 @@ int main()
|
||||||
char input[10];
|
char input[10];
|
||||||
|
|
||||||
printf("How many do you want to buy? ");
|
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)
|
if(atoi(input) > 0)
|
||||||
{
|
{
|
||||||
int item_amount = atoi(input);
|
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)
|
if (response.status == 0)
|
||||||
{
|
{
|
||||||
|
@ -195,28 +211,33 @@ int main()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("\033[0;31mSorry -- %s does not have enough items available\n",
|
printf("\e[31m%s -- %s %s\e[m\n",
|
||||||
response.vendor_name
|
"Sorry",
|
||||||
|
response.vendor_name,
|
||||||
|
"does not have enough items available"
|
||||||
);
|
);
|
||||||
reset_output();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("\033[0;31mYou must enter a positive whole number for the number of items\n");
|
printf("\e[31m%s\e[m\n",
|
||||||
reset_output();
|
"You must enter a positive whole number for the number of items"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (item.status == 2)
|
else if (item.status == 2)
|
||||||
{
|
{
|
||||||
printf("\033[0;31mThis item has duplicate records, please choose another item\n");
|
printf("\e[31m%s\e[m\n",
|
||||||
reset_output();
|
"This item has duplicate records, please choose another item"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("\033[0;31mNo vendor in this market sells %s\n", user_query);
|
printf("\e[31m%s %s\e[m\n",
|
||||||
reset_output();
|
"No vendor in this market sells",
|
||||||
|
user_query
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -248,22 +269,16 @@ int main()
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
Output color reset
|
|
||||||
*/
|
|
||||||
void reset_output()
|
|
||||||
{
|
|
||||||
printf("\033[0m");
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
read_input: Custom read input function
|
getstr: Custom read input function
|
||||||
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
|
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;
|
int ch, i = 0;
|
||||||
|
|
||||||
|
@ -285,7 +300,9 @@ int read_input(char str[], size_t n)
|
||||||
Parameters:
|
Parameters:
|
||||||
- s: String
|
- s: String
|
||||||
- n: Length of string
|
- n: Length of string
|
||||||
|
|
||||||
|
Return values:
|
||||||
|
- (char *): Lowercased version of the string.
|
||||||
*/
|
*/
|
||||||
char *strnlower(char *s, size_t n)
|
char *strnlower(char *s, size_t n)
|
||||||
{
|
{
|
||||||
|
@ -299,17 +316,18 @@ char *strnlower(char *s, size_t n)
|
||||||
Search function
|
Search function
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
- query: search query
|
- (string) query: search query
|
||||||
- vendors: the vendor table
|
- (vendor) vendors: the vendor table
|
||||||
- total_vendors: the amount of vendors
|
- (int) total_vendors: the amount of vendors
|
||||||
|
|
||||||
Return values:
|
Return values:
|
||||||
- i: Item Number
|
(search_r) response:
|
||||||
- 0: No results found
|
- status: 0 (Found item)
|
||||||
|
- status: 1 (No results)
|
||||||
|
- status: 2 (Found duplicate item)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* TO-DO IMPLEMENT RESPONSE STRUCT */
|
search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors)
|
||||||
struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int total_vendors)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < total_vendors; 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)
|
if (vendors[i].duplicate == 0)
|
||||||
{
|
{
|
||||||
struct search_r response;
|
search_r response;
|
||||||
response.status = 0;
|
response.status = 0;
|
||||||
response.item_id = i;
|
response.item_id = i;
|
||||||
|
|
||||||
|
@ -328,7 +346,7 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
struct search_r response;
|
search_r response;
|
||||||
response.status = 2;
|
response.status = 2;
|
||||||
return (response);
|
return (response);
|
||||||
}
|
}
|
||||||
|
@ -337,7 +355,7 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot
|
||||||
|
|
||||||
if (i == total_vendors)
|
if (i == total_vendors)
|
||||||
{
|
{
|
||||||
struct search_r response;
|
search_r response;
|
||||||
response.status = 1;
|
response.status = 1;
|
||||||
response.item_id = 0;
|
response.item_id = 0;
|
||||||
return (response);
|
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:
|
Parameters:
|
||||||
- item_id: The item id that the user is purchasing
|
- 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
|
- vendors: vendor table
|
||||||
|
|
||||||
Return values:
|
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)
|
if(amount <= vendors[item_id].inventory_count)
|
||||||
{
|
{
|
||||||
// Decrease vendor inventory
|
// Decrease vendor inventory
|
||||||
vendors[item_id].inventory_count = vendors[item_id].inventory_count - amount;
|
vendors[item_id].inventory_count = vendors[item_id].inventory_count - amount;
|
||||||
|
|
||||||
struct purchase_r response;
|
purchase_r response;
|
||||||
|
|
||||||
response.status = 0;
|
response.status = 0;
|
||||||
strcpy(response.vendor_name, vendors[item_id].vendor_name);
|
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;
|
vendors[item_id].revenue = vendors[item_id].item_price_baht * amount;
|
||||||
|
|
||||||
return (response);
|
return response;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
struct purchase_r response;
|
purchase_r response;
|
||||||
response.status = 1;
|
response.status = 1;
|
||||||
strcpy(response.vendor_name, vendors[item_id].vendor_name);
|
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:
|
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;
|
int i;
|
||||||
for (i = 0; i < total_vendors; 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)
|
if (vendors[i].inventory_count != 0 && vendors[i].duplicate == 0)
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// There aren't any
|
|
||||||
if (i == total_vendors)
|
if (i == total_vendors)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
Win phone 1 50
|
Win phone 1 50
|
||||||
Win books 1 70
|
Peter books 1 70
|
||||||
Win books 5 100
|
Amy teeshirts 5 100
|
||||||
Deez computers 5 80
|
Deez computers 5 80
|
||||||
|
|
Loading…
Reference in New Issue