Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #include <cstdint>
  2. #include <cstdlib>
  3. #include <exception>
  4. #include <fstream>
  5. #include <ios>
  6. #include <iostream>
  7. #include <istream>
  8. #include <limits>
  9. #include <map>
  10. #include <ostream>
  11. #include <stdexcept>
  12. #include <string>
  13. #include <vector>
  14.  
  15.  
  16. using namespace std;
  17.  
  18.  
  19.  
  20. void initdict(map<string,int> &enc,map<int,string> &dec, int &last)
  21. {
  22. last= 0;
  23. enc.clear();
  24. dec.clear();
  25. for (long long c = 0; c <= 255; c++,last++)
  26. {
  27. string s = {char(c)};
  28. enc[s] = last;
  29. dec[last] = s;
  30. }
  31. }
  32. void encodefile(string input, string output, map<string,int> enc, int last)
  33. {
  34. ifstream cin(input);
  35. ofstream cout (output);
  36. char c;
  37. string s;
  38. while (cin.get(c))
  39. {
  40. s.push_back(c);
  41.  
  42. if (enc.count(s) == 0)
  43. {
  44.  
  45. enc[s] = last;
  46. last++;
  47. s.pop_back();
  48.  
  49. cout << enc[s]<< " ";
  50. s = {c};
  51. }
  52. }
  53. if (!s.empty())
  54. cout << enc[s];
  55. }
  56. void decodefile(string input, string output, map<int,string> dec, int last)
  57. {
  58. ifstream cin(input);
  59. ofstream cout(output);
  60.  
  61. int code,lcode;
  62. cin >> lcode;
  63. cout << dec[lcode];
  64. while (cin>> code)
  65. {
  66. if (dec.count(code))
  67. {
  68. cout << dec[code];
  69. dec[last]=dec[lcode]+dec[code][0];
  70. }
  71. else
  72. {
  73. cout << dec[lcode]+dec[lcode][0];
  74. dec[last]=dec[lcode]+dec[lcode][0];
  75. }
  76. last++;
  77. lcode=code;
  78. }
  79. }
  80.  
  81. int main()
  82. {
  83. map<string,int>enc;
  84. map<int,string> dec;
  85. int last;
  86. initdict(enc, dec,last);
  87. string file1 = "input.txt";
  88. string file2 = "output.txt";
  89. string file3 = "input1.txt";
  90. encodefile(file1,file2,enc,last);
  91. decodefile(file2, file3, dec,last);
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement