Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 按时间顺序排列日期列表?_C_Sorting - Fatal编程技术网

C 按时间顺序排列日期列表?

C 按时间顺序排列日期列表?,c,sorting,C,Sorting,我的程序采用(月-日-年)格式的日期列表。然后按照最近事件的时间顺序进行排序。它应该从90年到99年,然后从00年到12年进行排序。例如,它应该对该列表进行排序 二零零一年一月一日 一月一日00 一九九九年二月二十八日 7月17日12 九月十一二日 七月一日00 1990年6月30日 2006年8月25日 2008年5月27日 2003年10月1日 这件事 九月十一二日 7月17日12 2008年5月27日 2006年8月25日 2003年10月1日 二零零一年一月一日 七月一日00 一月一日0

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

二零零一年一月一日

一月一日00

一九九九年二月二十八日

7月17日12

九月十一二日

七月一日00

1990年6月30日

2006年8月25日

2008年5月27日

2003年10月1日

这件事

九月十一二日

7月17日12

2008年5月27日

2006年8月25日

2003年10月1日

二零零一年一月一日

七月一日00

一月一日00

一九九九年二月二十八日

1990年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;
}
#包括
#包括
/*最大字符数、最大天数、最大年份、最大大小的常数*/
枚举{MAXC=12,MAX_DAY=31,MAX_YEAR=2017,MAX_SIZE=1000};
类型定义结构{
char month[MAXC];/*可以是静态的,也可以是单独分配的*/
未签名日;
未签字年份;
}日期;
/*stdin中剩余的空字符*/
无效空的\u stdin()
{
INTC;
而((c=getchar())!='\n'&&c!=EOF){}
}
/*按年份排序结构日期*/
整数排序(常量无效*a,常量无效*b)
{
日期*日期1=(日期*)a;
日期*日期2=(日期*)b;
如果(日期2->年份!=日期1->年份)
返回(date1->yearyear)-(date1->year>date2->year);
返回(date1->yearyear)-(date1->year>date2->year);
返回0;
}
/*输出struct date数组的n个元素*/
无效输出(日期*ar,整数n)
{
int i;
printf(“\n按年份排序的输出:\n\n”);
对于(i=0;i最大大小){/*无效输入>最大大小*/
fprintf(stderr,“错误:无效输入(n>%d)。\n”,最大大小);
goto tryagain;
}
break;/*如果我们在这里-我们有很好的价值,break*/
tryagain:;/*后藤跳过休息的标签*/
empty_stdin();/*保留在输入缓冲区中的空字符*/
}
empty_stdin();/*保留在输入缓冲区中的空字符*/
/*分配结构数组,n个元素*/
如果((ar=malloc(sizeof*ar*n))==NULL){
fprintf(stderr,“错误:虚拟内存耗尽。\n”);
返回1;
}
/*提供格式说明*/
//printf(“输入年月日\n”
//“格式,例如:2017年1月18日\n\n”);
对于(i=0;i
最简单的解决方案可能是在读入后修复年份(如果需要,在打印时将其转换回易受Y2K影响的代码)

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


您可以类似地将月份转换为整数。或者您甚至可以将输入转换为类似于朱利安日的内容,从而使排序变得微不足道,但输出稍微复杂一些。如果你有很多日期要排序,简化排序是一个胜利。

4>3显然(如04>03),但99>4也是,对吗?如果你想让事情以不同于正常的方式进行排序,你需要指定你想要控制的排序标准。我想让它在最近的日期按照这个标准进行排序,比较从00年到12年的年份,并按时间顺序进行排序,然后比较90年到99年并进行排序。我不知道如何排序,因为我不能按最大整数排序。我已经尝试过分类,如果是在同一年,我在代码中实现你的想法时遇到困难,你能详细说明一下吗?
unsigned yy;
... sscanf (buf, "%11s %u %u", ar[i].month, &ar[i].day, &yy) ...
ar[i].year = yy >= 90 ? 1900 + yy : 2000 + yy