我的问题发生了变化,我需要对我的日期列表进行分类,但遇到问题的问题不是正确地整理数年和数月。如果我发表评论并在几天之前排序,它可以正常工作。
输入n(1< = n< = 10000),然后是n行。每行都对应一个有效日期,该日期由一个字符串组成(" 1月"," 2月",...或" 12月"),一个整数在1到31之间,一个代表年份的两个数字整数(从90到9099,然后从00到12)。您不必担心日期验证。输入中的所有日期均为有效日期。请使用结构来存储日期。请使用Malloc动态分配足够的空间来用于N结构。要求您使用内置的QSORT函数按时间顺序分类。请从最近的日期到最古老,请输出排序列表,每行的一个日期。还请使用内置的BSearch函数允许用户查询检查特定日期是否在列表中,并输出"是"或"否"。
输入 - n,排序的日期,然后是n个日期,然后是格式日期的用户查询(例如" 1 1 00"或" 31 3 68")。(请注意,这是一种不同的格式,因为其余日期均以
表示输出 - 排序的日期列表,其次是"是"或"否",以指示用户的查询日期输入(例如1 1 00天的月份)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN_SIZE 0
#define MAX_SIZE 100
#define MAX_MONTH_STR 9
//Define a struct data type
typedef struct{
char* month;
int day;
int year;
int monthnum;
}date;
//Method to allocate memory for a list of date structures
date* allocateStruct(int size){
//Declaration of variables
date *datearray;
int i;
//Allocate memory for rows (to store 'size' many pointers to 'date' struct data type)
datearray = malloc(size*sizeof(date));
//For-loop to allocate memory for columns (to store 'size' many integers, and initialize them to zero)
for (i=0; i<size; i++){
datearray[i].month = calloc(MAX_MONTH_STR,sizeof(char));
datearray[i].day = 0;
datearray[i].year = 0;
}
return datearray;
}
int compare(const void *s1, const void *s2){
date *date1 = ( date *)s1;
date *date2 = ( date *)s2;
//if year not equal sort year
if (date2->year != date1->year){
int year2 = date2->year;
int year1 = date2->year;
if (year1<14){
year1 = year1+100;
}
if (year2<14){
year2 = year2+100;
}
int yearcompare = year2 - year1;
return -yearcompare;
}
else if (date2->monthnum != date1->monthnum){
//else if month not equal sort month
int monthcompare = date2->monthnum - date1->monthnum;
return -monthcompare;
}
else if (date2->day != date1->day){
//else sort day
int daycompare = date2->day - date1->day;
return -daycompare;
}
}
// if (daycompare == 0)
// return date1->id - date2->id;
// else
int main(){
//Declaration of variables
int n;
date* date_list;
int i, j, k; //used in loops
//Read input
do{
//printf("Enter number of dates you want to enter (between 1 and 10000):n");
scanf("%d", &n);
}
while(n<MIN_SIZE || n>MAX_SIZE);
//ALLOCATE MEMORY
date_list = allocateStruct(n);
//For-loop to store values in 'date_list'
for (i=0; i<n; i++){
//printf("Enter the date (month day year) in the following format: text number number");
scanf("%s", date_list[i].month);
scanf("%d", &date_list[i].day);
scanf("%d", &date_list[i].year);
if (strcmp(date_list[i].month,"January")==0){date_list[i].monthnum=1;}
else if (strcmp(date_list[i].month,"February")==0){date_list[i].monthnum=2;}
else if (strcmp(date_list[i].month,"March")==0){date_list[i].monthnum=3;}
else if (strcmp(date_list[i].month,"April")==0){date_list[i].monthnum=4;}
else if (strcmp(date_list[i].month,"May")==0){date_list[i].monthnum=5;}
else if (strcmp(date_list[i].month,"June")==0){date_list[i].monthnum=6;}
else if (strcmp(date_list[i].month,"July")==0){date_list[i].monthnum=7;}
else if (strcmp(date_list[i].month,"August")==0){date_list[i].monthnum=8;}
else if (strcmp(date_list[i].month,"September")==0){date_list[i].monthnum=9;}
else if (strcmp(date_list[i].month,"October")==0){date_list[i].monthnum=10;}
else if (strcmp(date_list[i].month,"Novermber")==0){date_list[i].monthnum=11;}
else if (strcmp(date_list[i].month,"December")==0){date_list[i].monthnum=12;}
}
qsort(date_list, n, sizeof(date), compare);
//Test print
for (i=0; i<n; i++){
//printf("Enter the date (month day year) in the following format: text number number");
printf("%s ", date_list[i].month);
printf("%d ", date_list[i].day);
printf("%dn", date_list[i].year);
printf("%s = %d nn", date_list[i].month, date_list[i].monthnum);
}
return 0;
}
您在用户输入时具有日期数。因此分配它们
ndates = // user input
struct Date *date = malloc(Ndates * sizeof(struct Date));
然后将它们加载到
中 for(i=0;i<ndates;i++)
{
scanf("%d %d %d ", &date[i].month, &date[i].day. &date[i].year);
}