In: Computer Science
Case Project 4-2: Configuring Preferences
Users in the Engineering Department need a higher level of access on their local computers than other users do. In addition, you want to set power options on mobile computers that Engineering users use. All Engineering Department user and computer accounts are in the Engineering OU.
What should you configure to meet the following criteria?
• When an Engineering user signs into a computer, the user account is added to the local Administrators group on that computer.
• Enable the hibernation power mode but only if the user’s computer is identified as a portable computer. Set the power scheme to hibernate mode if the laptop’s lid is closed or the power button is pressed.
CODE:
#include
#include
#include
#include
#include
#include
#include
#include
// PURPOSE: To tell the maximum value that a tree can have.
const int MAX_VALUE = 64;
// PURPOSE: To tell how many problems to do.
const int NUM_PROBLEMS = 4096;
#include "Node.h"
#include "NodeBuffer.h"
Node.h
This file defines classes for nodes used to represent math
expressions.
// PURPOSE: To distinguish among the mathematical operators.
typedef enum {
ADD_OP,
SUBTRACT_OP,
MULTIPLY_OP,
DIVIDE_OP,
NUM_OPS
}
operator_ty;
// PURPOSE: To serve as the base class for the Node classes.
class Node
{
public :
Node ()
{ }
virtual
~Node ()
{ }
virtual
double eval ()
const
= 0;
virtual
std::string toString ()
const
= 0;
};
// PURPOSE: To represent a constant.
class ConstNode : public Node
{
double constant_;
public :
ConstNode () :
Node(),
constant_((double)((rand() % MAX_VALUE) + 1) )
{ }
double eval ()
const
{ return(constant_); }
std::string toString ()
const
{
std::ostringstream stream;
stream << constant_;
return(stream.str());
}
};
// PURPOSE: To return a randomly generated Node.
extern
Node* makeNode ();
// PURPOSE: To represent an operation.
class OperatorNode : public Node
{
operator_ty operator_;
Node* lhsPtr_;
Node* rhsPtr_;
public :
OperatorNode () :
Node(),
operator_((operator_ty)(rand() % NUM_OPS)),
lhsPtr_(makeNode()),
rhsPtr_(makeNode())
{ }
~OperatorNode ()
{
delete(rhsPtr_);
delete(lhsPtr_);
}
double eval ()
const
{
double lhs = lhsPtr_->eval();
double rhs = rhsPtr_->eval();
double result;
switch (operator_)
{
case ADD_OP :
result = lhs + rhs;
break;
case SUBTRACT_OP :
result = lhs - rhs;
break;
case MULTIPLY_OP :
result = lhs * rhs;
break;
case DIVIDE_OP :
result = lhs / rhs;
break;
}
return(result);
}
std::string toString ()
const
{
std::ostringstream stream;
const char* operatorNameCPtr;
switch (operator_)
{
case ADD_OP :
operatorNameCPtr = " + ";
break;
case SUBTRACT_OP :
operatorNameCPtr = " - ";
break;
case MULTIPLY_OP :
operatorNameCPtr = " * ";
break;
case DIVIDE_OP :
operatorNameCPtr = " / ";
break;
}
stream << "(" << lhsPtr_->toString()
<< operatorNameCPtr
<< rhsPtr_->toString() << ")";
return(stream.str());
}
};
NodeBuffer.h
This file defines a class that implements a thread-safe
buffer of pointers to math expressions.
Version 1a 2018 February 22 Joseph Phillips
class NodeBuffer
{
enum { SIZE = 16 };
Node* array_[SIZE];
int inIndex_;
int outIndex_;
int numItems_;
public :
NodeBuffer ()
{
for (int i = 0; i < SIZE; i++)
{
array_[i] = NULL;
}
inIndex = outIndex = numItems_ = 0;
}
~NodeBuffer ()
{
}
int getNumItems () const
{ return(numItems_); }
void putIn (Node* nodePtr)
{
while (getNumItems() >= SIZE)
{
}
array_[inIndex_] = nodePtr;
inIndex_++;
numItems_++;
if (inIndex_ >= SIZE)
inIndex_ = 0;
}
Node* pullOut ()
{
while (getNumItems() <= 0)
{
}
Node* toReturn = array_[outIndex_];
array_[outIndex_] = NULL;
outIndex_++;
numItems_--;
if (outIndex_ >= SIZE)
outIndex_ = 0;
return(toReturn);
}
}
mathSolver.cpp
This file defines the high-level functions of the math
generator and solver program.
//
// Compile with:
// $ g++ mathSolver.cpp -o mathSolver -lpthread -g
//
#include "mathSolverHeader.h"
void evaluate (void vPtr
)
{
NodeBuffer* nodeBufferPtr = (NodeBuffer*)vPtr;
// YOUR CODE HERE
}
// PURPOSE: To return a randomly generated Node.
Node* makeNode ()
{
return( (rand() % 3) ? (Node*)new ConstNode() : (Node*)new OperatorNode() );
}
int main (int argc,
char* argv[]
)
{
NodeBuffer nodeBuffer;
pthread_t consumer0;
pthread_t consumer1;
int toReturn = EXIT_SUCCESS;
srand( (argc < 2) ? getpid() : atoi(argv[1]) );
return(toReturn);
}