-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightInfoSort.c
More file actions
170 lines (150 loc) · 4.09 KB
/
FlightInfoSort.c
File metadata and controls
170 lines (150 loc) · 4.09 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include "Flight.h"
//选择需要通过的信息排序航班信息
void SortSelectOne(flightLists* info)
{
int one = -1;
printf("\n");
puts("\t-------------------------");
puts("\t|\t1.航班号\t|");
puts("\t-------------------------");
puts("\t|\t2.票价\t\t|");
puts("\t-------------------------");
puts("\t|\t0.退出排序\t|");
puts("\t-------------------------");
printf("请输入想通过的信息排序航班信息(0-2):");
while (one < 0 || one > 2)
{
scanf("%d", &one);
switch (one)
{
case 0:
SelectOptions(info);
break;
case 1:
SortByFlightNum(info);
break;
case 2:
SortByPrice(info);
break;
default:
printf("请输入正确的信息来排序航班信息(0-2):");
}
}
}
//通过航班号进行升、降序排序
void SortByFlightNum(flightLists* info)
{
flightInfo temp; //交换,作为排序交换的媒介
char sort = 'A'; // 升序(ASC)或者降序(DESC),默认升序
printf("\n请输入通过升序(ASC)或者降序(DESC)排序,(A/D):");
scanf("%c", &sort);
scanf("%c", &sort);
printf("\n");
while (sort != 'A' && sort != 'a' && sort != '\n' && sort != 'D' && sort != 'd')
{
printf("请输入正确字符(A/D):");
scanf("%c", &sort);
}
if (sort == 'A' || sort == 'a' || sort == '\n')
{
for (int x = 0; x < info->length; x++)
{
for (int y = x + 1; y < info->length; y++)
{
if (strcmp(info->infos[x].FlightNum, info->infos[y].FlightNum) > 0)
{
temp = info->infos[x];
info->infos[x] = info->infos[y];
info->infos[y] = temp;
}
}
}
}
else if (sort == 'D' || sort == 'd')
{
for (int x = 0; x < info->length; x++)
{
for (int y = x + 1; y < info->length; y++)
{
if (strcmp(info->infos[x].FlightNum, info->infos[y].FlightNum) < 0)
{
temp = info->infos[x];
info->infos[x] = info->infos[y];
info->infos[y] = temp;
}
}
}
}
SortPrint(info);
}
//通过票价进行升、降序排序
void SortByPrice(flightLists* info)
{
flightInfo temp; //交换
char sort = 'A'; // 升序(ASC)或者降序(DESC),默认升序
printf("\n请输入通过升序(ASC)或者降序(DESC)排序,(A/D):");
scanf("%c", &sort);
scanf("%c", &sort);
printf("\n");
while (sort != 'A' && sort != 'a' && sort != '\n' && sort != 'D' && sort != 'd')
{
printf("请输入正确字符(A/D):");
scanf("%c", &sort);
}
if (sort == 'A' || sort == 'a' || sort == '\n')
{
for (int x = 0; x < info->length; x++)
{
for (int y = x + 1; y < info->length; y++)
{
if (info->infos[x].price > info->infos[y].price)
{
temp = info->infos[x];
info->infos[x] = info->infos[y];
info->infos[y] = temp;
}
}
}
}
else if (sort == 'D' || sort == 'd')
{
for (int x = 0; x < info->length; x++)
{
for (int y = x + 1; y < info->length; y++)
{
if (info->infos[x].price < info->infos[y].price)
{
temp = info->infos[x];
info->infos[x] = info->infos[y];
info->infos[y] = temp;
}
}
}
}
SortPrint(info);
}
//打印排序后信息
void SortPrint(flightLists* info)
{
char yOrn;
puts("\t-------------------------------------------------------------------------------------------------------------------------");
puts("\t| 航班号 | 起点站 | 终点站 | 起飞时间 | 到达时间 | 机型 | 票价 |");
puts("\t-------------------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < info->length; i++)
{
printf("\t| %s |\t %s | %s |\t%s | %s | %s | %d |\n", info->infos[i].FlightNum, info->infos[i].StartPoint, info->infos[i].EndPoint,
info->infos[i].StartTime, info->infos[i].EndTime, info->infos[i].PlaneType, info->infos[i].price);
puts("\t-------------------------------------------------------------------------------------------------------------------------");
}
printf("\n是否退出排序操作(y/n):");
scanf("%c", &yOrn);
scanf("%c", &yOrn);
printf("\n");
if (yOrn == 'y' || yOrn == 'Y' || yOrn == '\n')
SelectOptions(info);
else
SortSelectOne(info);
}