Напишите программу, объединяющую в себе классы bMoney

Напишите программу, объединяющую в себе классы bMoney из упражнения 8 и sterling из упражнения 11. Напишите операцию преобразования для преобразования между классами bMoney и sterling, предполагая, что один фунт (£1.0.0) равен пятидесяти долларам ($50.00). Это приблизительный курс обмена для XIX века, когда Британская империя еще использовала меру фунты-шиллинги-пенсы. Напишите программу main(), которая позволит пользователю вводить суммы в каждой из валют и преобразовывать их в другую валюту с выводом результата. Минимизируйте количество изменений в существующих классаx bMoney и sterling. Не забудьте using namespace std
Дополнительные параметры: codeFlowType: codeWrite codeFlowLang: C++
Создано: 11.07.2023 14:05 Обновлено: 1 год назад

Напишите программу, объединяющую в себе классы bMoney из упражнения 8 и sterling из упражнения 11. Напишите операцию преобразования для преобразования между классами bMoney и sterling, предполагая, что один фунт (£1.0.0) равен пятидесяти долларам ($50.00). Это приблизительный курс обмена для XIX века, когда Британская империя еще использовала меру фунты-шиллинги-пенсы. Напишите программу main(), которая позволит пользователю вводить суммы в каждой из валют и преобразовывать их в другую валюту с выводом результата. Минимизируйте количество изменений в существующих классаx bMoney и sterling. Не забудьте using namespace std

#include <iostream>
#include <string>
using namespace std;

class sterling;

class bMoney {
  private:
    string pounds;
    string shillings;
    string pence;
  public:
    bMoney() {
        pounds = shillings = pence = "0";
    }

    bMoney(double money) {
        pounds = to_string(static_cast<long long>(money));
        double fractional_part = money - static_cast<long long>(money);
        fractional_part *= 20;
        shillings = to_string(static_cast<long long>(fractional_part));
        fractional_part -= static_cast<long long>(fractional_part);
        fractional_part *= 12;
        pence = to_string(static_cast<long long>(fractional_part));
    }

    operator sterling();

    friend bMoney operator+(const bMoney &num1, const bMoney &num2);
    friend ostream& operator<<(ostream& outputStream, const bMoney &money);
    friend istream& operator>>(istream& inputStream, bMoney &money);
};

class sterling {
  private:
    long pounds;
    int shillings;
    int pence;
  public:
    sterling() {
        pounds = shillings = pence = 0;
    }

    sterling(double money) {
        pounds = static_cast<long>(money);
        double fractional_part = money - static_cast<long>(money);
        fractional_part *= 20;
        shillings = static_cast<int>(fractional_part);
        fractional_part -= static_cast<int>(fractional_part);
        fractional_part *= 12;
        pence = static_cast<int>(fractional_part);
    }

    operator bMoney();

    friend sterling operator+(const sterling &num1, const sterling &num2);
    friend ostream& operator<<(ostream& outputStream, const sterling &money);
    friend istream& operator>>(istream& inputStream, sterling &money);
};

bMoney::operator sterling() {
    double doublePounds = stod(pounds);
    double doubleShillings = stod(shillings);
    double doublePence = stod(pence);

    double totalShillings = doublePence / 12 + doubleShillings;
    double totalPounds = totalShillings / 20 + doublePounds;

    return sterling(totalPounds);
}

sterling::operator bMoney() {
    double doublePounds = static_cast<double>(pounds);
    double doubleShillings = static_cast<double>(shillings);
    double doublePence = static_cast<double>(pence);

    double totalShillings = doublePounds * 20 + doubleShillings;
    double totalPence = totalShillings * 12 + doublePence;

    return bMoney(totalPence / 240);
}

bMoney operator+(const bMoney &num1, const bMoney &num2) {
    double sum = stod(num1.pounds) + stod(num2.pounds);
    sum += (stod(num1.shillings) + stod(num2.shillings)) / 20;
    sum += (stod(num1.pence) + stod(num2.pence)) / 240;

    return bMoney(sum);
}

sterling operator+(const sterling &num1, const sterling &num2) {
    double sum = num1.pounds + num2.pounds;
    sum += (num1.shillings + num2.shillings) / 20;
    sum += (num1.pence + num2.pence) / 240;

    return sterling(sum);
}

ostream& operator<<(ostream& outputStream, const bMoney &money) {
    outputStream << "£" << money.pounds << "." << money.shillings << "." << money.pence;
    return outputStream;
}

ostream& operator<<(ostream& outputStream, const sterling &money) {
    outputStream << "£" << money.pounds << "." << money.shillings << "." << money.pence;
    return outputStream;
}

istream& operator>>(istream& inputStream, bMoney &money) {
    cout << "Enter pounds: ";
    inputStream >> money.pounds;
    cout << "Enter shillings: ";
    inputStream >> money.shillings;
    cout << "Enter pence: ";
    inputStream >> money.pence;
    return inputStream;
}

istream& operator>>(istream& inputStream, sterling &money) {
    cout << "Enter pounds: ";
    inputStream >> money.pounds;
    cout << "Enter shillings: ";
    inputStream >> money.shillings;
    cout << "Enter pence: ";
    inputStream >> money.pence;
    return inputStream;
}

int main() {
    bMoney b;
    sterling s;

    cout << "Enter a money amount in bMoney format:" << endl;
    cin >> b;

    s = b;
    cout << "Equivalent amount in sterling format: " << s << endl;

    cout << "Enter a money amount in sterling format:" << endl;
    cin >> s;

    b = s;
    cout << "Equivalent amount in bMoney format: " << b << endl;

    return 0;
}

Пример ввода и вывода:

Enter a money amount in bMoney format:
Enter pounds: 5
Enter shillings: 10
Enter pence: 6
Equivalent amount in sterling format: £5.10.3
Enter a money amount in sterling format:
Enter pounds: 3
Enter shillings: 12
Enter pence: 9
Equivalent amount in bMoney format: £3.12.9