重载按位运算符 XOR

overloading bitwise operator XOR

本文关键字:XOR 运算符 重载      更新时间:2023-10-16

当我完成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

编辑:太兴奋了,我忘记了这个问题

如果有人能帮助我展示出了什么问题以及为什么我得到错误的输出,那将不胜感激。谢谢!

我不知道

你为什么这么想

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: %dn",
          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;
}

输出:

美分 1: 21, 美分 2: 5, 最接近的美分: 20美分 1: 26, 美分 2: 5, 最接近的美分: 25美分 1: 29, 美分 2: 5, 最接近的美分: 30美分 1: 21, 美分 2: 10, 最接近的美分: 20美分 1: 26, 美分 2: 10, 最接近的美分: 30美分 1: 29, 美分 2: 10, 最接近的美分: 30

我不认为^运算符正在做你期望的事情。让我们举第一个例子。

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

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

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
} 

在这种情况下,x将是firstAmount.cents的,c将是secondAmount.cents的。