#include<bits/stdc++.h>
#include <xstring>
#include <iostream>
using namespace std;
//Global array to store the 0 or 1 in multidimentional array.
int arr[100][100];
/* It converts the integer to its corresponding string value.
* Like 123 converted to "123".
* @param: num, the number you want to convert into string. @return: string */
string toString(int num) {
/* It is just maping of single integer to its corresponding character value. */
vector<char> map = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
string convertedString = ""; /* Taking modulo of num and adding its corresponding character value into the right side of convertedString. */
while (
num > 0) {
convertedString = map[num % 10] + convertedString;
num = num / 10;
}
return convertedString;
}
/* It converts the integer string to its corresponding integer.
* Like "123" converted to 123.
* @param: str, it is the string value integer which you want to convert.
* @return: int */
int toInteger(
string str) {
int result = 0;
//Value to be multiplied
int power = 1;
//Traverse the string in reverse order and multiply with
// power of 10 everytime like str = "123", then
// first 3 will be multiplied with 1, then 2 with 10,
// and 1 with 100. Result will be result = 1x100 + 2x10 + 3x1 = 123.
for (int i = str.size() - 1; i >= 0; i--) {
result += power * (str[i] - '0');
power *= 10;
}
return result;
} /* encodeRow is a function to encode a particular row of multidimentional array.
* @param: arrRow[], one row of arr[][] containing 0 or 1.
* @param: size, It is the size of arrRow[]. @return: string of encoded values. */
string encodeRow(int arrRow[], int size) {
string result = "";
// cnt0 and cnt1 are counts of 0 and 1.
int cnt0 = 0, cnt1 = 0;
/* First you encounter 0 and keep incrementing you cnt0, when you encounter 1
* i.e, you are done with the count of 0, add cnt0 to your result string in string format do and make cnt0=0.
* Do the same for 1 and repeat. At the end you will get you encoded string.
* One thing to note that you should not add any count of 0 or 1 to your result,
* when your count is zero that is why that if statement is required. */
for (int i = 0; i < size; i++) {
if (arrRow[i] == 0) {
if (cnt1 > 0) {
result += toString(cnt1) + " ";
cnt1 = 0;
}
cnt0++;
} else {
if (cnt0 > 0) {
result += toString(cnt0) + " ";
cnt0 = 0;
}
cnt1++;
}
}
if (cnt0 > 0) result += toString(cnt0) + " ";
if (cnt1 > 0) result += toString(cnt1) + " ";
return result;
} /* This is the function to encode all your data.
* @param: row, number of rows in the array arr.
* @param: col, number of columns in the array arr.
* @return: None, it just print the encoded string. */
void encoding(int row, int col) {
for (int i = 0; i < row; i++) {
cout << encodeRow(arr[i], col) << endl;
}
}
/* This function is for decoding a single string or row.
* It just take the encoded string as input and decode it.
* @param: str, encoded string from compresed.txt, only one line. */
string decodingRow(string str) {
string result = "";
//Flag which tell us what to print 0 or 1.
// By defaut it is set to zero.
int flag = 0;
string num = "";
/* See we can have encoded string like 1 2 10 2 1.
* We need to store the every number into num string like num = "1", then num="2", then num = "10" and so on.
* After that we are converting it into integer using toInteger function.
* If the flag is zero add 0 to toInteger(num) times else add 1 toInteger(num) times to result string.
* After every addition we are changing the flag using xor operation. */
for (char ch : str) {
if (ch !=' ') { // "10" having two characters that is why we are adding
// adding ch into num everytime.
num += ch;
} else {
if (flag == 0) {
for (int i = 0; i < toInteger(num); i++) result += '0';
flag ^= 1;
} else {
for (int i = 0; i < toInteger(num); i++) result += '1';
flag ^= 1;
}
num = "";
}
}
if (flag == 1) for (int i = 0; i < toInteger(num); i++) result += '1';
if (flag == 0) for (int i = 0; i < toInteger(num); i++) result += '0';
return result;
}
int main() {
int option;
cout << "1 for encoding && 2 for decoding." << endl;
cout << "Enter your choice: ";
cin
>> option;
//Encoding operation
if (option == 1) {
freopen("data.txt", "r",
stdin);
freopen("compresed.txt", "w", stdout);
string str;
int row = 0, col = 0;
//getline function is used to read line.
while (getline(cin, str)) {
col = str.size();
for (int i = 0; i < col; i++) { arr[row][i] = str[i] - '0'; }
row++;
}
encoding(row, col);
} else {
//Decoding operation
freopen("compresed.txt", "r", stdin);
string str;
while (getline(cin, str)) { cout << decodingRow(str) << endl; }
}
return 0;
}
Comments
error in line 2 it says no such file or directory i need a c++ code done in Dev-C++ "Compiler set to configure TDM-GCC 4..9.2 64-bit Release"
Leave a comment