PTA-c++-时钟模拟

一个Time类,数据成员有时、分、秒。要求模拟秒表,每次走一秒,满60秒进位,秒又从零开始计数。满60分进位,分又从零开始计数。输出时、分和秒的值。(使用重载++运算符实现)

时间类定义:

class MyTime

测试程序样例:

/* 请在这里填写答案 */

int main()
{
	MyTime t1,t2(23,59,59),t3;
	cin>>t3;
	++t1;
	cout<<t1<<endl;
	++t2;
	cout<<t2<<endl;
	++t3;
	cout<<t3<<endl;
	return 0;
}

输入样例:

12 35 59

输出样例:

0:0:1
0:0:0
12:36:0

我的代码:

#include <iostream>
#include <string>
using namespace std;

class MyTime
{
    int hour,minute,second;
public:
    MyTime()
    {hour=0;minute=0;second=0;}
    MyTime(int ,int ,int );
    MyTime operator++( );
    friend istream & operator >>(istream &,MyTime &);
    friend ostream & operator <<(ostream &,MyTime &);
    
};

MyTime MyTime::operator ++()
{
    second++;
    if(second==60){second=0;minute++;}
    if(minute==60){minute=0;hour++;}
    if(hour==24){hour=0;}
    return MyTime(hour,minute,second);
}

MyTime::MyTime(int h,int m,int s)
{
    hour=h;minute=m;second=s;
}

istream &operator >>(istream &is,MyTime &t)
{
    is>>t.hour;    is>>t.minute;   is>>t.second;
    return is;
}
ostream &operator <<(ostream &os,MyTime &t)
{
    os<<t.hour<<":"<<t.minute<<":"<<t.second;
    return os;
}

 注意<< >>也要重新写一个与之匹配的重载函数 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
程序清单 按以下步骤向视图类(CClockView)添加下列数据成员及成员函数。 (1) 添加表示年、月、日、时、分、秒的变量。 int year; int month; int day; int hour; int minute; int second; (2) 添加秒表的计数变量。 int watch; (3) 添加时钟的画笔及画刷变量。 CPen m_HouPen, m_MinPen, m_SecPen; // 各种针的画笔 CBrush m_MarkBrush; // 表盘标记的画刷 (4) 添加时钟控制变量。 CPoint m_Center; // 表的中心 double m_Radius; // 表的半径 CPoint m_Hour [2], m_OldHour [2]; // 时针当前及前一次位置 CPoint m_Minute [2], m_OldMin [2]; // 分针当前及前一次位置 CPoint m_Second [2], m_OldSec [2]; // 秒针当前及前一次位置 (5) 添加秒表的两个按钮位置变量。 CRect m_WatchStart; CRect m_WatchStop; (6) 添加两个函数,计算时钟各指针位置。 void SetClock (int hour, int minute, int second); CPoint GetPoint (int nLenth, int nValue); (7) 在视图类构造函数中增加初始化语句: CClockView::~CClockView() { //设定时间 year=2010; month=11; day=22; hour=0; minute=0; second=0; //设定画笔画刷 m_HouPen.CreatePen(PS_SOLID,5,RGB(255,0,0));//时针画笔 m_MinPen.CreatePen(PS_SOLID,3,RGB(0,0,250));//分针画笔 m_SecPen.CreatePen(PS_SOLID,1,RGB(0,0,0));//秒针画笔 m_MarkBrush.CreateSolidBrush(RGB(250,250,0)); //设定表芯位置 m_Center.x=222; m_Center.y=222; //设定时钟半径 m_Radius=222; //计算指针位置 SetClock(hour,minute,second); //设定秒表计数器及按钮位置 watch=0; m_WatchStart=CRect(480,310,560,340);//启动按钮 m_WatchStop=CRect(590,310,670,340);//停止按钮 } 编写指针位置函数SetClock和GETpOINT。 首先在ClockView.cpp文件头部下添加下面两行代码,以便进行数学计算。 #define PI 3.14159265258 #include"math.h" 然后添加下列代码: //计算个指针位置的函数 void CClockView::SetClock(int hour,int minute,int second) { hour=hour*5; hour=hour+minute/12; //保存时针原位置 m_OldHour[0]=m_Hour[0]; m_OldHour[1]=m_Hour[1];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值