【问题标题】:C programming, how would I find the oldest date in an array of date structures?C 编程,如何在日期结构数组中找到最旧的日期?
【发布时间】:2013-07-02 20:16:15
【问题描述】:

我有一个嵌套结构,“内部结构”是带有成员的日期结构;日、月、年。这些结构包含在一个动态数组中。我想遍历结构并找出哪个结构具有最旧的日期。我是编程新手,不太确定如何处理这个问题。请帮忙。谢谢!

#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;
}

【问题讨论】:

  • 你有结构数组吗?给你看代码
  • 尝试先发布一些代码。
  • 遍历数组,并保持对迄今为止遇到的最旧的(大概是最小的,如果我们谈论的是纪元时间)日期的引用。就像基本的 Math.min() 实现一样。
  • 保留当前最旧值的副本(最初是数组中的第一个值);遍历数组的其余部分,将新值与最旧的值进行比较,必要时获取新发现的最旧值的副本。
  • 任何解决方案都可能涉及比较功能,通过循环查找最旧的需要知道什么是“旧”。也可以对列表进行排序,但同样需要比较函数。类似int date_compare(struct date * d1, struct date * d2)

标签: c arrays date loops structure


【解决方案1】:
#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);

【讨论】:

    【解决方案2】:

    好的,首先我要告诉你,这是一个可以有很多很多正确答案的问题。 您可以代替创建结构数组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;
    

    当您完成迭代后,最旧的记录应该是您要查找的记录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 2021-09-07
      相关资源
      最近更新 更多