找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1173|回复: 2

●●●●●很基础的类的题目●●●●●

[复制链接]
发表于 2009-11-3 01:44:36 | 显示全部楼层 |阅读模式 IP:江苏扬州
实现一个时钟类,要求实现以下方法:
class Time
{
private:
int hour;
int minute;
int second;
public:
void settime(int h,int m,int s); //设置时间
void showtime(); //输出时间
void run(); //每调用一次该方法,时间增加一秒
};
Input
在主函数中输入多组数据,每组两行,第一行为时分秒,中间用空格分隔。第二行为运行了多少时间:秒数n。
Output
程序读入输入的时分秒,输出运行了n秒后的时间。格式请根据样例。
Sample Input
0 3 0
4
0 0 0
60
1 2 3
45
5 6 7
12345
Sample Output
0:3:4
1:0:0
0:1:5
8:31:52
发表于 2009-11-3 01:44:38 | 显示全部楼层 IP:江苏扬州
#include<iostream>
using namespace std;
class Time
{
int hour;
int minute;
int second;
public:
void settime(int h,int m,int s); //设置时间
void showtime(){cout<<hour<<":"<<minute<<":"<<second<<endl;} //输出时间
void run(); //每调用一次该方法,时间增加一秒
};
void Time::settime(int h,int m,int s)
{
hour=h;minute=m;second=s;
m+=s/60;s%=60;
h+=m/60;h%=24;
m%=60;
}
void Time::run()
{
assert(second>=0&&second<=60);
if(second<59)
{
second+=1;
}
else if(second==59){minute+=1;second=0;}
else {minute+=1;second=1;}
}
int main()
{
cout<<"How many array you want to input: ";int arrNum;
cin>>arrNum;
Time time[arrNum];
for (int i=0;i<arrNum;i++)
{
cout<<"The "<<i+1<<"th array!\n";
int h,m,s;
cin>>h>>m>>s;time[i].settime(h,m,s);
int r;
cin>>r;
for (int j=0;j<r;j++)
{
time[i].run();
}
}
for (int k=0;k<arrNum;k++)
{
time[k].showtime();
}
system("pause");
return 0;
}
回复

使用道具 举报

发表于 2009-11-3 01:44:40 | 显示全部楼层 IP:江苏扬州
看看我这个行不?希望大家指教我一下 我是个菜鸟
#include<iostream.h>
class time
{
int hour,minute,second;
public:
time(int h,int m,int s)
{
hour=h;minute=m;second=s;
}
void run()
{
second=second+1;
if(hour>=0&&hour<=24&&minute>=0&&minute<59&&second==60)
{
second=0;
minute=minute+1;
}
else if(hour>=0&&hour<23&&minute==59&&second==60)
{
second=0;
minute=0;
hour=hour+1;
}
else if(hour==23&&minute==59&&second==60)
{
second=0;
minute=0;
hour=0;
}
}
void runto(int n)
{
for(int i=0;i<n;i++)
{
run();
}
}
void print()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
};
void main()
{
int m;
time a(23,59,59);
cout<<"intput the m:"<<endl;
cin>>m;
a.runto(m);
a.print();
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 07:25 , Processed in 0.175105 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表