2017-12-3 Crontab(字符串处理)
阅读原文时间:2023年07月15日阅读:2

Crontab

哈哈本人的不及格代码(暂留):

#include
#include
#include
#include
#include
using namespace std;
int n;
__int64 s,t;
char command[][][];

map mapmonth;
map mapweek;

bool month[][];
bool week[][];
bool minute[][];
bool hour[][];
bool day[][];

void judgeMon(bool* mon,char *tm,map mymap,int len)
{///判断月或是周是否符合条件
if(tm[]=='*'){
memset(mon,true,len);
return;
}
for(int i=;i<strlen(tm);)
{
if(tm[i] == ',') i++;
///如果是数字的话
int mon1 = ;
while(''<=tm[i] && tm[i]<='')
{
mon1 = mon1* + tm[i++]-'';
}
///如果是字母的话
char monstr[];int temp = ;bool flag = false;
while('A'<=tm[i]&&tm[i]<='Z' || 'a'<=tm[i]&&tm[i]<='z')
{
monstr[temp++] = tm[i++];
flag = true;
}
monstr[temp] = '\000';
if(flag) mon1 = mymap[monstr];

     mon\[mon1\] = true;

     if(tm\[i\] == ',') i++;  
     else if(tm\[i\]=='-')  
     {  
         i++;  
         ///如果是数字的话  
         int mon2 = ;  
         while(''<=tm\[i\] && tm\[i\]<='')  
         {  
             mon2 =mon2\* + tm\[i++\]-'';  
         }

         ///如果是字母的话  
         int temp = ;flag = false;  
         while('A'<=tm\[i\]&&tm\[i\]<='Z' || 'a'<=tm\[i\]&&tm\[i\]<='z'){  
             monstr\[temp++\] = tm\[i++\];  
             flag = true;  
         }  
         monstr\[temp\] = '\\000';  
         if(flag) mon2 = mymap\[monstr\];  
         for(int k=mon1;k<=mon2;k++)  
             mon\[k\] = true;  
     }  
 }  

}
void judgeNum(bool *mon,char *tm,int len)
{
if(tm[]=='*')
{
memset(mon,true,len);
return;
}
for(int i=;i<strlen(tm);)
{
if(tm[i] == ',') i++;
///如果是数字的话
int mon1 = ;
while(''<=tm[i] && tm[i]<='')
{
mon1 = mon1* + tm[i++]-'';
}

     mon\[mon1\] = true;

     if(tm\[i\] == ',') i++;  
     else if(tm\[i\]=='-')  
     {  
         i++;  
         ///如果是数字的话  
         int mon2 = ;  
         while(''<=tm\[i\] && tm\[i\]<='')  
         {  
             mon2 =mon2\* + tm\[i++\]-'';  
         }

         for(int k=mon1;k<=mon2;k++)  
             mon\[k\] = true;  
     }  
 }  

}

///年月日推算星期几
int weekday(int N, int M, int d)
{
int m, n, c, y, w;
m = (M - ) % ;
if (M >= ) n = N; else n = N - ;
c = n / ;
y = n % ;
w = (int)(d + floor( * m / ) + y + floor(y / ) + floor(c / ) - * c) % ;
while (w<) w += ;
return w;
}
bool isleapyear(int y)
{
return (y%==&&y%)||y%==;
}

int daysOfMonth(int yy,int mm)
{
if(mm == || mm== || mm== || mm== || mm== || mm== || mm==) return ;
if(mm== || mm== || mm== || mm==) return ;
if(mm == )
{
if(isleapyear(yy)) return ;
else return ;
}
return ;
}

int main()
{
mapmonth["Jan"] = ;mapmonth["Feb"] = ;mapmonth["Mar"] = ;mapmonth["Apr"] = ;mapmonth["May"] = ;
mapmonth["Jun"] = ;mapmonth["Jul"] = ;mapmonth["Aug"] = ;mapmonth["Sep"] = ;mapmonth["Oct"] = ;
mapmonth["Nov"] = ;mapmonth["Dec"] = ;
mapweek["Sun"] = ;mapweek["Mon"] = ;mapweek["Tue"] = ;mapweek["Wed"] = ;mapweek["Thu"] = ;
mapweek["Fri"] = ;mapweek["Sat"] = ;

 while(cin>>n>>s>>t)  
 {  
     for(int i=;i<n;i++)  
     {  
         for(int j=;j<;j++)  
         {  
             cin>>command\[i\]\[j\];  
         }  
     }  
     for(int i=;i<n;i++){  
         ///判断分钟  
         judgeNum(minute\[i\],command\[i\]\[\],);  
         ///判断小时  
         judgeNum(hour\[i\],command\[i\]\[\],);  
         ///判断每月的天数  
         judgeNum(day\[i\],command\[i\]\[\],);  
         ///判断月  
         judgeMon(month\[i\],command\[i\]\[\],mapmonth,);  
         ///判断周  
         judgeMon(week\[i\],command\[i\]\[\],mapweek,);  
     }

     int y,mon,d,h,min,we;

     \_\_int64 i= s;  
     y = i/;  
     mon = (i/)%;  
     d = (i/)%;  
     h = (i/)%;  
     min = i%;  
     while(i<t)  
     {  
         we = weekday(y,mon,d);//得到星期几  
         for(int j=;j<n;j++)  
         {  
             if(minute\[j\]\[min\] && hour\[j\]\[h\] && day\[j\]\[d\] && month\[j\]\[mon\] && week\[j\]\[we\])  
                 printf("%I64d %s\\n",i,command\[j\]\[\]);  
         }

         if(++min>=)  
         {  
             min = ;  
             if(++h>=)  
             {  
                 h = ;  
                 if(++d > daysOfMonth(y,mon))  
                 {  
                     d = ;  
                     if(++mon>)  
                     {  
                         mon = ;  
                         ++y;  
                     }  
                 }  
             }  
         }  
         i = y\*;  
         i \*= ;  
         i += mon\* + d\* + h\* + min;  
     }

 }  
 return ;  

}

