4.5年应该是4年零178年,我如何在C 中做到这一点
4.5 years supposed to be 4 years and 178, How I can do that In C++
标题只是一个示例。
我正在制作C 的应用程序,需要显示达到给定点所需的时间。如果我以轻速去Alpha Centauri,我将在4年133天后到达,但输出为4.367 years
。
如何将4.367 years
转换为4 years 133 days
?
#include <iostream>
using namespace std;
int main()
{
double percent = 0;
double speed_light = 1 ; // Light/Years
double alpha_centauri = 4.367;
double alpha_centauri_days = 1595.0467;
cout << "Please inter the percentage of light speed the spaceship is flying with:n";
cin >> percent;
double per_speed = percent * speed_light /100;
double year_time = alpha_centauri / per_speed ;
int days_time = alpha_centauri_days / per_speed ;
cout <<"The time required to reach alpha centauri at "<< percent
<< " percent of light speed is: n"<< year_time << " years!t" << days_time<< " days!n";
return 0;
}
做出简化的假设,即一年等于365天。然后除以365,以获取年份,Modulo 365剩下的日子。
int days_time = alpha_centauri_days / per_speed ;
cout <<"The time required to reach alpha centauri at "<< percent
<< " percent of light speed is: n"<< days_time/365 << " years!t" << days_time%365 << " days!n";
您不需要year_time
变量。
x-floor(x)
给您分数部分。
(x-floor(x))*365.25
从x
删除几年后剩下的几天。
相关文章:
- 这是什么类型的C++语法,我应该采取什么步骤来理解这一点
- 我无法理解这一点,有人可以帮我解释一下吗?
- 结构本身就包含在内.如何理解这一点?
- 只需要知道我在c ++中打印模式的方式是否有效,或者有另一种方法可以有效地做到这一点
- 我可以直接为指针分配地址吗?如果是,如何做到这一点
- 请证明这一点的合理性 C++.
- 为什么要按值传递string_view?为什么Visual Studio不能优化这一点?
- [expr.unary.op]/9 似乎暗示"运算符!()' 不能应用于下面的类型 A.但编译器不同意这一点
- 为什么我有一个无限循环,我如何以不同的方式做到这一点
- 我想使用 CMake 生成和编译 Visual Studio projet,我需要从构建中排除单元测试文件。有没有办法做到这一点?
- 所有版本的 GCC 都与默认成员初始值设定项作斗争,该初始值设定项捕获了这一点,并结合了继承的构造函数
- 如果有条件使用String.length()的条件,为什么不一致地评估这一点
- 对随机序列进行降采样会降低随机性吗?有没有原理/定理来证明这一点?
- 在C++有没有更好的方法可以做到这一点?检查哪些数字满足条件 [A*B*C = A! + B! + C!]
- 4.5年应该是4年零178年,我如何在C 中做到这一点
- 为什么添加constexpr使VS2013拒绝这一点
- 如何在GTEST中否定匹配者断言或期望这一点
- 循环工作时如何做到这一点?
- 如何做到这一点:Objet.getsObject().setssObject()
- 为什么我在使用 Qt creator 用 C 编译代码时出错,但可以使用 make 文件轻松做到这一点