实训C++语言设计——Clock模拟时钟系统

news/2025/2/27 11:24:32

平台:VC++ 2005 测试通过!
.vcproj
这是使用应用程序向导生成的 VC++ 项目的主项目文件。
它包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。
StdAfx.h, StdAfx.cpp
这些文件用于生成名为 twod.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。
这些都是使用应用程序向导生成的 VC++ 文件故不列出
我只列出程序主要部分!

/*
   本例中,通过CClock类型的实现演示了C++中
   操作符重载,类型的显式,隐式转换.
   1. 为CClock类型提供++,+,-三个操作符重载函数,
       使得++,+,-三个操作符可用于CClock类型的变量
    的运算.
   2. 对于客户代码中的t3 = t1 * 5; t3 = 6 * t3;要实现
       此功能有两种途径:
    2.1 通过CClock类型的类型转换函数
          CClock(unsigned long i);  将unsigned long转换为CClock
    operator unsigned long();将CClock转换为unsigned long
    编译器将隐式地调用上述转换函数来支持
    t3 = t1 * 5; t3 = 6 * t3;运算,具体过程是:
    编译器隐式地调用operator unsigned long(),将
             t1 * 5,6 * t3中的t1,t3自动转换为unsigned long
    然后执行*法运算,然后,再调用CClock(unsigned long i)
    将结果转换为CClock类型赋值给t3.
  2.2 通过实现CClock类型的*法重载函数,来支持CClock
       类型与unsigned long类型的*法运算.实现*法重载的
    方式又有两种:
    2.2.1 实现CClock类型的*法重载"成员函数"
              CClock operator *(unsigned long m);此函数
        只支持诸如:t1 * 5方式的运算,而5*t1运算是
        不支持的.
       2.2.2 实现CClock类型的*法重载"友元函数"
             friend CClock operator *(unsigned long m, CClock c);  
                      friend CClock operator *(CClock c, unsigned long m);
       可支持t1 * 5运算,及5*t1运算.

*/

#include <iostream>
using namespace std;

class CClock {
   //下面是三个全局friend函数
 friend CClock operator+(int i, const CClock& c);
 friend CClock operator+(const CClock& c, int i);
   //friend CClock operator +(const CClock& c1, const CClock& c2);  
   */ 
public:
   /*constructor作为ADT conversions
       此处的构造函数CClock可以
    把unsigned long类型的变量
       转换为CClock类型.在C++中凡是
    只带一个参数的构造函数
       都定义了一个隐式的转换*/  
   CClock(unsigned long i);  
   CClock(const CClock& rc);

   void  print() const;      //formatted printout
   void  tick();             //add one second

   /*重载++,-两个操作符,使它们可
   用于CClock的变量*/
   CClock operator++(){ tick(); return *this; }
   CClock operator++(int){ tick(); return *this; }
   CClock operator +(const CClock& c);
  // CClock operator +(int i);
   /*CClock operator -(const CClock&rc);
   //CClock operator *(unsigned long m);
   
   /*定义一个由CClock类型转换为unsigned long类型
   变量的函数*/
   operator unsigned long(){ return this->_totsecs;};
private:
   unsigned long  _totsecs; //时间总长
   unsigned long  _secs, _mins, _hours, _days;//秒,分,小时,天
};

CClock::CClock(unsigned long i) {
 /*将时间转换为秒,分,小时,天表示*/
   _totsecs = i;
   _secs = _totsecs % 60;
   _mins = (_totsecs / 60) % 60;
   _hours = (_totsecs  / 3600) % 24;
   _days = _totsecs  / 86400;
}

CClock::CClock(const CClock& rc){
   _totsecs = rc._totsecs;
   _secs = rc._secs;  _mins = rc._mins;
   _hours = rc._hours; _days = rc._days;
}


void CClock::tick(){
   CClock  temp(++_totsecs);
   _secs = temp._secs;  _mins = temp._mins;
   _hours = temp._hours;  _days = temp._days;
}

CClock CClock::operator +(const CClock& rc){
 /*注意此函数返回值要求是CClock,
 而return语句的内容是unsigned long
 类型的this->_totsecs + rc._totsecs,隐式
 类型转换发生*/
 return (this->_totsecs + rc._totsecs);
}
/*
CClock CClock::operator -(const CClock& rc){
    return (this->_totsecs - rc._totsecs);
}*/


/*CClock CClock::operator *(unsigned long m){
 _totsecs = _totsecs * m;
 return(this->_totsecs);
}
*/

