📜  货币转换器 c++ (1)

📅  最后修改于: 2023-12-03 14:57:46.865000             🧑  作者: Mango

货币转换器 C++

货币转换器是一款用于将不同货币之间进行转换的软件,通常用于为旅行者提供便利。在这个项目中,我们将使用C++编写一个简单的货币转换器,允许用户在美元、欧元和英镑之间进行转换。

设计概要

首先,我们需要考虑程序需要实现的功能,包括什么样的用户界面以及什么样的输入和输出格式。

该程序应该具有以下功能:

  • 允许用户从以下货币中选择一个:美元(USD)、欧元(EUR)或英镑(GBP)。
  • 允许用户输入一个整数数量,表示他们希望转换多少钱。
  • 将转换后的金额显示为小数点后两位的带货币符号的字符串。

由于本文旨在为初学者提供帮助,因此我们将简化这个项目,使输入和输出格式变得更加简单,并省略货币符号,设计如下:

  • 允许用户从以下货币中选择一个:美元(USD)、欧元(EUR)或英镑(GBP)。
  • 允许用户输入一个整数数量,表示他们希望转换多少钱。
  • 显示转换后的金额(不带货币符号)。
实现步骤
步骤1:用户界面

我们将创建一个简单的用户界面,提示用户输入货币和金额,并使用“cout”语句将这些信息输出到控制台。

#include <iostream>

using namespace std;

int main() {
    int choice, amount;
    cout << "Welcome to currency converter!" << endl;
    cout << "Please choose a currency (1 for USD, 2 for EUR, 3 for GBP): ";
    cin >> choice;
    cout << "Please enter the amount you would like to convert: ";
    cin >> amount;

    cout << "You selected currency " << choice << " and amount " << amount << "." << endl;

    return 0;
}
步骤2:添加货币转换

我们将使用简单的if语句,根据用户的选择将金额转换成美元、欧元或英镑。由于我们省略了货币符号,将结果输出为整数,将小数点后两位四舍五入。我们还需要将转换后的金额作为整数显示,我们将使用“static_cast”将其转换回整数。

#include <iostream>

using namespace std;

int main() {
    int choice, amount;
    double convertedAmount;
    cout << "Welcome to currency converter!" << endl;
    cout << "Please choose a currency (1 for USD, 2 for EUR, 3 for GBP): ";
    cin >> choice;
    cout << "Please enter the amount you would like to convert: ";
    cin >> amount;

    if (choice == 1) {
        convertedAmount = amount / 1.16;
    } else if (choice == 2) {
        convertedAmount = amount / 0.85;
    } else {
        convertedAmount = amount / 1.39;
    }

    int finalAmount = static_cast<int>(convertedAmount + 0.5);

    cout << "Your converted amount is " << finalAmount << "." << endl;

    return 0;
}
步骤3:完善用户界面

我们将添加一些代码,将输出改进为字符串,将货币名称包括在内,并向用户提供选项重新运行转换器或退出程序。

#include <iostream>
#include <string>

using namespace std;

int main() {
    bool shouldContinue = true;
    while (shouldContinue) {
        int choice, amount;
        double convertedAmount;
        string currencyName;
        cout << "Welcome to currency converter!" << endl;
        cout << "Please choose a currency (1 for USD, 2 for EUR, 3 for GBP): ";
        cin >> choice;
        cout << "Please enter the amount you would like to convert: ";
        cin >> amount;

        if (choice == 1) {
            convertedAmount = amount / 1.16;
            currencyName = "USD";
        } else if (choice == 2) {
            convertedAmount = amount / 0.85;
            currencyName = "EUR";
        } else {
            convertedAmount = amount / 1.39;
            currencyName = "GBP";
        }

        int finalAmount = static_cast<int>(convertedAmount + 0.5);

        cout << "Your converted " << currencyName << " amount is " << finalAmount << "." << endl;

        cout << "Would you like to convert another currency? (y/n)" << endl;
        char choice;
        cin >> choice;
        shouldContinue = (choice == 'y' || choice == 'Y');
    }

    return 0;
}
步骤4:处理错误

如果用户在输入整数金额时提供了无效的输入,我们需要添加一些代码来处理它。在这种情况下,我们将要求用户输入一个有效的整数,并验证其已输入一个有效的货币类型。

#include <iostream>
#include <string>

using namespace std;

int main() {
    bool shouldContinue = true;
    while (shouldContinue) {
        int choice, amount;
        double convertedAmount;
        string currencyName;
        cout << "Welcome to currency converter!" << endl;
        
        bool isChoiceValid = false;
        while (!isChoiceValid) {
            cout << "Please choose a currency (1 for USD, 2 for EUR, 3 for GBP): ";
            cin >> choice;
            if (choice == 1 || choice == 2 || choice == 3) {
                isChoiceValid = true;
            } else {
                cout << "Invalid currency choice. Please enter 1 for USD, 2 for EUR, or 3 for GBP." << endl;
            }
        }
        
        bool isAmountValid = false;
        while (!isAmountValid) {
            cout << "Please enter the amount you would like to convert: ";
            if (cin >> amount) {
                isAmountValid = true;
            } else {
                cout << "Invalid input. Please enter a valid integer." << endl;
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
            }
        }

        if (choice == 1) {
            convertedAmount = amount / 1.16;
            currencyName = "USD";
        } else if (choice == 2) {
            convertedAmount = amount / 0.85;
            currencyName = "EUR";
        } else {
            convertedAmount = amount / 1.39;
            currencyName = "GBP";
        }

        int finalAmount = static_cast<int>(convertedAmount + 0.5);

        cout << "Your converted " << currencyName << " amount is " << finalAmount << "." << endl;

        cout << "Would you like to convert another currency? (y/n)" << endl;
        char choice;
        cin >> choice;
        shouldContinue = (choice == 'y' || choice == 'Y');
    }

    return 0;
}
结论

现在,我们已经成功地创建了一个货币转换器,使用C++编写,用户可以选择将金额从美元、欧元或英镑转换为整数。这个项目是一个良好的起点,需要更进一步完善,以使其适用于更广泛的货币转换。