fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <stdlib.h>
  6. using namespace std;
  7. bool isInteger(const std::string & s)
  8. {
  9. if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;
  10.  
  11. char * p ;
  12. strtol(s.c_str(), &p, 10) ;
  13.  
  14. return (*p == 0) ;
  15. }
  16. int main() {
  17. // Open our file
  18. ifstream inFile("inputfile.txt",ifstream::in);
  19.  
  20. // If we can read/write great
  21. std::vector<string> strVector;
  22. std::vector<string> outVector;
  23. string str;
  24. int num;
  25. int code;
  26. bool trueCode = false;
  27. //if (inFile.good()) {
  28. while(cin>>str){
  29. strVector.push_back(str);
  30. }
  31. vector<string>::iterator it= strVector.begin();;
  32. bool preIsInt = true;//0:number, 1:str
  33. while(it != strVector.end()) {
  34. bool isInt = isInteger(*it);
  35. if(isInt){
  36. int n_of_code = atoi((*it).c_str());
  37. outVector.push_back((*it));
  38. it++;
  39. while(n_of_code) {
  40. outVector.push_back(" ");
  41. outVector.push_back((*it));
  42. if(!isInteger(*it)){
  43. n_of_code--;
  44. }
  45. it++;
  46. }
  47. if((it+1)==strVector.end()||isInteger(*(it+1))){
  48. outVector.push_back(" ");
  49. outVector.push_back((*it));
  50. it++;
  51. }
  52. outVector.push_back("\n");
  53. }
  54. }
  55. for(it=outVector.begin();it!=outVector.end();it++) {
  56. cout<<(*it);
  57. }
  58. // inFile.close();
  59. //}
  60. //system("pause");
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 15240KB
stdin
1
    xy 2
2 z            xy
5       R 1004  I 5678
      E 2000  R
8002  E 7001  0      1 z
6 R 8001  E 1000  E
1000  E 3000  R 1002  A 1010
0
1
z
2
R
5001
E 4000
1 z 2
2
xy z
3 A 8000
E 1001  E 2000
stdout
1 xy 2
2 z xy
5 R 1004 I 5678 E 2000 R 8002 E 7001
0
1 z
6 R 8001 E 1000 E 1000 E 3000 R 1002 A 1010
0
1 z
2 R 5001 E 4000
1 z 2
2 xy z
3 A 8000 E 1001 E 2000