Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. //============================================================================
  2. // Name        : reverse-cpp.cpp
  3. // Author      : Francesco Laurita
  4. // Version     :
  5. // Copyright   : Francesco Laurita
  6. // Description :
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <string>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <math.h>
  14. #include <vector>
  15. using namespace std;
  16.  
  17. string apply(string in, int pos){
  18.     int slen = in.length();
  19.     if (slen <= pos){
  20.         return in;
  21.     }else{
  22.         int mid = ceil(slen / 2);
  23.         string right = in.substr(mid,slen);
  24.         string left  = in.substr(0,mid);
  25.         return apply(right,pos) + apply(left,pos);
  26.     }
  27. }
  28. int main(int argc, char **argv) {
  29.  
  30.     ifstream fp(argv[1]);
  31.     if (!fp){
  32.         cerr << "Unble to open file!" << endl;
  33.         return 1;
  34.     }
  35.     int nlines;
  36.     fp >> nlines;
  37.     fp.ignore();
  38.     for (int i = 0; i < nlines; i++){
  39.         string line,tok;
  40.         getline(fp,line);
  41.         stringstream ss(line);
  42.         vector<string> tokens;
  43.         int j = 1;
  44.         while (ss >> tok){
  45.             tokens.push_back(apply(tok,j));
  46.             j++;
  47.         }
  48.         for (vector<string>::iterator it = tokens.begin(); it != tokens.end(); ++it){
  49.             cout << *it;
  50.             if (it != tokens.end()-1)
  51.                 cout << " ";
  52.         }
  53.         tokens.empty();
  54.         cout << endl;
  55.     }
  56.     fp.close();
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement