Last active
October 17, 2023 08:07
-
-
Save isaacssemugenyi/31412fb1efb8ef597f67afe5d193e784 to your computer and use it in GitHub Desktop.
Rust currency converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
/** | |
* https://www.oanda.com/currency-converter/en/ | |
* As of 1st October 2023 | |
*/ | |
void display_name(); | |
void execute_conversion(); | |
float switch_currency(char* from_type, float amount, char* to_type); | |
float convert_to_ugx(char* from_type, float amount); | |
float convert_to_gbp(char* from_type, float amount); | |
float convert_to_kes(char* from_type, float amount); | |
float convert_to_usd(char* from_type, float amount); | |
float convert_to_eur(char* from_type, float amount); | |
int main() | |
{ | |
printf("Welcome to My Currency Converter\n"); | |
printf("****Where Quality Matters****\n"); | |
display_name(); | |
return 0; | |
} | |
void display_name(){ | |
char repeat[10]; | |
execute_conversion(); | |
printf("\nType Yes/ YES/ Y/ y to continue or No to stop: "); | |
scanf("%s", repeat); | |
while( | |
strcmp(repeat, "yes") == 0 || | |
strcmp(repeat, "Y") == 0 || | |
strcmp(repeat, "y") == 0 || | |
strcmp(repeat, "YES") == 0 | |
) { | |
execute_conversion(); | |
printf("\nType Yes/ YES/ Y/ y to continue or No to stop: "); | |
scanf("%s", repeat); | |
} | |
printf("\nThank you for choosing JIPP Currency Convertor."); | |
} | |
void execute_conversion(){ | |
float amount; | |
char from_currency[10]; | |
char to_currency[10]; | |
printf("Enter the amount you want to convert: "); | |
scanf("%f", &amount); | |
printf("Enter the currency type for the amount entered: "); | |
scanf("%s", from_currency); | |
printf("Enter the currency you want to convert to: "); | |
scanf("%s", to_currency); | |
float result = switch_currency(from_currency, amount, to_currency); | |
if(result <= 0) { | |
printf("\nInvalid currency type or wrong amount entry. \nWe support currency conversions for \n => UGX \n => EUR \n => KES \n => USD \n => GBP only. \n\nPlease type 'YES' below to try again with the right currency types. Thank You.\n"); | |
} else { | |
printf("Converted from %s %.2f to %s %.4f ", from_currency, amount, to_currency, result); | |
} | |
} | |
/** | |
* @description function to switch between currencies | |
*/ | |
float switch_currency(char* from_type, float amount, char* to_type) | |
{ | |
if(strcmp(to_type, "UGX") == 0 || strcmp(to_type, "ugx") == 0) | |
{ | |
return convert_to_ugx(from_type, amount); | |
} | |
else if(strcmp(to_type, "USD") == 0 || strcmp(to_type, "usd") == 0) | |
{ | |
return convert_to_usd(from_type, amount); | |
} | |
else if(strcmp(to_type, "EUR") == 0 || strcmp(to_type, "eur") == 0) | |
{ | |
return convert_to_eur(from_type, amount); | |
} | |
else if(strcmp(to_type, "GBP") == 0 || strcmp(to_type, "gbp") == 0) | |
{ | |
return convert_to_gbp(from_type, amount); | |
} | |
else if(strcmp(to_type, "KES") == 0 || strcmp(to_type, "kes") == 0) | |
{ | |
return convert_to_kes(from_type, amount); | |
} | |
else { | |
return 0.00; | |
} | |
} | |
float convert_to_ugx(char* from_type, float amount) | |
{ | |
float gbp_to_ugx_rate = 4564.81; | |
float eur_to_ugx_rate = 3955.52; | |
float kes_to_ugx_rate = 25.168; | |
float usd_to_ugx_rate = 3741.22; | |
float output = 0.00; | |
if(strcmp(from_type, "GBP") == 0 || strcmp(from_type, "gbp") == 0) | |
{ | |
output = amount * gbp_to_ugx_rate; // GBP to UGX | |
} | |
else if (strcmp(from_type, "EUR") == 0 || strcmp(from_type, "eur") == 0) | |
{ | |
output = amount * eur_to_ugx_rate; // EUR to UGX | |
} | |
else if (strcmp(from_type, "KES") == 0 || strcmp(from_type, "kes") == 0) | |
{ | |
output = amount * kes_to_ugx_rate; // KES to UGX | |
} | |
else if (strcmp(from_type, "USD") == 0 || strcmp(from_type, "usd") == 0) | |
{ | |
output = amount * usd_to_ugx_rate; // USD to UGX | |
} | |
return output; | |
} | |
float convert_to_gbp(char* from_type, float amount) | |
{ | |
float ugx_to_gbp_rate = 0.00022; | |
float eur_to_gbp_rate = 0.86615; | |
float kes_to_gbp_rate = 0.00551; | |
float usd_to_gbp_rate = 0.81921; | |
float output = 0.00; | |
if(strcmp(from_type, "UGX") == 0 || strcmp(from_type, "ugx") == 0) | |
{ | |
output = amount * ugx_to_gbp_rate; | |
} | |
else if (strcmp(from_type, "EUR") == 0 || strcmp(from_type, "eur") == 0) | |
{ | |
output = amount * eur_to_gbp_rate; | |
} | |
else if (strcmp(from_type, "KES") == 0 || strcmp(from_type, "kes") == 0) | |
{ | |
output = amount * kes_to_gbp_rate; | |
} | |
else if (strcmp(from_type, "USD") == 0 || strcmp(from_type, "usd") == 0) | |
{ | |
output = amount * usd_to_gbp_rate; | |
} | |
return output; | |
} | |
float convert_to_kes(char* from_type, float amount) | |
{ | |
float gbp_to_kes_rate = 180.275; | |
float eur_to_kes_rate = 156.213; | |
float ugx_to_kes_rate = 0.03905; | |
float usd_to_kes_rate = 147.75; | |
float output = 0.00; | |
if(strcmp(from_type, "UGX") == 0 || strcmp(from_type, "ugx") == 0) | |
{ | |
output = amount * ugx_to_kes_rate; | |
} | |
else if (strcmp(from_type, "EUR") == 0 || strcmp(from_type, "eur") == 0) | |
{ | |
output = amount * eur_to_kes_rate; | |
} | |
else if (strcmp(from_type, "GBP") == 0 || strcmp(from_type, "gbp") == 0) | |
{ | |
output = amount * gbp_to_kes_rate; | |
} | |
else if (strcmp(from_type, "USD") == 0 || strcmp(from_type, "usd") == 0) | |
{ | |
output = amount * usd_to_kes_rate; | |
} | |
return output; | |
} | |
float convert_to_usd(char* from_type, float amount) | |
{ | |
float gbp_to_usd_rate = 1.22014; | |
float eur_to_usd_rate = 1.05728; | |
float ugx_to_usd_rate = 0.00026; | |
float kes_to_usd_rate = 0.00673; | |
float output = 0.00; | |
if(strcmp(from_type, "UGX") == 0 || strcmp(from_type, "ugx") == 0) | |
{ | |
output = amount * ugx_to_usd_rate; | |
} | |
else if (strcmp(from_type, "EUR") == 0 || strcmp(from_type, "eur") == 0) | |
{ | |
output = amount * eur_to_usd_rate; | |
} | |
else if (strcmp(from_type, "GBP") == 0 || strcmp(from_type, "gbp") == 0) | |
{ | |
output = amount * gbp_to_usd_rate; | |
} | |
else if (strcmp(from_type, "KES") == 0 || strcmp(from_type, "kes") == 0) | |
{ | |
output = amount * kes_to_usd_rate; | |
} | |
return output; | |
} | |
float convert_to_eur(char* from_type, float amount) | |
{ | |
float gbp_to_eur_rate = 1.15335; | |
float usd_to_eur_rate = 0.94554; | |
float ugx_to_eur_rate = 0.00025; | |
float kes_to_eur_rate = 0.00636; | |
float output = 0.00; | |
if(strcmp(from_type, "UGX") == 0 || strcmp(from_type, "ugx") == 0) | |
{ | |
output = amount * ugx_to_eur_rate; | |
} | |
else if (strcmp(from_type, "USD") == 0 || strcmp(from_type, "usd") == 0) | |
{ | |
output = amount * usd_to_eur_rate; | |
} | |
else if (strcmp(from_type, "GBP") == 0 || strcmp(from_type, "gbp") == 0) | |
{ | |
output = amount * gbp_to_eur_rate; | |
} | |
else if (strcmp(from_type, "KES") == 0 || strcmp(from_type, "kes") == 0) | |
{ | |
output = amount * kes_to_eur_rate; | |
} | |
return output; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
// A Struct to act as a return type for display_data() function | |
struct FinalResult { | |
original_currency: String, | |
original_amount: f32, | |
final_currency: String, | |
final_amount: f32, | |
} | |
// Main function that calles the display_data function to run the application | |
fn main() { | |
let mut continue_conversion: String = String::new(); | |
let mut result = display_data(); | |
println!( | |
"{:?} {:?} was converted to {:?} {:?}", | |
result.original_currency, | |
result.original_amount, | |
result.final_currency, | |
result.final_amount | |
); | |
// Loops for even when Y or Yes is typed in to perform another conversion | |
loop { | |
println!("Do you still want to perform another conversion"); | |
stdin() | |
.read_line(&mut continue_conversion) | |
.expect("Not a valid string"); | |
if continue_conversion.trim().to_lowercase() == "y" || continue_conversion.trim().to_lowercase() == "yes" { | |
result = display_data(); | |
println!( | |
"{:?} {:?} was converted to {:?} {:?}", | |
result.original_currency, | |
result.original_amount, | |
result.final_currency, | |
result.final_amount | |
); | |
// Reset the variable continue_conversion as it was concatinating values of the previous input | |
continue_conversion = String::new(); | |
} else { | |
println!("Thanks for using our currency converter. Come again next time"); | |
break | |
} | |
} | |
} | |
// Function pick all the neccessary user input for the conversion | |
fn display_data() -> FinalResult { | |
let num: f32; | |
let mut amount_input: String = String::new(); | |
let mut from_currency: String = String::new(); | |
let mut to_currency: String = String::new(); | |
println!("Enter a number:"); | |
stdin() | |
.read_line(&mut amount_input) | |
.expect("Not a valid string"); | |
num = amount_input | |
.trim() | |
.parse::<f32>() | |
.expect("Not a valid number"); | |
println!("Enter currency type of amount entered:"); | |
stdin() | |
.read_line(&mut from_currency) | |
.expect("Not a valid string"); | |
println!("Enter currency type to convert to:"); | |
stdin() | |
.read_line(&mut to_currency) | |
.expect("Not a valid string"); | |
let result = match_from_currency( | |
&from_currency.trim().to_uppercase(), | |
&to_currency.trim().to_uppercase(), | |
&num); | |
return FinalResult { | |
original_currency: String::from(&from_currency.trim().to_uppercase()), | |
original_amount: num, | |
final_currency: String::from(&to_currency.trim().to_uppercase()), | |
final_amount: result | |
}; | |
} | |
// match_from_currency matches which function to forward the conversion too incase a given curreny type from_currency is used | |
fn match_from_currency(from_currency: &str, to_currency: &str, num: &f32) -> f32 { | |
match &*from_currency.trim() { | |
"USD" => convert_from_usd(&to_currency, &num), | |
"EUR" => convert_from_eur(&to_currency, &num), | |
"GBP" => convert_from_gbp(&to_currency, &num), | |
"KES" => convert_from_kes(&to_currency, &num), | |
"UGX" => convert_from_ugx(&to_currency, &num), | |
_ => 0.0, | |
} | |
} | |
// Converts currency from UGX to any other -> USD, EUR, GBP, KES | |
// The same for the below functions | |
fn convert_from_ugx(to_currency: &str, num: &f32) -> f32 { | |
let gbp_to_ugx_rate = 4564.81; | |
let eur_to_ugx_rate = 3955.52; | |
let kes_to_ugx_rate = 25.168; | |
let usd_to_ugx_rate = 3741.22; | |
let amount: f32 = match to_currency { | |
"USD" => *num * usd_to_ugx_rate, | |
"EUR" => *num * eur_to_ugx_rate, | |
"GBP" => *num * gbp_to_ugx_rate, | |
"KES" => *num * kes_to_ugx_rate, | |
"UGX" => *num, | |
_ => 0.0, | |
}; | |
return amount; | |
} | |
fn convert_from_usd(to_currency: &str, num: &f32) -> f32 { | |
let gbp_to_usd_rate = 1.22014; | |
let eur_to_usd_rate = 1.05728; | |
let ugx_to_usd_rate = 0.00026; | |
let kes_to_usd_rate = 0.00673; | |
let amount: f32 = match to_currency { | |
"EUR" => *num * eur_to_usd_rate, | |
"GBP" => *num * gbp_to_usd_rate, | |
"KES" => *num * kes_to_usd_rate, | |
"UGX" => *num * ugx_to_usd_rate, | |
"USD" => *num, | |
_ => 0.0, | |
}; | |
return amount; | |
} | |
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c81549461fa67e79df562daeafa5de63 | |
fn convert_from_eur(to_currency: &str, num: &f32) -> f32 { | |
let gbp_to_eur_rate = 1.15335; | |
let usd_to_eur_rate = 0.94554; | |
let ugx_to_eur_rate = 0.00025; | |
let kes_to_eur_rate = 0.00636; | |
let amount: f32 = match to_currency { | |
"USD" => *num * usd_to_eur_rate, | |
"GBP" => *num * gbp_to_eur_rate, | |
"KES" => *num * kes_to_eur_rate, | |
"UGX" => *num * ugx_to_eur_rate, | |
"EUR" => *num, | |
_ => 0.0, | |
}; | |
return amount; | |
} | |
fn convert_from_gbp(to_currency: &str, num: &f32) -> f32 { | |
let ugx_to_gbp_rate = 0.00022; | |
let eur_to_gbp_rate = 0.86615; | |
let kes_to_gbp_rate = 0.00551; | |
let usd_to_gbp_rate = 0.81921; | |
let amount: f32 = match to_currency { | |
"USD" => *num * usd_to_gbp_rate, | |
"EUR" => *num * eur_to_gbp_rate, | |
"KES" => *num * kes_to_gbp_rate, | |
"UGX" => *num * ugx_to_gbp_rate, | |
"GBP" => *num, | |
_ => 0.0, | |
}; | |
return amount; | |
} | |
fn convert_from_kes(to_currency: &str, num: &f32) -> f32 { | |
let gbp_to_kes_rate = 180.275; | |
let eur_to_kes_rate = 156.213; | |
let ugx_to_kes_rate = 0.03905; | |
let usd_to_kes_rate = 147.75; | |
let amount: f32 = match to_currency { | |
"USD" => *num * usd_to_kes_rate, | |
"EUR" => *num * eur_to_kes_rate, | |
"GBP" => *num * gbp_to_kes_rate, | |
"UGX" => *num * ugx_to_kes_rate, | |
"KES" => *num, | |
_ => 0.0, | |
}; | |
return amount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment