首页 > 解决方案 > 从向量中读取和排序的文件中的负输出值

问题描述

我是通用编程的新手,我试图让它读入一个文本文件,将它存储在一个向量中,对其进行排序,然后输出它们。下面显示了代码和我的输出:

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 驱动程序文件的一部分:

主.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 中都有不同的值。

标签: c++

解决方案


您的代码中有一个错字:x = y

Point2D::Point2D(int x, int y) 
{
    x = y;
    y = y;
    distFrOrigin = 0.0;
}

推荐阅读