/************************************************************************** * * Currency Converter * * program : multi_convert * dependencies : wget * author : yetimach * date : 4 July 2022 * * * usage: multi_convert FROM TO AMOUNT * * Also works without arguments. * ***************************************************************************/ #include #include #include #include #include #define USD 1 #define EUR 2 #define GBP 3 #define CAD 4 #define AUD 5 #define JPY 6 #define INR 7 #define PLN 8 #define PEN 9 #define MAX_CH 4 #define MAX_NL 20 //max number of chars per number #define MAX_URL 200 #define MAX_CMD 220 #define MAX_PAT 100 #define TEMP_FILE "convert123456temp.txt" char g_curr_str[MAX_CH]; //global currency string FILE *convert_file; int s_curr, d_curr; // source currency, destination currency (from to) char s_curr_str[MAX_CH], d_curr_str[MAX_CH]; // strings to print long int s_curr_amt, d_curr_amt; // amounts for calculation // check for valid currency code and return its number code int valid_curr(char *curr_str){ //Here list all valid currency names if (strcmp(curr_str, "usd") == 0) return USD; if (strcmp(curr_str, "eur") == 0) return EUR; if (strcmp(curr_str, "gbp") == 0) return GBP; if (strcmp(curr_str, "cad") == 0) return CAD; if (strcmp(curr_str, "aud") == 0) return AUD; if (strcmp(curr_str, "jpy") == 0) return JPY; if (strcmp(curr_str, "inr") == 0) return INR; if (strcmp(curr_str, "pln") == 0) return PLN; if (strcmp(curr_str, "pen") == 0) return PEN; return 0; } int currency_selector(){ int success = 0; int ch, i = 0; while ((ch = getchar()) != '\n'){ if (isalpha(ch)){ success = 1; ch = tolower(ch); g_curr_str[i++] = ch; } if (i > MAX_CH - 1) break; } if (ch != '\n') while (getchar() != '\n') ; //read to newline g_curr_str[i] = '\0'; if (!success) return 0; return valid_curr(g_curr_str); } void id_curr(int curr, char *curr_str, int argc){ switch (curr) { case USD: strcpy(curr_str, "USD"); break; case EUR: strcpy(curr_str, "EUR"); break; case GBP: strcpy(curr_str, "GBP"); break; case CAD: strcpy(curr_str, "CAD"); break; case AUD: strcpy(curr_str, "AUD"); break; case JPY: strcpy(curr_str, "JPY"); break; case INR: strcpy(curr_str, "INR"); break; case PLN: strcpy(curr_str, "PLN"); break; case PEN: strcpy(curr_str, "PEN"); break; } if (argc != 4) printf("\n\tYou have selected %s ", curr_str); } int long convert_str_to_int(char *string){ char units_str[MAX_NL]; char cents_str[4]; int long amount; int i = 0, i2 = 0, ch; while ((units_str[i] = string[i]) != '\0' && string[i] != '.') ++i; units_str[i] = '\0'; amount = (atoi(units_str) * 100); if (string[i] == '.'){ ++i; while ((ch = string[i++]) != '\0') if (isdigit(ch)) cents_str[i2++] = ch; if (i2 == 1){ cents_str[i2++] = '0'; cents_str[i2] = '\0'; }else cents_str[i2] = '\0'; amount += atoi(cents_str); } return amount; } long int read_amount(){ char amount_str[MAX_NL]; int i = 0, cents = 0, count_cents = 0, ch; while ((ch = getchar()) != '\n'){ if (isdigit(ch) || ch == '.'){ if (ch == '.') count_cents = 1; if ((i < MAX_NL - 2) && (cents < 3)){ amount_str[i++] = ch; }else{ ; // do nothing } if (count_cents) ++cents; } } if (cents == 2){ amount_str[i++] = '0'; amount_str[i] = '\0'; }else amount_str[i] = '\0'; return convert_str_to_int(amount_str); } double get_con_amount(){ char convert_url[MAX_URL] = "\"www.xe.com/currencyconverter/convert/?From="; strcat(convert_url, s_curr_str); strcat(convert_url, "&To="); strcat(convert_url, d_curr_str); strcat(convert_url, "\" "); char wget_cmd[MAX_CMD] = "wget "; strcat(wget_cmd, convert_url); strcat(wget_cmd, " -q -O "); strcat(wget_cmd, TEMP_FILE); system(wget_cmd); int pattern_len = 0; int ch; char num_str[MAX_NL]; char pattern[MAX_PAT] = "1 "; strcat( pattern, s_curr_str); strcat( pattern, ""); convert_file = fopen(TEMP_FILE, "r"); while ((ch = pattern[pattern_len++]) != '\0') ; --pattern_len; int matches = 0; int sp = 0; while ((ch = getc(convert_file)) != EOF){ if (ch != pattern[matches]) matches = 0; else ++matches; if (matches == pattern_len) break; } if (ch == EOF) return -1; int i = 0; while ((ch = getc(convert_file)) != '<'){ i++; if (isdigit(ch) || ch == '.') num_str[sp++] = ch; if (i > MAX_NL) break; } num_str[sp] = '\0'; fclose(convert_file); system("rm "TEMP_FILE); return atof(num_str); } void print_valid_options(){ printf("\n\tUSD - US Dollar, EUR - Euro, GBP - British Pound," "\n\tCAD - Canadian Dollar, AUD - Australian Dollar," "\n\tJPY - Japanese Yen, INR - Indian Rupee," "\n\tPLN - Polish Zloty, PEN - Peruvian New Sol"); return; } void print_usage(){ printf("\n\n\tTo use with arguments,\n\n\tusage: multi_convert FROM TO AMOUNT" "\n\n\twhere FROM and TO are the three-letter currency codes" "\n\tfor the currencies you wish to convert from and to:"); print_valid_options(); printf("\n\n\tex: multi_convert USD EUR 130.40\n\n\tAlso works with no arguments.\n\n"); return; } int main(int argc, char *argv[]) { if (argc != 1 && argc != 4){ print_usage(); return 0; }else if (argc == 4){ int i = 0, ch; for (int ai = 1; ai < 3; ai++){ i = 0; while (( ch = argv[ai][i]) != '\0'){ argv[ai][i++] = tolower(ch); } } if ((s_curr = valid_curr(argv[1])) == 0){ print_usage(); return 0; }else{ id_curr(s_curr, s_curr_str, argc); } if ((d_curr = valid_curr(argv[2])) == 0){ print_usage(); return 0; }else{ id_curr(d_curr, d_curr_str, argc); } if ((s_curr_amt = convert_str_to_int(argv[3])) < 0){ print_usage(); return 0; } double con_amount = get_con_amount(); d_curr_amt = lround(s_curr_amt * con_amount); printf("\n\tAt the current rate of %lf,\n\t%s %li.%02li is %s %li.%02li.\n\n", con_amount, s_curr_str, s_curr_amt / 100, s_curr_amt % 100, d_curr_str, d_curr_amt / 100, d_curr_amt % 100); return 0; }else if (argc == 1){ double con_amount; printf("\n\t:::::::::::::Currency Converter:::::::::::::" "\n\n\tChoose a currency to convert from"); print_valid_options(); printf("\n\tcurrency: "); while ((s_curr = currency_selector()) == 0) printf("\n\tInvalid input: try again: "); id_curr(s_curr, s_curr_str, argc); printf("as your source currency.\n"); printf("\n\tChoose a currency to convert to" "\n\tcurrency: "); while ((d_curr = currency_selector()) == 0 || d_curr == s_curr){ if (d_curr == s_curr) printf("\n\tYou must select a different currency to convert to.\n\n\tTry again: "); else printf("\n\tInvalid input: try again: "); } id_curr(d_curr, d_curr_str, argc); printf("as your destinaton currency.\n\n\tEnter amount of %s you wish to convert to %s: ", s_curr_str, d_curr_str); s_curr_amt = read_amount(); con_amount = get_con_amount(); d_curr_amt = lround(s_curr_amt * con_amount); printf("\n\tAt the current rate of %lf,\n\t%s %li.%02li is %s %li.%02li.\n\n", con_amount, s_curr_str, s_curr_amt / 100, s_curr_amt % 100, d_curr_str, d_curr_amt / 100, d_curr_amt % 100); } return 0; }