In: Computer Science
Act 1: Class BooleanFunc
Overview
BooleanFunc will contain a truthTable (a dynamic array) which defines the boolean function being represented. You can load this truthTable using a mutator, setTruthTable() , and change the function it represents by calling setTruthTable() to give the table new values. You will not be able to change the size of the table (i..e. #inputs to the function) after an object is instantiated using a setTableSize-style method, but you can totally redefine the object using the assignment operator, which will have the effect of giving it a new size. You will need a method to evaluate the state (output) of the function based on input integers, and this will take the form or an eval( int ) method. Finally, you will store the most recently evaluated output in an internal bool state member, so you can call eval() once, but get the resulting state later without having to re-call eval(): a method getState() will do the job nicely.
Static (Public) Members
Instance (Private) Members
Instance (Public) Methods
Supply helper methods as appropriate.
Testing Specification Act 1
Sample Client
Here is some client code to use while debugging. You should be able to determine the correct run that results. You should provide some code that is more complete than this in your testing.
BooleanFunc segA, segB( 13 ), segC( 100, true ); int evenFunc[] = { 0, 2, 4, 6, 8, 10, 12, 14 }, inputX; short sizeEvenFunc = sizeof(evenFunc) / sizeof(evenFunc[0]); int greater9Func[] = { 10, 11, 12, 13, 14, 15 }; short sizeGreater9Func = sizeof(greater9Func) / sizeof(greater9Func[0]); int greater3Func[] = { 0, 1, 2, 3 }; short sizeGreater3Func = sizeof(greater3Func) / sizeof(greater3Func[0]); segA.setTruthTableUsingTrue( evenFunc, sizeEvenFunc ); segB.setTruthTableUsingTrue( greater9Func, sizeGreater9Func ); segC.setTruthTableUsingFalse( greater3Func, sizeGreater3Func ); // testing class BooleanFunc cout << "before eval()\n"; cout << "\n A(x) = " << segA.getState() << "\n B(x) = " << segB.getState() << "\n C(x) = " << segC.getState() << endl << endl; cout << "looping with eval()\n"; for ( inputX = 0; inputX < 10; inputX++ ) { segA.eval( inputX ); segB.eval( inputX ); segC.eval( inputX ); cout << "Input: " << inputX << "\n A(x) = " << segA.getState() << "\n B(x) = " << segB.getState() << "\n C(x) = " << segC.getState() << endl << endl; } segA.eval( inputX );
Act 2: Class MultiSegmentLogic
Overview
MultiSegmentLogic encapsulates any number of segments as a dynamic array of BooleanFunc objects and allows them to work together to produce some multi-segment display. Upon instantiation, a MultiSegmentLogic object obtains its size (the number of segments) from the user. This can be changed (a major resetting of the object) through the mutator setNumSegs(). After construction, or a resetting of the object via setNumSegs(), the truth table for each segment needs to be loaded, which is done using mutator setSegment(). setSegment() sends a BooleanFunc to a particular segment. For a seven segment display this would have to be called seven times, once for each segment. You will do that in the derived class (Act 3), not in the client.
Static (Public) Members
(None are needed.)
Instance (Protected) Members
Instance (Public) Methods
Supply helper methods as appropriate.
Testing Specification Act 2
Sample Client
You can create and test your own sample client, but not to hand it in. I want to see the sample client of the derived class, SevenSegmentLogic, which will implicitly test MultiSegmentLogic.
Act 3: Derived Class SevenSegmentLogic
Overview
This class is derived from MultiSegmentLogic. It has no new members but adds the specifics of seven segment display logic, namely the number of segments (= 7) and the actual truth tables for a seven-segment display.
Static (Public) Members
(None are needed.)
Instance (Protected) Members
(None are needed.)
Instance (Public) Methods
Helper methods
You will need some private helpers to load the segments, called somewhere in the constructor. Ultimately, somewhere you will need to call setSegment( k, bFunc) for k = 0 through 6, where bFunc is the boolean function for the kth segment. For example, if a, b, c, d, e, f and g, are the universal names for the segments, let's say that you associate segs[0] ↔ a, segs[1] ↔ b, segs[2] ↔ c and so on. The boolean function for segment a (your segs[0]) is easily found along with all the others, on-line. Here is the truth table for segment a:
input | output |
0 | T |
1 | F |
2 | T |
3 | T |
4 | F |
5 | T |
6 | T |
7 | T |
8 | T |
9 | T |
10 | T |
11 | F |
12 | T |
13 | F |
14 | T |
15 | T |
You would, therefore, instantiate a BooleanFunc that realizes this truth table (using the BooleanFunc mutator setTruthTableUsing...()) then pass that BooleanFunc object to the setSegment() method of the base class. All of this is done in the helper function.
Testing Specification Act 3
Here is some client code use while debugging. You should be able to determine the correct run that results. You should provide some code that is more complete than this in your testing. This example tests the copy constructor as well as the other methods.
int inputX, k; SevenSegmentLogic my7Seg; SevenSegmentLogic myCopy( my7Seg ); for ( inputX = 0; inputX < 16; inputX++ ) { myCopy.eval( inputX ); cout << "\n| "; for ( k = 0; k < 7; k++ ) cout << myCopy.getValOfSeg( k ) << " | "; cout << endl; }
What to Turn In
Hand in 5 files: No zip files.
Below is complete code. Please note that Act3 requires additional class files that i have added, if you do not require them in your assignment just ignore them. Let me know if you have any proble or doubt. Thank you.
=====================================================================
boolean.h
=========================
#ifndef BOOLEAN_H
#define BOOLEAN_H
class BooleanFunc
{
private:
int tableSize; // size of truth table
bool* truthTable; // array to represent truth
table
bool evalReturnIfError; // flag to check error
present in input
bool state; // result of the most recent call to
eval(int)
public:
static const int MAX_TABLE_FOR_CLASS = 65536; //
2^16 ~ 16 binary input lines
static const int DEFAULT_TABLE_SIZE = 16;
BooleanFunc(int tableSize = DEFAULT_TABLE_SIZE,
bool evalReturnIfError = false); // constructor
~BooleanFunc(); // destructor
BooleanFunc(const BooleanFunc& other); //
copy constructor
// assignment operator
void operator=(const BooleanFunc&
other);
// Mutators
bool setTruthTableUsingTrue(int
inputsThatProduceTrue[], int arraySize);
bool setTruthTableUsingFalse(int
inputsThatProduceFalse[], int arraySize);
// evaluates next state for given input
bool eval(int input);
// returns last evaluated state
bool getState() const;
};
#endif // !BOOLEAN_H
=========================
boolean.cpp
=========================
#include "boolean.h"
BooleanFunc::BooleanFunc(int tableSize, bool
evalReturnIfError)
{
// initialize data
this->tableSize = tableSize;
this->evalReturnIfError = evalReturnIfError;
this->state = evalReturnIfError; // default state
is no input present
// dynamicly allocate array
this->truthTable = new bool[tableSize];
// initialize truth table to false for all
inputs
for (int i = 0; i < tableSize; i++)
{
this->truthTable[i] =
false;
}
}
BooleanFunc::~BooleanFunc()
{
// delete array to free memory
delete[] truthTable;
}
BooleanFunc::BooleanFunc(const BooleanFunc& other)
{
// use assignment operator to deep copy current
object
*this = other;
}
void BooleanFunc::operator=(const BooleanFunc& other)
{
// check for self assignment
if (this == &other) {
return;
}
// copy values form other
this->tableSize = other.tableSize;
this->evalReturnIfError =
other.evalReturnIfError;
this->state = other.state;
// delete old array
delete[] this->truthTable;
// create new array
this->truthTable = new bool[tableSize];
// copy truth tale
for (int i = 0; i < this->tableSize; i++)
{
this->truthTable[i] =
other.truthTable[i];
}
}
bool BooleanFunc::setTruthTableUsingTrue(int
inputsThatProduceTrue[], int arraySize)
{
// set truth table only if array size is valid
if (arraySize > tableSize)
{
return false;
}
// initialize truth table to false for all
inputs
for (int i = 0; i < tableSize; i++)
{
this->truthTable[i] =
false;
}
// set all entries in truth table within array as
true
for (int i = 0; i < arraySize; i++)
{
// filter bad inputs and ignore
them
if (inputsThatProduceTrue[i] <
tableSize)
{
this->truthTable[inputsThatProduceTrue[i]] = true;
}
}
return true;
}
bool BooleanFunc::setTruthTableUsingFalse(int
inputsThatProduceFalse[], int arraySize)
{
// set truth table only if array size is valid
if (arraySize > tableSize)
{
return false;
}
// initialize truth table to true for all inputs
for (int i = 0; i < tableSize; i++)
{
this->truthTable[i] =
true;
}
// set all entries in truth table within array as
false
for (int i = 0; i < arraySize; i++)
{
// filter bad inputs and ignore
them
if (inputsThatProduceFalse[i] <
tableSize)
{
this->truthTable[inputsThatProduceFalse[i]] = false;
}
}
return true;
}
bool BooleanFunc::eval(int input)
{
// check for valid input
if (input < tableSize)
{
// set new state to remember last
eval
state =
truthTable[input];
}
else
{
// input contains error
// set new state to default value
for errors
state = evalReturnIfError;
}
// return new state
return getState();
}
bool BooleanFunc::getState() const
{
return state;
}
=========================
logic.h
=========================
#include "boolean.h"
#ifndef LOGIC_H
#define LOGIC_H
class MultiSegmentLogic {
protected:
int numSegs; // the number of BooleanFuncs
BooleanFunc* segs; // array to represent logic
segments
public:
MultiSegmentLogic(int numSegs = 0); //
constructor
~MultiSegmentLogic(); // destructor
MultiSegmentLogic(const MultiSegmentLogic&
other); // copy constructor
// assignment operator
void operator=(const MultiSegmentLogic&
other);
// Mutators
bool setNumSegs(int numSegs);
bool setSegment(int segNum, BooleanFunc&
funcForThisSeg);
void eval(int input); // calls eval() for each
of the segs in the object.
};
#endif // !LOGIC_H
=========================
logic.cpp
=========================
#include "logic.h"
MultiSegmentLogic::MultiSegmentLogic(int numSegs)
{
// initialize data
this->numSegs = numSegs;
this->segs = new BooleanFunc[numSegs];
}
MultiSegmentLogic::~MultiSegmentLogic()
{
// delete array to free memory
delete[] segs;
}
MultiSegmentLogic::MultiSegmentLogic(const
MultiSegmentLogic& other)
{
// use assignment operator to deep copy current
object
*this = other;
}
void MultiSegmentLogic::operator=(const MultiSegmentLogic&
other)
{
// check for self assignment
if (this == &other) {
return;
}
// copy data from other
this->numSegs = other.numSegs;
// delete old array
delete[] this->segs;
// create new array
this->segs = new BooleanFunc[numSegs];
// copy segments from other
for (int i = 0; i < numSegs; i++)
{
// use overloaded assignment
operator to deep copy each segment
this->segs[i] =
other.segs[i];
}
}
bool MultiSegmentLogic::setNumSegs(int numSegs)
{
// check for valid input
if (numSegs < 0)
{
return false;
}
// reset number of segment
this->numSegs = numSegs;
// delete old segments
delete[] segs;
// re assign array of segment in their default
state
segs = new BooleanFunc[numSegs];
return true;
}
bool MultiSegmentLogic::setSegment(int segNum, BooleanFunc&
funcForThisSeg)
{
// check for valid segNum
if (segNum < numSegs)
{
// reset given BooleanFunc
segs[segNum] =
funcForThisSeg;
return true;
}
return false;
}
void MultiSegmentLogic::eval(int input)
{
// call eval for each segment
for (int i = 0; i < numSegs; i++)
{
segs[i].eval(input);
}
}
=========================
sevenSegLogic.h
=========================
#include "logic.h"
#ifndef SEVEN_SEGMENT_LOGIC_H
#define SEVEN_SEGMENT_LOGIC_H
class SevenSegmentLogic: public MultiSegmentLogic {
private:
void loadSegments(); // private helper function
public:
SevenSegmentLogic(); // default constructor
bool getValOfSeg(int seg); // Returns whatever is in
the state variable of that segment
};
#endif // !SEVEN_SEGMENT_LOGIC_H
=========================
sevenSegLogic.cpp
=========================
#include "sevenSegLogic.h"
void SevenSegmentLogic::loadSegments()
{
// get truth table for each segment
/*
bool a[16] = { true, false, true, true, false, true,
true, true, true, true, true, false, true, false, true, true
};
bool b[16] = { true, true, true, true, true, false,
false, true, true, true, true, false, false, true, false, false
};
bool c[16] = { true, true, false, true, true, true,
true, true, true, true, true, true, false, true, false, false
};
bool d[16] = { true, false, true, true, false, true,
true, false, true, true, true, true, true, true, true, false
};
bool e[16] = { true, false, true, false, false, false,
true, false, true, false, true, true, true, true, true, true
};
bool f[16] = { true, false, false, false, true, true,
true, false, true, true, false, true, true, false, true, true
};
bool g[16] = { false, false, true, true, true, true,
true, false, true, true, true, true, false, true, true, true
};
*/
// create boolean function for each segment
int a[] = { 1,4,11,13 };
BooleanFunc segA;
segA.setTruthTableUsingFalse(a, 4);
int b[] = { 5,6,11,12,14,15 };
BooleanFunc segB;
segB.setTruthTableUsingFalse(b, 6);
int c[] = { 2,12,14,15 };
BooleanFunc segC;
segC.setTruthTableUsingFalse(c, 4);
int d[] = { 1,4,7,15 };
BooleanFunc segD;
segD.setTruthTableUsingFalse(d, 4);
int e[] = { 1,3,4,5,7,9 };
BooleanFunc segE;
segE.setTruthTableUsingFalse(e, 6);
int f[] = { 1,2,3,7,10,13 };
BooleanFunc segF;
segF.setTruthTableUsingFalse(f, 6);
int g[] = { 0,1,7,12 };
BooleanFunc segG;
segG.setTruthTableUsingFalse(g, 4);
// load individual segment
setSegment(0, segA);
setSegment(1, segB);
setSegment(2, segC);
setSegment(3, segD);
setSegment(4, segE);
setSegment(5, segF);
setSegment(6, segG);
}
SevenSegmentLogic::SevenSegmentLogic() :
MultiSegmentLogic(7)
{
// initialize with 7 segments
// load each segment
loadSegments();
}
bool SevenSegmentLogic::getValOfSeg(int seg)
{
// checck for valid segment and return its state
if (seg < 7)
{
return
this->segs[seg].getState();
}
return false; // error value
}
=========================
a7.cpp
=========================
#include "sevenSegLogic.h"
#include <iostream>
using namespace std;
int main() {
// Testing Specification Act 1
BooleanFunc segA, segB(13), segC(100, true);
int evenFunc[] = { 0, 2, 4, 6, 8, 10, 12, 14
}, inputX;
short sizeEvenFunc = sizeof(evenFunc) /
sizeof(evenFunc[0]);
int greater9Func[] = { 10, 11, 12, 13, 14, 15
};
short sizeGreater9Func = sizeof(greater9Func) /
sizeof(greater9Func[0]);
int greater3Func[] = { 0, 1, 2, 3 };
short sizeGreater3Func = sizeof(greater3Func) /
sizeof(greater3Func[0]);
segA.setTruthTableUsingTrue(evenFunc,
sizeEvenFunc);
segB.setTruthTableUsingTrue(greater9Func,
sizeGreater9Func);
segC.setTruthTableUsingFalse(greater3Func,
sizeGreater3Func);
// testing class BooleanFunc
cout << "before eval()\n";
cout
<< "\n A(x) =
"
<<
segA.getState()
<< "\n B(x) =
"
<<
segB.getState()
<< "\n C(x) =
"
<<
segC.getState()
<< endl <<
endl;
cout << "looping with eval()\n";
for (inputX = 0; inputX < 20; inputX++)
{
segA.eval(inputX);
segB.eval(inputX);
segC.eval(inputX);
cout
<< "Input: " << inputX
<< "\n A(x) = "
<< segA.getState()
<< "\n B(x) = "
<< segB.getState()
<< "\n C(x) = "
<< segC.getState()
<< endl << endl;
}
// Testing Specification Act 3
int k;
SevenSegmentLogic my7Seg;
SevenSegmentLogic myCopy(my7Seg);
for (inputX = 0; inputX < 16;
inputX++)
{
myCopy.eval(inputX);
cout << "\n|
";
for (k = 0; k < 7;
k++)
cout << myCopy.getValOfSeg(k) << " | ";
cout <<
endl;
}
// here code for printing seven segment logic
for LED number
for (int i = 0; i < 10; i++)
{
my7Seg.eval(i);
for (int j = 1; j <
16; j++)
{
// print emplty space
if (j % 2 != 0)
cout << " ";
// print LED lines
else if (j == 2 && my7Seg.getValOfSeg(0))
cout << "-";
else if (j == 4 && my7Seg.getValOfSeg(5))
cout << "|";
else if (j == 6 && my7Seg.getValOfSeg(1))
cout << "|";
else if (j == 8 && my7Seg.getValOfSeg(6))
cout << "-";
else if(j == 10 && my7Seg.getValOfSeg(4))
cout << "|";
else if(j == 12 && my7Seg.getValOfSeg(2))
cout << "|";
else if(j == 14 && my7Seg.getValOfSeg(3))
cout << "-";
else
cout << " ";
// print new lines
if (j % 3 == 0)
cout << endl;
}
cout <<
endl;
}
return 0;
}
=====================================================================