Question: Complete in C + + . Instructions: MobileElement implementation with your balance ( ) code. Your task is to complete a program that reads a

Complete in C++. Instructions: MobileElement implementation with your balance() code.
Your task is to complete a program that reads a description of a mobile and computes, for each bar, the position along that bar where the string should be tied so that, when the bar is hanging from that string, it will be balanced. This position will be expressed as a fraction (between 0 and 1) of the length of the bar from the left end to the place where the string is tied.
The main program will be named runMobiles and will take its input from a file named in a command-line parameter or, if no parameter is provided, from standard in. E.g., it can be run on an input in test.in like this:
./runMobiles < test.in
or like this
./runMobiles test.in
Your task is to write the function balance(), which computes the connection points for the bar it is given as a parameter and for all bars hanging from it, directly or indirectly. Given a mobile with N bars, it should accomplish this task in O(N) time.
Attached is mobile.cpp:
#include "mobile.h"
#include
#include
#include
using namespace std;
// Create a weight element
MobileElement::MobileElement (double w)
: isABar(false), weight(w), id(), left(nullptr), right(nullptr), balancePoint(-1.0)
{}
// Create a bar element
MobileElement::MobileElement (string idstr, Mobile lft, Mobile rght)
: isABar(true), weight(-1.0),
id(idstr), left(lft), right(rght), balancePoint(-1.0)
{}
MobileElement::~MobileElement()
{
delete left;
delete right;
}
// Read and write mobiles
Mobile MobileElement::readTree (istream& in)
{
char c;
in >> c;
if (c =='(')
{
// This is the start of a new bar description
string id;
in >> id;
Mobile left = readTree(in);
Mobile right = readTree(in);
Mobile newBar = new MobileElement (id, left, right);
in >> c;
assert (c ==')'); // Check for syntax error
return newBar;
}
else
{
// A decorative weight
double w;
in.putback(c);
in >> w;
return new MobileElement(w);
}
}
std::istream& operator>>(std::istream& in, Mobile& m)
{
m = MobileElement::readTree (in);
return in;
}
void MobileElement::printTree (ostream& out, int level) const
{
out << string(3*level,'');
if (isABar)
{
out <<"("<< id <<"
";
left->printTree(out, level+1);
out <<"
";
right->printTree(out, level+1);
out <<")";
}
else
out << fixed << setprecision(2)<< weight;
}
void MobileElement::listBars (ostream& out) const
{
if (isABar)
{
out << id <<": "<< fixed << setprecision(2)<< balancePoint << endl;;
left->listBars(out);
right->listBars(out);
}
else
{
// Do nothing for decorative elements
}
}
std::ostream& operator<<(std::ostream& out, Mobile m)
{
if (m !=0)
{
m->printTree (out,0);
out <<"
";
m->listBars(out);
}
return out;
}
double MobileElement::balance()
{
//** Insert your code here, replacing the return statement below
return -3.14159;
}
Attached is mobile.h:
#ifndef MOBILE_H
#define MOBILE_H
#include
#include
#include
class MobileElement;
typedef MobileElement* Mobile;
class MobileElement {
bool isABar;
// For decorative objects only, ignored for bars:
double weight;
// For bars only, ignored for decorative objects
std::string id;
Mobile left;
Mobile right;
double balancePoint;
public:
// Create a weight element
MobileElement (double weight);
// Create a bar element
MobileElement (std::string idstr, Mobile left, Mobile right);
~MobileElement();
/**
* When invoked on a bar, compute and set the balancePoint
* for that bar and for all bars that hang from it, directly
* or indirectly.
*
* @return the total weight of all elements hanging from this bar
*(directly or indirectly)
*/
double balance ();
double getBalancePoint() const {return balancePoint;}
private:
static Mobile readTree (std::istream& in);
void printTree (std::ostream& out, int level) const;
void listBars (std::ostream& out) const;
friend std::istream& operator>>(std::istream& in, Mobile& m);
friend std::ostream& operator<<(std::ostream& out, Mobile m);
};
// Read and write mobiles
std::istream& operator>>(std::istream& in, Mobile& m);
std::ostream& operator<<(std::ostream& out, Mobile m);
#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!