내 문제가 변경되었으므로 날짜 목록을 정렬해야하지만 문제가 발생하여 년 및 월을 올바르게 정렬하지 않아야합니다. 내가 주석으로 처리하고 며칠 씩 정렬하면 잘 동작합니다.C 가변 입력으로 structs 및 malloc을 사용하는 방법
n (1 < = n < = 10000)을 입력 한 다음 n 행을 입력하십시오. 각 행은 하나의 문자열 ("January", "February", ... 또는 "December"), 1과 31 사이의 하나의 정수 및 연도를 나타내는 하나의 두 자리 정수 (90에서 99, 00 ~ 12). 당신은 날짜 검증에 대해 걱정할 필요가 없습니다. 입력 된 모든 날짜는 유효한 날짜입니다. 구조체를 사용하여 날짜를 저장하십시오. malloc을 사용하여 n 구조체에 충분한 공간을 동적으로 할당하십시오. 내장 된 qsort 함수를 사용하여 연대순으로 날짜를 정렬하라는 메시지가 표시됩니다. 가장 최근 날짜부터 오래된 날짜까지 한 줄에 하나씩 정렬 된 목록을 출력하십시오. 또한 내장 bsearch 함수를 사용하여 사용자 쿼리가 특정 날짜가 목록에 있는지 여부를 확인하고 "예"또는 "아니요"를 출력 할 수 있습니다.
INPUT- n, 정렬 할 날짜 수, 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;
}
에서 그들을로드 할당? 그렇게 믿을만하지 않은 것 같습니다 ... –
C는 프로세스 지향이며 소스 코드 라인 번호 (스크립트)를 지향하지 않습니다 ... –
코드가 임의의 방향으로'main'에서 유출 된 것으로 보입니다. 당신은 그걸 모두 뒤로 모으는 것이 가장 좋습니다. – StoryTeller