zoj 1623
View Code
1 #include <iostream> 2 #include <fstream> 3 using namespace std; 4 #include <string> 5 #include <string.h> 6 7 int main() 8 { 9 /*ifstream cin("1.txt");//*/ 10 /*ofstream cout("2.txt");//*/ 11 int cas; 12 cin>>cas; 13 char ch[10000]; 14 while(cas--) 15 { 16 int line,row; 17 int counts = 0;// number of comment 18 cin >> line; 19 getchar();// 20 string str; 21 for(row = 0; row < line; ++ row){ 22 gets(ch); 23 str += ch; 24 str += '\n'; 25 } 26 unsigned int begin = 0,end = 0, onepiece = 0,start = 0,i; 27 28 begin = str.find_first_of('/',start); 29 30 while(begin != string::npos){ 31 if(begin + 1 < str.length() && str[begin + 1] == '/'){// null comment is count as one too:asdffdsa// 32 ++ counts;//find one comment 33 end = str.find_first_of('\n',begin + 1);// 34 for(i = begin ; i < end ; ++i){ 35 if(str[i] >= 'a' && str[i] <= 'z'){ 36 str[i] -= 32; 37 } 38 } 39 start = end + 1; 40 if(start >= str.length())break; 41 begin = str.find_first_of('/',start);// 42 continue; 43 } 44 bool find = false; 45 if(begin + 1 < str.length() && str[begin + 1] == '*'){ 46 end = str.find_first_of('/',begin + 1); 47 while(end != string::npos){ 48 if(str[end - 1] == '*' && (end - begin > 2)){ 49 find = true; 50 ++ counts;//find a comment 51 start = end + 1; 52 for(i = begin ; i < end; ++i){ 53 if(str[i] >= 'a' && str[i] <= 'z'){ 54 str[i] -= 32; 55 } 56 } 57 break; 58 } 59 else end++; 60 if(end + 1 < str.length()){ 61 end = str.find_first_of('/',end + 1); 62 } 63 else break; 64 } 65 } 66 if(!find)++ start; 67 if(start >= str.length())break; 68 begin = str.find_first_of('/',start);// 69 } 70 cout << counts << endl << str << endl; 71 } 72 return 0; 73 }
好像这样更方便
View Code
1 #include <iostream> 2 #include <fstream> 3 using namespace std; 4 #include <string> 5 #include <string.h> 6 7 int main() 8 { 9 /*ifstream cin("1.txt");//*/ 10 /*ofstream cout("2.txt");//*/ 11 int cas; 12 cin>>cas; 13 char ch[10000]; 14 while(cas--) 15 { 16 int line; 17 int counts = 0;// number of comment 18 cin >> line; 19 getchar();// 20 string str; 21 while(line--){ 22 gets(ch); 23 str += ch; 24 str += '\n'; 25 } 26 unsigned int begin = 0,end = 0, onepiece = 0,start = 0,i; 27 28 while(true){ 29 begin = str.find("/*",start); 30 onepiece = str.find("//",start); 31 if(begin != string::npos)end = str.find("*/",begin + 2); 32 else end = string::npos; 33 34 if((begin != string::npos && end != string::npos) || onepiece != string::npos){ 35 int ch = 2;//onepiece 36 if((begin != string::npos && end != string::npos) && onepiece != string::npos){ 37 if(begin < onepiece) ch = 1;//begin-end 38 } 39 else if(onepiece == string::npos) ch = 1; 40 41 if(ch == 2){ 42 begin = onepiece; 43 end = str.find_first_of('\n',onepiece); 44 } 45 46 for(i = begin; i < end; ++i){ 47 if(str[i] >= 'a' && str[i] <= 'z') 48 str[i] -= 32; 49 } 50 51 start = end + 1; 52 ++ counts; 53 } 54 else 55 break; 56 } 57 cout << counts << endl << str << endl; 58 } 59 return 0; 60 }
int p1 = findstr(start,s,"/*");
int p2 = findstr(start,s,"//");
int p3 = findstr(p1+2,s,"*/");