初学c++,对取模的部分理解不透彻,求教育
我想把下面时间转换的部分用取模的方式写出来,应该怎么改啊
[code=C/C++][/code]#include <iostream>
int main()
{
using namespace std;
const float spm=60.0f;
const float mph=60.0f;
const float hpd=24.0f;
const float spd=86400.0f;
const float sph=3600.0f;
cout<<"输入要查询的秒数: ";
long seconds;
cin>>seconds;
float days=seconds/spd;
cout<<seconds<<" 秒 = "<<int(days)<<"天,";
float hours=(seconds/spd-int(days))*hpd;
cout<<int(hours)<<" 小时,";
float minutes=((seconds/spd-int(days))*hpd-int(hours))*mph;
cout<<int(minutes)<<" 分钟,";
seconds=(((seconds/spd-int(days))*hpd-int(hours))*mph-int(minutes))*spm;
cout<<int(seconds)<<" 秒.\n";
return 0;
}
[解决办法]
我猜 float hours=(seconds/spd-int(days))*hpd;==>float hours=seconds%spd 其他类似吧
[解决办法]
#include <iostream>
int main()
{
using namespace std;
const int spm=60;
const int mph=60;
const int hpd=24;
const long spd=86400;
const int sph=3600;
cout<<"输入要查询的秒数: ";
long seconds;
cin>>seconds;
cout<<seconds<<" 秒 = ";
long days=seconds/spd;
seconds=seconds%spd;
cout<<days<<"天,";
int hours=seconds/sph;
seconds=seconds%sph;
cout<<hours<<" 小时,";
int minutes=seconds/spm;
seconds=seconds%spm;
cout<<minutes<<" 分钟,";
cout<<int(seconds)<<" 秒.\n";
return 0;
}