#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);
}
但它並沒有真正的工作,所以任何幫助,將不勝感激!
'conio.h'不是標準標題。 'fflush(stdin)'是錯誤的。切勿使用'gets'。 'void main'是錯誤的。避免用戶輸入的'scanf'。 'scanf「%s」'是錯誤的。 「不起作用」不是問題描述。 – melpomene
另外:'for(i = 0; i < - (* nr); i ++)'。也許你打算寫'for(i = 0; i <=(* nr); i ++)'? –
對於(i = 0; i <=(* nr); i ++) @melpomene - 這就是他們在學校教我們的方式!這可能是錯誤的,但在這一點上,我只是一個初學者,現在我想專注於如何解決簡單的事情! – user1908349