题目大概是这样的:输入两个时间,然后输出这两个时间相加的和。这个实现起来应该很简单,现在想做的是通用版的。那就用类来做吧。废话少说,直接上代码
#ifndef __STIME_H#define __STIME_H#include#include #include using std::string;using std::vector;using std::ostream;class STime{public: typedef vector ::iterator str_iter;//方便操作 enum TYPES{ HOUR, MINUTE, SECOND }; STime():m_hour(0),m_minute(0),m_second(0),isValite(true),m_day(0){} STime(bool validat):m_hour(0),m_minute(0),m_second(0),isValite(validat),m_day(0){} STime(int hour,int minute,int second); STime(string stime,string pattern=":"); //输入string的时间构造 void ToString(); //和 cout< <效果一样,方便以后扩展 friend stime operator+(stime o_time,stime n_time); 重载操作符方便使用 operator-(stime 重载减操作符 ostream& operator<<(ostream &out,const stime& stimer); 重载输出 bool validate(int value,types type); 所有校验都通过它来具体实施 validate( vector & str_time); //校验 vector中的时间是否合法 bool Validate(int v_arr[]); //校验由 day minute second组成的数组合法 int GetHour(void)const{return m_hour;} //得到hour int GetMinute(void)const{return m_minute;} //得到Minute int GetSecond(void)const{return m_second;} //得到Second int GetDay(void)const{return m_day;} //得到 day int GetValidate(void)const{return isValite;} //当前对象中保存的时间是否有效 void SetHour(int hour){ if(Validate(hour,HOUR)){m_hour=hour%24; m_day=m_hour/24;}} //设置hour void SetMiute(int minute){if(Validate(minute,MINUTE))m_minute=minute;} //设置minute void SetSecond(int second){if(Validate(second,SECOND))m_second=second;} //设置second long GetTotalSecond(); //得到当前时间的总秒数,返回-1表示时间有误 static STime ParseFromSecond(long seconds);//通过用户给出的秒数反推时间protected:private: void Init(int hour,int minute,int second,bool validat ); int m_hour; int m_minute; int m_second; bool isValite; //是否合法 int m_day; //输出的时候指示这是离当前时间的天数};#endif 效果一样,方便以后扩展>
这是类的实现
#include#include #include #include "stime.h"#include "strutls.h"using std::cout;using std::stringstream;STime::STime(int hour, int minute, int second){ if(Validate(minute,MINUTE) && Validate(second,SECOND)) { Init(hour,minute,second,true); }else isValite=false; }bool STime::Validate(int value,TYPES type){ int vmin=0,vmax=0; switch (type) { case HOUR: vmax=INT_MAX; break; case MINUTE: vmax=59; break; case SECOND: vmax=59; break; default: break; } return (value>=vmin) && (value<=vmax);}bool STime::Validate( vector & str_time){ //还需要实现 trim //由于stringstream 将string转int 时,非法数字会转成0所以这里还得继续判断数字合不合法 for (str_iter begin=str_time.begin(),end=str_time.end();begin!=end;begin++) { string& tmp=trim(*begin); for (size_t beg=0 ,strend=tmp.length();beg spliters=split(str_time,pattern); if (spliters.size()!=3) { isValite=false; return ; }else { if (!Validate(spliters)) { isValite=false; return ; }else { int tmp_arr[3]={0}; stringstream ss; int tmp_int=0; for (int i=0;i<3;i++) { string tmp=spliters.at(i);; ss< >tmp_int; ss.clear(); tmp_arr[i]=tmp_int; } if (!Validate(tmp_arr)) isValite=false; else Init(tmp_arr[0],tmp_arr[1],tmp_arr[2],true); } }}void STime::Init(int hour, int minute, int second, bool validat){ m_hour=hour%24; m_minute=minute; m_second=second; isValite=validat; m_day=hour/24;}//实现两个时间的相加STime operator+(STime o_time,STime n_time){ if (o_time.isValite && n_time.isValite) { int tmp_h=0; int tmp_m=0; int tmp_s=0; if ((tmp_s=o_time.m_second+n_time.m_second)>=60) { tmp_s-=60; ++tmp_m; } if ((tmp_m+=(o_time.m_minute+n_time.m_minute))>=60) { tmp_m-=60; ++tmp_h; } tmp_h+=(o_time.m_hour+n_time.m_hour); return STime(tmp_h,tmp_m,tmp_s); }else return STime(false);}STime operator-(STime o_time,STime n_time){ if (o_time.isValite && n_time.isValite) { long distance=0L; if ((distance=o_time.GetTotalSecond()-n_time.GetTotalSecond())>=0) { return STime::ParseFromSecond(distance); }else return STime(false); } return STime(false);}void STime::ToString(){ if (!isValite) cout<<"时间有误,无法格式化输出!\n"; else { cout<<"第"< <<"天:"< <<":"< <<":"<
用到的处理string的工具类
#ifndef __STRUTLS_H#define __STRUTLS_H#include#include using std::string;using std::vector;vector split(string str,string pattern){ string::size_type pos; vector result; str+=pattern;//扩展字符串以方便操作 string::size_type size=str.size(); for(string::size_type i=0; i
最后上点测试
#include#include "stime.h"using std::cout;int main(){ string str=" 12 : 45: 57";//可以在前后加空格 STime timer(str); string str2="12: 34: 1"; STime timer2(str2); STime timer3=timer+timer2; STime timer4=timer-timer2; cout<