-
Notifications
You must be signed in to change notification settings - Fork 0
/
armcode.cpp
64 lines (61 loc) · 2.03 KB
/
armcode.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
#include<bits/stdc++.h>
using namespace std;
struct Node{
double prob,rangefrom,rangeto;
};
int main(){
unordered_map<char,Node> alphas;
//unordered_map<char,double> rangefrom;
//unordered_map<char,double> rangeto;
double val;
char letter;
int n;
cout<<"\nenter the no of letters in dictionary:";
cin>>n;
cout<<"\nenter the alphabets with their probabilities(like A 0.1):";
double J = 0;
for(int i = 0;i < n;i++){
cin>>letter>>val;
alphas[letter].prob = val;
alphas[letter].rangefrom = J;
alphas[letter].rangeto = J + val;
//cout<<"\nrangefrom of "<<letter<<":"<<J;
//cout<<"\nrangeto of "<<letter<<":"<<J+val<<endl;
J = alphas[letter].rangeto;
}
cout<<endl;
string s1;
cout<<"\nenter the string :";
cin>>s1;
//encoding here.......
cout<<endl<<"\t\t\tEncoding";
cout<<endl<<"Symbol\t\t\tlowV\t\t"<<"highV\t\t"<<"diff";
double lowV = 0.0,highV = 1.0,diff = 1.0,t;
for(int i = 0;i < s1.size();i++){
cout<<endl<<s1[i]<<"\t\t\t"<<lowV<<"\t\t"<<highV<<"\t\t"<<diff;
t = lowV;
lowV = t + (diff*alphas[s1[i]].rangefrom);
highV = t + (diff*alphas[s1[i]].rangeto);
diff = highV - lowV;
//cout<<endl<<lowV;
}
cout<<endl<<lowV;
cout<<endl<<"\t\t\tDecoding";
cout<<endl<<"code\t\t\toutput\t\trangefrom\t\trangeto";
double code = lowV;
string op;
bool flag = false;
while(op.size() != s1.size()){
for(auto it = alphas.begin();it != alphas.end();it++){
char ch = it->first;
if(code >= alphas[ch].rangefrom && code < alphas[ch].rangeto){
op.push_back(ch);
code = (code - alphas[ch].rangefrom) / (alphas[ch].rangeto - alphas[ch].rangefrom);
cout<<endl<<code<<"\t\t\t"<<ch<<"\t\t"<<alphas[ch].rangefrom<<"\t\t"<<alphas[ch].rangeto;
break;
}
}
}
cout<<endl<<op;
return 0;
}