Rewrite to C89 standards, preserve compatibility with old x86 machines
This commit is contained in:
parent
ab12cec361
commit
0d66f50b72
1 changed files with 16 additions and 10 deletions
26
sccash.c
26
sccash.c
|
|
@ -24,7 +24,8 @@ const char* currency_names[] = {"USD", "JPY", "EUR"};
|
||||||
|
|
||||||
Currency parse_currency(const char* input) {
|
Currency parse_currency(const char* input) {
|
||||||
char upper[4] = {0};
|
char upper[4] = {0};
|
||||||
for (int i = 0; i < 3 && input[i]; i++) {
|
int i;
|
||||||
|
for (i = 0; i < 3 && input[i]; i++) {
|
||||||
upper[i] = toupper(input[i]);
|
upper[i] = toupper(input[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,8 +54,9 @@ double convert_from_usd(double usd_amount, Currency to) {
|
||||||
}
|
}
|
||||||
|
|
||||||
double convert_currency(double amount, Currency from, Currency to) {
|
double convert_currency(double amount, Currency from, Currency to) {
|
||||||
|
double usd_amount;
|
||||||
if (from == to) return amount;
|
if (from == to) return amount;
|
||||||
double usd_amount = convert_to_usd(amount, from);
|
usd_amount = convert_to_usd(amount, from);
|
||||||
return convert_from_usd(usd_amount, to);
|
return convert_from_usd(usd_amount, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,10 +75,11 @@ void print_table_footer(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_all_conversions(double amount, Currency from) {
|
void print_all_conversions(double amount, Currency from) {
|
||||||
|
int i;
|
||||||
printf("\n");
|
printf("\n");
|
||||||
print_table_header();
|
print_table_header();
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
for (i = 0; i < 3; i++) {
|
||||||
if (i != from) {
|
if (i != from) {
|
||||||
Currency to = (Currency)i;
|
Currency to = (Currency)i;
|
||||||
double result = convert_currency(amount, from, to);
|
double result = convert_currency(amount, from, to);
|
||||||
|
|
@ -94,7 +97,8 @@ void print_all_conversions(double amount, Currency from) {
|
||||||
|
|
||||||
void interactive_mode(void) {
|
void interactive_mode(void) {
|
||||||
char from_input[10], to_input[10];
|
char from_input[10], to_input[10];
|
||||||
double amount;
|
char show_all[10];
|
||||||
|
double amount, result;
|
||||||
Currency from, to;
|
Currency from, to;
|
||||||
|
|
||||||
printf("\n=== sccash - Simple C Cash ===\n\n");
|
printf("\n=== sccash - Simple C Cash ===\n\n");
|
||||||
|
|
@ -129,7 +133,7 @@ void interactive_mode(void) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
double result = convert_currency(amount, from, to);
|
result = convert_currency(amount, from, to);
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
print_table_header();
|
print_table_header();
|
||||||
|
|
@ -138,7 +142,6 @@ void interactive_mode(void) {
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
printf("Show all conversions? (y/n): ");
|
printf("Show all conversions? (y/n): ");
|
||||||
char show_all[10];
|
|
||||||
scanf("%9s", show_all);
|
scanf("%9s", show_all);
|
||||||
if (show_all[0] == 'y' || show_all[0] == 'Y') {
|
if (show_all[0] == 'y' || show_all[0] == 'Y') {
|
||||||
print_all_conversions(amount, from);
|
print_all_conversions(amount, from);
|
||||||
|
|
@ -146,6 +149,9 @@ void interactive_mode(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void cli_mode(int argc, char* argv[]) {
|
void cli_mode(int argc, char* argv[]) {
|
||||||
|
double amount, result;
|
||||||
|
Currency from, to;
|
||||||
|
|
||||||
if (argc < 4) {
|
if (argc < 4) {
|
||||||
fprintf(stderr, "Usage: %s <amount> <from> <to>\n", argv[0]);
|
fprintf(stderr, "Usage: %s <amount> <from> <to>\n", argv[0]);
|
||||||
fprintf(stderr, " amount: numeric value to convert\n");
|
fprintf(stderr, " amount: numeric value to convert\n");
|
||||||
|
|
@ -156,14 +162,14 @@ void cli_mode(int argc, char* argv[]) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
double amount = atof(argv[1]);
|
amount = atof(argv[1]);
|
||||||
if (amount <= 0) {
|
if (amount <= 0) {
|
||||||
fprintf(stderr, "Error: Invalid amount '%s'\n", argv[1]);
|
fprintf(stderr, "Error: Invalid amount '%s'\n", argv[1]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Currency from = parse_currency(argv[2]);
|
from = parse_currency(argv[2]);
|
||||||
Currency to = parse_currency(argv[3]);
|
to = parse_currency(argv[3]);
|
||||||
|
|
||||||
if (from == CURRENCY_INVALID) {
|
if (from == CURRENCY_INVALID) {
|
||||||
fprintf(stderr, "Error: Invalid source currency '%s'\n", argv[2]);
|
fprintf(stderr, "Error: Invalid source currency '%s'\n", argv[2]);
|
||||||
|
|
@ -177,7 +183,7 @@ void cli_mode(int argc, char* argv[]) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
double result = convert_currency(amount, from, to);
|
result = convert_currency(amount, from, to);
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
print_table_header();
|
print_table_header();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue