【问题标题】:Sorting a list of dates by chronological order?按时间顺序对日期列表进行排序?
【发布时间】:2017-03-19 01:43:13
【问题描述】:

我的程序采用(月日年)格式的日期列表。然后它按最近事件的时间顺序对其进行排序。它应该从 90 到 99,然后从 00 到 12 对年份进行排序。例如,它应该对这个列表进行排序。

1 月 1 日 01

1 月 1 日 00

2 月 28 日 99

12 年 7 月 17 日

2012 年 9 月 10 日

7 月 1 日 00

90 年 6 月 30 日

8 月 25 日 06 日

2008 年 5 月 27 日

10 月 1 日 03 日

进入这个。

2012 年 9 月 10 日

12 年 7 月 17 日

2008 年 5 月 27 日

8 月 25 日 06 日

10 月 1 日 03 日

1 月 1 日 01

7 月 1 日 00

1 月 1 日 00

2 月 28 日 99

90 年 6 月 30 日

但是我似乎无法让排序正常工作。

#include <stdio.h>
#include <stdlib.h>

/* constants for max chars, max day, max year, max size */
enum { MAXC = 12, MAX_DAY = 31, MAX_YEAR = 2017, MAX_SIZE = 1000 };

typedef struct {
    char month[MAXC];   /* either make static or allocate separately */
    unsigned day;
    unsigned year;
} date;

/* empty character remaining in stdin */
void empty_stdin ()
{
    int c;
    while ((c = getchar ()) != '\n' && c != EOF) {}
}

/* sort struct date on year */
int sort (const void *a, const void *b)
{
    date *date1 = (date *) a;
    date *date2 = (date *) b;

    if (date2->year != date1->year)
        return (date1->year < date2->year) - (date1->year > date2->year);

    return (date1->year < date2->year) - (date1->year > date2->year);




    return 0;
}

/* output n elements of array of struct date */
void output (date *ar, int n)
{
    int i;

    printf ("\nOutput sorted by year:\n\n");

    for (i = 0; i < n; i++)
        printf ("  %s %d %d\n", ar[i].month, ar[i].day, ar[i].year);
}

int main (void) {

    int i, n;
    date *ar = NULL;

    while (1) {     /* obtain valid 'n', compare with using fgets below */

        int rtn; /* varaible to save return of scanf -- always validate */

        //printf ("Enter number of dates to be entered (between 1 & 1000): ");
        if ((rtn = scanf ("%d", &n)) != 1) {   /* if conversion failed */
            if (rtn == EOF) {   /* test for user cancelation of input */
                fprintf (stderr, "note: user canceled input, exiting.\n");
                return 0;
            }                   /* otherwise simply an invalid input */
            fprintf (stderr, "error: invalid input.\n");
            goto tryagain;
        }

        if (n < 0) {            /* invalid input < 0 */
            fprintf (stderr, "error: invalid input (n < 0).\n");
            goto tryagain;
        }

        if (n > MAX_SIZE) {     /* invalid input > MAX_SIZE */
            fprintf (stderr, "error: invalid input (n > %d).\n", MAX_SIZE);
            goto tryagain;
        }

        break;      /* if we are here - we have a good value, break */

      tryagain:;    /* label for goto to jump over break */

        empty_stdin ();   /* empty characters that remain in input buffer */
    }

    empty_stdin ();     /* empty characters that remain in input buffer */

    /* allocate array of struct ar, n elements */
    if ((ar = malloc (sizeof *ar * n)) == NULL) {
        fprintf (stderr, "error: virtual memory exhausted.\n");
        return 1;
    }

    /* provide format instructions */
    //printf ("Enter the date month day year\n"
           // "  format, e.g.:  Jan 18 2017\n\n");

    for (i = 0; i < n;) {   /* loop until all elements filled */

        char buf[MAX_DAY + 1] = "", ans[MAXC] = "";

        //printf (" date[%2d] : ", i + 1);    /* prompt for input */

        /* if fgets return is NULL, EOF encountered */
        if (fgets (buf, MAX_DAY + 1, stdin) == NULL) {
            fprintf (stderr, "note: user canceled input, exiting.\n");
            return 0;
        }

        if (*buf == '\n') { /* if first char is '\n', user just hit enter */
            printf ("no input provided, quit (y/n)? ");
            if (fgets (ans, MAXC, stdin) && (*ans == 'y' || *ans == 'Y'))
                return 0;
            else if (!*ans) {   /* if ans NULL, EOF encountered */
                fprintf (stderr, "note: user canceled input, exiting.\n");
                return 0;
            }
        }

        /* parse with sscanf, validate 3 conversion took place */
        if (sscanf (buf, "%11s %u %u", ar[i].month, &ar[i].day, &ar[i].year) != 3)
        {
            fprintf (stderr, "error: invalid input.\n");
            continue;
        }

        i++;    /* only increment if valid sscanf conversion took place */
    }

    qsort (ar, n, sizeof (date), sort);     /* sort by year */

    output (ar, n);     /* output results */

    free (ar);      /* free ar - you allocate it, you free it */

    return 0;
}

【问题讨论】:

  • 4 > 3 显然(如 04 > 03),但 99 > 4 也是,对吧?如果您希望对事物进行不同于正常的排序,则需要指定要控制的排序标准。
  • 嗯,我希望它在最近日期的这个标准中按照这个标准排序,比较从 00 到 12 的年份并按时间顺序对它们进行排序,然后比较 90 到 99 的年份并排序。我不知道如何排序,因为我不能只按最大整数排序。我已经尝试对是否在同一年进行排序

标签: c sorting


【解决方案1】:

最简单的解决方案可能是在您读入它后修复年份(然后在打印出来时将其转回为 Y2K 倾向的代码,如有必要)。

unsigned yy;
... sscanf (buf, "%11s %u %u", ar[i].month, &ar[i].day, &yy) ...
ar[i].year = yy >= 90 ? 1900 + yy : 2000 + yy

您必须调整 90 以获得您打算使用的实际截止值。

您可以类似地将月份转换为整数。或者您甚至可以将输入转换为儒略日之类的东西,从而使排序变得微不足道,但输出稍微复杂一些。如果您有很多日期要排序,那么简化排序是一种胜利。

【讨论】:

  • 在我的代码中实现你的想法时遇到了麻烦,你能详细说明一下吗?
猜你喜欢
  • 1970-01-01
  • 2012-12-22
  • 2019-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
相关资源
最近更新 更多