自己用C++补写的电子钟

自己用C++补写的电子钟,感动!!!!喜欢自己!!!

设计一款电子钟类,用于显示时、分、秒

1.       含有形参有默认值的默认构造函数;

2.       重载 前缀++ 和 后缀—用于调整时间,每次调整均对秒进行调整,若秒满60,则分加1,若分满60则时加1,时满24,则清零重新开始;

3.       重载插入运算符 >> 用于输入(设定)时间;

4.       重载插入运算符 << 用于输出时间。

#pragmaonce
#include<windows.h>//
#include<iostream>
usingnamespacestd;
classtime
{
       inthour;
       intminute;
       intsecond;
public:
       friendistream&operator>>(istream&,time&);
       friendostream&operator<<(ostream&,time&);
       //friend int operator==(time&, time&);
       voidoperator++();
       voidoperator--();
    time(void);
       ~time(void);
};
#include"time.h"
time::time(void):hour(0),minute(0),second(0){}
istream&operator>>(istream&input,time&a)
{
input>>a.hour>>a.minute>>a.second;
returninput;
}
 ostream&operator<<(ostream&output,time&a)
 {
output<<a.hour<<":"<<a.minute<<":"<<a.second<<endl;
returnoutput;
 }
 voidtime::operator++()
 {
 second++;
 if(second>=60)
 {minute++;
 second=0;}
 if(minute>=60)
 {hour++;
 minute=0;
 hour%=24;
 //当hour=12时:hour%=24;的结果还是12;
 //当hour=24时:hour%=24;的结果是0;
 //当hour=25时:hour%=24;的结果是1;
 //这个能保证:时满24,则清零重新开始
 //记住这种算法就行了,这个老师没讲过,但是很好用
 }
 }
 voidtime::operator--()
 {
 second--;
 if(second<=0)
 {minute--;second=59;}
 if(minute<0)
 {minute=59;hour--;}
 if(hour<0)
        hour=23;
 }
 //普通等价函数
/*int operator==(time&a,time&b)
 {
         bool found;
         if(a.hour == b.hour&&
         a.minute == b.minute&&
         a.second == b.second)
          bool found=true;
         else bool found=false;
         return found;
 }*/
time::~time(void){}
#include"time.h"
intmain()
{
       cout<<"选择功能:"<<endl<<"钟表功能输入1;"<<'\t'<<"正计时功能输入2;"<<'\t'<<"正计时功能输入3;"<<endl;
       inti;
       cin>>i;
       while(i==1)
{
       cout<<"请先校准时间:"<<endl;
       timea;
       cin>>a;
       for(;;a++)
       {
              system("cls");//清屏操作
              //先对秒加,在对分钟、小时加
              cout<<a;
              Sleep(1000);//间隔为1秒
       }
}
while(i==2)
{
       timea;
       cout<<"正计时功能:"<<endl;
       for(;;a++)
       {
              system("cls");//清屏操作
              //a++;//先对秒加,在对分钟、小时加
              cout<<a;
              Sleep(1000);//间隔为1秒
       }
}
while(i==3)
{
       cout<<"倒计时功能:";
       timea;
       timeb;
       cout<<"请输入时间:";
       cin>>a;
       for(;;a--)
       {
              system("cls");
        cout<<a;//运算符对应的数据也是受限制的;不太灵活
              //cout<<a--;无法实现
              Sleep(1000);//间隔为1秒
              /*
                  if(a==b)
                  {
                           cout << "STOP!" << endl;
                           return 0;
                  }*/
       }
}
return0;
}

 

转载于:https://www.cnblogs.com/daisy99lijing/p/9596510.html

程序清单 按以下步骤向视图类(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、付费专栏及课程。

余额充值