首页 文章

C如何使用带有变量输入的结构和malloc

提问于
浏览
-2

我的问题已经发生了变化,我需要对日期列表进行排序,但遇到问题却没有正确分类年份和月份 . 如果我评论出来并按天排序它可以正常工作 .

输入n(1 <= n <= 10000),然后输入n行 . 每行对应一个有效日期,包括一个字符串(“1月”,“2月”,......或“12月”),1到31之间的一个整数,以及代表年份的一个两位数整数(从90到99,然后从00到12) . 您不必担心日期验证 . 输入中的所有日期都是有效日期 . 请使用结构来存储日期 . 请使用malloc为n个结构动态分配足够的空间 . 系统会要求您使用内置qsort函数按时间顺序对日期进行排序 . 请输出排序列表,每行一个日期,从最近的日期到最早的日期 . 还请使用内置bsearch函数允许用户查询以检查列表中是否有特定日期,并输出“是”或“否” .

INPUT-,要排序的日期数,后跟n个日期,然后是格式日月份的用户查询(例如“1 1 00”或“31 3 68”) . (注意这是一个不同的格式,因为其余的日期显示在)

OUTPUT-排序的日期列表,后跟“是”或“否”,以指示用户输入的查询日期(例如,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("%d\n", date_list[i].year);
    printf("%s = %d \n\n", date_list[i].month, date_list[i].monthnum);
}

return 0;

}

1 回答

  • 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);
       }
    

相关问题