re-formatted code

This commit is contained in:
Win 2024-09-19 18:09:43 +07:00
parent a90038c80e
commit db321f132b
4 changed files with 102 additions and 49 deletions

Binary file not shown.

View File

@ -15,7 +15,7 @@
#define MAX_VENDORS 20 #define MAX_VENDORS 20
#define MAX_FILENAME_LENGTH 4096 #define MAX_FILENAME_LENGTH 4096
/* STRUCTURES */ /* STRUCTURE DECLARATION */
typedef struct vendor typedef struct vendor
{ {
char vendor_name[20]; char vendor_name[20];
@ -41,8 +41,11 @@ typedef struct purchase_r
int price_owe_baht; int price_owe_baht;
} purchase_r; } purchase_r;
// The global vendors array
vendor vendors[MAX_VENDORS]; vendor vendors[MAX_VENDORS];
/* END OF STRUCTURE DECLARATION*/
/* FUNCTION DECLARATION */ /* FUNCTION DECLARATION */
int getstr(char str[], size_t n); int getstr(char str[], size_t n);
char *strnlower(char *s, size_t n); char *strnlower(char *s, size_t n);
@ -62,6 +65,7 @@ purchase_r purchase
); );
int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors); int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors);
/* END OF FUNCTION DECLARATION */
int main() int main()
{ {
@ -72,8 +76,8 @@ int main()
FILE *fp; FILE *fp;
if ((fp = fopen(file_name, "r")) == NULL) if ((fp = fopen(file_name, "r")) == NULL)
{ {
printf("%s cannot be opened.\n", file_name); perror("Error opening file");
exit(EXIT_FAILURE); return 1;
} }
// Calculate the file size // Calculate the file size
@ -83,10 +87,14 @@ int main()
// Dynamically allocate buffer for file content // 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; // The index for the while loop below.
// Reading the file with fgets and formatting them using strtok // 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, " ");
strncpy(vendors[index].vendor_name, token, strlen(token) + 1); strncpy(vendors[index].vendor_name, token, strlen(token) + 1);
@ -103,9 +111,15 @@ int main()
index++; index++;
} }
// Error handling
if (ferror(fp)) if (ferror(fp))
{ {
printf(""); perror("An error occured while reading the file.\n");
fclose(fp);
free(file_buffer);
return 1;
} }
int total_vendors = index; int total_vendors = index;
@ -125,7 +139,11 @@ int main()
*/ */
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[j].duplicate = 1; vendors[j].duplicate = 1;
} }
@ -185,11 +203,12 @@ int main()
} }
else else
{ {
// convert string to lowercase - each character one by one
int user_query_length = strlen(user_query); int user_query_length = strlen(user_query);
strnlower(user_query, user_query_length); strnlower(user_query, user_query_length);
struct search_r item = search(user_query, vendors, total_vendors); struct search_r item = search(user_query,
vendors,
total_vendors);
if(item.status == 0) if(item.status == 0)
{ {
char input[10]; char input[10];
@ -197,13 +216,12 @@ int main()
printf("How many do you want to buy? "); printf("How many do you want to buy? ");
getstr(input, 10); getstr(input, 10);
// If a character of a string was sent, the return value would always be 0 if(atoi(input) > 0) // atoi returns 0 if character was sent
// If a negative number was sent, the return value will always be negative. { // atoi returns -x if -x was provided
if(atoi(input) > 0)
{
int item_amount = atoi(input); int item_amount = atoi(input);
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)
{ {
@ -224,7 +242,8 @@ int main()
else else
{ {
printf("\e[31m%s\e[m\n", printf("\e[31m%s\e[m\n",
"You must enter a positive whole number for the number of items" "You must enter a positive whole number"
" for the number of items"
); );
} }
@ -232,7 +251,8 @@ int main()
else if (item.status == 2) else if (item.status == 2)
{ {
printf("\e[31m%s\e[m\n", printf("\e[31m%s\e[m\n",
"This item has duplicate records, please choose another item" "This item has duplicate records,"
" please choose another item"
); );
} }
else else
@ -277,9 +297,14 @@ int main()
Parameters: Parameters:
- str = string itself - str = string itself
- n = max amount of characters that the function is able to read into buffer - 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
Return values:
- (int) i: length of string
*/ */
int getstr(char str[], size_t n) int getstr(char str[], size_t n)
{ {
@ -325,9 +350,9 @@ char *strnlower(char *s, size_t n)
Return values: Return values:
(search_r) response: (search_r) response:
- status: 0 (Found item) - status: 0 (Found record) + id of record
- status: 1 (No results) - status: 1 (No results)
- status: 2 (Found duplicate item) - status: 2 (Found duplicate record)
*/ */
search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors) search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors)
@ -376,6 +401,7 @@ search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors)
Return values: Return values:
(purchase_r) response: (purchase_r) response:
- status: 0 (Purchase successful) - status: 0 (Purchase successful)
Also includes the vendor_name, price owe and revenue
- status: 1 (Vendor does not have enough items) - status: 1 (Vendor does not have enough items)
*/ */
@ -423,7 +449,8 @@ 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++)
{ {
// Condition: Vendors that are not empty (Check if not marked as duplicates) // Condition: Vendors that are not empty
// (only ones that are not 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;
@ -435,3 +462,4 @@ int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors)
return 0; return 0;
} }
}; };
;

Binary file not shown.

View File

@ -15,7 +15,7 @@
#define MAX_VENDORS 20 #define MAX_VENDORS 20
#define MAX_FILENAME_LENGTH 4096 #define MAX_FILENAME_LENGTH 4096
/* STRUCTURES */ /* STRUCTURE DECLARATION */
typedef struct vendor typedef struct vendor
{ {
char vendor_name[20]; char vendor_name[20];
@ -41,8 +41,11 @@ typedef struct purchase_r
int price_owe_baht; int price_owe_baht;
} purchase_r; } purchase_r;
// The global vendors array
vendor vendors[MAX_VENDORS]; vendor vendors[MAX_VENDORS];
/* END OF STRUCTURE DECLARATION*/
/* FUNCTION DECLARATION */ /* FUNCTION DECLARATION */
int getstr(char str[], size_t n); int getstr(char str[], size_t n);
char *strnlower(char *s, size_t n); char *strnlower(char *s, size_t n);
@ -62,6 +65,7 @@ purchase_r purchase
); );
int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors); int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors);
/* END OF FUNCTION DECLARATION */
int main() int main()
{ {
@ -73,7 +77,7 @@ int main()
if ((fp = fopen(file_name, "r")) == NULL) if ((fp = fopen(file_name, "r")) == NULL)
{ {
perror("Error opening file"); perror("Error opening file");
exit(EXIT_FAILURE); return 1;
} }
// Calculate the file size // Calculate the file size
@ -83,10 +87,14 @@ int main()
// Dynamically allocate buffer for file content // 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; // The index for the while loop below.
// Reading the file with fgets and formatting them using strtok // 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, " ");
strncpy(vendors[index].vendor_name, token, strlen(token) + 1); strncpy(vendors[index].vendor_name, token, strlen(token) + 1);
@ -106,7 +114,12 @@ int main()
// Error handling // Error handling
if (ferror(fp)) if (ferror(fp))
{ {
printf("An error occured while reading the file.\n"); perror("An error occured while reading the file.\n");
fclose(fp);
free(file_buffer);
return 1;
} }
int total_vendors = index; int total_vendors = index;
@ -126,12 +139,15 @@ int main()
*/ */
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[j].duplicate = 1; vendors[j].duplicate = 1;
} }
if ((strcmp(vendors[i].vendor_name, vendors[j].vendor_name)) == 0) if ((strcmp(vendors[i].vendor_name,
vendors[j].vendor_name)) == 0)
{ {
vendors[j].duplicate = 1; vendors[j].duplicate = 1;
} }
@ -186,11 +202,12 @@ int main()
} }
else else
{ {
// convert string to lowercase - each character one by one
int user_query_length = strlen(user_query); int user_query_length = strlen(user_query);
strnlower(user_query, user_query_length); strnlower(user_query, user_query_length);
struct search_r item = search(user_query, vendors, total_vendors); struct search_r item = search(user_query,
vendors,
total_vendors);
if(item.status == 0) if(item.status == 0)
{ {
char input[10]; char input[10];
@ -198,13 +215,12 @@ int main()
printf("How many do you want to buy? "); printf("How many do you want to buy? ");
getstr(input, 10); getstr(input, 10);
// If a character of a string was sent, the return value would always be 0 if(atoi(input) > 0) // atoi returns 0 if character was sent
// If a negative number was sent, the return value will always be negative. { // atoi returns -x if -x was provided
if(atoi(input) > 0)
{
int item_amount = atoi(input); int item_amount = atoi(input);
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)
{ {
@ -225,7 +241,8 @@ int main()
else else
{ {
printf("\e[31m%s\e[m\n", printf("\e[31m%s\e[m\n",
"You must enter a positive whole number for the number of items" "You must enter a positive whole number"
" for the number of items"
); );
} }
@ -233,7 +250,8 @@ int main()
else if (item.status == 2) else if (item.status == 2)
{ {
printf("\e[31m%s\e[m\n", printf("\e[31m%s\e[m\n",
"This item has duplicate records, please choose another item" "This item has duplicate records,"
" please choose another item"
); );
} }
else else
@ -278,9 +296,14 @@ int main()
Parameters: Parameters:
- str = string itself - str = string itself
- n = max amount of characters that the function is able to read into buffer - 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
Return values:
- (int) i: length of string
*/ */
int getstr(char str[], size_t n) int getstr(char str[], size_t n)
{ {
@ -326,9 +349,9 @@ char *strnlower(char *s, size_t n)
Return values: Return values:
(search_r) response: (search_r) response:
- status: 0 (Found item) - status: 0 (Found record) + id of record
- status: 1 (No results) - status: 1 (No results)
- status: 2 (Found duplicate item) - status: 2 (Found duplicate record)
*/ */
search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors) search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors)
@ -377,6 +400,7 @@ search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors)
Return values: Return values:
(purchase_r) response: (purchase_r) response:
- status: 0 (Purchase successful) - status: 0 (Purchase successful)
Also includes the vendor_name, price owe and revenue
- status: 1 (Vendor does not have enough items) - status: 1 (Vendor does not have enough items)
*/ */
@ -424,7 +448,8 @@ 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++)
{ {
// Condition: Vendors that are not empty (Check if not marked as duplicates) // Condition: Vendors that are not empty
// (only ones that are not 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;