-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.cpp
66 lines (61 loc) · 1.15 KB
/
checksum.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
#include<bits/stdc++.h>
using namespace std;
string add(string sum , string db)
{
string ans = sum;
int n = sum.length();
int carry= 0;
for(int i = n -1;i>=0;i--)
{
if(carry + (sum[i] - '0') + (db[i] - '0') == 2 )
{
ans[i] = '0';
carry = 1;
}
else if (carry + (sum[i] - '0') + (db[i] - '0') == 3 )
{
ans[i] = '1';
carry = 1;
}
else
{
ans[i] = carry + (sum[i] - '0') + (db[i] - '0') == 1 ? '1' : '0';
carry = 0;
}
}
if(carry != 0)
{
string temp = "";
ans = add(ans,temp.append(n-1,'0') + to_string(carry));
}
return ans;
}
int main()
{
string data;
cout<<"Enter Data : ";
cin>>data;
int block = 0;
cout<<"Enter block size : ";
cin>>block;
string checksum = data.substr(0,block);
string original_data = data.substr(block,data.length() - block);
string sum = "";
sum.append(block,'0');
for(int i = 0;i<original_data.length();)
{
sum = add(sum,original_data.substr(i,block));
i = i + block;
}
sum = add(sum,checksum);
string t= "";
if(t.append(block,'1') == sum)
{
cout<<"Correct Data";
}
else
{
cout<<"Error Data";
}
return 0;
}