简体   繁体   English

"C++ 程序中的转换"

[英]Conversions in C++ program

Please see the image below for this complex problem:对于这个复杂的问题,请参见下图: 在此处输入图像描述<\/a> After reading that: So far I have a lot of the code written up.读完之后:到目前为止,我已经编写了很多代码。 I just dont know quite how to add together the pounds, shellings, and pence because of the conversions.由于转换,我只是不知道如何将英镑、炮弹和便士加在一起。 I think I may have been able to add up the pounds and shellings properly, but I'm going crazy over trying to find a way to add up the shellings, while keeping conversions and format proper!我想我可能已经能够正确地把磅和炮弹加起来,但我正在疯狂地试图找到一种方法来把炮弹加起来,同时保持转换和格式正确! Please help!请帮忙!

/*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;
}
  1. Convert everything to the smallest unit. 将所有内容转换为最小单位。
  2. Add them together. 将它们加在一起。
  3. Divide up into larger units if desired. 如果需要,可以分成更大的单位。

In the USA, our smallest denomination is the penny. 在美国,我们最小的面额是便士。 A quarter is equal to 25 pennies, a dime is equal to 10 pennies. 四分之一等于25便士,一角钱等于10便士。

To add a dime and a quarter, one would convert them to pennies and add, resulting in 35 pennies (equivalence). 要添加一角硬币和四分之一,可以将它们转换为便士并添加,从而产生35便士(等值)。

Lastly, use a debugger to execute each statement separately. 最后,使用调试器分别执行每个语句。 Also, print out, display or watch the contents of the variables. 此外,打印,显示或观察变量的内容。

You are confused because there are no types with two decimal points in the number, right? 你很困惑,因为数字中没有带两个小数点的类型,对吧? Then you have to use a string value for old amount and a float one for new amount like this: 然后你必须使用旧金额的字符串值和新的金额浮点数,如下所示:

#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;
}

Result 结果

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

Posting an answer to help others who might be stumped with the same or similar question.发布答案以帮助可能被相同或类似问题难倒的其他人。 You can chain the inputs collected through cin.您可以链接通过 cin 收集的输入。 You can modify this code to add the old pound.您可以修改此代码以添加旧英镑。

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

int main()
{
    cout << "Enter the amount in pound-shillings-pence format (e.g. 5.2.8): ";
    int pounds, shillings, pence;
    char separator;
    cin >> pounds >> separator >> shillings >> separator >> pence;

    //Imperial pound to imperial pence (1 pound = 240 pence)
    pence += 240 * pounds;
    //Imperial shilling to pence (1 Shilling = 12 pence)
    pence += 12 * shillings;
    //Decimal pounds
    //Unicode for the pound sign = \u00A3
    cout << "Decimal pounds: \u00A3" << fixed << setprecision(2) << (float) pence/240 << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM