题目大概是这样的:输入两个时间,然后输出这两个时间相加的和。这个实现起来应该很简单,现在想做的是通用版的。那就用类来做吧。废话少说,直接上代码
#ifndef __STIME_H #define __STIME_H #include<string> #include <vector> #include <ostream> using std::string; using std::vector; using std::ostream; class STime { public: typedef vector<string>::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); //重载操作符方便使用 friend STime operator-(STime o_time,STime n_time); //重载减操作符 friend ostream& operator<<(ostream &out,const STime& stimer); //重载输出 bool Validate(int value,TYPES type); //所有校验都通过它来具体实施 bool Validate( vector<string>& 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 <iostream> #include <sstream> #include <ctype.h> #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<string>& 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<strend;beg++) { if (!isdigit(tmp.at(beg))) return false; } } return true; } bool STime::Validate(int v_arr[]) { return Validate(v_arr[0],HOUR)&&Validate(v_arr[1],MINUTE) && Validate(v_arr[2],SECOND); } STime::STime(string str_time,string pattern) { vector<string> 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; 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<<"第"<<m_day<<"天:"<<m_hour<<":"<<m_minute<<":"<<m_second; } } ostream& operator<<(ostream& out,const STime& stimer) { if (!stimer.isValite) out<<"时间有误,无法格式化输出!\n"; else { out<<"第"<<stimer.m_day<<"天:"<<stimer.m_hour<<":"<<stimer.m_minute<<":"<<stimer.m_second; } return out; } long STime::GetTotalSecond() { long sums=-1L; if (isValite) { sums=0; sums+=m_second; sums+=(m_minute*60); sums+=(m_hour*3600); sums+=(m_day*86400); } return sums; } STime STime::ParseFromSecond(long seconds) { if (seconds<=0) return STime(false); else { int tmp_h=seconds/3600; int tmp_minute=(seconds%3600)/60; int tmp_second=seconds%60; return STime(tmp_h,tmp_minute,tmp_second); } }
用到的处理string的工具类
#ifndef __STRUTLS_H #define __STRUTLS_H #include <string> #include <vector> using std::string; using std::vector; vector<string> split(string str,string pattern) { string::size_type pos; vector<string> result; str+=pattern;//扩展字符串以方便操作 string::size_type size=str.size(); for(string::size_type i=0; i<size; i++) { pos=str.find(pattern,i); if(pos<size) { string s=str.substr(i,pos-i); result.push_back(s); i=pos+pattern.size()-1; } } return result; } // string trim的简单高效实现 string& LTrim(string& str) { if (str.find_first_not_of(" \n\r\t")!=string::npos){ str = str.substr(str.find_first_not_of(" \n\r\t")); } return str; } string& RTrim(string& str) { if (str.find_first_not_of(" \n\r\t")!=string::npos){ str = str.substr(0,str.find_last_not_of(" \n\r\t")+1); } return str; } string& trim(string& str) { return LTrim(RTrim(str)); } #endif
最后上点测试
#include <iostream> #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<<timer3; cout<<timer4; system("pause"); return 0; }