خرید بک لینک

Vote count: 0

I try to write a console application which is able to sort a struct data type.

I declared this kind of struct data type:

typedef struct Rechteck *TPRechteck;
typedef struct Rechteck
{
    int x;
    int y;
} TRechteck;

It describes a rectangle. x and y are the two sides.

I declared a function to calculate the area of each rectangle (x*y). With the help of that, i want to sort it. Important is to say that it has to be a array of void pointers. I'm sitting here for hours and can't find my mistake. My compiler accepts my code but it crashes after starting. Can you help me? It's probably just one pointer.

int comp(void *a, void *b);
void sort(void *f[], int anzahl, int (*comp)(void *x, void *y));
int calcA(int x, int y);

int main()
{
    int i;
    TRechteck r[MAX];

    for(i=0; i<MAX;i++)
    {
        printf("%d. Rechteck, Seitenlaenge a: ",(i+1));
        scanf("%d",&(r[i].x));
        printf("%d. Rechteck, Seitenlaenge b: ",(i+1));
        scanf("%d",&(r[i].y));
    }

    void* f;
    f = (void*)r;
    sort(f, MAX, comp);

    retu 1;
}

// this function compares which rectangle has the bigger area
int comp(void *a, void *b)
{        
    if ( calcA(((TPRechteck)a)->x, ((TPRechteck)a)->y) < 
         calcA(((TPRechteck)b)->x, ((TPRechteck)b)->y)) 
    {
        retu 1;
    }


    if ( calcA(((TPRechteck)a)->x, ((TPRechteck)a)->y) > 
         calcA(((TPRechteck)b)->x, ((TPRechteck)b)->y)) 
    {
        retu 0;
    }

    else retu (-1);
}

int calcA(int x, int y)
{
    retu x*y;
}

void sort(void *f[], int anzahl, int (*comp)(void *x, void *y))
{
    int i,j;
    void *temp;

    // Bubblesort
    for(i = 0; i < MAX-1; i++)
    {
        for(j = 0; j < MAX-i-1; j++)
        {
            if(comp(f[j],f[j+1]) == 1)
            {
                temp = f[j];
                f[j] = f[j+1];
                f[j+1] = temp;
            }

            else continue;     
        } 
    }
}

asked 30 secs ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 17 فروردين 1395 ساعت: 4:22

صفحه بندی