#include
using namespace std;
char vMon[][]={"","jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
char vWek[][]={"sun","mon","tue","wed","thu","fri","sat"};
int mtharray[]={,,,,,,,,,,,,};
map mMon,mWek;
map > mrt;
void buildMonAndWekMap()
{
for(int i=;i<=;++i) mMon[vMon[i]]=i;//月份 for(int i=;i<=;++i) mWek[vWek[i]]=i;//星期 } string to_string_x(int n)//c++11 surport { stringstream ss; ss<>ans;
return ans;
}
void toStandard(string &str)//转化为标准小写
{
int len=str.size();
for(int i=;i splitStringAndbuildVector(string &str,int TAG)//TAG=0(other),1(month),2(dayofweek);
{
str+=",";
vector vret;
size_t found=str.find(",");
while(found!=string::npos)
{
string x=str.substr(,found);
str=str.substr(found+,str.size()-found-);
size_t fdx=x.find("-");
if(fdx==string::npos)//非连续值
{
if(TAG==&&isalpha(x[])) x=to_string_x(mMon[x]);//是month英文缩写,转换为数字
if(TAG==&&isalpha(x[])) x=to_string_x(mWek[x]);//是day of week英文缩写,转换为数字
if(x.size()==) x=""+x;//添加0
vret.push_back(x);
}
else//连续值
{
string L=x.substr(,fdx),R=x.substr(fdx+,x.size()-fdx-);
int left,right;
if(TAG==) left=stoi_x(L),right=stoi_x(R);
else if(TAG==)//month
{
left=(isalpha(L[]))?mMon[L]:stoi_x(L);
right=(isalpha(R[]))?mMon[R]:stoi_x(R);
}
else if(TAG==)//day of week
{
left=(isalpha(L[]))?mWek[L]:stoi_x(L);
right=(isalpha(R[]))?mWek[R]:stoi_x(R);
}
while(left<=right) { string num=to_string_x(left); if(num.size()==)num=""+num; vret.push_back(num); ++left; } } found=str.find(","); } return vret; } bool isleapyear(int y) { return (y%==&&y%)||y%==; } string getWeekday(string year,string month,string day) { int y=stoi_x(year),m=stoi_x(month),d=stoi_x(day); int by=,countday=; while(by>n>>st>>et;
string syy=st.substr(,),smm=st.substr(,),sdd=st.substr(,),sHH=st.substr(,),sMM=st.substr(,);
string eyy=et.substr(,),emm=et.substr(,),edd=et.substr(,),eHH=et.substr(,),eMM=et.substr(,);
int syInt=stoi_x(syy),eyInt=stoi_x(eyy);
while(n--)
{
vector vmts,vhur,vdfm,vmth,vdfw;
string minutes,hours,dofmon,month,dofwek,command;
cin>>minutes>>hours>>dofmon>>month>>dofwek>>command;
toStandard(month);//不区别大小写,转化为标准小写
toStandard(dofwek);//不区别大小写,转化为标准小写
if(minutes=="*") minutes="0-59";
vmts=splitStringAndbuildVector(minutes,);//应该执行的分钟
if(hours=="*") hours="0-23";
vhur=splitStringAndbuildVector(hours,); //应该执行的小时
if(dofmon=="*") dofmon="1-31";
vdfm=splitStringAndbuildVector(dofmon,);//应该执行的日期
if(month=="*") month="1-12";
vmth=splitStringAndbuildVector(month,);//应该执行的月份
if(dofwek=="*") dofwek="0-6";
vdfw=splitStringAndbuildVector(dofwek,);//应该周几执行
set wekexist;
for(size_t i=;imtharray[stoi_x(curm)])continue;
//命令行中不包含该星期或者当前天数超过当前月份的应有天数时
for(size_t Hi=;Hi=st&&datetime >::iterator it=mrt.begin();it!=mrt.end();++it)
{
map isprt;
for(size_t i=;isecond.size();++i)
{
string dis=it->first+" "+it->second[i];
if(isprt.count(dis)==)
{
cout<<dis<<endl;
isprt[dis]=;
}
}
}
return ;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器