/* * sccash - Simple C Cash * A lightweight CLI currency converter for USD, JPY, and EUR * Compile: gcc -o sccash sccash.c */ #include #include #include #include #define RATE_USD_JPY 159.67 #define RATE_USD_EUR 0.866 #define RATE_EUR_JPY 184.20 typedef enum { CURRENCY_USD, CURRENCY_JPY, CURRENCY_EUR, CURRENCY_INVALID } Currency; const char* currency_names[] = {"USD", "JPY", "EUR"}; Currency parse_currency(const char* input) { char upper[4] = {0}; int i; for (i = 0; i < 3 && input[i]; i++) { upper[i] = toupper(input[i]); } if (strcmp(upper, "USD") == 0) return CURRENCY_USD; if (strcmp(upper, "JPY") == 0) return CURRENCY_JPY; if (strcmp(upper, "EUR") == 0) return CURRENCY_EUR; return CURRENCY_INVALID; } double convert_to_usd(double amount, Currency from) { switch (from) { case CURRENCY_USD: return amount; case CURRENCY_JPY: return amount / RATE_USD_JPY; case CURRENCY_EUR: return amount / RATE_USD_EUR; default: return 0.0; } } double convert_from_usd(double usd_amount, Currency to) { switch (to) { case CURRENCY_USD: return usd_amount; case CURRENCY_JPY: return usd_amount * RATE_USD_JPY; case CURRENCY_EUR: return usd_amount * RATE_USD_EUR; default: return 0.0; } } double convert_currency(double amount, Currency from, Currency to) { double usd_amount; if (from == to) return amount; usd_amount = convert_to_usd(amount, from); return convert_from_usd(usd_amount, to); } void print_table_header(void) { printf("+--------+---------------+--------+----------------+\n"); printf("| FROM | AMOUNT | TO | CONVERTED |\n"); printf("+--------+---------------+--------+----------------+\n"); } void print_table_row(const char* from, double amount, const char* to, double converted) { printf("| %-6s | %13.2f | %-6s | %14.2f |\n", from, amount, to, converted); } void print_table_footer(void) { printf("+--------+---------------+--------+----------------+\n"); } void print_all_conversions(double amount, Currency from) { int i; printf("\n"); print_table_header(); for (i = 0; i < 3; i++) { if (i != from) { Currency to = (Currency)i; double result = convert_currency(amount, from, to); print_table_row(currency_names[from], amount, currency_names[to], result); } } print_table_footer(); printf("\nExchange Rates (as of March 26, 2026):\n"); printf(" 1 USD = %.2f JPY\n", RATE_USD_JPY); printf(" 1 USD = %.3f EUR\n", RATE_USD_EUR); printf(" 1 EUR = %.2f JPY\n", RATE_EUR_JPY); printf("\n"); } void interactive_mode(void) { char from_input[10], to_input[10]; char show_all[10]; double amount, result; Currency from, to; printf("\n=== sccash - Simple C Cash ===\n\n"); printf("Available currencies: USD, JPY, EUR\n\n"); printf("Enter amount: "); if (scanf("%lf", &amount) != 1 || amount < 0) { fprintf(stderr, "Error: Invalid amount\n"); return; } printf("Convert from (USD/JPY/EUR): "); scanf("%9s", from_input); from = parse_currency(from_input); if (from == CURRENCY_INVALID) { fprintf(stderr, "Error: Invalid currency '%s'\n", from_input); return; } printf("Convert to (USD/JPY/EUR): "); scanf("%9s", to_input); to = parse_currency(to_input); if (to == CURRENCY_INVALID) { fprintf(stderr, "Error: Invalid currency '%s'\n", to_input); return; } if (from == to) { printf("\nResult: %.2f %s (same currency)\n", amount, currency_names[from]); return; } result = convert_currency(amount, from, to); printf("\n"); print_table_header(); print_table_row(currency_names[from], amount, currency_names[to], result); print_table_footer(); printf("\n"); printf("Show all conversions? (y/n): "); scanf("%9s", show_all); if (show_all[0] == 'y' || show_all[0] == 'Y') { print_all_conversions(amount, from); } } void cli_mode(int argc, char* argv[]) { double amount, result; Currency from, to; if (argc < 4) { fprintf(stderr, "Usage: %s \n", argv[0]); fprintf(stderr, " amount: numeric value to convert\n"); fprintf(stderr, " from: source currency (USD, JPY, EUR)\n"); fprintf(stderr, " to: target currency (USD, JPY, EUR)\n\n"); fprintf(stderr, "Example: %s 100 USD JPY\n", argv[0]); fprintf(stderr, "Or run without arguments for interactive mode\n"); exit(1); } amount = atof(argv[1]); if (amount <= 0) { fprintf(stderr, "Error: Invalid amount '%s'\n", argv[1]); exit(1); } from = parse_currency(argv[2]); to = parse_currency(argv[3]); if (from == CURRENCY_INVALID) { fprintf(stderr, "Error: Invalid source currency '%s'\n", argv[2]); fprintf(stderr, "Available: USD, JPY, EUR\n"); exit(1); } if (to == CURRENCY_INVALID) { fprintf(stderr, "Error: Invalid target currency '%s'\n", argv[3]); fprintf(stderr, "Available: USD, JPY, EUR\n"); exit(1); } result = convert_currency(amount, from, to); printf("\n"); print_table_header(); print_table_row(currency_names[from], amount, currency_names[to], result); print_table_footer(); printf("\n"); } int main(int argc, char* argv[]) { if (argc == 1) { interactive_mode(); } else { cli_mode(argc, argv); } return 0; }