Python 如何使用prim';使用pygraphics和C+创建圆的s算法+;?
我在这里和谷歌上看过很多Prim算法的例子,但没有找到这个问题的真正答案。。。请原谅我对这个问题的看法。我对S/O打印出来的东西很糟糕 我有一个输入文件“Python 如何使用prim';使用pygraphics和C+创建圆的s算法+;?,python,c++,algorithm,pygraphics,Python,C++,Algorithm,Pygraphics,我在这里和谷歌上看过很多Prim算法的例子,但没有找到这个问题的真正答案。。。请原谅我对这个问题的看法。我对S/O打印出来的东西很糟糕 我有一个输入文件“SmallGraph.txt”,其中包含一组坐标和顶部的顶点数: 9 50 100 100 150 200 150 300 150 350 100 300 50 200 50 100 50 150 100 我在试图弄清楚如何读取这些输入项时遇到了很多麻烦,这样我的while循环就可以为上面提到的每个垂直打印
SmallGraph.txt
”,其中包含一组坐标和顶部的顶点数:
9
50 100
100 150
200 150
300 150
350 100
300 50
200 50
100 50
150 100
我在试图弄清楚如何读取这些输入项时遇到了很多麻烦,这样我的while循环就可以为上面提到的每个垂直打印一个“圆”,这样我就可以运行Prim的最小生成树算法
到目前为止,我用while循环打印出的代码是什么。另外,有几个类可以实现Prim的算法,其中包括我需要通过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();
}
#包括
#包括
#包括
#包括
#包括
#包括
#include/*pow()函数*/
//该行允许使用常用函数,而无需指定
//在其中定义该函数的库。例如,这条线允许
//使用“cout”而不是完整的规范“cout”
使用名称空间std;
整数类集合
{
公众:
//构造函数。您希望对该类执行的任何设置操作。
集合积分()
{
成员。清除();
}//结束构造函数
void add(int m)//不重复地将成员添加到集合中
{
用于(自动i:成员)
{
if(i==m)return;//不添加现有成员
}
成员。推回(m);
}
int size(){return members.size();}
void show(){for(auto i:members)cout我使用了错误的命令从输入文件请求信息。我使用了以下命令:
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)
第二个命令不正确。我需要使用.txt文件作为“a”(执行)的参数
我已经设置好将输入以这种方式放入.dat文件中,我只是看不到它…您是在尝试实现Prim的算法还是将某些内容渲染为图形?您的描述说您在阅读文本文件时遇到问题!?如果您相应地减少问题,以便将重点放在基本内容上,这会有所帮助。我两者都有一点。我正在使用Prim的算法将某些东西渲染为图形。这个特定的问题是如何从文本文件中获取输入,并使用这些坐标通过python图形创建圆。我仍在努力解决这个问题。总之,这个问题与Prim的算法没有直接关系,因为它不管数据来自何处。因此,这里有两个部分问题,输入和输出(可选地加上中间的一些处理),这正好给了你一个确切的问题?好吧,两者都有。我不知道如何读取输入,以允许我的while循环在输入文件的坐标中创建一个圆的输出。你看到了吗?你知道如何打开一个文件并从中读取单个数字吗?如果不知道,阅读教程,搜索网页等,这应该不难找到答案。然后,朝着读取多个数字的方向迈出下一步。从小处开始。以小的增量进行改进。创建一个单独的项目设置,仅用于在将新事物添加到较大的项目之前单独测试它们。当您遇到问题时,您已经接近发布指南中提到的最小示例(你确实研究过,对吗?)。
a SmallGraph.txt > PrimData.dat