Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- int pow(const int &base, const int &index)
- {
- if(index == 0)
- {
- return 1;
- }
- else
- {
- int res = 1;
- for(int i = 0; i < index; ++i)
- {
- res *= base;
- }
- return res;
- }
- }
- int binary_to_decimal(const std::string &invertedBinary)
- {
- int convertedDecimal = 0;
- const short ZERO_IN_ASCII = 48;
- const short BASE_OF_BINARY = 2;
- for(int i = 0; i < invertedBinary.size(); ++i)
- {
- int binaryCypher = static_cast<int>(invertedBinary[i]) - ZERO_IN_ASCII;
- convertedDecimal += (binaryCypher * pow(BASE_OF_BINARY,invertedBinary.size()-i-1));
- }
- return convertedDecimal;
- }
- std::string invert_binary_number(const std::string &binary)
- {
- std::string encryptedBinary = "";
- for(int i = 0; i < binary.size(); ++i)
- {
- if(binary[i] == '0')
- {
- encryptedBinary += '1';
- }
- else if(binary[i] == '1')
- {
- encryptedBinary += '0';
- }
- }
- return encryptedBinary;
- }
- std::string decimal_to_binary(int dec)
- {
- std::string unreversedConvertedBinary = "";
- while(dec > 0)
- {
- unreversedConvertedBinary += std::to_string(dec%2);
- dec /= 2;
- }
- for(int i = 0; i < unreversedConvertedBinary.size()/2; ++i)
- {
- int j = unreversedConvertedBinary.size() - i -1;
- int temp = unreversedConvertedBinary[i];
- unreversedConvertedBinary[i] = unreversedConvertedBinary[j];
- unreversedConvertedBinary[j] = temp;
- }
- return unreversedConvertedBinary;
- }
- std::string encode(const std::string &input)
- {
- std::string addToResult = "";
- for(int i = 0; i < input.size(); ++i)
- {
- addToResult += static_cast<char>(binary_to_decimal
- (invert_binary_number
- (decimal_to_binary(static_cast<int>(input[i])))));
- }
- return addToResult;
- }
- int main()
- {
- std::fstream file ("input.txt", std::fstream::in);
- std::fstream output("output.txt", std::fstream::out|std::fstream::trunc);
- std::string input;
- while(getline(file,input))
- {
- output<<encode(input);
- }
- file.close();
- output.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment