#ifndef time_time_h
#define time_time_h
class time
{
public:
time();
void settime(int h, int m, int s);
void printuniversal();
void printmilitary();
void printstandard();
private:
int hour;
int minute;
int second;
};
#endif
//////////////////.hfile////////////////
#include <iostream>
using namespace std;
#include "time.h"
time::time()
{
int h, m, s;
hour=0;
minute=0;
second=0;
hour=h;
minute=m;
second=s;
}
void time::settime(int h, int m, int s)
{
if (h<=0||h>23||h==99)
{
h=0;
}
if (m<=0||m>59||m==99)
{
m=0;
}
if (s<=0||s>59||s==99)
{
s=0;
}
}
void time::printuniversal()
{
if (hour<10)
{
cout<<"0"<<hour<<":";
}
else
cout<<hour<<":";
if (minute<10)
{
cout<<"0"<<minute<<":";
}
else
cout<<minute<<":";
if (second<10)
{
cout<<"0"<<second<<"\n";
}
else
cout<<second<<"\n";
}
void time::printmilitary()
{
if (hour<10)
{
cout<<"0"<<hour<<":";
}
else
cout<<hour<<":";
if (minute<10)
{
cout<<"0"<<minute<<"\n";
}
else
cout<<minute<<"\n";
}
void time::printstandard()
{
if (hour>12)
{
hour=hour-12;
cout<<hour<<":";
}
else if(hour<10)
{
cout<<"0"<<minute<<":";
}
else
{
cout<<hour<<":";
}
if(minute<10)
{
cout<<"0"<<minute<<":";
}
else
{
cout<<minute<<":";
}
if(second<10)
{
cout<<"0"<<second;
}
else
{
cout<<second;
}
if (hour>12)
{
cout<<" PM\n";
}
else
{
cout<<"AM\n";
}
}
////////////////////cpp file//////////////
#include <iostream>
using namespace std;
#include "time.h"
int main()
{
int h, m, s;
time::time test;
test.settime(0,0,0);
test.printstandard();
test.printmilitary();
test.printuniversal();
time::time test2;
test2.settime(23, 4, 8);
test2.printstandard();
test2.printmilitary();
test2.printuniversal();
time::time test3;
test3.settime(99, 99, 99);
test3.printstandard();
test3.printmilitary();
test3.printuniversal();
cout<<"Enter hour, minutes, seconds:\n";
cin>>h>>m>>s;
time::time user;
user.settime(h, m, s);
user.printstandard();
user.printmilitary();
user.printuniversal();
return 0;
}
////////////main/////////////////////
它应该与 settime (0,0,0)、settime(23,4,8) 和 settime(99,99,99) 一起输出。给予: 00:00:00AM 00:00 00:00:00 11:04:08AM 23:04 23:04:08 00:00:00AM 00:00 00:00:00 “输入小时、分钟、秒”
但是,我得到了一些荒谬的数字: 32755:2074578800:01 PM 32755:2074578800 32755:2074578800:01 32755:2074578800:01 PM 32755:2074578800 32755:2074578800:01
我似乎无法弄清楚我做错了什么......