4.5年应该是4年零178年,我如何在C 中做到这一点

4.5 years supposed to be 4 years and 178, How I can do that In C++

本文关键字:这一点 5年 4年零 178年      更新时间:2023-10-16

标题只是一个示例。

我正在制作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.25x删除几年后剩下的几天。

相关文章: