forked from Ishj21/cpp-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparity.cpp
More file actions
95 lines (87 loc) · 1.44 KB
/
parity.cpp
File metadata and controls
95 lines (87 loc) · 1.44 KB
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
#include <bits/stdc++.h>
using namespace std;
class stackArr
{
private:
char *data;
int nextIndex;
int capacity;
public:
stackArr(int ts)
{
data = new char[ts];
nextIndex = 0;
capacity = ts;
}
int size()
{
return nextIndex;
}
bool isEmpty()
{
return nextIndex == 0;
}
void push(char ele)
{
if (nextIndex == capacity)
{
cout << "Stack is Full";
return;
}
data[nextIndex] = ele;
nextIndex++;
}
//insert pop here
int top()
{
if (isEmpty())
{
return INT_MIN;
}
return data[nextIndex - 1];
}
};
char oneCount(char a[], stackArr s)
{
for (int i = 0; i < 15; i++)
{
if (a[i] == '1')
{
if (s.isEmpty())
{
s.push('1');
}
else
{
s.pop();
}
}
}
if (!s.isEmpty())
{
return '1';
}
else
{
return '0';
}
}
int main()
{
stackArr s(1);
char a[16] = {};
cout<<endl<<"Enter BIT String for checking"<<endl;
for (int i = 0; i < 15; i++)
{
cin >> a[i];
}
char b = oneCount(a, s);
cout<<endl<<"Output with Parity Bit is: "<<endl;
for (int i = 0; i < 15; i++)
{
cout << a[i];
}
cout << b;
cout<<endl;
return 0;
}