我是通用编程的新手,我试图将其读取到文本文件中,将其存储在向量中,对其进行排序然后输出。下面显示了代码和我的输出:
Point2D.cpp:
Point2D::Point2D()
{
x = 0;
y = 0;
distFrOrigin = 0.0;
}
Point2D::Point2D(int x, int y)
{
x = x;
y = y;
distFrOrigin = 0.0;
}
Point2D::~Point2D() {
//destructor
}
void Point2D::setX(int x) {
this->x = x;
}
void Point2D::setY(int y) {
this->y = y;
}
int Point2D::getX() {
return x;
}
int Point2D::getY() {
return y;
}
double Point2D::getScalarValue() {
setDistFrOrigin();
return distFrOrigin;
}
void Point2D::setDistFrOrigin() {
distFrOrigin = sqrt(abs((pow(x, 2)) + pow(y, 2)));
}
ostream& operator<<(ostream &out, const Point2D &p2d)
{
out << "[" << setw(4) << p2d.x << "," << setw(4) << p2d.y << "]" << setw(3) << "" << endl;
return out;
}
我具有这些排序功能,这些功能也在驱动程序文件中列出:
bool sortP2DAscX(const Point2D & rhs, const Point2D & lhs) {
return lhs.x > rhs.x;
}
bool sortP2DDscX(const Point2D & rhs, const Point2D & lhs) {
return lhs.x < rhs.x;
}
bool sortP2DAscY(const Point2D & rhs, const Point2D & lhs) {
return lhs.y > rhs.y;
}
bool sortP2DDscY(const Point2D & rhs, const Point2D & lhs) {
return lhs.y < rhs.y;
}
bool sortP2DOAsc)(const Point2D & rhs, const Point2D & lhs) {
return lhs.distFrOrigin < rhs.distFrOrigin;
}
bool sortP2DODsc)(const Point2D & rhs, const Point2D & lhs) {
return lhs.distFrOrigin > rhs.distFrOrigin;
}
我的代码向我显示一个负值,文本文件如下: messy.txt:
Point2D, [324, -374]Point2D, [882, -588]Point2D, [-993, 958]Point2D, [6, -922]Point2D, [376, 219]Point2D, [600, 203]Point2D, [425, -676]
这是我在文本文件中读取的main.cpp驱动程序文件的一部分:
main.cpp:
void inputTextFile() {
string fileName;
cout << endl;
cout << "Please enter filename : "; cin >> fileName;
ifstream file(fileName, ios::in);
while (getline(file, fileName)) {
if (!fileName.empty()) {
stringstream linestream(fileName);
string readDataType = "";
string omnom;
string stringX1, stringY1, stringZ1, stringX2, stringY2, stringZ2;
int bX1, bY1, bZ1, bX2, bY2, bZ2;
// Save PID into StockArray
getline(linestream, readDataType, ',');
if (readDataType == "Point2D") {
// [bX, bY]
//Trim
getline(linestream, omnom, '[');
//Get X coordinate
getline(linestream, stringX1, ',');
bX1 = stoi(stringX1);
//Get Y coordinate
getline(linestream, stringY1, ']');
bY1 = stoi(stringY1);
Point2D bP2D = Point2D(bX1, bY1);
GP2D.push_back(bP2D);
records++;
}
}
}
}
void viewData() {
cout << endl;
cout << "[ View data ... ]" << endl;
cout << "Filtering Criteria: " << curFilterOpt << endl;
cout << "Sorting Criteria : " << curSortCri << endl;
cout << "Sorting Order: " << curSortOrder << endl;
cout << endl;
if (curFilterOpt == "Point2D")
{
if (curSortCri == "x-ordinate") {
if (curSortOrder == "ASC") {
sort(GP2D.begin(), GP2D.end(), sortP2DAscX);
}
else if (curSortOrder == "DSC") {
sort(GP2D.begin(), GP2D.end(), sortP2DDscX);
reverse(GP2D.begin(), GP2D.end());
}
} else if (curSortCri == "y-ordinate") {
if (curSortOrder == "ASC") {
sort(GP2D.begin(), GP2D.end(), sortP2DAscY);
}
else if (curSortOrder == "DSC") {
sort(GP2D.begin(), GP2D.end(), sortP2DDscY);
}
}
cout << setw(5) << "X" << setw(6) << "Y" << " Dist. Fr Origin" << endl;
cout << "-------------------------------" << endl;
for (int i = 0; i < GP2D.size(); ++i) {
cout << GP2D[i] << " " << fixed << setprecision(3) << GP2D[i].getScalarValue() << endl;
}
}
为什么输出中的所有值都是负数?它们都是相同的数字。文本文件中的值可以在-999到999的范围内。但是每个Point2D中的值都不同。
答案 0 :(得分:2)
您的代码中有一个错字:x = y
Point2D::Point2D(int x, int y)
{
x = y;
y = y;
distFrOrigin = 0.0;
}