2017-12-3 Crontab(字符串处理)
Crontab
哈哈本人的不及格代码(暂留):
1 #include<iostream> 2 #include<queue> 3 #include<cmath> 4 #include<map> 5 #include<cstring> 6 using namespace std; 7 int n; 8 __int64 s,t; 9 char command[21][6][100]; 10 11 map<string,int> mapmonth; 12 map<string,int> mapweek; 13 14 bool month[21][13]; 15 bool week[21][7]; 16 bool minute[21][61]; 17 bool hour[21][24]; 18 bool day[21][32]; 19 20 void judgeMon(bool* mon,char *tm,map<string,int> mymap,int len) 21 {///判断月或是周是否符合条件 22 if(tm[0]=='*'){ 23 memset(mon,true,len); 24 return; 25 } 26 for(int i=0;i<strlen(tm);) 27 { 28 if(tm[i] == ',') i++; 29 ///如果是数字的话 30 int mon1 = 0; 31 while('0'<=tm[i] && tm[i]<='9') 32 { 33 mon1 = mon1*10 + tm[i++]-'0'; 34 } 35 ///如果是字母的话 36 char monstr[100];int temp = 0;bool flag = false; 37 while('A'<=tm[i]&&tm[i]<='Z' || 'a'<=tm[i]&&tm[i]<='z') 38 { 39 monstr[temp++] = tm[i++]; 40 flag = true; 41 } 42 monstr[temp] = '\000'; 43 if(flag) mon1 = mymap[monstr]; 44 45 mon[mon1] = true; 46 47 if(tm[i] == ',') i++; 48 else if(tm[i]=='-') 49 { 50 i++; 51 ///如果是数字的话 52 int mon2 = 0; 53 while('0'<=tm[i] && tm[i]<='9') 54 { 55 mon2 =mon2*10 + tm[i++]-'0'; 56 } 57 58 ///如果是字母的话 59 int temp = 0;flag = false; 60 while('A'<=tm[i]&&tm[i]<='Z' || 'a'<=tm[i]&&tm[i]<='z'){ 61 monstr[temp++] = tm[i++]; 62 flag = true; 63 } 64 monstr[temp] = '\000'; 65 if(flag) mon2 = mymap[monstr]; 66 for(int k=mon1;k<=mon2;k++) 67 mon[k] = true; 68 } 69 } 70 } 71 void judgeNum(bool *mon,char *tm,int len) 72 { 73 if(tm[0]=='*') 74 { 75 memset(mon,true,len); 76 return; 77 } 78 for(int i=0;i<strlen(tm);) 79 { 80 if(tm[i] == ',') i++; 81 ///如果是数字的话 82 int mon1 = 0; 83 while('0'<=tm[i] && tm[i]<='9') 84 { 85 mon1 = mon1*10 + tm[i++]-'0'; 86 } 87 88 mon[mon1] = true; 89 90 if(tm[i] == ',') i++; 91 else if(tm[i]=='-') 92 { 93 i++; 94 ///如果是数字的话 95 int mon2 = 0; 96 while('0'<=tm[i] && tm[i]<='9') 97 { 98 mon2 =mon2*10 + tm[i++]-'0'; 99 } 100 101 for(int k=mon1;k<=mon2;k++) 102 mon[k] = true; 103 } 104 } 105 } 106 107 108 ///年月日推算星期几 109 int weekday(int N, int M, int d) 110 { 111 int m, n, c, y, w; 112 m = (M - 2) % 12; 113 if (M >= 3) n = N; else n = N - 1; 114 c = n / 100; 115 y = n % 100; 116 w = (int)(d + floor(13 * m / 5) + y + floor(y / 4) + floor(c / 4) - 2 * c) % 7; 117 while (w<0) w += 7; 118 return w; 119 } 120 bool isleapyear(int y) 121 { 122 return (y%4==0&&y%100)||y%400==0; 123 } 124 125 int daysOfMonth(int yy,int mm) 126 { 127 if(mm == 1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12) return 31; 128 if(mm==4 || mm==6 || mm==9 || mm==11) return 30; 129 if(mm == 2) 130 { 131 if(isleapyear(yy)) return 29; 132 else return 28; 133 } 134 return 0; 135 } 136 137 int main() 138 { 139 mapmonth["Jan"] = 1;mapmonth["Feb"] = 2;mapmonth["Mar"] = 3;mapmonth["Apr"] = 4;mapmonth["May"] = 5; 140 mapmonth["Jun"] = 6;mapmonth["Jul"] = 7;mapmonth["Aug"] = 8;mapmonth["Sep"] = 9;mapmonth["Oct"] = 10; 141 mapmonth["Nov"] = 11;mapmonth["Dec"] = 12; 142 mapweek["Sun"] = 0;mapweek["Mon"] = 1;mapweek["Tue"] = 2;mapweek["Wed"] = 3;mapweek["Thu"] = 4; 143 mapweek["Fri"] = 5;mapweek["Sat"] = 6; 144 145 while(cin>>n>>s>>t) 146 { 147 for(int i=0;i<n;i++) 148 { 149 for(int j=0;j<6;j++) 150 { 151 cin>>command[i][j]; 152 } 153 } 154 for(int i=0;i<n;i++){ 155 ///判断分钟 156 judgeNum(minute[i],command[i][0],60); 157 ///判断小时 158 judgeNum(hour[i],command[i][1],25); 159 ///判断每月的天数 160 judgeNum(day[i],command[i][2],32); 161 ///判断月 162 judgeMon(month[i],command[i][3],mapmonth,13); 163 ///判断周 164 judgeMon(week[i],command[i][4],mapweek,7); 165 } 166 167 int y,mon,d,h,min,we; 168 169 __int64 i= s; 170 y = i/100000000; 171 mon = (i/1000000)%100; 172 d = (i/10000)%100; 173 h = (i/100)%100; 174 min = i%100; 175 while(i<t) 176 { 177 we = weekday(y,mon,d);//得到星期几 178 for(int j=0;j<n;j++) 179 { 180 if(minute[j][min] && hour[j][h] && day[j][d] && month[j][mon] && week[j][we]) 181 printf("%I64d %s\n",i,command[j][5]); 182 } 183 184 if(++min>=60) 185 { 186 min = 0; 187 if(++h>=24) 188 { 189 h = 0; 190 if(++d > daysOfMonth(y,mon)) 191 { 192 d = 1; 193 if(++mon>12) 194 { 195 mon = 1; 196 ++y; 197 } 198 } 199 } 200 } 201 i = y*1000; 202 i *= 100000; 203 i += mon*1000000 + d*10000 + h*100 + min; 204 } 205 206 } 207 return 0; 208 }
1 #include <bits/stdc++.h> 2 using namespace std; 3 char vMon[][4]={"","jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"}; 4 char vWek[][4]={"sun","mon","tue","wed","thu","fri","sat"}; 5 int mtharray[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 6 map<string,int> mMon,mWek; 7 map<string,vector<string> > mrt; 8 void buildMonAndWekMap() 9 { 10 for(int i=1;i<=12;++i) mMon[vMon[i]]=i;//月份 11 for(int i=0;i<=6;++i) mWek[vWek[i]]=i;//星期 12 } 13 string to_string_x(int n)//c++11 surport 14 { 15 stringstream ss; 16 ss<<n; 17 return ss.str(); 18 } 19 int stoi_x(const string &str)//c++11 surport 20 { 21 int ans; 22 stringstream ss(str); 23 ss>>ans; 24 return ans; 25 } 26 void toStandard(string &str)//转化为标准小写 27 { 28 int len=str.size(); 29 for(int i=0;i<len;++i)str[i]=tolower(str[i]); 30 } 31 vector<string> splitStringAndbuildVector(string &str,int TAG)//TAG=0(other),1(month),2(dayofweek); 32 { 33 str+=","; 34 vector<string> vret; 35 size_t found=str.find(","); 36 while(found!=string::npos) 37 { 38 string x=str.substr(0,found); 39 str=str.substr(found+1,str.size()-found-1); 40 size_t fdx=x.find("-"); 41 if(fdx==string::npos)//非连续值 42 { 43 if(TAG==1&&isalpha(x[0])) x=to_string_x(mMon[x]);//是month英文缩写,转换为数字 44 if(TAG==2&&isalpha(x[0])) x=to_string_x(mWek[x]);//是day of week英文缩写,转换为数字 45 if(x.size()==1) x="0"+x;//添加0 46 vret.push_back(x); 47 } 48 else//连续值 49 { 50 string L=x.substr(0,fdx),R=x.substr(fdx+1,x.size()-fdx-1); 51 int left,right; 52 if(TAG==0) left=stoi_x(L),right=stoi_x(R); 53 else if(TAG==1)//month 54 { 55 left=(isalpha(L[0]))?mMon[L]:stoi_x(L); 56 right=(isalpha(R[0]))?mMon[R]:stoi_x(R); 57 } 58 else if(TAG==2)//day of week 59 { 60 left=(isalpha(L[0]))?mWek[L]:stoi_x(L); 61 right=(isalpha(R[0]))?mWek[R]:stoi_x(R); 62 } 63 while(left<=right) 64 { 65 string num=to_string_x(left); 66 if(num.size()==1)num="0"+num; 67 vret.push_back(num); 68 ++left; 69 } 70 } 71 found=str.find(","); 72 } 73 return vret; 74 } 75 bool isleapyear(int y) 76 { 77 return (y%4==0&&y%100)||y%400==0; 78 } 79 string getWeekday(string year,string month,string day) 80 { 81 int y=stoi_x(year),m=stoi_x(month),d=stoi_x(day); 82 int by=1970,countday=0; 83 while(by<y) 84 { 85 countday+=(isleapyear(by))?366:365; 86 ++by; 87 } 88 for(int i=1;i<m;++i) countday+=mtharray[i]; 89 countday+=d-1; 90 return "0"+to_string_x((4+countday%7)%7); 91 } 92 int main() 93 { 94 int n; 95 string st,et; 96 buildMonAndWekMap(); 97 cin>>n>>st>>et; 98 string syy=st.substr(0,4),smm=st.substr(4,2),sdd=st.substr(6,2),sHH=st.substr(8,2),sMM=st.substr(10,2); 99 string eyy=et.substr(0,4),emm=et.substr(4,2),edd=et.substr(6,2),eHH=et.substr(8,2),eMM=et.substr(10,2); 100 int syInt=stoi_x(syy),eyInt=stoi_x(eyy); 101 while(n--) 102 { 103 vector<string> vmts,vhur,vdfm,vmth,vdfw; 104 string minutes,hours,dofmon,month,dofwek,command; 105 cin>>minutes>>hours>>dofmon>>month>>dofwek>>command; 106 toStandard(month);//不区别大小写,转化为标准小写 107 toStandard(dofwek);//不区别大小写,转化为标准小写 108 if(minutes=="*") minutes="0-59"; 109 vmts=splitStringAndbuildVector(minutes,0);//应该执行的分钟 110 if(hours=="*") hours="0-23"; 111 vhur=splitStringAndbuildVector(hours,0); //应该执行的小时 112 if(dofmon=="*") dofmon="1-31"; 113 vdfm=splitStringAndbuildVector(dofmon,0);//应该执行的日期 114 if(month=="*") month="1-12"; 115 vmth=splitStringAndbuildVector(month,1);//应该执行的月份 116 if(dofwek=="*") dofwek="0-6"; 117 vdfw=splitStringAndbuildVector(dofwek,2);//应该周几执行 118 set<string> wekexist; 119 for(size_t i=0;i<vdfw.size();++i) wekexist.insert(vdfw[i]);//更快的检索当前日期(dayofweek)是不是应该执行 120 int curyear=syInt;//从开始年份执行 121 while(curyear<=eyInt) 122 { 123 if(isleapyear(curyear)) mtharray[2]=29;//leapyear的2月份应该是29天 124 else mtharray[2]=28; 125 string year=to_string_x(curyear);//年份 126 for(size_t mi=0;mi<vmth.size();mi++)//month 127 { 128 string curm=vmth[mi];//当前月份 129 for(size_t di=0;di<vdfm.size();di++)//day of month 130 { 131 string curd=vdfm[di];//当前日期 132 string wd=getWeekday(year,curm,curd);//该年,该月,该日是星期几 133 if(wekexist.count(wd)==0||stoi_x(curd)>mtharray[stoi_x(curm)])continue; 134 //命令行中不包含该星期或者当前天数超过当前月份的应有天数时 135 for(size_t Hi=0;Hi<vhur.size();++Hi)//hour 136 { 137 for(size_t Mi=0;Mi<vmts.size();++Mi)//minutes 138 { 139 string datetime=year+curm+curd+vhur[Hi]+vmts[Mi]; 140 if(datetime>=st&&datetime<et) mrt[datetime].push_back(command);//在当前日期时间内 141 } 142 } 143 } 144 } 145 ++curyear;//进入下一年 146 } 147 } 148 for(map<string,vector<string> >::iterator it=mrt.begin();it!=mrt.end();++it) 149 { 150 map<string,int> isprt; 151 for(size_t i=0;i<it->second.size();++i) 152 { 153 string dis=it->first+" "+it->second[i]; 154 if(isprt.count(dis)==0) 155 { 156 cout<<dis<<endl; 157 isprt[dis]=1; 158 } 159 } 160 } 161 return 0; 162 }