按位运算符XOR重载

时间:2015-02-25 04:50:28

标签: c++ operator-overloading rounding

当我完成Money Class项目的工作时,我只剩下一条指令,它使用按位运算符^来舍入两个Money对象。

在我的头文件中,我有:

#ifndef MONEY_H
#define MONEY_H
#include <iostream>
using namespace std;

class Money{
    public:
        Money(int dollars, int cents);
        Money(int dollars);
        Money();
        int getDollars() const {return dollars;};
        int getCents() const {return cents;};
        void setDollarsAndCents(int dollars, int cents);
        double getAmount() const {return dollars + cents / 100.0    ;};
        void setAmount(double amount);

        //Define bit wise operator
        friend Money operator^(const Money& firstAmount, const Money& secondAmount);

        //Define the input and output operator
        friend istream& operator>>(istream& inputStream, const Money& money);
        friend ostream& operator<<(ostream& outStream, const Money& money);

    private:
        int dollars, cents;
        double amount;
};

const Money LOONIE = Money(1 , 0);
const Money TOONIE = Money(2 , 0);
const Money QUARTER = Money(0 , 25);
const Money DIME = Money(0 , 10);
const Money NICKEL = Money(0 , 5);
#endif

在我的实施文件中:

    #include "Money.h"

// Construct a money object with dollars and cents
Money::Money(int newDollars, int newCents)
{
    dollars = newDollars;
    cents = newCents;
    amount = dollars + cents/100.0;
}
// Construct a money object with JUST the dollars
Money::Money(int newDollars)
{
    dollars = newDollars;
    cents = 0;
    amount = dollars + cents;
}
// Construct a money object with no arguments (default amount = 0)
Money::Money()
{
    amount = 0.0;
}
// Set dollars and cents
void Money::setDollarsAndCents(int newDollars, int newCents)
{
    dollars = newDollars;
    cents = newCents;
    amount = dollars + cents/100.0;
}
// Set monetary amount
void Money::setAmount(double newAmount)
{
    amount = newAmount;
}

// Round up first Money object to the nearest second Money object
Money operator^(const Money& firstAmount, const Money& secondAmount)
{
    int finalDollars = firstAmount.dollars + secondAmount.dollars); 
    firstAmount.cents += secondAmount.cents/2; 
    return Money(finalDollars, ((firstAmount.cents/secondAmount.cents)*secondAmount.cents));
}
// Define the input operator
istream& operator>>(istream& inputStream, const Money& money)
{
    inputStream >> money.dollars >> money.cents;
    return inputStream;
}
// Define the output operator
ostream& operator<<(ostream& outputStream, const Money& money)
{
    outputStream << money.dollars << "." << money.cents;
   return outputStream;
}

注意:我认为我不需要做int finalDollars因为我没有四舍五入到最接近的美元。

最后在我的主要内容:

 #include "Money.h"

int main()
{

    //Test round-off operator ^
    Money m14(4 , 19); //round off to the nearest nickel : $4.19 = $4.20
    cout << (m14 ^ NICKEL) << endl;
    Money m15(-3, -1); //round off to the nearest nickel : $-3.01 = $-3.00
    cout << (m15 ^ NICKEL) << endl;

    return 0;
}

Output: 4.22
       -2.-106
编辑:太兴奋了,我忘了这个问题

如果有人能帮助我表明错误以及为什么我得错了输出,那将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

我不知道你为什么想到

unsigned int finalCents = (firstAmount.cents ^ secondAmount.cents);

会给你最近的便士。

这对我有用:

int closestPennies(int cents1, int cents2)
{
   cents1 += cents2/2;
   return (cents1/cents2)*cents2;
}

这是一个示例程序和输出:

#include <stdio.h>

int closestPennies(int cents1, int cents2)
{
   cents1 += cents2/2;
   return (cents1/cents2)*cents2;
}

void printClosestPennies(int cents1, int cents2)
{
   int closest = closestPennies(cents1, cents2);
   printf("Cents 1: %d, Cents 2: %d, Closest Cents: %d\n",
          cents1, cents2, closest);
}

int main()
{
   printClosestPennies(21, 5);
   printClosestPennies(26, 5);
   printClosestPennies(29, 5);
   printClosestPennies(21, 10);
   printClosestPennies(26, 10);
   printClosestPennies(29, 10);
   return 0;
}

输出:

Cents 1: 21, Cents 2: 5, Closest Cents: 20
Cents 1: 26, Cents 2: 5, Closest Cents: 25
Cents 1: 29, Cents 2: 5, Closest Cents: 30
Cents 1: 21, Cents 2: 10, Closest Cents: 20
Cents 1: 26, Cents 2: 10, Closest Cents: 30
Cents 1: 29, Cents 2: 10, Closest Cents: 30

答案 1 :(得分:0)

我不认为^运营商正在按照您的预期行事。我们来看你的第一个例子。

Money m14(4 , 19); //round off to the nearest nickel : $4.19 = $4.20
cout << (m14 ^ NICKEL) << endl;

评估firstAmount.cents ^ secondAmount.cents会给我们19^5。如果我们看一下二进制中的按位XOR ......

0000 0101 <-- five
0001 0011 <-- nineteen
0001 0110 <-- XOR = twenty-two 

所以你最终得到4.22作为输出。


您的问题的一个简单解决方案是使用%的模块化算法。我们可以使用19/5检查19%5的剩余部分。如果结果大于或等于5/2,我们会向上舍入(添加5和余数之间的差异),否则,我们向下舍入(减去余数)。

19%5==44>5/2因此我们使用19 + 5 - 4进行汇总以获得20

一般情况下,给定x美分和一些硬币值c,我们可以按如下方式进行舍入:

int r = x%c;     // get remainder
if (r >= c/2) {
    x += c - r;  // round up
} else {
    x -= r;      // round down
} 

在这种情况下,xfirstAmount.centscsecondAmount.cents