Question

In: Computer Science

Begin by defining the data type Point that has two coordinate members x and y. Specifically,...

  1. Begin by defining the data type Point that has two coordinate members x and y. Specifically, Point will be implemented as a structure; define operators as needed to satisfy problem requirements.

b, prompt the user to input several (x,y) pairs. As the data is entered, store it in a vector of Points called orginal_points. Input is terminated with the EOF character (differs by OS type). To be clear, I am asking you to define an input stream operator to read the point format: (x,y). As an example, the input of (5,6) is the correct input format for a point. While the I/O format includes the ‘(‘, ‘,’, and ‘)’ characters, the internal storage contains only the x and y values. Please Note -- I am expecting a robust, user-friendly way to ensure Points are entered correctly and in a valid format. A simple termination of the program is not acceptable here -- If I’ve entered a number of valid points, don’t discard them -- recover from the error so that input can continue (if possible) and if input cannot continue, ensure that the program proceeds to save the points I have entered. Also important -- this input stream operator needs to work with standard input (cin) and formatted (text) file input streams.

  1. Print the data in original_points to the screen so we can see what it looks like. Create an output stream operator for this that works with both cout and output file streams.
  1. Open an ofstream and write each Point to a file named mydata.txt, using the operator you created in #3 above. Be sure to include the Points in the correct format, one Point per line. Explicitly close the file. Next, inspect the file. It should have the same contents as what you printed on the screen. Does it?
  1. Open the file and, using the same input logic that you created in b, read in the Point data and print the results to the screen. (This is to demonstrate that your input operator is flexible to be used with multiple stream types. Since the data in the file should conform perfectly to the required format, your input checking should run smoothly here…) it's c++ language and Input error handling -- Points data structure
    #include <iostream>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    struct Point{
        int x, y;
        bool operator==(const Point& p2) {
            return this->x == p2.x and this->y == p2.y;
        }
        bool operator!=(const Point& p2) {
            return this->x != p2.x or this->y != p2.y;
        }
        friend ostream &operator<<( ostream &out, const Point &P ) { 
             out << "(" << P.x << ", " << P.y << ")";
             return out;
        }
    
        friend istream &operator>>( istream  &in, Point &P ) { 
            char d1, d2, d3;
            // input format: (1, 2)
            in >> d1 >> P.x >> d2 >> P.y >> d3;
            return in;
        }
    };
    
    int main()
    {
        vector<Point> original_points;
        cout << "Enter points :\n";
        Point P;
        while( cin >> P )
        {
            original_points.push_back(P);
        }
    
        ofstream out("mydata.txt");
        cout << "You entered the points:\n";
        for(Point p: original_points)
        {
            cout << p << '\n';
            out << p << '\n';
        }
        out.close();
        //pause
        cin.get();
        char ch;
        cout << "Press enter to continue: ";
        getchar();
    
        ifstream in("mydata.txt");
        vector<Point> processed_points;
        while( in >> P )
        {
            processed_points.push_back(P);
        }
        int n = original_points.size();
        for(int i=0; i<n; i++)
        {
            if(original_points[i] == processed_points[i])
            {
                cout << "Points at index " << i << " are same\n" 
                     << original_points[i] << " "
                     << processed_points[i] << '\n';
            }
            if(original_points[i] != processed_points[i])
            {
                cout << "Points at index " << i << " are not same\n"
                     << original_points[i] << " "
                     << processed_points[i] << '\n';
            }
        }
    } depend on this code write error handling code

Solutions

Expert Solution

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct Point{
int x, y;
bool operator==(const Point& p2) {
return this->x == p2.x and this->y == p2.y;
}
bool operator!=(const Point& p2) {
return this->x != p2.x or this->y != p2.y;
}
friend ostream &operator<<( ostream &out, const Point &P ) {
out << "(" << P.x << ", " << P.y << ")";
return out;
}

friend istream &operator>>( istream &in, Point &P ) {
char d1, d2, d3;
// input format: (1, 2)
in >> d1 >> P.x >> d2 >> P.y >> d3;
return in;
}
};

int main()
{
vector<Point> original_points;
cout << "Enter points :\n";
Point P;
// loop continues until any error occurs
while( cin >> P )
{
// if any input error occurs, exit the loop keeping the valid points in vector
if(cin.fail())
{
cin.clear(); // back in 'normal' operation mode
cin.ignore(100,'\n'); // and remove the bad input
break;
}

original_points.push_back(P);
}

ofstream out("mydata.txt");
cout << "You entered the points:\n";
for(Point p: original_points)
{
cout << p << '\n';
out << p << '\n';
}
out.close();
//pause
//cin.get();
char ch;
cout << "Press enter to continue: ";
cin>>ch;

ifstream in("mydata.txt");
vector<Point> processed_points;

// loop to read the data from input file
while( in >> P )
{
// if input file read error occurs, exit the loop
if(in.fail())
{
in.clear(); // back in 'normal' operation mode
in.ignore(100,'\n'); // and remove the bad input
break;
}
processed_points.push_back(P);
}

int n = original_points.size();
for(int i=0; i<n; i++)
{
if(original_points[i] == processed_points[i])
{
cout << "Points at index " << i << " are same\n"
<< original_points[i] << " "
<< processed_points[i] << '\n';
}
if(original_points[i] != processed_points[i])
{
cout << "Points at index " << i << " are not same\n"
<< original_points[i] << " "
<< processed_points[i] << '\n';
}
}

return 0;
}

//end of program

Output:

Output file: mydata.txt


Related Solutions

A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In...
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In this problem, you will write a Point class which stores this information and provides useful methods to work with points. We have provided an __init__ definition for the Point class. You will note that this accepts two arguments, being the x- and y-coordinates of the point. We have also included a __repr__ method, which will provide a canonical string representation of the point. Make...
find the x-coordinate of the point, correct to two decimal places, on the parabola y=3.08-x^2 at...
find the x-coordinate of the point, correct to two decimal places, on the parabola y=3.08-x^2 at which the tangent line cuts from the first quadrant the triangle with the smallest area.
In a rectangular coordinate system, a positive point charge 5.0nC is placed at x=2cm, y=0cm and...
In a rectangular coordinate system, a positive point charge 5.0nC is placed at x=2cm, y=0cm and a negative point charge -5.0nC is placed at x=-2cm,y=0cm. Point P is at x=2cm, y=3cm K=8.99 x 10^9 (Nxm^2)/C^2 a) Calculate the magnitude of the force between the positive charge and negative charge b) Draw the electric field vector at point P, caused by the positive charge. Find its magnitude c) Draw the electric field vector at point P, caused by the negative charge....
Given the plot of y=f(x) below, find the plot of y=f−1(x). A coordinate plane has a...
Given the plot of y=f(x) below, find the plot of y=f−1(x). A coordinate plane has a horizontal x-axis labeled from negative 7 to 7 in increments of 1 and a vertical y-axis labeled from negative 7 to 7 in increments of 1. A curve starts at the point left-parenthesis negative 1 comma 0 right-parenthesis, rises at an increasing rate from left to right and passes through left-parenthesis 1 comma 1 right-parenthesis and left-parenthesis 4 comma 6 right-parenthesis. Select the correct...
C Language Let us define a Point type to store two-dimensional coordinates (x, y)! Write the...
C Language Let us define a Point type to store two-dimensional coordinates (x, y)! Write the following functions operating on this data type: dist(): calculates the distance between the two points received (using the Pythagorean theorem) equal(): checks if to points are equal or not read(): reads a point from the keyboard and returns it In the main, define two points, and test all these functions. When all tests are passed, solve the following task by utilizing the structure and...
Consider a two-dimensional ideal flow in the x-y plane (with radial coordinate r2 = x2 +...
Consider a two-dimensional ideal flow in the x-y plane (with radial coordinate r2 = x2 + y2). Given the velocity potentials of 1) a uniform flow, 2) a source φ = (q/2π) ln r, and 3) a dipole φ = −d · r/(2πr2): a) Using the principle of superposition, construct a linear combination of the ingredients above that gives the flow past an infinite cylinder. [10 points] b) Sketch the streamlines of the flow everywhere in space. [10 points]
Imagine a person’s utility function over two goods, X and Y, where Y represents dollars. Specifically,...
Imagine a person’s utility function over two goods, X and Y, where Y represents dollars. Specifically, assume a Cobb-Douglas utility function: U(X,Y) = Xa Y(1-a) where 0<a<1. Let the person’s budget be B. The feasible amounts of consumption must satisfy the following equation:                                                                 B = pX+Y where p is the unit price of X and the price of Y is set to 1. Solving the budget constraint for Y and substituting into the utility function yields                                                                                 U =...
x' = x − y y' = 4y − x^2*y Linearize the system about the point...
x' = x − y y' = 4y − x^2*y Linearize the system about the point (2, 2). Classify the type and stability of the critical point at (2, 2) by examining the linearized system.
Consider a region R bound by the coordinate axes and y = ( 9 + x...
Consider a region R bound by the coordinate axes and y = ( 9 + x 2 ) − 1 2 on 0 ≤ x ≤ 4. a. Find the area of R. b. Suppose R is revolved about the x-axis to form a solid. Find the volume of the solid. c. Suppose R is revolved about the y-axis to form a solid. Find the volume of the solid.
A charge of -3.00 nC is placed at the origin of an x-y coordinate system, and...
A charge of -3.00 nC is placed at the origin of an x-y coordinate system, and a charge of 2.00 nC is placed on the y-axis at y = 4.00 cmb. If a third charge of 5.00 nC is now placed at the point x = 3.00 cm, y = 4.00 cm, what are the x and y components of the total force exerted on the charge by the other two charges?c.e. What are the magnitude and direction of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT