为什么我的计算是关闭的?1 个工作日应该等于 8 小时
Why are my calculations are off? 1 work day is supposed to equal 8 hours.
我认为我的数学在二传手函数中没有正确完成。我已经盯着它太久了。提前感谢程序员!
#include <iostream>
using namespace std;
class numDays
{
int hours;
int days;
public:
numDays(int hrs); //constructor prototype
void setHours(int);
void setDays(int);
int getHours();
int getDays();
double operator+ (const numDays Object1) //overloading the + operator to return the sum of two objects' hours members
{
return hours + Object1.hours;
}
double operator-(const numDays Object1) //overloading the + operator to return the difference of two objects' hours members
{
return hours - Object1.hours;
}
numDays operator++() //prefix increment operator to increment # of hours stored in object. Days are recalculated.
{
++hours;
days = hours/8;
return *this;
}
numDays operator++(int) //postfix increment operator to increment # of hours stored in object. Days are recalculated.
{
numDays temp(hours);
hours++;
days = hours/8;
return temp;
}
numDays operator--() //prefix decrement operator to decrement # of hours stored in object. Days are recalculated.
{
--hours;
days = hours/8;
return *this;
}
numDays operator-- (int) //prefix decrement operator to decrement # of hours stored in object. Days are recalculated.
{
numDays temp(hours);
hours--;
days = hours/8;
return temp;
}
};
numDays::numDays(int hrs) //constructor that accepts a number of hours
{hours = hrs;}
我希望问题出在 setHours 函数或 setDays 函数中。
void numDays::setHours(int hrs) //mutator function to store the amount of hours
{
hours = hrs;
days = hrs/8;
}
void numDays::setDays(int d) //mutator function to store the amount of days
{
hours = d;
days = hours % 8;
}
int numDays::getHours() //accessor function to get the amount of hours
{
return hours;
}
int numDays::getDays() //accessor function to get the amount of days
{
return days;
}
int main()
{
int workHours;
numDays object2(0);
cout << "Please type in a certain amount of hours to see how much work in days it is: ";
cin >> workHours;
object2.setHours(workHours);
object2.setDays(workHours);
cout << "The number of hours you put in is " << object2.getHours() << endl;
cout << "That means you worked " <<object2.getDays() << " days " << endl;
return 0;
}
也许我没有正确理解 setDays() 的目的是做什么,但似乎这就是你想要的:
void numDays::setDays(int d) //mutator function to store the amount of days
{
days = d;
hours = days * 8;
}
相关文章:
- 为什么在读取文件大小时文件IO速度会发生变化
- 在容量内调整矢量大小时的性能影响
- 如何在新数组较小时创建新数组并将旧数组的最后一个元素复制到新数组中?
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 尝试使用 EvtSetChannelConfigProperty() 函数更新最大事件日志文件大小时插入的错误值
- 当我的阵列达到一定大小时,Qt 不起作用
- 如何遍历几个每小时一次的根(.root)文件,并将它们组合成更大的每日数据.root文件?
- const-ref传递的模板化参数是否经过优化,以便在足够小时按值传递
- 为什么在使用VK_PRESENT_MODE_FIFO_KHR呈现模式调整交换链图像大小时出现白色闪烁
- std::设置大小时,vector的性能较低
- 将秒转换为小时、分钟、秒.百分之一秒
- 获取大小时是否必须锁定 std::queue?
- 通过拖动边框调整大小时不显示SDL_WINDOWEVENT_SIZE_CHANGED?
- 时间转换为 24 小时格式 cpp
- 格式化的时间字符串在解析为time_point时会产生小时差异
- C++ 旧时间到现在的时间(以小时为单位)
- 以C++为单位将毫秒转换为小时:分钟:秒:毫秒
- 获取作为类变量的 Deque 大小时未定义的行为
- 如何在调整列表视图大小时将标题宽度设置为等于其列表视图控件宽度
- 为什么我的计算是关闭的?1 个工作日应该等于 8 小时