第九周任务二:CTime类的扩展

2023-11-08 07:32
文章标签 扩展 任务 ctime 第九

本文主要是介绍第九周任务二:CTime类的扩展,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

* (程序头部注释开始)
 * 程序的版权和版本声明部分
 * Copyright (c) 2011, 烟台大学计算机学院学生
 * All rights reserved.
 * 文件名称:
 * 作者:吴瑕
 * 完成日期: 2012年 04月16日
 * 版本号:
 *对任务及求解方法的描述部分
 * 输入描述:定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

#include <iostream>
using namespace std;
class CTime
{
private:unsigned short int hour;    // 时unsigned short int minute;  // 分unsigned short int second;  // 秒
public:CTime(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}void setTime(int h,int m,int s);//比较运算符(二目)的重载bool operator > (CTime &t);bool operator < (CTime &t);bool operator >= (CTime &t);bool operator <= (CTime &t);bool operator == (CTime &t);bool operator != (CTime &t);//二目运算符的重载CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15CTime operator-(CTime &c);//对照+理解CTime operator+(int s);//返回s秒后的时间CTime operator-(int s);//返回s秒前的时间//一目运算符的重载CTime operator++(int);//后置++,下一秒CTime operator++();//前置++,下一秒CTime operator--(int);//后置--,前一秒CTime operator--();//前置--,前一秒//赋值运算符的重载     CTime operator+=(CTime &c);CTime operator-=(CTime &c);CTime operator+=(int s);//返回s秒后的时间CTime operator-=(int s);//返回s秒前的时间//运算符<<和>>的重载为友元函数friend istream &operator >> (istream&,CTime&);friend ostream &operator << (ostream&,CTime&);
};
//下面实现所有的运算符重载代码。
//为简化编程,请注意通过调用已有函数,利用好各函数之间的关系
void CTime::setTime(int h,int m,int s)
{  hour=h;  minute=m;  second=s;  }//比较运算符(二目)的重载
bool CTime::operator > (CTime &t)
{if(hour>t.hour){return true;}else {if(hour==t.hour){if(minute>t.minute){return true;}else {if(minute==t.minute){if(second>t.second){return true;}else {return false;}}else {return false;}}}}
}
bool CTime::operator < (CTime &t)
{if(hour<t.hour){return true;}else {if(hour==t.hour){if(minute<t.minute){return true;}else {if(minute==t.minute){if(second<t.second){return true;}else {return false;}}else {return false;}}} }
}
bool CTime::operator >= (CTime &t)
{if(*this<t){return false;}else{return true;}
}
bool CTime::operator <= (CTime &t)
{if(*this>t){return false;}else{return true;}
}
bool CTime::operator == (CTime &t)
{if(hour==t.hour&&minute==t.minute&&second==t.second){return true;}else{return false;}
}
bool CTime::operator != (CTime &t)
{if(*this==t){return false;}else{return true;}
}//二目运算符的重载
CTime  CTime::operator+(CTime &c)//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
{CTime  c1;c1.second=second+c.second;c1.minute=minute+c.minute;c1.hour=hour+c.hour;if(c1.second>60){c1.minute=c1.minute+c1.second/60;c1.second=c1.second%60;}if(c1.minute>60){c1.hour=c1.hour+c1.minute/60;c1.minute=c1.minute%60;}if(c1.hour>24){c1.hour=c1.hour%24;}return c1;}CTime  CTime::operator-(CTime &c)//对照+理解
{CTime  c1;if(second>c.second){c1.second=second-c.second;}else{c1.second=second+60-c.second;minute=minute-1;}if(minute>=c.minute){c1.minute=minute-c.minute;}else{c1.minute=minute+60-c.minute;hour=hour-1;}c1.hour=hour-c.hour;return c1;
}
CTime  CTime::operator+(int s)//返回s秒后的时间
{CTime  c1;c1.second=second+s;if(c1.second>=60){c1.minute=minute+c1.second/60;c1.second=c1.second%60;}else{c1.minute=minute;c1.hour=hour;}if(c1.minute>=60){c1.hour=hour+c1.minute/60;c1.minute=c1.minute%60;}else{c1.minute=minute+c1.second/60;c1.hour=hour;}if(c1.hour>24){c1.hour=hour%24;}else{c1.hour=hour;}return c1;
}
CTime  CTime::operator-(int s)//返回s秒前的时间
{ CTime  c1;if(second>=s){c1.second=second-s;c1.minute=minute;c1.hour=hour;}else{c1.second=second+60-s;c1.minute=minute-1;if(c1.minute<0){c1.minute=59;c1.hour=hour-1;}else{c1.hour=hour;}return c1;}}//一目运算符的重载
CTime CTime::operator++(int)//后置++,下一秒
{CTime temp(*this);*this=*this+1;return temp;
}
CTime CTime::operator++()//前置++,下一秒
{*this=*this+1;return (*this);
}
CTime CTime::operator--(int)//后置--,前一秒
{CTime temp(*this);*this=*this-1;return temp;
}
CTime CTime::operator--()//前置--,前一秒
{*this=*this-1;return (*this);
}//赋值运算符的重载     
CTime CTime::operator+=(CTime &c)
{*this=*this+c;return *this;
}
CTime CTime::operator-=(CTime &c)
{*this=*this-c;return *this;
}
CTime CTime::operator+=(int s)//返回s秒后的时间
{return(*this+s);
}
CTime CTime::operator-=(int s)//返回s秒前的时间
{return(*this-s);
}
//定义运算符>>重载函数istream &operator >> (istream& input,CTime& c){cout<<"请输入时间";input>>c.hour>>c.minute>>c.second;return input;}
//定义运算符<<重载函数ostream &operator << (ostream& output,CTime& c){output<<"("<<c.hour<<":"<<c.minute<<":"<<c.second<<")";return output;}
void main()
{CTime t1,t2,t;int s=30;cin>>t1>>t2;cout<<"t1为:"<<t1<<endl;cout<<"t2为:"<<t2<<endl;cout<<"下面比较两个时间大小:\n";if (t1>t2) cout<<"t1>t2"<<endl;if (t1<t2) cout<<"t1<t2"<<endl;if (t1==t2) cout<<"t1=t2"<<endl; if (t1!=t2) cout<<"t1≠t2"<<endl;if (t1>=t2) cout<<"t1≥t2"<<endl;if (t1<=t2) cout<<"t1≤t2"<<endl;cout<<endl;//下面自行设计对其他运算符的重载的测试t=t1+t2;cout<<"t1+t2为:"<<t<<endl;t=t2-t1;cout<<"t2-t1为:"<<t<<endl;t=t1+s;cout<<"t1+s为:"<<t<<endl;t=t1-s;cout<<"t1-s为:"<<t<<endl;t=t1++;cout<<"t1++为:"<<t<<endl;t=++t1;cout<<"++t1为:"<<t<<endl;t=t1--;cout<<"t1--为:"<<t<<endl;t=--t1;cout<<"--t1为:"<<t<<endl;t1+=t2;cout<<"t1+=t2为:"<<t<<endl;t1-=t2;cout<<"t1-=t2为:"<<t<<endl;t=t1-=s;cout<<"t1-=s为:"<<t<<endl;t=t1+=s;cout<<"t1+=s为:"<<t<<endl;system("pause");}


 

运行结果:

上机感言:

其实深入的看书也是学习C++的重要途径!

这篇关于第九周任务二:CTime类的扩展的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/368568

相关文章

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed

Python Invoke自动化任务库的使用

《PythonInvoke自动化任务库的使用》Invoke是一个强大的Python库,用于编写自动化脚本,本文就来介绍一下PythonInvoke自动化任务库的使用,具有一定的参考价值,感兴趣的可以... 目录什么是 Invoke?如何安装 Invoke?Invoke 基础1. 运行测试2. 构建文档3.

解决Cron定时任务中Pytest脚本无法发送邮件的问题

《解决Cron定时任务中Pytest脚本无法发送邮件的问题》文章探讨解决在Cron定时任务中运行Pytest脚本时邮件发送失败的问题,先优化环境变量,再检查Pytest邮件配置,接着配置文件确保SMT... 目录引言1. 环境变量优化:确保Cron任务可以正确执行解决方案:1.1. 创建一个脚本1.2. 修

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

如何使用celery进行异步处理和定时任务(django)

《如何使用celery进行异步处理和定时任务(django)》文章介绍了Celery的基本概念、安装方法、如何使用Celery进行异步任务处理以及如何设置定时任务,通过Celery,可以在Web应用中... 目录一、celery的作用二、安装celery三、使用celery 异步执行任务四、使用celery

什么是cron? Linux系统下Cron定时任务使用指南

《什么是cron?Linux系统下Cron定时任务使用指南》在日常的Linux系统管理和维护中,定时执行任务是非常常见的需求,你可能需要每天执行备份任务、清理系统日志或运行特定的脚本,而不想每天... 在管理 linux 服务器的过程中,总有一些任务需要我们定期或重复执行。就比如备份任务,通常会选在服务器资

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

Spring框架5 - 容器的扩展功能 (ApplicationContext)

private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");} BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanF

FreeRTOS学习笔记(二)任务基础篇

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、 任务的基本内容1.1 任务的基本特点1.2 任务的状态1.3 任务控制块——任务的“身份证” 二、 任务的实现2.1 定义任务函数2.2 创建任务2.3 启动任务调度器2.4 任务的运行与切换2.4.1 利用延时函数2.4.2 利用中断 2.5 任务的通信与同步2.6 任务的删除2.7 任务的通知2