1

私はネストされた構造を持っています。「内部構造」はメンバーを持つ日付構造です。日、月、年。構造体は動的配列に含まれています。構造体をループして、最も古い日付を持つ構造体を見つけたいと思います。私はプログラミングが初めてで、これにアプローチする方法がよくわかりません。助けてください。ありがとう!

#include <stdio.h>
#include <stdlib.h>

//define structure to store students birth date
struct date
{
    int month;
    int day;
    int year;
};
//define structure to store student info and date structure for birth date
struct studentInfo
{
    int iD;
    struct date birthDate;
    int phone;
};

int main(void)
{
    //declare and initialize variables
    int recNum = 0;     //number of records
    struct studentInfo * records = NULL;    //struct pointer, array
    //request user input  and store in recNum for record amount
    printf("\nHow many students do you need to enter records for?:");
    scanf ("%d",&recNum);
    //dynamically allocate memory
    records = (struct studentInfo*)malloc((sizeof(struct studentInfo)*recNum));
    //loop through records and request/store values from user
    int count;
    int studentNum=1;
    for(count=0;count<recNum;count++)
    {
        printf("Please enter the following for student number %d\n",studentNum);
        //request and store student ID
        printf("Student ID#:");
        scanf ("%d",&records[count].iD);
        //request and store student phone number
        printf("Student phone# (numbers only, 10 digits):");
        scanf ("%d",&records[count].phone);

        //error checking, check if phone number is 10 digits
        int phoneCount = 0;
        int phoneCopy = records[count].phone;
        while(phoneCopy != 0)
        {
            phoneCopy /= 10;
            phoneCount++;
        }
        if (phoneCount != 10)
        {
            printf("The number you have entered is not 10 digits, please re-enter:");
            scanf ("%d",&records[count].phone);
        }

        //request and store student birthdate
        printf("Student birthday (mm/dd/yyyy):");
        scanf("%d/%d/%d",&records[count].birthDate.month,&records[count].birthDate.day,
                &records[count].birthDate.year);

        //test stuff
        printf("Student number %d has an ID of %d and a phone number of %d\n", studentNum,
                records[count].iD, records[count].phone);
        studentNum++;
    }
    return 0;
}
4

2 に答える 2

0
#define NUMCMP(x,y) (((x) < (y)) ? -1 : ((x) > (y)) ? 1 : 0)

int compar_student_byDate(const struct studentInfo *student1, const struct studentInfo *student2){
    struct date date1 = student1->birthDate;
    struct date date2 = student2->birthDate;
    int tmp;
    if((tmp=NUMCMP(date1.year, date2.year))==0){
        if((tmp=NUMCMP(date1.month, date2.month))==0)
            return NUMCMP(date1.day, date2.day);
        else
            return tmp;
    } else
        return tmp;
}

struct studentInfo *oldest(struct studentInfo *records, int recNum){
    struct studentInfo *old = records;
    int i;
    for(i = 1;i<recNum;++i){
        if(compar_student_byDate(old, &records[i])>0)
            old = &records[i];
    }
    return old;
}

メインで

struct studentInfo *old_student = oldest(records, recNum);
于 2013-07-04T22:35:13.243 に答える
-1

わかりました、最初に、これは非常に多くの正解がある質問であることをお伝えしておく必要があります。構造体の配列を作成する代わりにstudentInfo、ツリーを作成し、新しいノードをツリーに挿入するたびに、他の多くのソリューションの中でルート ノードを最も古いものとして保持することができます。とにかく、あなたの単純なケースでは、単純な解決策は次のようになります

ステップ 0. 見つけたい

studentInfo *oldest = records;

ステップ 1. 配列を反復処理する

studentInfo *traveler;
for(traveler = records; i<recNum; i++, traveler++)

ステップ 2. 最も古い日付と旅行者の日付を比較します。

if(oldest->birthDate.year > traveler->birthDate.year && 
   oldest->birthDate.month > traveler->birthDate.month &&
   oldest->birthDate.day > traveler->birthDate.day)

ステップ 3. 見つけるたびに、見つけた最も古い記録を更新する

oldest = traveler;

反復を終了すると、最も古いレコードが探しているレコードになります。

于 2013-07-04T21:58:14.127 に答える