简体   繁体   English

如何在不使用复制功能的情况下对具有存储在向量中的日/月/年的实例进行排序

[英]How to sort an instance with day/month/years stored in a vector without using the copy function

I'm in an intro C++ class and we were tasked with creating a program to take data from a file regarding the weather, input it into an array, then sort it in descending order (where the most recent data is in the array's first index), and output it. 我在C ++入门课程中,我们的任务是创建一个程序,以从文件中获取有关天气的数据,将其输入到数组中,然后以降序排序(最新数据在数组的第一个索引中) ),然后将其输出。

I can get my sort function to work for the years, but once the year is the same and you're sorting by month is where I'm having trouble. 我可以让我的排序功能工作多年,但是一旦年份相同,而您按月排序就是我遇到的麻烦。 And to add even more fuss, we haven't learned the sort() function yet so we are not allowed to use it in the code. 更麻烦的是,我们还没有学习sort()函数,因此我们不允许在代码中使用它。

I've attached what I have so far, but be warned it's pretty sloppy and not commented. 我已经附上了到目前为止的内容,但请注意,它很草率,没有评论。 This is because it's to be submitted online in chunks of code, where sometimes the rest of a bit of code is written for us. 这是因为它将以代码块的形式在线提交,有时会为我们编写其余的代码。 I've done my best to piece it together, but some things may not make sense/be out of order. 我已经尽力将其拼凑在一起,但是有些事情可能没有意义/混乱。 My sortData function is at the bottom, and I've got my most recent attempt at a solution still in there. 我的sortData函数位于底部,并且我仍在尝试解决方案。 It doesn't work, obviously, but maybe it'll lend to how I'm trying to go about this. 显然,这是行不通的,但也许会有助于我尝试解决此问题。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

struct WeatherData {
   int day;
   int month;
   int year;
   int clouds;
   double temp;
   double wind;
   string watch;
};

class WeatherStation {
private:
    vector<WeatherData> m_data;
public:
    WeatherStation();
    int getNumData();
    WeatherData getData(int);
    void sortData();
    void addData(const WeatherData&);
};

int main() {
   string line;
   string file_name;

   cin >> file_name;

   //Weather variables
   int day, month, year, clouds;
   double wind, temp;
   string watch;

   WeatherStation station;
   istringstream sin;
   ifstream fin;
   string watchLine;

   fin.open(file_name.c_str());

    if(fin.fail()) {
        cout << "Could not open file.\n";
        return 1;
    }

    while(getline(fin, line)) {
        sin.clear();
        sin.str(line);
        sin >> day >> month >> year >> clouds >> temp >> wind >> watch;
        getline(sin, watchLine);
        watch = watch + watchLine;

        WeatherData data;
        data.day = day;
        data.month = month;
        data.year = year;
        data.clouds = clouds;
        data.temp = temp;
        data.wind = wind;
        data.watch = watch;

        station.addData(data);
    }

    station.sortData();

    int i;
    for(i = 0; i < getNumData(); i++) {
        cout << "Weather data for " << station.getData(i).month << "/";
        cout << station.getData(i).day << "/";
        cout << station.getData(i).year << ": ";
        cout << "Temp: " << fixed << showpoint << setprecision(2) << station.getData(i).temp;
        cout << ", Wind: " << fixed << showpoint << setprecision(3) << station.getData(i).wind;
        cout << ", Clouds: " << station.getData(i).clouds;
        cout << "\nNotes: " << setw(28) << right << station.getData(i).watch << "\n\n";
    }

    return 0;
}

int WeatherStation::getNumData() {
    return m_data.size();
}

WeatherData WeatherStation::getData(int index) {
    return m_data[index];
}

void WeatherStation::addData(const WeatherData& data) {
    m_data.push_back(data);
}

void WeatherStation::sortData() {
    int i, j, k, l, firstYear, firstMonth;
    WeatherData tempData;
    for(i = 0; i < m_data.size(); i++) {
        firstYear = i;
        for(j = i + 1; j < m_data.size(); j++) {
            if(m_data[j].year > m_data[firstYear].year) {
                firstYear = j;
            }
            else if(m_data[j].year == m_data[firstYear].year) {

                for(k = 0; k < m_data.size(); k++) {
                    firstMonth = k;
                    for(l = k + 1; l < m_data.size(); l++) {
                        if(m_data[l].month > m_data[firstMonth].month) {
                            firstMonth = l;
                        }
                    }
                }

            }
        }
        if(firstYear != i) {
            tempData = m_data[i];
            m_data[i] = m_data[firstYear];
            m_data[firstYear] = tempData;
        }
        else if(firstMonth != k) {
            tempData = m_data[k];
            m_data[k] = m_data[firstMonth];
            m_data[firstMonth] = tempData;
        }
    }
}

If you change your variable name from firstYear to latestDate you might find it clearer. 如果将变量名从firstYearlatestDate ,则可能会更清楚。 firstYear makes you think you only have a year to work with, but you have the entire WeatherData structure to work with instead. firstYear让您认为自己只有一年的时间,但是您可以使用整个WeatherData结构。

You don't need a third inner loop (your k loop in your current code) - you can compare the year, month, and day for each item as you check it. 您不需要第三个内部循环(当前代码中的k循环)-您可以在检查每个项目时比较它们的年,月和日。

So you can do 所以你可以做

if(m_data[j].year > m_data[latestDate].year) {
    latestDate = j;
}
else if(m_data[j].year == m_data[latestDate].year) {
    if(m_data[j].month> m_data[latestDate].month) {
        latestDate = j;
    }
    else if(m_data[j].month == m_data[latestDate].month) {
        ... do the same for day

Ideally you would encapsulate this in a member function of WeatherData or even as a standalone compare function which takes two WeatherData objects, but you can leave it in the sort method if that is what is required by the task. 理想情况下,您可以将其封装在WeatherData的成员函数中,甚至封装成一个带有两个WeatherData对象的独立比较函数,但是如果任务需要,可以将其保留在sort方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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