May 22, 2021 at 2:00pm UTC
Hi, I'm working on a project reading in a boolean function from a .txt file.
The .txt file is like:
ABcD+ABCD+aBcD+aBCD.
A 0.2
B 0.4
C 0.6
D 0.8
The number stands for the probability of variable being 0.
The goal is to partition the boolean function into several minterms.
In this case, I want to get ABcD, ABCD, aBcD, aBCD four terms.
My code so far is:
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
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string str("ABcD+ABCD+aBcD+aBCD." );
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << endl;
}
vector<string> minterm_list;
stringstream ss;
string minterm;
char c;
ss << str;
while (ss >> c)
{
if (c =='+' )
{
minterm_list.push_back(minterm);
minterm = "" ;
}
else
{
minterm = minterm + c;
}
}
cout << "There are " << minterm_list.size() << " minterm in total" << endl;
for (int i=0; i < minterm_list.size(); i++)
{
cout << minterm_list[i] << endl;
}
return 0;
}
And the output is
A
B
c
D
+
A
B
C
D
+
a
B
c
D
+
a
B
C
D
.
There are 3 minterm in total
ABcD
ABCD
aBcD
Why isn't the fourth term aBcD not pushed by the vector?
Thx in advance!
Last edited on May 22, 2021 at 2:06pm UTC
May 22, 2021 at 2:14pm UTC
I've figured out the problem and modified the if-else condition and get the correct result.
1 2 3 4 5
else if (c == '.' )
{
minterm_list.push_back(minterm);
minterm = "" ;
}
A
B
c
D
+
A
B
C
D
+
a
B
c
D
+
a
B
C
D
.
There are 4 minterms in total, which are :
ABcD
ABCD
aBcD
aBCD
Last edited on May 22, 2021 at 2:14pm UTC