Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.8 kB
16
Indexable
Never
#include <stdio.h>

struct Data
    {
        int id;
        char name[20];
        struct datum
        {
            int bd;
            int bm;
            int by;
        }date;
    };

int freq[10000];

int compare(struct Data d1, struct Data d2);

int freqCalc(struct Data d1);

int main()
{
    int n, x, y;
    FILE *fp=fopen("file.txt", "r");
    fscanf(fp,"%d", &n);

    struct Data d[n];
    for (int i = 0; i <n; i++)
    {
        fscanf(fp,"%d", &d[i].id);
        fscanf(fp, "%s",d[i].name);
        
        fscanf(fp,"%d%d%d", &d[i].date.bd, &d[i].date.bm, &d[i].date.by);

        ///calculate frequency
        freqCalc(d[i]);
    }   
    fscanf(fp,"%d%d", &x, &y);
  
    fclose(fp);
    int a = compare(d[x],d[y]);

    FILE *ffp = fopen("student_output.txt", "w");
    if(a==0)
        fprintf(ffp,"%d %s\n %d %s", d[x].id, d[x].name, d[y].id, d[y].name );
    else if(a==1)
        fprintf(ffp,"%d %s\n", d[x].id, d[x].name);
    else
        fprintf(ffp,"%d %s\n", d[y].id, d[y].name);
    fclose(ffp);

    return 0;
}

int freqCalc(struct Data d1)
{
//increment the birthyear freq arr can be big as
//said by teacher
    freq[d1.date.by]++;
}
///Concept: if return is 1 then the first struct 
// data (d[x]) is bigger in age, 
// if 2 then second (d[y]) and
// if 0 then both are equal
int compare(struct Data d1, struct Data d2)
{
    if(d1.date.by<d2.date.by)
        return 1;
    else if (d1.date.by>d2.date.by)
        return 2;
    else
    {
        if(d1.date.bm<d2.date.bm)
            return 1;
        else  if(d1.date.bm>d2.date.bm)
            return 2;
        else
        {
            if(d1.date.bd<d2.date.bd)
                return 1;
            else if (d1.date.bd>d2.date.bd)
                return 2;
            else if(d1.date.bd == d2.date.bd)
                return 0;
        }
    }

}