简体   繁体   中英

Calling funtions in C in main

I am trying to call my functions in the main part of the program in the C language but i am having trouble with figuring out which parameters are correct in order to get them to work. If someone could point me in the right direction that would be very helpful. This C program reads data from a file then either sorts the floats or int's from low to high or high to low depending on the menu option selected.

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

struct data {
    char color[40];
    float engine;
    int n;
    char make[40];
};

void IntL2H( int a[], int n) {
    int i, j, t;
    for( i = n - 2; i >= 0; i--) {
        for(j = 0; j <= i; j++) {
            if(a[j] > a[j + 1]) {
                    t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                } 
            }
        }
}
void IntH2L( int a[], int n) {
    int i, j, t;
    for( i = n - 2; i >= 0; i--) {
        for( j = 0; j <= i; j++) {
            if(a[j] < a[j +1]) {
                t = a[j];
                a[j] = a[j + 1];
                a[j +1] = t;
            }
        }
    }
}
void FloatL2H(float a[], int n) {
    int i, j;
    float t;
    for(i = n - 2; i >= 0; i--) {
        for(j = 0; j <= i; j++) {
            if(a[j] > a[j + 1]) {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    }
}
void FloatH2L(float a[], int n) {
    int i, j;
    float t;
    for(i = n - 2; i >= 0; i--) {
        for(j = 0; j <= i; j++) {
            if(a[j] , a[j + 1]) {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    }
}
void readData(struct data *D, int n) {
    char color[40];
    float engine;
    char make[40];
    FILE *fp;
    int i = 0, counter = 0, len;

    fp = fopen("hw2.txt", "r");
    if (fp == NULL) {
        printf("\n Error unable to open file");
        exit(0); }
    while(!feof(fp)) {
        fscanf(fp, "%s %f %d %s",(D + (n))->color,&(D + (n))-> engine,&(D + (n))->n,(D + (n))->make);
        (n)++;
    }
    fclose(fp);
}


int main () {
    int input;
    struct data *D;
    do  {
        printf("1. Sort the data by the float vlaue& print high to low\n"
               "2. Sort the data by the float value & print low to high\n"
               "3. Sort the data by the int value & print high to low\n"
               "4. Sort the data by the int value & print low to hugh\n"
               "5. Exit\n");
        printf("Enter a number\n");
        scanf("%d", &input);}
    while (input != 5);

    do {
        switch(input) {
            case 1:
                FloatH2L(data, n);
                break;
        }
    }
}

You have a number of bugs:

  1. You are trying to sort your struct but all your sort functions take a pointer to int/float when they should take struct data *
  2. You really want [only] two sort functions for (L2H or H2L) and they take an extra "compare" function pointer (similar to qsort ).
  3. In readData , your fscanf format does not match the order of the data in your input file lines.
  4. readData should return the count of the number of records read in. Otherwise, you don't know the value for n to pass to the sort functions.
  5. Don't use feof
  6. In main , your prompting loop (eg scanf ) to get number will never call a sort/print function.
  7. Your struct members are a bit misnamed. Your data has a model number and not a color.
  8. You only handled one case in your switch

Unfortunately, because of the above issues, there was a lot of refactored code. But, here it is:

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

struct data {
    char make[40];
    char model[40];
    float engine;
    int n;
};

void
data_print(const struct data *d)
{

    printf("%s %s %.1f %d\n",d->make,d->model,d->engine,d->n);
}

void
array_print(const struct data *a,int n)
{

    for (;  n > 0;  --n, ++a)
        data_print(a);

}

void
SortL2H(struct data *a, int n, int (*cmp)(const void *,const void *))
{
    int i, j;
    struct data t;

    for (i = n - 2; i >= 0; i--) {
        for (j = 0; j <= i; j++) {
            if (cmp(&a[j],&a[j + 1]) > 0) {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    }

    array_print(a,n);
}

void
SortH2L(struct data *a, int n, int (*cmp)(const void *,const void *))
{
    int i, j;
    struct data t;

    for (i = n - 2; i >= 0; i--) {
        for (j = 0; j <= i; j++) {
            if (cmp(&a[j],&a[j + 1]) < 0) {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    }

    array_print(a,n);
}

int
cmpfloat(const void *vlhs,const void *vrhs)
{
    const struct data *lhs = vlhs;
    const struct data *rhs = vrhs;
    int cmp;

    do {
        if (lhs->engine > rhs->engine) {
            cmp = 1;
            break;
        }

        if (lhs->engine < rhs->engine) {
            cmp = -1;
            break;
        }

        cmp = 0;
    } while (0);

    return cmp;
}

int
cmpint(const void *vlhs,const void *vrhs)
{
    const struct data *lhs = vlhs;
    const struct data *rhs = vrhs;
    int cmp;

    do {
        if (lhs->n > rhs->n) {
            cmp = 1;
            break;
        }

        if (lhs->n < rhs->n) {
            cmp = -1;
            break;
        }

        cmp = 0;
    } while (0);

    return cmp;
}

int
readData(struct data *D, int n)
{
    FILE *fp;
    int i = 0,
        counter = 0,
        len;

    fp = fopen("hw2.txt", "r");
    if (fp == NULL) {
        printf("\n Error unable to open file");
        exit(0);
    }

// NOTE/BUG: do _not_ use feof
#if 0
    while (!feof(fp)) {
        fscanf(fp, "%s %f %d %s", (D + (n))->color, &(D + (n))->engine,
            &(D + (n))->n, (D + (n))->make);
        (n)++;
    }

// NOTE/FIX: simplify with a "current" pointer (e.g. "p")
#else
    struct data *p = D;
    i = 0;
    for (;  i < n;  ++i, ++p) {
        if (fscanf(fp, "%s %s %f %d",p->make,p->model,&p->engine,&p->n) != 4)
            break;
    }
#endif

    fclose(fp);

    return i;
}

int
main(void)
{
    int input;
    struct data D[1000];

    int n = readData(D,sizeof(D) / sizeof(D[0]));

    while (1) {
        printf("1. Sort the data by the float value & print high to low\n"
            "2. Sort the data by the float value & print low to high\n"
            "3. Sort the data by the int value & print high to low\n"
            "4. Sort the data by the int value & print low to hugh\n"
            "5. Exit\n");

        printf("Enter a number\n");
        if (scanf("%d", &input) != 1) {
            printf("bad number\n");
            continue;
        }

        if (input == 5)
            break;

        switch (input) {
        case 1:
            SortH2L(D,n,cmpfloat);
            break;
        case 2:
            SortL2H(D,n,cmpfloat);
            break;
        case 3:
            SortH2L(D,n,cmpint);
            break;
        case 4:
            SortL2H(D,n,cmpint);
            break;
        default:
            printf("unknown number\n");
            break;
        }
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM