我应该如何着手建立一个货币兑换系统



我对C++非常陌生,为了扩大我对它的了解,我决定制作一个货币转换器。然而,在对大约200种货币进行编码后,我希望能够转换为和从中转换,我意识到这将需要很长时间才能进行编码。因此,我的问题是,是否有可能存储诸如";1美元是__欧元";在一个数组中,并使用该数组将欧元等其他货币转换为CAD?有没有一种针对C++的更有效的方法来做到这一点?我目前的代码如下,提前感谢大家。

#include <iostream>
#include <string>
#include <map>
using namespace std;
int choice;
int choice2;
int choice3;
int main()
{
cout << "Hello! Welcome to this converter! If you want to see all possible conversions, type 1. If not, type 0 to skip straight to the conversions!";  
cin >> choice;
cout << "Your choice: " << choice << endl;

cin >> choice;
cin >> choice2;
cin >> choice3;  

if (choice == 1)
{
cout << "Major World Currencies: United States Dollar, $ (USD)  Euro, € (EUR)  BrEuropitish Pound Sterling, £ (GBP)  Japanese Yen, ¥ (JPY)  Swiss Franc, Fr (CHF)  Australian Dollar, $ (AUD)  Canadian Dollar, $ (CAD)  Hong Kong Dollar, $ (HKD)  Chinese Yuan, ¥ (CNY)  Russian Ruble, ₽ (RUB)" << endl;
cout << " " << endl;
cout << "European Currencies:  Albanian Lek (ALL)  Bosnia-Herzegovina Convertible Mark (BAM)  Bulgarian Lev (BGN)  Belarusian Ruble (BYN)  Czech Republic Koruna (CZK)  Danish Krone (DKK)  Guernsey Pound (GGP)  Gibraltar Pound (GIP)  Croatian Kuna (HRK)  Hungarian Forint (HUF)  Icelandic Króna (ISK)  Jersey Pound (JEP)  Moldovan Leu (MDL)  Macedonian Denar (MKD)  Manx pound (Isle of Man) (IMP)  Norwegian Krone (NOK)  Polish Zloty (PLN)  Romanian Leu (RON)  Serbian Dinar (RSD)  Swedish Krona (SEK)  Ukrainian Hryvnia (UAH)" << endl;
cout << ' ' << endl;
cout << " Middle East Currencies United Arab Emirates Dirham (AED)  Bahraini Dinar (BHD)  Israeli New Sheqel (ILS)  Iraqi Dinar (IQD)  Iranian Rial (IRR)  Kuwaiti Dinar (KWD)  Omani Rial (OMR)  Qatari Rial (QAR)  Saudi Riyal (SAR)  Syrian Pound (SYP)  Turkish Lira (TRY)  Yemeni Rial (YER)  » hide » North American Currencies Bermudan Dollar (BMD)  Mexican Peso (MXN)  " << endl;
cout << "" << endl;
cout << "Central American Currencies: Belize Dollar (BZD)  Costa Rican Colón (CRC)  Salvadoran Colón (SVC)  Guatemalan Quetzal (GTQ)  Honduran Lempira (HNL)  Nicaraguan Cordoba (NIO)  Panamanian Balboa (PAB)" << endl;
cout << "" << endl;
cout << "Currencies of the Caribbean:  Aruban Florin (AWG)  Barbadian Dollar (BBD)  Bahamian Dollar (BSD)  Cayman Islands Dollar (KYD)  Cuban Convertible Peso (CUC)  Cuban Peso (CUP)  Dominican Peso (DOP)  Haitian Gourde (HTG)  Jamaican Dollar (JMD)  East Caribbean Dollar (XCD)  Netherlands Antillean Guilder (ANG)  » hide » South American Currencies Argentine Peso (ARS)  Bolivian Boliviano (BOB)  Brazilian Real (BRL)  Chilean Peso (CLP)  Chilean Unidad de Fomento (CLF)" << endl; 
cout << "" << endl;
cout << "South American Currencies: Argentine Peso (ARS)  / Bolivian Boliviano (BOB)  / Brazilian Real (BRL)  / Chilean Peso (CLP)  / Chilean Unidad de Fomento (CLF)  / Colombian Peso (COP)  / Guyanaese Dollar (GYD)  / Peruvian Sol (PEN)  / Paraguayan Guarani (PYG)  / Surinamese Dollar (SRD)  / Trinidad and Tobago Dollar (TTD)  / Uruguayan Peso (UYU)  / Venezuelan Bolívar Soberano (VES)" << endl;
cout << "" << endl;
cout << "African Curriencies: Angolan Kwanza (AOA)  / Burundian Franc (BIF)  / Botswanan Pula (BWP)  / Congolese Franc (CDF)  / Cape Verdean Escudo (CVE)  / Djiboutian Franc (DJF)  / Algerian Dinar (DZD)  / Egyptian Pound (EGP)  / Eritrean Nakfa (ERN)  / Ethiopian Birr (ETB)  / Falkland Islands Pound (FKP)  / Gambian Dalasi (GMD)  / Ghanaian Cedi (GHS)  / Guinean Franc (GNF)  / Kenyan Shilling (KES)  / Comorian Franc (KMF)  / Lebanese Pound (LBP)  / Liberian Dollar (LRD)  / Lesotho Loti (LSL)  / Libyan Dinar (LYD)  / Moroccan Dirham (MAD)  / Malagasy Ariary (MGA)  / Mauritanian Ouguiya (MRU)  / Mauritian Rupee (MUR)  / Malawian Kwacha (MWK)  / Mozambican Metical (MZN)  / Namibian Dollar (NAD)  / Nigerian Naira (NGN)  / Rwandan Franc (RWF)  / Seychellois Rupee (SCR)  / Sudanese Pound (SDG)  / South Sudanese Pound (SSP)  / Saint Helena Pound (SHP)  / Sierra Leonean Leone (SLL)  / Somali Shilling (SOS)  / São Tomé and Príncipe Dobra (pre-2018) (STD)  / São Tomé and Príncipe Dobra (since 2018) (STD)  / Swazi Lilangeni (SZL)  / Tunisian Dinar (TND)  / Tanzanian Shilling (TZS)  / Ugandan Shilling (UGX)  / CFA Franc BEAC (XAF)  / CFA Franc BCEAO (XOF)  / South African Rand (ZAR)  / Zambian Kwacha (ZMW)  / Zimbabwean Dollar (ZWL)" << endl;
cout << "" << endl;
cout << "Currencies of Australia and Oceania: Fijian Dollar (FJD)  / French Polynesia CFP Franc (XPF)  / New Zealand Dollar (NZD)  / Papua New Guinean Kina (PGK)  / Samoan Tala (WST)  / Solomon Islands Dollar (SBD)  / Tongan Paʻanga (TOP)  / Vanuatu Vatu (VUV) " << endl;
cout << "" << endl;
cout << "Other Currencies and More: Bitcoin (BTC)  / Gold (troy ounce) (XAU)  / Silver (troy ounce) (XAG)  / Palladium (troy ounce) (XPD)  / Platinum (troy ounce) (XPT)  / Special Drawing Rights (International Monetary Fund) (XDR) " << endl;
}
else if (choice == 0)
{
cout << "Type in the amount of USD you want to convert. NOTE: Other starting currencies coming soon. " << endl;
cin >> choice2;
cout << "Type in the currency you want to convert USD to. " << endl;
cin >> choice3;
}

您可以使用unordered_map,它将字符串(货币(映射为浮动或包含美元汇率的双精度。例如CCD_ 2等于CCD_。要从非美元货币转换为另一种非美元货币,可以将第一种货币转换为美元,然后再将其转换为第二种货币。

#include <iostream>
#include <unordered_map>
#include <string>
int main(){
std::unordered_map<std::string, float> exchange_rate{
/* the exchange rates from USD go here */
};
std::string currency1, currency2;
float currency1_amount;
std::cin >> currency1 >> currency2 >> currency1_amount;
// make sure the currencies exist
if (exchange_rate[currency1] <= 0 || exchange_rate[currency2] <= 0){
std::cout << "Currency does not existn";
return 1;
}
// convert currency1 to USD
float currency1_in_usd = currency1_amount / exchange_rate[currency1];
// convert to currency 2
float conversion = currency1_in_usd * exchange_rate[currency2];
std::cout << currency1_amount << " " << currency1 << " = " << conversion << " " << currency2 << "n";
}

当然,你可以使用另一种基础货币。还要注意,汇率会随着时间的推移而变化,这意味着您必须更新地图中的值,或者使用API获取它们。

最新更新