重载按位运算符 XOR
overloading bitwise operator XOR
当我完成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==4
和4>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
的。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 使用模板化运算符重载 XOR 运算符失败
- 如何通过使用 2 位或更多数字的 XOR 运算符来执行此操作C++问题
- C++ XOR 运算符优先级
- 重载按位运算符 XOR
- 通过 xor 运算符使用引用或指针交换两个变量的值
- Xor运算符是如何工作的