【问题标题】:overloading bitwise operator XOR [closed]重载按位运算符 XOR [关闭]
【发布时间】:2015-02-25 04:50:28
【问题描述】:

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

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

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

【问题讨论】:

  • @JohnKugelman 编辑:抱歉!
  • @ArcRanges 好吧,您仍然无法提供MCVE
  • @πάνταῥεῖ 对所有的混乱感到抱歉。现在一切都应该在那里。
  • 基本上这个:unsigned int finalCents = (firstAmount.cents ^ secondAmount.cents); 对舍入没有意义。你知道异或运算符是做什么的吗?
  • @MichaelBurr 可以请教一下吗?

标签: c++ operator-overloading rounding


【解决方案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;
}

输出:

美分 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

【讨论】:

  • 试过你的方法:int finalDollars = firstAmount.dollars + secondAmount.dollars); firstAmount.cents += secondAmount.cents/2; return Money(finalDollars, ((firstAmount.cents/secondAmount.cents)*firstAmount.cents));没有运气。
  • 最后一个词是不对的。它必须是secondAmount.cents
  • 我试过了,还是不行.. :(
  • @ArcRanges,不知道如何进一步提供帮助。祝你好运。
【解决方案2】:

我不认为^ 操作符正在做你期望的事情。让我们以您的第一个示例为例。

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&gt;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.centsc 将是 secondAmount.cents

【讨论】:

  • 我现在明白了。你能指导我如何解决这个问题,这样我就可以获得 4.20 美元吗?
  • @ArcRanges 我添加了一个解决方案。
  • 我尝试实现您的代码:没有运气。我想我很讨厌遵循指示。为实现您的代码而编辑的帖子。
  • @ArcRanges 我得到了与 R Sahu 的测试用例相同的结果。你到底出了什么问题。您是否收到错误消息,或者只是错误的结果?
  • 我认为程序无法识别 ^ 的运算符重载。它只是继续使用按位函数而不是正在实现的函数。
猜你喜欢
  • 2017-02-27
  • 2019-02-21
  • 2015-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多