C ++程序中的转换

时间:2016-01-24 02:07:45

标签: c++

请查看下面这个复杂问题的图片: enter image description here 看完之后:到目前为止,我已经编写了很多代码。由于转换,我只是不知道如何将磅,炮弹和便士加在一起。我想我可能已经能够正确地加上磅和炮弹了,但是我在试图找到一种方法来加入炮弹时会发疯,同时保持转换和格式正确!请帮忙!

/*Write a program that asks the user to enter two money amounts expressed in old-pounds and will then add the two amounts and display the answer both in old-pounds and in decimal-pounds.
OLD CURRENCY
1 pound= 20 shillings
1 pound=240 pence
1 shilling= 12 pence
conversion rate from old pence to new pence *.416
*/

#include <iostream>
using namespace std;

int main()
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    int pounds=0;
    int shillings=0;
    int pence=0;
    char dot1;
    char dot2;
    int pounds2=0;
    int shillings2=0;
    int pence2=0;
    char dot12=0;
    char dot22=0;
    double Poundtotal=0;
    int shillingstotal=0;
    int pencetotal=0;
    double x;
    double y;

    cout << "Enter first old-pound amount:";
    cin >> pounds >> dot1 >> shillings >> dot2 >> pence;
    cout << "Enter second old-pound amount:";
    cin >> pounds2 >> dot12 >> shillings2 >> dot22 >> pence2;
    Poundtotal= (pounds+pounds2);
    shillingstotal=(shillings+shillings2);
    pencetotal=pence+pence2;
    x=(shillingstotal*12)+pencetotal;
    while (x>240)
    {
        Poundtotal=Poundtotal+1;
        x=x-240;
    }


    cout<<x<<endl;
    cout<<Poundtotal<<endl;

    /*need to find a way to add the rest!*/

    return 0;
}

2 个答案:

答案 0 :(得分:3)

  1. 将所有内容转换为最小单位。
  2. 将它们组合在一起。
  3. 如果需要,可以分成更大的单位。
  4. 在美国,我们最小的面额是便士。四分之一等于25便士,一分钱等于10便士。

    要添加一角硬币和四分之一,可以将它们转换为便士并添加,从而产生35便士(等值)。

    最后,使用调试器分别执行每个语句。此外,打印,显示或观察变量的内容。

答案 1 :(得分:0)

你很困惑,因为数字中没有带两个小数点的类型,对吧?然后你必须使用旧金额的字符串值和新的金额浮点数,如下所示:

#include <iostream>
#include <string>
#include <iomanip>

class Money
{
public:
    Money(int pound, int shilling, int pence):
        pence(pence),
        shilling(shilling),
        pound(pound)
    {
    }

    Money& operator+=(const Money& money)
    {
        auto newPence = this->pence + money.pence;
        auto newShilling = this->shilling + money.shilling + newPence / 12;
        this->pence = newPence % 12;
        this->shilling = newShilling % 20;
        this->pound += money.pound;
        this->pound += newShilling / 20;
    }
    std::string GetOldAmount()
    {
        return std::to_string(pound) + "." + std::to_string(shilling) + "." + std::to_string(pence);
    }
    float GetNewAmount()
    {
        auto totalPences = 1.0f * (pound * 240 + shilling * 12 + pence) / 240;
        return totalPences;
    }
private:
    int pence = 0;
    int shilling = 0;
    int pound = 0;
};

int main()
{
    int pound, shill, pence;
    char dot1, dot2;
    std::cout << "Enter first old-pound amount:";
    std::cin >> pound >> dot1 >> shill >> dot2 >> pence;
    Money money1(pound, shill, pence);
    std::cout << "Enter second old-pound amount:";
    std::cin >> pound >> dot1 >> shill >> dot2 >> pence;
    Money money2(pound, shill, pence);
    money1 += money2;
    std::cout << "Old-pound total = ";
    std::cout << money1.GetOldAmount() << std::endl;
    std::cout << "Decimal-pound total = ";
    std::cout << std::setprecision(3) << money1.GetNewAmount() << std::endl;
}

结果

Enter first old-pound amount:5.10.11
Enter second old-pound amount:3.9.5
Old-pound total = 9.0.4
Decimal-pound total = 9.02