From aacbd56e5cffd802948761b6414098b90710ad67 Mon Sep 17 00:00:00 2001 From: Rory Healy Date: Sat, 23 May 2020 23:19:05 +1000 Subject: [PATCH] Stage 1 initial (w/ memory leaks) in test.c --- src/main.c | 9 +---- src/test.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/test.c diff --git a/src/main.c b/src/main.c index 9abd710..0adff9f 100644 --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,7 @@ #include #include +#include /* constants */ #define STAGE_FORMATTING_STRING "=========================" @@ -77,14 +78,6 @@ int main(int argc, char *argv[]) { /* holds all the credit cards */ credit_card_t all_cards[MAX_NUM_CARDS]; - /* holds all the transactions */ - transaction_t all_transactions[MAX_NUM_CARDS]; - /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - * Dynamic memory allocation required as - * size of all_transactions isn't known - * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - */ - /* stage 1: reading one credit card record */ stage_one(); diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..e871dbf --- /dev/null +++ b/src/test.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include + +#define CREDIT_CARD_ID_LEN 8 + +/* structure typedefs */ +typedef struct { + char card_id[CREDIT_CARD_ID_LEN]; + int daily_lim; + int trans_lim; +} credit_card_t; + +typedef struct { + char* str; +} words_t; + +/* function prototypes */ +void stage_one(credit_card_t* all_cards, int* num_cards); +void read_one_card(credit_card_t* one_card); + +int main(int argc, char *argv[]) { + /* all_cards will store all credit card information */ + credit_card_t* all_cards = (credit_card_t*)malloc(sizeof(credit_card_t)); + int num_cards = 0; + stage_one(all_cards, &num_cards); + + free(all_cards); + all_cards = NULL; + return 0; +} + +void stage_one(credit_card_t* all_cards, int* num_cards) { + printf("=========================Stage 1=========================\n"); + credit_card_t curr_card; + read_one_card(&curr_card); + printf("Card ID: %s\n", curr_card.card_id); + printf("Daily limit: %d\n", curr_card.daily_lim); + printf("Transaction limit: %d\n", curr_card.trans_lim); + printf("\n"); +} + +/* read a line of input credit cards into one_card */ +void +read_one_card(credit_card_t* one_card) { + int i = 0, c; + + /* records the length of the current field */ + int index = 1; + char card_id_str[CREDIT_CARD_ID_LEN]; + char* daily_lim_str = (char*)malloc(index * sizeof(char) + 1); + char* trans_lim_str = (char*)malloc(index * sizeof(char) + 1); + assert(daily_lim_str != NULL && trans_lim_str != NULL); + + /* read the card ID from the input line */ + while (((c = getchar()) != EOF) && (c != '\n') && (c != '\r')) { + if (i < CREDIT_CARD_ID_LEN) { + card_id_str[i++] = c; + index += 1; + } else { + break; + } + } + card_id_str[i] = '\0'; + + /* copy the card id to one_card */ + strncpy(one_card->card_id, card_id_str, CREDIT_CARD_ID_LEN); + one_card->card_id[CREDIT_CARD_ID_LEN] = '\0'; + + /* read the daily limit from the input line */ + i = 0; + while (((c = getchar()) != EOF) && (c != '\n') && (c != '\r')) { + if (c != ' ') { + daily_lim_str[i++] = c; + index += 1; + } else { + break; + } + } + daily_lim_str[i] = '\0'; + one_card->daily_lim = atoi(daily_lim_str); + + /* read the transaction limit from the input line */ + i = 0; + while (((c = getchar()) != EOF) && (c != '\n') && (c != '\r')) { + trans_lim_str[i++] = c; + index += 1; + } + trans_lim_str[i] = '\0'; + one_card->trans_lim = atoi(trans_lim_str); + + free(daily_lim_str); + free(trans_lim_str); + daily_lim_str = trans_lim_str = NULL; +} \ No newline at end of file