/*CClock operator+(CClock c1, CClock c2){
   //注意此处c1._totsecs + c2._totsecs是unsigned long
   //但函数的返回值类型是CClock,编译器自动调用了类CClock的
   //构造函数进行了ADT Conversion处理
   return (c1._totsecs + c2._totsecs);
}

CClock CClock::operator-(CClock c){
   return (_totsecs - c._totsecs);
}


CClock operator*(unsigned long m, CClock c)
{
   return (m * c._totsecs);
}

CClock operator*(CClock c, unsigned long m)
{
   return (c * m);
}
*/

/*CClock operator+(const CClock& c1, const CClock& c2){
  return (c1._totsecs + c2._totsecs);
}
*/

CClock operator+(int i, const CClock& c){
  return (i + c._totsecs);
}

/*CClock CClock::operator +(int i){
    return ( i + _totsecs);
}*/

CClock operator+(const CClock& c, int i){
  return (i + c._totsecs);
}

void CClock::print() const
{
   cout << _days << " d :" << _hours << " h :"
        << _mins << " m :" << _secs << " s" << endl;
}
 

 

// ClockPro.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "CClock.h"


int _tmain(int argc, _TCHAR* argv[])
{
 CClock  t1(59), t2(172799); //172799 = 2 days - 1 sec
    CClock t3(0);
    CClock c1(900), c2(400);

    cout << "initial times are" << endl;
    t1.print();  t2.print();
 
 int i;
 cin >> i;
 (t1 + t2).print();
 (i + t1).print(); 
 //(t1 + i).print(); 

   cout << "after one second times are" << endl;
   /*(++t1).print();  (++t2).print();

   t3 = t1 + t2;     t3.print();
   t3 = t3 - t1;      t3.print();
   /*程序中并没有实现*操作符的重载
   此处的*运算是如何实现的呢?*/
   /*t3 = t1 * 5;       t3.print();
   t3 = 6 * t3;      t3.print();
 
   cout << "/n c1 and c2 /n";
   c1.print();   c2.print();
   */
 return 0;
}

 


http://www.niftyadmin.cn/n/3242391.html

相关文章

KEIL 268错误,声明不能出现在块中可执行语句之后

user\sx5g.c(67): error: #268: declaration may not appear after executable statement in block 声明不能出现在块中可执行语句之后

实训C++语言设计——COMPUTER电脑类设计

平台&#xff1a;VC 2005 测试通过&#xff01;.vcproj这是使用应用程序向导生成的 VC 项目的主项目文件。 它包含生成该文件的 Visual C 的版本信息&#xff0c;以及有关使用应用程序向导选择的平台、配置和项目功能的信息。StdAfx.h, StdAfx.cpp这些文件用于生成名为 twod.pc…

一个代码轻松搞定continue,break,goto

continue语句只结束本次循环 break 结束整个循环 goto无条件跳转语句

对话框管理器第六章:消息循环中的细节

在上一篇文章中&#xff0c;我留下了一道课后作业&#xff1a;找到EndManualModalDialog和模态对话框消息循环之间的细微Bug。 微妙之处在于&#xff1a;EndManualModalDialog会在内部设置了一些标志&#xff0c;但没有强制消息循环注意到该标志已实际设置。回想一下&#xff…

python转xml为dbc问题解决Unable to import ‘win32ui‘

翻译&#xff1a;没有导入win32ui包 如果单纯的下载win32ui会提示报错&#xff0c;没有相应的版本 PS C:\Users\17548> python -m pip install win32ui Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple ERROR: Could not find a version that satisfies the…

实训C++语言设计——多项式Polynomial运算

平台&#xff1a;VC 2005 测试通过&#xff01;.vcproj这是使用应用程序向导生成的 VC 项目的主项目文件。 它包含生成该文件的 Visual C 的版本信息&#xff0c;以及有关使用应用程序向导选择的平台、配置和项目功能的信息。StdAfx.h, StdAfx.cpp这些文件用于生成名为 twod.pc…

Qt 工具栏菜单栏显示图标文字

mainToolBar QToolBar QAction 菜单栏中输入内容&#xff0c;增加列表&#xff0c;在下栏Actio Editor中会出现一个actionfind 点击右键编辑可以添加图标 拖拽到细小的&#xff1a;&#xff08;mainToolBar&#xff09;上&#xff0c;同理可以创建其他的菜单按钮 关于功能&am…

Visual Studio: Arm64EC官方支持来了

去年&#xff0c;微软官宣了一则消息&#xff1a;通过Arm64EC ABI技术&#xff0c;x64版本的应用程序可以正常运行在ARM架构上的Windows 11&#xff0c;这是一种将应用程序带到ARM版本Windows的新方式。 借助 Arm64EC&#xff0c;你可以在同一进程中混合使用 Arm 和 x64 代码&a…