Cleaning things up for final submission
This commit is contained in:
parent
b5bbc46ad3
commit
a82174d10c
4 changed files with 14 additions and 219 deletions
|
@ -1,66 +0,0 @@
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
void read_one_line(char* rtn_line);
|
|
||||||
int is_seperating_line(char* one_line);
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
char* seperating_line = "%%%%%%%%%%";
|
|
||||||
printf("Seperating line:\n%s\n", seperating_line);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
char* a_line = (char*)malloc(sizeof(char));
|
|
||||||
read_one_line(a_line);
|
|
||||||
|
|
||||||
while (!is_seperating_line(a_line)) {
|
|
||||||
printf("line %d:\n%s\n\n", i++, a_line);
|
|
||||||
read_one_line(a_line);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* checks if the current message is a seperating line */
|
|
||||||
int
|
|
||||||
is_seperating_line(char* one_line) {
|
|
||||||
char* seperating_line = "%%%%%%%%%%";
|
|
||||||
if (!strncmp(one_line, seperating_line, strlen(seperating_line))) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* reads one line of input into one_line */
|
|
||||||
void
|
|
||||||
read_one_line(char* rtn_line) {
|
|
||||||
int c;
|
|
||||||
/* used to keep track of the size of the input */
|
|
||||||
size_t index = 0, size = 0;
|
|
||||||
char* one_line = (char*)malloc(sizeof(char)), *temp;
|
|
||||||
while (((c = getchar()) != EOF) && (c != '\n') && (c != '\r')) {
|
|
||||||
/* reallocate space if there's not enough */
|
|
||||||
if (size <= index) {
|
|
||||||
size *= 2;
|
|
||||||
size += 1;
|
|
||||||
temp = realloc(one_line, size);
|
|
||||||
assert(temp != NULL);
|
|
||||||
one_line = temp;
|
|
||||||
}
|
|
||||||
one_line[index++] = c;
|
|
||||||
}
|
|
||||||
one_line[index] = '\0';
|
|
||||||
|
|
||||||
/* copy the input to rtn_line */
|
|
||||||
temp = realloc(rtn_line, index);
|
|
||||||
assert(temp != NULL);
|
|
||||||
rtn_line = temp;
|
|
||||||
strncpy(rtn_line, one_line, index);
|
|
||||||
|
|
||||||
/* and of course, free the allocated space */
|
|
||||||
free(one_line);
|
|
||||||
one_line = NULL;
|
|
||||||
}
|
|
21
src/main.c
21
src/main.c
|
@ -61,7 +61,6 @@ typedef struct {
|
||||||
char card_id[CREDIT_CARD_ID_LEN + 1];
|
char card_id[CREDIT_CARD_ID_LEN + 1];
|
||||||
char trans_time[TRANSACTION_TIME_LEN + 1];
|
char trans_time[TRANSACTION_TIME_LEN + 1];
|
||||||
int trans_amount;
|
int trans_amount;
|
||||||
|
|
||||||
} transaction_t;
|
} transaction_t;
|
||||||
|
|
||||||
/* function prototypes */
|
/* function prototypes */
|
||||||
|
@ -72,9 +71,12 @@ void stage_three();
|
||||||
void stage_four();
|
void stage_four();
|
||||||
|
|
||||||
void read_one_line(char* rtn_line);
|
void read_one_line(char* rtn_line);
|
||||||
|
void read_one_card(char* one_line, credit_card_t* one_card);
|
||||||
|
int is_seperating_line(char* one_line);
|
||||||
|
|
||||||
/* the main function */
|
/* the main function */
|
||||||
int main(int argc, char *argv[]) {
|
int
|
||||||
|
main(int argc, char *argv[]) {
|
||||||
/* holds all the credit cards */
|
/* holds all the credit cards */
|
||||||
credit_card_t all_cards[MAX_NUM_CARDS];
|
credit_card_t all_cards[MAX_NUM_CARDS];
|
||||||
/* counts the number of credit cards read */
|
/* counts the number of credit cards read */
|
||||||
|
@ -96,7 +98,8 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* print the stage header */
|
/* print the stage header */
|
||||||
void print_stage(int stage_num) {
|
void
|
||||||
|
print_stage(int stage_num) {
|
||||||
printf(STAGE_FORMATTING_STRING);
|
printf(STAGE_FORMATTING_STRING);
|
||||||
printf("Stage %d", stage_num);
|
printf("Stage %d", stage_num);
|
||||||
printf(STAGE_FORMATTING_STRING);
|
printf(STAGE_FORMATTING_STRING);
|
||||||
|
@ -199,7 +202,8 @@ is_seperating_line(char* one_line) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* stage one: reading one credit card record */
|
/* stage one: reading one credit card record */
|
||||||
void stage_one(credit_card_t* one_card) {
|
void
|
||||||
|
stage_one(credit_card_t* one_card) {
|
||||||
print_stage(STAGE_ONE);
|
print_stage(STAGE_ONE);
|
||||||
char* one_line = (char*)malloc(sizeof(char));
|
char* one_line = (char*)malloc(sizeof(char));
|
||||||
|
|
||||||
|
@ -218,7 +222,8 @@ void stage_one(credit_card_t* one_card) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* stage 2: reading all credit card records */
|
/* stage 2: reading all credit card records */
|
||||||
void stage_two(credit_card_t* all_cards, int* num_cards) {
|
void
|
||||||
|
stage_two(credit_card_t* all_cards, int* num_cards) {
|
||||||
print_stage(STAGE_TWO);
|
print_stage(STAGE_TWO);
|
||||||
|
|
||||||
/* the current card being analysed */
|
/* the current card being analysed */
|
||||||
|
@ -269,7 +274,8 @@ void stage_two(credit_card_t* all_cards, int* num_cards) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* stage 3: reading the transactions */
|
/* stage 3: reading the transactions */
|
||||||
void stage_three() {
|
void
|
||||||
|
stage_three() {
|
||||||
print_stage(STAGE_THREE);
|
print_stage(STAGE_THREE);
|
||||||
|
|
||||||
|
|
||||||
|
@ -278,7 +284,8 @@ void stage_three() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* stage 4: checking for fraudulent transactions */
|
/* stage 4: checking for fraudulent transactions */
|
||||||
void stage_four() {
|
void
|
||||||
|
stage_four() {
|
||||||
print_stage(STAGE_FOUR);
|
print_stage(STAGE_FOUR);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,70 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#define MAX 1
|
|
||||||
#define CREDIT_CARD_ID_LEN 8
|
|
||||||
|
|
||||||
struct credit_card_t {
|
|
||||||
char id[CREDIT_CARD_ID_LEN + 1];
|
|
||||||
int daily_lim;
|
|
||||||
int trans_lim;
|
|
||||||
};
|
|
||||||
|
|
||||||
void get_next_line(char* new_line);
|
|
||||||
void read_credit_card(struct credit_card_t* arr_credit_card_t, char* new_line, int* num_lines);
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
struct credit_card_t arr_credit_card_t[MAX];
|
|
||||||
char new_line[100];
|
|
||||||
int num_cards = 0;
|
|
||||||
get_next_line(new_line);
|
|
||||||
read_credit_card(arr_credit_card_t, new_line, &num_cards);
|
|
||||||
|
|
||||||
printf("Card ID: %s\n", arr_credit_card_t[0].id);
|
|
||||||
printf("Daily limit: %d\n", arr_credit_card_t[0].daily_lim);
|
|
||||||
printf("Transaction limit: %d\n", arr_credit_card_t[0].trans_lim);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_next_line(char* new_line) {
|
|
||||||
int i = 0, c;
|
|
||||||
while (((c = getchar()) != EOF) && (c != '\n') && (c != '\r')) {
|
|
||||||
new_line[i++] = c;
|
|
||||||
}
|
|
||||||
new_line[i] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
void read_credit_card(struct credit_card_t* arr_credit_card_t, char* new_line, int* num_lines) {
|
|
||||||
int i, daily_limit_found, daily_index = 0, trans_index = 0;
|
|
||||||
char* daily_limit_str = (char*)malloc(sizeof(daily_limit_str) + 1);
|
|
||||||
char* trans_limit_str = (char*)malloc(sizeof(trans_limit_str) + 1);
|
|
||||||
for (i = 0; new_line[i] != '\0'; i++) {
|
|
||||||
if (i < CREDIT_CARD_ID_LEN) {
|
|
||||||
arr_credit_card_t[*num_lines].id[i] = new_line[i];
|
|
||||||
} else if (i == CREDIT_CARD_ID_LEN) {
|
|
||||||
arr_credit_card_t[*num_lines].id[i++] = '\0';
|
|
||||||
continue;
|
|
||||||
} else if (!daily_limit_found) {
|
|
||||||
daily_limit_str = (char*)realloc(daily_limit_str, strlen(daily_limit_str) + 1);
|
|
||||||
daily_limit_str[daily_index++] = new_line[i];
|
|
||||||
} else if (new_line[i] == ' ') {
|
|
||||||
daily_limit_found = 1;
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
trans_limit_str = (char*)realloc(trans_limit_str, strlen(trans_limit_str) + 1);
|
|
||||||
trans_limit_str[trans_index++] = new_line[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("newline | %s\n", new_line);
|
|
||||||
printf("card id | %s\n", arr_credit_card_t[0].id);
|
|
||||||
printf("dailyli | %s\n", daily_limit_str);
|
|
||||||
printf("transli | %s\n", trans_limit_str);
|
|
||||||
|
|
||||||
free(trans_limit_str);
|
|
||||||
free(daily_limit_str);
|
|
||||||
trans_limit_str = NULL;
|
|
||||||
daily_limit_str = NULL;
|
|
||||||
|
|
||||||
}
|
|
76
src/test.c
76
src/test.c
|
@ -1,76 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define CREDIT_CARD_ID_LEN 8
|
|
||||||
|
|
||||||
/* structure typedefs */
|
|
||||||
|
|
||||||
/* stores information about a single credit card */
|
|
||||||
typedef struct credit_card_t {
|
|
||||||
char card_id[CREDIT_CARD_ID_LEN + 1];
|
|
||||||
int daily_lim;
|
|
||||||
int trans_lim;
|
|
||||||
} credit_card_t;
|
|
||||||
|
|
||||||
/* function prototypes */
|
|
||||||
void stage_one(credit_card_t* one_card);
|
|
||||||
void read_one_line(char* a_line);
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
/* all_cards will store all credit card information */
|
|
||||||
credit_card_t* all_cards = (credit_card_t*)malloc(sizeof(all_cards));
|
|
||||||
int num_cards = 0;
|
|
||||||
|
|
||||||
stage_one(&all_cards[num_cards]);
|
|
||||||
num_cards += 1;
|
|
||||||
|
|
||||||
free(all_cards);
|
|
||||||
all_cards = NULL;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* stage one: reading one credit card record */
|
|
||||||
void stage_one(credit_card_t* one_card) {
|
|
||||||
printf("=========================Stage 1=========================\n");
|
|
||||||
|
|
||||||
/* read the first line */
|
|
||||||
char* a_card = (char*)malloc(sizeof(char));
|
|
||||||
read_one_line(a_card);
|
|
||||||
printf("\noutside:\none_line:\n%s\n", a_card);
|
|
||||||
printf("strlen(one_line) = %ld\n", strlen(a_card));
|
|
||||||
|
|
||||||
/* Then print the required output */
|
|
||||||
//printf("Card ID: %s\n", one_card->card_id);
|
|
||||||
//printf("Daily limit: %d\n", one_card->daily_lim);
|
|
||||||
//printf("Transaction limit: %d\n", one_card->trans_lim);
|
|
||||||
free(a_card);
|
|
||||||
a_card = NULL;
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* read a line of input into one_line */
|
|
||||||
void
|
|
||||||
read_one_line(char* a_line) {
|
|
||||||
int c;
|
|
||||||
size_t index = 0, size = 0;
|
|
||||||
while (((c = getchar()) != EOF) && (c != '\n') && (c != '\r')) {
|
|
||||||
/* the first character is read */
|
|
||||||
if (!size) {
|
|
||||||
size++;
|
|
||||||
a_line[index++] = c;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (size <= index) {
|
|
||||||
/* if there's not enough space, allocate some more */
|
|
||||||
size *= 2;
|
|
||||||
a_line = realloc(a_line, size);
|
|
||||||
assert(a_line != NULL);
|
|
||||||
}
|
|
||||||
a_line[index++] = c;
|
|
||||||
}
|
|
||||||
a_line[index] = '\0';
|
|
||||||
printf("inside:\n%s\n", a_line);
|
|
||||||
printf("index = %ld\n", index);
|
|
||||||
}
|
|
Loading…
Reference in a new issue