#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);
}
但它并没有真正起作用,所以任何帮助将不胜感激!
最佳答案
你的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
。 nr
的引用参数也是多余的,因为您甚至不尝试修改它。
所以,你只需调用:
maximum(cr, n);
另一件事是,您应该检查函数中是否有错误,因为在当前状态下,如果列表中没有元素,它可能会崩溃。
您的代码中还有一些与问题无关的“道德上不正确的内容”,但我建议您阅读 Google 上有关结构列表的一些文章。
关于c - 显示C—结构中所有汽车的最大气缸容量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13905193/