私の問題は変わりました。私は日付のリストを並べ替える必要がありますが、年月を正しく並べ替えるのに問題があります。私がコメントアウトして、ちょうどそれを並べ替えるとうまくいきます。C可変入力のstructsとmallocの使い方
n(1 < = n < = 10000)を入力してn行を入力します。各行は、1つの文字列( "January"、 "February"、...または "December")、1〜31の整数、および年を表す1つの2桁の整数(90〜 99、次に00〜12)。日付の検証について心配する必要はありません。入力のすべての日付は有効な日付です。 日付を格納する構造体を使用してください。 mallocを使用して、n個の構造体に十分なスペースを動的に割り当てるようにしてください。組み込みのqsort関数を使用して日付を時間順にソートするように求められます。最新の日付から古いものまでソートされたリストを1行に1つずつ出力してください。また、組み込みのbsearch関数を使用して、特定の日付がリストにあるかどうかをユーザー照会で検査し、「はい」または「いいえ」のいずれかを出力できるようにしてください。
INPUT- n、ソートする日付の数、n個の日付、その後の日の形式のユーザークエリ(例:「1 1 00」または「31 3 68」)。 (日付の残りの部分に示されているように、これは異なるフォーマットであることに注意してください)日付の
出力 - ソートされたリスト、「はい」または「いいえ」を示すために、続いか否かをユーザによってクエリ日付入力( 1月1日の月)がリストに含まれています。ユーザーがそれを入力すると
#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