1 #include <iostream> 2 #include <string> 3 #include <fstream> 4 #include <stdlib.h> 5 #include <iomanip> 6 #include <ctype.h> 7 using namespace std; 8 #define N 41 9 10 bool IsOperators(char *b,char buffer) //判断是否为运算符,肯定是单个字符,即s[0] 11 { 12 for(int i=0;i<7;i++) 13 { 14 if(buffer==b[i]) 15 return true; 16 } 17 return false; 18 } 19 20 bool IsDilimeter(char *c,char buffer) //判断是否为分隔符 21 { 22 for(int i=0;i<9;i++) 23 { 24 if(buffer==c[i]) 25 return true; 26 } 27 return false; 28 } 29 30 //不需要用正则表达式 31 bool IsIdentifer(string s) 32 { 33 int i=0; 34 while(i<sizeof(s)) 35 { 36 if((s[i]<='z'&&s[i]>='a')||(s[i]>=0&&s[i]<=9)||(s[i]='_')) 37 i++; 38 else 39 return false; 40 } 41 return true; 42 } 43 44 int main() 45 { 46 string a[N]={"int","main","define","include","using","namespace","std","cout","iostream", 47 "return","endl","for","while","public","private","bool","break","case","catch","char", 48 "class","const","continue","default","delete","do","double","else","enum","export","extern", 49 "false","float","friend","goto","if","inline","int","long","new","register"}; //关键字 50 char c[9]={'[',']','(',')','{','}',';','#','"'}; //分隔符,并且把#作为分隔符 51 char b[7]={'+','-','*','/','<','>','='}; //运算符 52 ifstream huibian("text.txt"); 53 ofstream Opentext("result.txt"); 54 char buffer; 55 if(!huibian.is_open()) //判断文件是否已经打开 56 { 57 cout<<"Error opening file"; 58 exit(1); 59 } 60 huibian.get(buffer); 61 while(!huibian.eof()) //读取到文件的结束为止 62 { 63 while(buffer == ' '|| buffer=='\t'||buffer == '\n') 64 {huibian.get(buffer);} 65 if (IsDilimeter(c,buffer)) 66 { 67 Opentext<<buffer<<"\t"<<"Dilimiter!"<<endl; 68 huibian.get(buffer); 69 continue; 70 } 71 else if(IsOperators(b,buffer)) //判断是否为运算符 72 { 73 if (buffer=='/') 74 { 75 huibian.get(buffer); 76 if (buffer=='/') 77 { 78 Opentext<<"//"<<"\t"<<"Dilimiter"<<endl; 79 string s; 80 huibian.get(buffer); 81 while(buffer!='\n') 82 { 83 s.push_back(buffer); 84 huibian.get(buffer); 85 } 86 Opentext<<s<<"\t"<<"注释"<<endl; 87 continue; 88 } 89 else if(buffer=='*') 90 { 91 Opentext<<"/*"<<"\t"<<"Dilimiter"<<endl; 92 string s; 93 huibian.get(buffer); 94 while(buffer!='*') 95 { 96 s.push_back(buffer); 97 huibian.get(buffer); 98 } 99 huibian.get(buffer); 100 if (buffer=='/') 101 { 102 Opentext<<s<<"\t"<<"注释"<<endl; 103 Opentext<<"*/"<<"\t"<<"\tDilimiter"<<endl; 104 } 105 } 106 else 107 continue; 108 } 109 string s; 110 s.push_back(buffer); 111 huibian.get(buffer); 112 if (((s[0]=='+'||s[0]=='-'||s[0]=='*'||s[0]=='/'||s[0]=='<'||s[0]=='>')&&(buffer=='='))||(s[0]=='+'&&buffer=='+')||(s[0]=='-'&&buffer=='-')||(s[0]=='='&&buffer=='=')||(s[0]=='>'&&buffer=='>')||(s[0]=='<'&&buffer=='<')) 113 { 114 s.push_back(buffer); 115 Opentext<<s<<"\t"<<"Operator"<<endl; 116 } 117 else if ((s[0]=='+'||s[0]=='-')&&(buffer<='9'&&buffer>='0')) 118 { 119 s.push_back(buffer); 120 huibian.get(buffer); 121 while(buffer>='0'&&buffer<='9') 122 { 123 s.push_back(buffer); 124 huibian.get(buffer); 125 } 126 if (buffer=='.') 127 { 128 s.push_back(buffer); 129 huibian.get(buffer); 130 while(buffer>='0'&&buffer<='9') 131 { 132 s.push_back(buffer); 133 huibian.get(buffer); 134 } 135 if (buffer=='e'||buffer=='E') 136 { 137 s.push_back(buffer); 138 huibian.get(buffer); 139 if (buffer=='+'||buffer=='-') 140 { 141 huibian.get(buffer); 142 while(buffer>='0'&&buffer<='9') 143 { 144 s.push_back(buffer); 145 huibian.get(buffer); 146 } 147 Opentext<<s<<"\t"<<"Number"<<endl; 148 continue; 149 } 150 else if ((buffer<='9'&&buffer>'0')) 151 { 152 s.push_back(buffer); 153 huibian.get(buffer); 154 while(buffer<='9'&&buffer>='0') 155 { 156 s.push_back(buffer); 157 huibian.get(buffer); 158 } 159 Opentext<<s<<"\t"<<"Number"<<endl; 160 continue; 161 } 162 } 163 else 164 { 165 Opentext<<s<<"\t"<<"Number"<<endl; 166 continue; 167 } 168 } 169 else 170 { 171 Opentext<<s<<"\t"<<"Number"<<endl; 172 continue; 173 } 174 } 175 /** 176 if(IsOperators(b,buffer)) 177 { 178 s.push_back(buffer); 179 int k=-1; 180 Opentext<<s<<"\t"<<"Operator"<<endl; 181 } 182 **/ 183 else 184 { 185 Opentext<<s<<"\t"<<"Operator"<<endl; 186 continue; 187 } 188 } 189 else if(buffer>='0'&&buffer<='9') //判断是否为数字 190 { 191 string s; //这里s不能初始化为NULL,否则将出现很大的问题!!!!! 192 s.push_back(buffer); 193 huibian.get(buffer); 194 while(buffer>='0'&&buffer<='9') 195 { 196 s.push_back(buffer); 197 huibian.get(buffer); 198 } 199 if (buffer=='.') 200 { 201 s.push_back(buffer); 202 huibian.get(buffer); 203 while(buffer>='0'&&buffer<='9') 204 { 205 s.push_back(buffer); 206 huibian.get(buffer); 207 } 208 if (buffer=='e'||buffer=='E') 209 { 210 s.push_back(buffer); 211 huibian.get(buffer); 212 if (buffer=='+'||buffer=='-') 213 { 214 huibian.get(buffer); 215 while(buffer>='0'&&buffer<='9') 216 { 217 s.push_back(buffer); 218 huibian.get(buffer); 219 } 220 Opentext<<s<<"\t"<<"Number"<<endl; 221 continue; 222 } 223 else if ((buffer<='9'&&buffer>'0')) 224 { 225 s.push_back(buffer); 226 huibian.get(buffer); 227 while(buffer<='9'&&buffer>='0') 228 { 229 s.push_back(buffer); 230 huibian.get(buffer); 231 } 232 Opentext<<s<<"\t"<<"Number"<<endl; 233 continue; 234 } 235 } 236 else 237 { 238 Opentext<<s<<"\t"<<"Number"<<endl; 239 continue; 240 } 241 } 242 else 243 { 244 Opentext<<s<<"\t"<<"Number"<<endl; 245 continue; 246 } 247 248 Opentext<<s<<"\t"<<"Number"<<endl; 249 continue; 250 } 251 else if((buffer<='z'&&buffer>='a')||(buffer<='Z'&&buffer>='A')) 252 { 253 string s; 254 s.push_back(buffer); 255 huibian.get(buffer); 256 while((buffer<='z'&&buffer>='a')||(buffer<='Z'&&buffer>='A')) 257 { 258 s.push_back(buffer); 259 huibian.get(buffer); 260 } 261 bool flag=false; 262 for(int i=0;i<N;i++) 263 { 264 if(a[i]==s) 265 flag=true; 266 } 267 if(flag) 268 { 269 Opentext<<setiosflags(ios::right)<<s<<"\t"<<"KeyWord"<<endl; 270 continue; 271 } 272 else 273 { 274 //这里还有一个问题! 275 /** 276 if((buffer='_')||(buffer<=9&&buffer>=0)) 277 { 278 s.push_back(buffer); 279 huibian.get(buffer); 280 while((buffer<='z'&&buffer>='a')||(buffer<='Z'&&buffer>='A')||(buffer='_')||(buffer<=9&&buffer>=0)) 281 { 282 s.push_back(buffer); 283 huibian.get(buffer); 284 } 285 } 286 **/ 287 Opentext<<s<<"\t"<<"Identifiers"<<endl; 288 continue; 289 } 290 } 291 else 292 { 293 Opentext<<buffer<<"\t"<<"This is nonsense identifier!"<<endl; 294 continue; 295 } 296 huibian.get(buffer); 297 } 298 huibian.close(); 299 return 0; 300 }
这是第三次上传的代码,加入了有关数字和双字符的判断,基本上所有的功能都实现了,所以。。。大家指正吧