Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // hex-to-oct
  4. //
  5. // Created by aaron67 on 2017/1/23.
  6. // Copyright © 2017年 aaron67. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <sstream>
  12. #include <string>
  13. #include <fstream>
  14. using namespace std;
  15.  
  16. int main(int argc, const char * argv[]) {
  17. ifstream input_file("input.txt");
  18. ofstream output_file("output.txt");
  19. int n_test_cases;
  20. input_file >> n_test_cases;
  21. while (n_test_cases--) {
  22. string hex_text;
  23. input_file >> hex_text;
  24.  
  25. int i = hex_text.length() % 3;
  26. if (i == 0) {
  27. i = 3;
  28. }
  29. stringstream ss;
  30. ss << hex_text.substr(0, i);
  31. int j;
  32. ss >> hex >> j;
  33. output_file << oct << j;
  34. while (i < hex_text.length()) {
  35. ss.clear();
  36. ss << hex_text.substr(i, 3);
  37. int j;
  38. ss >> hex >> j;
  39. output_file << oct << setw(4) << setfill('0') << j;
  40. i += 3;
  41. }
  42. output_file << endl;
  43. }
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement