如何在C ++中存储根据日期排序的预定义值?

问题描述 投票:-2回答:1

我想将预定义(硬编码)值存储在person对象中,并在屏幕上显示日期排序。如何实现?

这是问题和我的代码。

社会保存成员列表并使用程序来存储列表。对于每个成员,记录以下信息

会员编号姓氏姓氏,加入社团的日期,无论他们是否仍然是会员该计划需要提供三个选项1)输入会员的详细信息(当前或过去的会员)。可以输入会员您可以假设会员编号可以从某个现有列表中获得,因此只需要将其与其他数据一起输入到程序中。

2)提示当前日期并作为输出产生已成为会员至少10年(目前仍为会员)的人员列表该列表应按日期顺序加入并根据他们已经过了多长时间分开会员(50岁以上,40岁以上等)如果同一天有多个人加入,则应按姓氏的字母顺序给出,如果他们有相同的姓氏,名字。该名单应由将成员集分类为所需的顺序。

3)提示会员编号并将该会员资格标记为不是最新的。

输出应该看起来像这样

Long-standing members at 20/02/2012

50+years
mem no date joined name
432 21/07/1963 Xerxes Smith
3103 20/02/1968 Aloysius Baker

40+years
mem no. date joined name
4934 21/02/1968 Hermione Turner
0123 08/06/1975 Bartholomew Wright
1498 08/06/1975 Ermintrude Wright

30+year

—————————————————————————————————

c++ arrays sorting object quicksort
1个回答
0
投票

我只给你泡泡排序。

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

class person
{
public:
  person(string doj)
  {
    memset(&m_doj, 0, sizeof(tm));
    std::istringstream ssDate(doj);
    ssDate >> get_time(&m_doj, "%d/%m/%Y");
  }
  tm m_doj;
  bool operator> (person &person2)
  {
    return mktime(&m_doj) > mktime(&person2.m_doj);
  }
  void output()
  {
    cout << put_time(&m_doj, "%d/%m/%Y") << "\n";
  }
};

void swap(person *xp, person *yp)
{
  person temp = *xp;
  *xp = *yp;
  *yp = temp;
}

void bubbleSort(person arr[], int n)
{
  int i, j;
  for (i = 0; i < n - 1; i++)

    // Last i elements are already in place   
    for (j = 0; j < n - i - 1; j++)
      if (arr[j] > arr[j + 1])
        swap(&arr[j], &arr[j + 1]);
}

void main()
{
  person people[5] = { person("22/02/2012"), person("21/03/2012"), person("21/02/2018"), person("21/02/1969"), person("20/02/2012") };
  bubbleSort(people, 5);
  for (int i = 0; i < 5; ++i)
  {
    people[i].output();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.