| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 453 人关注过本帖
标题:●●●●●很基础的类的题目●●●●●
只看楼主 加入收藏
hztddbc614
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-5-6
收藏
 问题点数:0 回复次数:2 
●●●●●很基础的类的题目●●●●●

实现一个时钟类,要求实现以下方法:
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

搜索更多相关主题的帖子: 基础 
2006-05-06 21:28
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

#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;
}


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-05-06 22:55
heliujin
Rank: 2
等 级:论坛游民
帖 子:249
专家分:14
注 册:2006-3-14
收藏
得分:0 

看看我这个行不?希望大家指教我一下 我是个菜鸟
#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();
}


2006-05-07 11:56
快速回复:●●●●●很基础的类的题目●●●●●
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014623 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved