//------------------------------------------------------------------------ // Date //------------------------------------------------------------------------ Date::Date(std::string _date) { if (_date.length() == 0) { paramter_undefined = true; return; } code = date_to_int(_date); day = code % 100; month = (code / 100) % 100; year = code / 10000; }
/*********************************************************************** * function : ask_dates() * purpose : ask user for date(s) and store it/them in a struct * of type dates * argument : * — bool multiple : true to ask for two dates * false to ask for one date * return : pointer to struct of type dates * notes : * — ***********************************************************************/ dates_ptr ask_dates(bool multiple){ dates_ptr dt = NULL; bool exit_loop = false; int ub_date_req = 0, lb_date_req = 0, swap; char ub_date_req_str[DTSL], lb_date_req_str[DTSL], swap_str[DTSL]; /*********************************************************************** *@ bool type * multiple -> date range (true) or specific date (false) *@ int type * ub_date_req -> upper bound date integer * lb_date_req -> lower bound date integer * swap -> auxiliary integer for swap *@ char type * ub_date_req_str[DTSL] -> upper bound date string * lb_date_req_str[DTSL] -> lower bound date string * swap_str[DTSL] -> auxiliary string for swap *@ dates_ptr type * request -> pointer to struct containing dates ***********************************************************************/ while (!exit_loop){ if (multiple) printf("\n / /\r"); else printf("\n / /\r"); printf(" Please give draw's "); if ((multiple) && (ub_date_req <= 0)) printf("UPPER bound "); else if ((multiple) && (lb_date_req <= 0)) printf("LOWER bound "); printf("DATE in dd/mm/yyyy format\t:\t"); fflush(stdin); if (ub_date_req <= 0){ scanf("%s", &ub_date_req_str); ub_date_req = date_to_int(ub_date_req_str); if (!multiple){ strcpy(lb_date_req_str, ub_date_req_str); lb_date_req = ub_date_req; } } else{ scanf("%s", &lb_date_req_str); lb_date_req = date_to_int(lb_date_req_str); } getchar(); exit_loop = (lb_date_req > 0) && (ub_date_req > 0); if ((ub_date_req <= 0) && (lb_date_req <= 0)) printf("\n\a Invalid date\n"); } /* swap if inputs not in correct order */ if (lb_date_req > ub_date_req){ swap = ub_date_req; strcpy(swap_str, ub_date_req_str); ub_date_req = lb_date_req; strcpy(ub_date_req_str, lb_date_req_str); lb_date_req = swap; strcpy(lb_date_req_str, swap_str); } dt = (dates_ptr)malloc(sizeof(struct dates)); assert(dt); dt->lb_date_req = lb_date_req; dt->ub_date_req = ub_date_req; strcpy(dt->lb_date_req_str, lb_date_req_str); strcpy(dt->ub_date_req_str, ub_date_req_str); return dt; }
/*********************************************************************** * function : search_draws() * purpose : search for draws in date range specified by dt * argument : * — dates_ptr dt : pointer to struct of type dates * return : pointer to struct of type draw * notes : * — returned pointer points to list of structs of type draw ***********************************************************************/ draw_ptr search_draws(dates_ptr dt){ int date_fnd, *win_nums, arr_pos = 0; char id_str[IDSL], date_str[DTSL], win_nums_str[WNSL], joker_str[JSL]; draw_ptr new_draw, list_head = NULL; /*********************************************************************** *@ int type * date_fnd -> checking draw's date inverted integer * win_nums -> pointer to checking draw's wining numbers integer array * arr_pos -> index on array position *@ char type * id_str -> checking draw's id string * date_str -> checking draw's date string * win_nums_str -> checking draw's wining numbers string * joker_str -> checking draw's joker string *@ dates_ptr * dt -> pointer to struct containing dates *@ draw_ptr type * list_head -> pointer to list of structs represents draws * new_draw -> pointer to new struct draw ***********************************************************************/ rewind(rf_ptr); fscanf(rf_ptr, "%s%s%s%s", id_str, date_str, win_nums_str, joker_str); /* * Scan reading file line by line. Every line contains four fields, id, * date, winning numbers and joker of a draw, respectively. For every * reading line, check if date is less than upper date bound and more * than lower date bound, simultaneously. If that indeed happend, save * draws data in a struct of type draw and put it in head of a list. * Repeat until find either EOF, or date of a draw is less than lower * date bound. */ do{ if (feof(rf_ptr)) break; fscanf(rf_ptr, "%s%s%s%s", id_str, date_str, win_nums_str, joker_str); date_fnd = date_to_int(date_str); if ((dt->ub_date_req >= date_fnd) && (dt->lb_date_req <= date_fnd)){ new_draw = (draw_ptr)malloc(sizeof(struct draw)); assert(new_draw); new_draw->id = atoi(id_str); strcpy(new_draw->date, date_str); win_nums = winnums_to_int(win_nums_str); if (win_nums == NULL) return NULL; for (arr_pos = 0; arr_pos < WNAL; arr_pos++){ new_draw->nums[arr_pos] = win_nums[arr_pos]; } free(win_nums); new_draw->joker = atoi(joker_str); new_draw->winners = 0; new_draw->next = NULL; new_draw->previous = NULL; if (list_head == NULL) list_head = new_draw; else{ list_head->previous = new_draw; new_draw->next = list_head; list_head = list_head->previous; } } } while (date_fnd >= dt->lb_date_req); /* if not found any draw */ if (list_head == NULL){ if (dt->lb_date_req != dt->ub_date_req) printf("\n Sorry! No draws were held between DATEs %s & %s", dt->lb_date_req_str, dt->ub_date_req_str); else printf("\n Sorry! No draw was held on DATE %s", dt->ub_date_req_str); } return list_head; }