re-formatted code
This commit is contained in:
parent
a90038c80e
commit
db321f132b
BIN
#vendorInfo#
BIN
#vendorInfo#
Binary file not shown.
|
@ -15,7 +15,7 @@
|
|||
#define MAX_VENDORS 20
|
||||
#define MAX_FILENAME_LENGTH 4096
|
||||
|
||||
/* STRUCTURES */
|
||||
/* STRUCTURE DECLARATION */
|
||||
typedef struct vendor
|
||||
{
|
||||
char vendor_name[20];
|
||||
|
@ -41,8 +41,11 @@ typedef struct purchase_r
|
|||
int price_owe_baht;
|
||||
} purchase_r;
|
||||
|
||||
// The global vendors array
|
||||
vendor vendors[MAX_VENDORS];
|
||||
|
||||
/* END OF STRUCTURE DECLARATION*/
|
||||
|
||||
/* FUNCTION DECLARATION */
|
||||
int getstr(char str[], size_t n);
|
||||
char *strnlower(char *s, size_t n);
|
||||
|
@ -62,9 +65,10 @@ purchase_r purchase
|
|||
);
|
||||
|
||||
int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors);
|
||||
/* END OF FUNCTION DECLARATION */
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
char file_name[MAX_FILENAME_LENGTH];
|
||||
printf("Enter name of input file: ");
|
||||
getstr(file_name, MAX_FILENAME_LENGTH);
|
||||
|
@ -72,8 +76,8 @@ int main()
|
|||
FILE *fp;
|
||||
if ((fp = fopen(file_name, "r")) == NULL)
|
||||
{
|
||||
printf("%s cannot be opened.\n", file_name);
|
||||
exit(EXIT_FAILURE);
|
||||
perror("Error opening file");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Calculate the file size
|
||||
|
@ -83,10 +87,14 @@ int main()
|
|||
|
||||
// Dynamically allocate buffer for file content
|
||||
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
|
||||
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, " ");
|
||||
strncpy(vendors[index].vendor_name, token, strlen(token) + 1);
|
||||
|
@ -103,11 +111,17 @@ int main()
|
|||
index++;
|
||||
}
|
||||
|
||||
// Error handling
|
||||
if (ferror(fp))
|
||||
{
|
||||
printf("");
|
||||
perror("An error occured while reading the file.\n");
|
||||
|
||||
fclose(fp);
|
||||
free(file_buffer);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int total_vendors = index;
|
||||
printf("\n>>> Read %d vendors from file %s\n", total_vendors, file_name);
|
||||
|
||||
|
@ -125,7 +139,11 @@ int main()
|
|||
*/
|
||||
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;
|
||||
}
|
||||
|
@ -185,25 +203,25 @@ int main()
|
|||
}
|
||||
else
|
||||
{
|
||||
// convert string to lowercase - each character one by one
|
||||
int user_query_length = strlen(user_query);
|
||||
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)
|
||||
{
|
||||
char input[10];
|
||||
|
||||
printf("How many do you want to buy? ");
|
||||
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) // atoi returns 0 if character was sent
|
||||
{ // atoi returns -x if -x was provided
|
||||
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)
|
||||
{
|
||||
|
@ -224,7 +242,8 @@ int main()
|
|||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
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
|
||||
|
@ -277,9 +297,14 @@ int main()
|
|||
|
||||
Parameters:
|
||||
- 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)
|
||||
{
|
||||
|
@ -325,9 +350,9 @@ char *strnlower(char *s, size_t n)
|
|||
|
||||
Return values:
|
||||
(search_r) response:
|
||||
- status: 0 (Found item)
|
||||
- status: 0 (Found record) + id of record
|
||||
- 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)
|
||||
|
@ -376,6 +401,7 @@ search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors)
|
|||
Return values:
|
||||
(purchase_r) response:
|
||||
- status: 0 (Purchase successful)
|
||||
Also includes the vendor_name, price owe and revenue
|
||||
- status: 1 (Vendor does not have enough items)
|
||||
*/
|
||||
|
||||
|
@ -423,7 +449,8 @@ int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors)
|
|||
int 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)
|
||||
{
|
||||
return 1;
|
||||
|
@ -435,3 +462,4 @@ int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors)
|
|||
return 0;
|
||||
}
|
||||
};
|
||||
;
|
||||
|
|
BIN
vendorInfo
BIN
vendorInfo
Binary file not shown.
73
vendorInfo.c
73
vendorInfo.c
|
@ -15,7 +15,7 @@
|
|||
#define MAX_VENDORS 20
|
||||
#define MAX_FILENAME_LENGTH 4096
|
||||
|
||||
/* STRUCTURES */
|
||||
/* STRUCTURE DECLARATION */
|
||||
typedef struct vendor
|
||||
{
|
||||
char vendor_name[20];
|
||||
|
@ -41,8 +41,11 @@ typedef struct purchase_r
|
|||
int price_owe_baht;
|
||||
} purchase_r;
|
||||
|
||||
// The global vendors array
|
||||
vendor vendors[MAX_VENDORS];
|
||||
|
||||
/* END OF STRUCTURE DECLARATION*/
|
||||
|
||||
/* FUNCTION DECLARATION */
|
||||
int getstr(char str[], size_t n);
|
||||
char *strnlower(char *s, size_t n);
|
||||
|
@ -62,9 +65,10 @@ purchase_r purchase
|
|||
);
|
||||
|
||||
int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors);
|
||||
/* END OF FUNCTION DECLARATION */
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
char file_name[MAX_FILENAME_LENGTH];
|
||||
printf("Enter name of input file: ");
|
||||
getstr(file_name, MAX_FILENAME_LENGTH);
|
||||
|
@ -73,7 +77,7 @@ int main()
|
|||
if ((fp = fopen(file_name, "r")) == NULL)
|
||||
{
|
||||
perror("Error opening file");
|
||||
exit(EXIT_FAILURE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Calculate the file size
|
||||
|
@ -83,10 +87,14 @@ int main()
|
|||
|
||||
// Dynamically allocate buffer for file content
|
||||
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
|
||||
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, " ");
|
||||
strncpy(vendors[index].vendor_name, token, strlen(token) + 1);
|
||||
|
@ -106,7 +114,12 @@ int main()
|
|||
// Error handling
|
||||
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;
|
||||
|
@ -126,12 +139,15 @@ int main()
|
|||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -186,25 +202,25 @@ int main()
|
|||
}
|
||||
else
|
||||
{
|
||||
// convert string to lowercase - each character one by one
|
||||
int user_query_length = strlen(user_query);
|
||||
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)
|
||||
{
|
||||
char input[10];
|
||||
|
||||
printf("How many do you want to buy? ");
|
||||
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) // atoi returns 0 if character was sent
|
||||
{ // atoi returns -x if -x was provided
|
||||
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)
|
||||
{
|
||||
|
@ -225,7 +241,8 @@ int main()
|
|||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
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
|
||||
|
@ -278,9 +296,14 @@ int main()
|
|||
|
||||
Parameters:
|
||||
- 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)
|
||||
{
|
||||
|
@ -326,9 +349,9 @@ char *strnlower(char *s, size_t n)
|
|||
|
||||
Return values:
|
||||
(search_r) response:
|
||||
- status: 0 (Found item)
|
||||
- status: 0 (Found record) + id of record
|
||||
- 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)
|
||||
|
@ -377,6 +400,7 @@ search_r search(char query[], vendor vendors[MAX_VENDORS], int total_vendors)
|
|||
Return values:
|
||||
(purchase_r) response:
|
||||
- status: 0 (Purchase successful)
|
||||
Also includes the vendor_name, price owe and revenue
|
||||
- status: 1 (Vendor does not have enough items)
|
||||
*/
|
||||
|
||||
|
@ -424,7 +448,8 @@ int check_vendors(vendor vendors[MAX_VENDORS], int total_vendors)
|
|||
int 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)
|
||||
{
|
||||
return 1;
|
||||
|
|
Loading…
Reference in New Issue