-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNrzI_encoding_decoding.cpp
144 lines (141 loc) · 3.12 KB
/
NrzI_encoding_decoding.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*@athour....
Name: Md.Manzurul Alam
Dept. of CSE, GUB
ID:202902003
CSE 308 || Data Communication
*/
#include <bits/stdc++.h>
using namespace std;
string encoding1(string str)
{
// if initial state H
string data1;
for (int i = 0; i < str.length(); i++)
{
bool prv;
if (i == 0)
{
if (str[i] == '0')
{
data1.push_back('H');
prv = 1;
}
else
{
data1.push_back('L');
prv = 0;
}
}
else if (str[i] == '0')
{
if (prv == 0)
{
data1.push_back('L');
}
else
{
data1.push_back('H');
}
}
else if (str[i] == '1')
{
if (prv == 0)
{
data1.push_back('H');
prv = 1;
}
else
{
data1.push_back('L');
prv = 0;
}
}
}
return data1;
}
string encoding2(string str)
{
// If initial state is L
string data1;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == 'L')
{
data1.push_back('H');
}
if (str[i] == 'H')
{
data1.push_back('L');
}
}
return data1;
}
string decoding1(string str)
{
string data1;
for (int i = 0; i < str.length(); i++)
{
char prv;
if (i == 0)
{
if (str[i] == 'L')
{
data1.push_back('1');
prv = 'L';
}
else
{
data1.push_back('0');
prv = 'H';
}
}
else if (str[i] == prv)
{
data1.push_back('0');
prv = str[i];
}
else if (str[i] != prv)
{
data1.push_back('1');
prv = str[i];
}
}
return data1;
}
string decoding2(string str)
{
// If initial state is L
string data1;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == '0')
{
data1.push_back('1');
}
if (str[i] == '1')
{
data1.push_back('0');
}
}
return data1;
}
int main()
{
// encoding part.........
string str1;
cout << "Enter a data for encoding: ";
cin >> str1;
string ans1 = encoding1(str1);
cout << "The encoding data is (if intial state is H): " << ans1 << endl;
string ans2 = encoding2(ans1);
cout << "The encoding data is (if intial state is L): " << ans2 << endl;
cout << endl;
// decoding part......
string str2;
cout << "Enter a data for Decoding: ";
cin >> str2;
string ans3 = decoding1(str2);
cout << "The Decoding data is (if intial state is H): " << ans3 << endl;
string ans4 = decoding2(ans3);
cout << "The decoding data is (intial state is L): " << ans4 << endl;
}