-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.cpp
75 lines (68 loc) · 1.32 KB
/
generator.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
#include <iostream>
#include <stdio.h>
#define OR 0
#define AND 1
using namespace std;
// int prevOp = OR;
void printStream(int bits[], int i, int limit){
if(i>limit) return;
if(i==limit){
printf("p");
return;
}
if(i==limit-1){
if(bits[i]==0){
printf("p * ");
printStream(bits, limit, limit);
// prevOp = AND;
}else{
printf("p | ");
printStream(bits, limit, limit);
// prevOp = OR;
}
return;
}
if(bits[i]==0 && bits[i+1] == 1){
printf("p * (");
printStream(bits, ++i, limit);
printf(")");
// prevOp = AND;
}else{
if(bits[i]==1){
printf("p | ");
}else{
printf("p * ");
}
printStream(bits, ++i, limit);
}
}
int main()
{
float prob, t_prob;
int precision, t_prec;
cout << "Enter the probability: ";
cin >> prob;
if(prob < 0.0f || prob > 1.0f){
cout << "Invalid input for probability: MUST BE BETWEEN 0 and 1" << endl;
return 1;
}
else if (prob == 0.0f || prob == 1.0f){
cout << prob << endl;
return 0;
}
// 0 < prob < 1
cout << "Precision in binary digits: ";
cin >> precision;
t_prob = prob;
int bits[precision];
for(int i=0; i<precision; ++i){
t_prob *= 2;
bits[i] = (int)t_prob;
t_prob = (t_prob >= 1) ? t_prob - 1 : t_prob;
}
int limit = precision -1 ;
while(limit > 0 && bits[limit]==0) --limit;
printStream(bits, 0, limit);
cout << endl;
return 0;
}