Member Avatar for Member #920253

Hey everyone, I am working on this project and do not know where to start. Can anyone lead me in the right direction? I am very poor in this background and could use all the help I can get. So, any help would be very much appreciated. Here are the details:

  1. Problem Statement
    Definition: A polyline is a sequence of points P1, P2, … , Pn in a plan. The points are determined by its coordinates: P1(x1, y1), P2 (x2, y2), … , Pn (xn, yn), n≥1.

A polyline is presented as an XML (text) file, called input2.txt. The tag names used in the file are:
• <point> // The tag for a point.
• <x> // The tag for x-coordinate.
• <y> // The tag for y-coordinate.

Example [A Polyline Represented as an XML file]
<Point> <x> 10 </x> <y> 10 </y> </Point>
<Point> <x> 20 </x> <y> 20 </y> </Point>
<Point>
<x> 10 </x> <y> 40 </y>
</Point>
<Point>
<x> 40 </x>
<y> 40 </y>
</Point>
<Point> <x> 30 </x> <y> 0 </y> </Point>
(A) Write a class PolyLine with the following interface (member functions):
• PolyLine – By default constructor for creating a polyline from an XML text file with name input2.txt.
• length - Calculates the length of the current polyline object (length = sum of all segments = P1P2 + P2P3 + … Pn-1Pn);
• insert – Inserts a new point in the polyline object (first, middle, last);
• remove – Removes a point from the polyline object;
• show – Show the current polyline object as sequence of (x, y) coordinates on the monitor;
• size – the current size of the polyline (number of points);
• store – Store the current polyline object as an XML text file output2.txt.

(B) Write a driver to test the PolyLine class. The driver should offer a friendly menu for multiple executions of all operations on a PolyLine object.
• Creates an object of class PolyLine.
• Calculating the length
• Inserting new node
• Removing a node
• Show the current polyline object
• Show the current size of the polyline
• Exit
Input Data: input2.txt - XML text file, representing a polyline
Output Data: output2.txt - XML text file, representing the updated polyline.

Important Note:
The internal representation of the PolyLine must be a list. You could use the STL List class or you could create your own linked list class.

Dani AI

Generated

Good plan in ’s reply: parse the XML first, model a Point, then build the PolyLine API and driver. Important correction for the assignment: the internal container must be a linked list (STL list or a custom linked list). A vector will work functionally but violates the spec and makes mid-list insert/remove O(n) instead of O(1).

Suggested private section for the PolyLine header:

private:
    std::list<Point> points_;
    size_t count_ = 0;            // keep if compiling pre-C++11 (std::list::size() not guaranteed O(1))
    mutable double cachedLength_ = 0.0;
    mutable bool lengthDirty_ = true;
    std::string sourceFile_;     // remember input (or default to "input2.txt")

    void parseXML(const std::string &fname);
    void writeXML(const std::string &fname) const;
    void invalidateCache() noexcept { lengthDirty_ = true; }

Rationale and practical notes: store coordinates as double unless the assignment forces int. Maintain count_ for guaranteed O(1) size() on older compilers. Keep a cached length and mark it dirty on any insert/remove so length() can be lazy and fast when unchanged. Provide insertion APIs that accept an index and/or an iterator; iterator-based public functions keep operations O(1). On removal update count_ and call invalidateCache().

Parsing and robustness: a tiny ad-hoc parser that looks for <x>/</x> and <y>/</y> is fine for this simple format, but handle case/whitespace and malformed files. For production-grade parsing use a small XML library (tinyxml2 or similar) to avoid fragile string parsing. Test edge cases: empty file, single point, repeated points, negative coordinates, bad tags. Ensure store() writes consistent, well-formed XML (consistent tag case, reasonable precision). Default constructor can open "input2.txt" but also provide an overload that accepts a filename for easier testing.

Recommended Answers

All 2 Replies

Member Avatar for Member #920253

I just want advice of what I should start off with. I know I have to start with the header file, but I do not know how to start it off. I can worry about the implementation and the test driver later, but I really need to know how to start off the header file. Like, what should my private section of my class? I am confused when it comes to all this.

You should do it in pieces.

1.Start with reading and parsing the XML file first. This may be the function that takes you the longest. Create a program the takes in the file name, opens the file and parses out each (x, y) and prints them to the screen. Once you can do that, it's easy to paste that code into the PolyLine constructor.

2.Make a point class that holds the (x,y) pair.

   class Point {
     public:
       Point(int xVal, int yVal): x(xVal), y(yVal){};
       int getX() const { return x;};
       int getY() const { return y;};
       Point & setX(int xVal) { x=xVal; return *this;};
       Point & setY(int yVal) { y=yVal; return *this;};
     private:
       int x;
       int y;
   };

3.Next you need the PolyLine class with all of the functions that are required.

   class PolyLine {
     public:
       PolyLine(string XMLFileName);
       ~PolyLine();
       void show();
       bool store(string XMLFileName);
       float  length();
       int size();
       bool  insert();
       bool  remove();
     private:
       vector<Point> theLine;
   };

4.As you add each function to the PolyLine class, you will be creating the driver test program at the same time. Add the menu options as you get to them.
5.When testing, use a line that is simple. Like for calculating the length of the line use (0,0) & (3,0) and see that you get 3. Or (0,0) & (3,4) should be 5 (3, 4, 5 triangle).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.