-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPLB_L_A08_OPT1_EX2.c
118 lines (95 loc) · 2.98 KB
/
CPLB_L_A08_OPT1_EX2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Tạo struct
struct Vehicle
{
char cCode[20];
char cTye[30];
char cBrand[30];
char cMainColor[24];
int nPrice; // USD
int nSeat; // Slot
int nWeight; // Ton
};
struct Vehicle InputVehicleInfo(struct Vehicle *vehicle) {
printf("Nhap vao ma xe: ");
fflush(stdin);
fgets(&vehicle->cCode, sizeof(vehicle->cCode), stdin);
printf("Nhap vao loai xe: ");
fflush(stdin);
fgets(&vehicle->cTye, sizeof(vehicle->cTye), stdin);
printf("Nhap vao thuong hieu xe: ");
fflush(stdin);
fgets(&vehicle->cBrand, sizeof(vehicle->cBrand), stdin);
printf("Nhap vao mau xe: ");
fflush(stdin);
fgets(&vehicle->cMainColor, sizeof(vehicle->cMainColor), stdin);
printf("Nhap vao gia xe: ");
fflush(stdin);
scanf("%d", &vehicle->nPrice);
printf("Nhap vao cho ngoi xe: ");
fflush(stdin);
scanf("%d",&vehicle->nSeat);
printf("Nhap vao khoi luong xe: ");
fflush(stdin);
scanf("%d", &vehicle->nWeight);
}
void PrintVehicleInfo(struct Vehicle vehicle) {
printf("Ma so dinh danh: %s\n",vehicle.cCode);
printf("Kieu xe: %s\n",vehicle.cBrand);
printf("Thuong hieu xe: %s\n",vehicle.cBrand);
printf("Color: %s\n",vehicle.cMainColor);
printf("Gia xe: %d $\n",vehicle.nPrice);
printf("Cho ngoi: %d slot\n",vehicle.nSeat);
printf("Khoi luong xe: %d Ton",vehicle.nWeight);
}
int main()
{
int i,n;
printf("Nhap vao so loai xe trong danh sach: ");
scanf("%d",&n);
struct Vehicle ListVehicle[n];
// Tìm xe giá cao nhất:
for (i = 0; i < n; i++)
{
InputVehicleInfo(ListVehicle+i);
}
int HighestPrice = ListVehicle[0].nPrice;
for (i = 0; i < n; i++)
{
if (HighestPrice < ListVehicle[i].nPrice)
{
HighestPrice = ListVehicle[i].nPrice;
}
}
printf("Xe co gia cao nhat la: ");
for (i = 0; i < n; i++)
{
if (ListVehicle[i].nPrice==HighestPrice)
{
printf("\n------------------------------------------------------------------\n");
PrintVehicleInfo(ListVehicle[i]);
printf("\n------------------------------------------------------------------\n");
}
}
// Tìm xe có giá thấp nhất:
int LowestPrice = ListVehicle[0].nPrice;
for (i = 0; i < n; i++)
{
if (LowestPrice > ListVehicle[i].nPrice)
{
LowestPrice = ListVehicle[i].nPrice;
}
}
printf("Xe co gia thap nhat la: ");
for (i = 0; i < n; i++)
{
if (ListVehicle[i].nPrice==LowestPrice)
{ printf("\n------------------------------------------------------------------\n");
PrintVehicleInfo(ListVehicle[i]);
printf("\n------------------------------------------------------------------\n");
}
}
return 0;
}