0
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

typedef struct {
    char model[50], mark[50], color[50];
    int cylinderCap;
} car;

void read(car *cr, int *nr) {
    printf("Insert mark: ");
    (*nr)++;
    fflush(stdin);
    gets((cr + *nr)->mark);
    printf("Insert model: ");
    gets((cr + *nr)->model);
    printf("Insert color: ");
    gets((cr + *nr)->color);
    printf("Insert the cylinder capacity: ");
    scanf("%d", &((cr + *nr)->cylinderCap));
}

void display(car *cr, int nr) {
    printf("\n%-10s \t%-10s \t%-10s %d", (cr + nr)->mark, (cr + nr)->model,
           (cr + nr)->color, cr[nr].cylinderCap);
}

void search_model(car *cr, int *nr, char mod[50]) {
    int i;
    for(i = 0; i <= (*nr); i++)
        if(strcmp((cr + i)->model, mod) == 0)
            display(cr, i);
}

void search_cc(car *cr, int *nr, int cc) {
    int i;
    for(i = 0; i <= (*nr); i++)
        if((cr + i)->cylinderCap >= cc)
            display(cr, i);
}


void clear(car *cr, int *nr, char mod[50]) {
    int k = 0,i,j;

    for(i = 0; i <= (*nr); i++)
        if(strcmp((cr + i)->model, mod) == 0)
        {
            k++;
            for(j = i; j <= (*nr - k); j++)
                *(cr + j) = cr[j + 1];
        }
    *nr = *nr - k;
}


void main() {

    car cr[50];
    int opt, n = -1, i, cc = 1900;
    char mod[50];

    do{
        system("CLS");
        printf("1.Add a car\n");
        printf("2.Display cars\n");
        printf("3.Search a car after its model\n");
        printf("4.Display all the cars with cc > 1900\n");
        printf("5.Remove a car after its model\n");
        printf("6.Exit\n");
        printf("7.Display the biggest cyclinder capacity of all the cars\n");
        printf("Insert option: ");
        scanf("%d", &opt);

        switch(opt) {
            case 1:
                read(&cr[0], &n);
                break;

            case 2:
                printf("\n%-10s \t%-10s \t%-10s %s", "mark", "Model", "color", "Capacity");

                for(i = 0; i <= n; i++)
                    display(cr, i);
                break;

            case 3:
                printf("Insert model: ");
                scanf("%s", mod);
                printf("\n%-10s \t%-10s \t%-10s %s", "mark", "Model", "color", "Capacity");
                search_model(&cr[0], &n, mod);
                break;

            case 4:
                printf("\n%-10s \t%-10s \t%-10s %s", "mark", "Model", "color", "Capacity");
                search_cc(&cr[0], &n, cc);
                break;

            case 5:
                printf("Insert the model you wish to delete: ");
                scanf("%s", mod);
                clear(&cr[0], &n, mod);
                break;

            case 6:
                break;

            case 7:
                break;

            default: printf("Error! Please try another option!\n");
                break;
        }
        getch();
    }while(opt != 6);

    getch();
}

这将显示:

1.Add a car
2.Display cars
3.Search a car after its model
4.Display all the cars with cc > 1900
5.Remove a car after its model
6.Exit
7.Display the biggest cylinder capacity of all the cars
Insert option:

我不知道如何做最后一个,我什至不知道如何开始!我尝试了这样的事情,但没有成功:

void maximum(car *cr, int *nr, int max) {
    int i;
    for(i = 0; i <- (*nr); i++)
        if(((cr + i)->car) >=max))
            max = (cr + i)->car;
        display(max);
}

但它并没有真正起作用,所以任何帮助将不胜感激!

4

1 回答 1

1

你的maximum功能没有任何意义。您正在尝试使用对象类型作为结构的项目,使用多余的指针算术和其他已经在您的帖子评论中讨论过的东西。

void maximum(car *cr, int nr)
{
    int i, max = 0;
    for(i = 1; i <= nr - 1; i++)
    {
        if(cr[i].cilinderCap >= cr[max].cilinderCap)
        {
            max = i;
        }
    }
    display(cr, max);
}

注意:在将其复制到您的文件之前,请尝试理解我的代码。

这是人们可以获得的最直观的方法。注意我使用“汽缸容量”变量来实际检查,嗯,最大汽缸容量。

另请注意,如果您从索引 0 开始您的汽车,您应该搜索到n - 1,而不是n。引用的参数 fornr也是多余的,因为您甚至不尝试修改它。

所以,你只需调用:

maximum(cr, n);

另一件事是您应该检查函数中的错误,因为在其当前状态下,如果列表中没有元素,它可能会崩溃。

您的代码中还有其他一些与问题无关的“道德上不正确的东西”,但我建议阅读 Google 上有关结构列表的一些文章。

于 2012-12-16T21:00:48.890 回答