简体   繁体   中英

How to use coordinates from an input file with prim's algorithm in order to create circles using pygraphics and C++?

I have looked at many many examples of Prim's Algorithm on here and Google and found no real answer to this question... Please excuse my structure of this problem. I'm terrible with how S/O prints things out.

I have an input file " SmallGraph.txt " that contains a set of coordinates and the number of vertices at the top:

9  
50 100  
100 150  
200 150  
300 150  
350 100  
300 50  
200 50  
100 50  
150 100

I'm having a lot of trouble trying to figure out how to get these input items read so that my while loop will be able to print a "circle" for every vertice mentioned above so I can run Prim's algorithm for a minimum spanning tree.

What code I have so far of me attempting to get something printed out with a while loop. Also, a few classes to implement Prim's algorithm with those points I need to plot through python:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cstring>
#include <math.h>       /* pow() function */


// This line allows commonly-used functions to be used without specifying the 
// library in which that function is defined. For instance, this line allows
// the use of "cout" rather than the full specification "cout"
using namespace std;

class SetOfIntegers
{
    public:
        // Constructor. Any setup operation you wish for the class.
        SetOfIntegers()
        {
            members.clear();
        } // end constructor


        void add(int m)  // Add members to set WITHOUT repetition
        { 
            for (auto i : members)
            {
                if (i == m) return;  // no addition of existing member
            }
            members.push_back(m); 
        }
        int size() { return members.size(); }
        void show() { for (auto i: members) cout << i << "  "; cout << endl; }

        bool isMember(int m) 
        {
            //cout << "isMember(" << m << ") is ";
            for (auto i : members)
            {
                if (i == m) 
                {
                    //cout << " true" << endl;
                    return true;
                }
            }
            //cout << " false" << endl;
            return false;
        }

    private:
        vector<int> members;

};

//--------------------------------------------------------------------------
class Point
{

    public:

        // Constructor. Any setup operation you wish for the class.
        Point()
        {
            x = 0; y = 0;  
        } // end constructor
        Point(int a, int b, int id)
        {
            x = a; y = b; pointID = id;
        } // end constructor

        int getX() { return x; }
        int getY() { return y; }
        int getID() { return pointID; }

    private:
        int x = 0; 
        int y = 0;
        int pointID = 0;

}; // end class Point


//--------------------------------------------------------------------------
class Edge
{

    public:

        // Constructor. Any setup operation you wish for the class.
        Edge()
        {

        } // end constructor
        Edge(Point ptA, Point ptB)
        {
            pointA = ptA;
            pointB = ptB;
            length = sqrt(pow(abs(pointA.getX() - pointB.getX() ), 2) + pow(abs(pointA.getY() - pointB.getY() ), 2) );
        } // end constructor

        Point getPtA() { return pointA; }
        Point getPtB() { return pointB; }
        double getLen() { return length; }
        int getPtAID() { return pointA.getID(); }
        int getPtBID() { return pointB.getID(); }

    private:
        Point pointA;
        Point pointB;
        double length;

}; // end class Edge



// NOTE: DO NOT declare with empty parentheses, as vector<Point> myPointvector();
vector<Point> myPointvector;  // vector will expand as needed
vector<Edge> MinSpanTree;



// Pass arguments or parameters from command-line execution. argc is the count of
// those parameters, including the executable filename. argv[] is an array of the 
// parameters.
int main (int argc, char *argv[])
{
    string token;
    int xValue, yValue;
    ifstream fin;
    int coordPairs;  // number of coordinate pairs in the file
    int ptX, ptY;
    vector<Edge> unsortedEdgeVector;
    vector<Edge> sortedEdgeVector;

    int loopCounter;
    int pointCounter = 0;
    double MSTLength = 0.0;


    // Check the number of arguments. Expected: filename of a file
    if (argc != 2)  // This check is often hardcoded
    {   // If failure in parameters, offer advice for correction
        cout << "\nThis program uses command-line argument.\n";
        cout << "Usage: a.exe <filename>\n";
        exit(0);
    }



    try  // All lines within this block are part of the same exception handler
    {
        fin.open(argv[1]);
    } 
    catch (exception& ex) 
    {
        cout << ex.what();  // display standard explanation of the exception
        exit(0);  // exit the program
    }


    // Read from the file, one token at a time. If the type of token is known, it
    // can be read into a corresponding variable type, such as 
    //          in >> x;    // Read the first item into an integer variable x.
    //          in >> str;  // Read the next item into a string variable str.
    //for (int i = 0; 1 != 10; i++) {
    //  fin >> ptX[2] >> ptY[2];
    //}
    //cout << ptX << endl;

    // This line provides the graphic window setup. 
    cout << "800 600 white" << endl;

    fin >> coordPairs;
    while (fin >> ptX)
    {
        // Do something with the element read from the file
        cout << ptX << endl;
        fin >> ptY;   
        cout << ptY << endl;

        cout << "circle " << ptX << " " << ptY << " " << 20 << " seagreen" << endl;

        /*
        Point dummyPoint(ptX, ptY, pointCounter++);
        myPointvector.push_back(dummyPoint);  // vector will expand as needed

        cout << "Now myPointvector has size " << myPointvector.size() << endl;
        */

    } // end while

    fin.close();

}

As you can see... I have a while loop in my main function that is attempting to create a "circle" based on ptX and ptY. That's what I'm having trouble with.. How do I read from this input file in order to get these points and get them to create a circle through python? If you notice.. I've attempted a for loop that is currently commented out for reading the file.

I was using the wrong command for requesting information from the input file. I was using the commands:

g++ -std=c++11 PrimMSTAlgor.cpp (Compile the code)
a PrimMSTAlgor.cpp > PrimData.dat (Put data into primData.dat from the .cpp file)
python BearPlot.py PrimData.dat (use python to apply graphics)

The second command is incorrect. I need to use the .txt file as the argument for "a" (the execution).

a SmallGraph.txt > PrimData.dat

What I have is already set up to put the input into the .dat file this way, I just couldn't see it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM