Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

What is paper chromatography?

Answered: 1 week ago

Question

Explain the cost of capital.

Answered: 1 week ago

Question

Define capital structure.

Answered: 1 week ago