#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);
}
但它并没有真正起作用,所以任何帮助将不胜感激!