C语言实现航班订票系统

时间:2022-12-03 19:15:59

本文实例为大家分享了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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
using namespace std;
const int MAXN=250;
typedef struct
{
  string p_id;
  int sum;
  int r;
  int c;
  int selected;
  int select;
  string start;
  string startp;
  string arrive;
  string arrivep;
  int acx[MAXN][MAXN];
} node;
typedef struct Pnode
{
  node data;
  struct Pnode *next;
} Pnode,*Plist;
typedef struct
{
  int r;
  int c;
  string name;
  string kp_id;
  string k_id;
} node1;
typedef struct Knode
{
  node1 data;
  struct Knode *next;
} Knode,*Klist;
void init(Plist &l)
{
  l=new Pnode;
  l->next=NULL;
}
void init(Klist &L)
{
  L=new Knode;
  L->next=NULL;
}
void creatp(Plist &l,int e)
{
  cout<<endl<<endl;
  Plist r=new Pnode;
  r=l;
  for(int i=0; i<e; i++)
  {
    Plist ll=new Pnode;
    cout<<endl;
    cout<<"请依次输入航班班次,起飞时间,起飞地点,到达时间,到达地点,座位行数,列数,总座位数,已被购买的数目,未被购买的数目"<<endl;
    cout<<'\t';
    cin>>ll->data.p_id;
    cout<<" ";
    cin>>ll->data.start;
    cout<<" ";
    cin>>ll->data.startp;
    cout<<" ";
    cin>>ll->data.arrive;
    cout<<" ";
    cin>>ll->data.arrivep;
    cout<<" ";
    cin>>ll->data.r;
    cout<<" ";
    cin>>ll->data.c;
    cout<<" ";
    cin>>ll->data.sum;
    cout<<" ";
    cin>>ll->data.selected;
    cout<<" ";
    cin>>ll->data.select;
    for(int j=1; j<=ll->data.r; j++)
      for(int v=1; v<=l->data.c; v++)
        ll->data.acx[j][v]=0;
    ll->next=NULL;
    r->next=ll;
    r=ll;
  }
}
void creatk(Klist &L,node1 e)
{
  Klist LL=new Knode;
  LL->data=e;
  LL->next=NULL;
  Klist r;
  r=L;
  while(r->next!=NULL)
  {
    r=r->next;
  }
  r->next=LL;
  //cout<<L->next->data.r<<endl;
}
void show(Plist &l)
{
  Plist p=new Pnode;
  p=l->next;
  while(p!=NULL)
  {
    cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<p->data.p_id<<" "<<p->data.start<<" "<<p->data.startp<<" "<<p->data.arrive<<" "<<p->data.arrivep<<" "<<p->data.sum<<" "<<p->data.selected<<" "<<p->data.select<<endl;
    for(int i=1; i<=p->data.r; i++)
    {
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t';
      for(int j=1; j<=p->data.c; j++)
        cout<<p->data.acx[i][j];
      cout<<endl;
    }
    p=p->next;
  }
  return ;
}
void alter(Plist &l,node1 e,int flag)
{
  Pnode *p,*pre;
  p=l->next;
  while(p->data.p_id!=e.kp_id)
  {
    pre=p;
    p=p->next;
  }
  if(flag)
  {
    p->data.select-=1;
    p->data.selected+=1;
    p->data.acx[e.r][e.c]=1;
  }
  else
  {
    p->data.select+=1;
    p->data.selected-=1;
    p->data.acx[e.r][e.c]=0;
  }
  return ;
}
int delet(Klist &L,node1 e)
{
  Klist p,pre;
  p=L;
  while(p->next!=NULL)
  {
    if(p->data.name==e.name&&p->data.k_id==e.k_id&&p->data.kp_id==e.kp_id)
      break;
    pre=p;
    p=p->next;
  }
  if(p==NULL)
    return 0;
  else
  {
    //cout<<"hjdhfjks"<<endl;
    pre->next=p->next;
    free(p);
    return 1;
  }
}
int searchh(Klist &L,node1 e)
{
  Knode *p;
  p=L->next;
  while(p!=NULL)
  {
    if(p->data.name==e.name&&p->data.k_id==e.k_id&&p->data.kp_id==e.kp_id)
    {
      cout<<endl;
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"您的位置是"<<p->data.r<<"行"<<p->data.c<<"列"<<endl;
      return 1;
    }
    p=p->next;
  }
  return 0;
}
void showone(Plist &l,node1 e)
{
  Pnode *p;
  p=l->next;
  while(p!=NULL)
  {
    if(p->data.p_id==e.kp_id)
    {
      cout<<endl;
      cout<<'\t'<<"您的航班信息如下(依次为航班班次,起飞时间,起飞地点,到达时间,到达地点,座位总数,已购座位数,未购座位数)"<<endl;
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<p->data.p_id<<" "<<p->data.start<<" "<<p->data.startp<<" "<<p->data.arrive<<" "<<p->data.arrivep<<" "<<p->data.sum<<" "<<p->data.selected<<" "<<p->data.select<<endl;
      return ;
    }
  }
  return ;
}
int judge(Plist &l,node1 e)
{
  Pnode *p;
  p=l->next;
  while(p!=NULL)
  {
    //cout<<p->data.acx[e.r][e.c]<<endl;
    if(p->data.p_id==e.kp_id)
    {
      if(p->data.acx[e.r][e.c])
        return 0;
    }
    p=p->next;
  }
  return 1;
}
int main()
{
  Plist l;
  Klist L;
  init(l);
  init(L);
  int choose,n;
  node p;
  node1 k;
  cout<<endl<<endl;
  cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"初始化存储航班信息"<<endl;
  cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"输入航班总数:";
  cin>>n;
  system("cls");
  creatp(l,n);
  system("cls");
  while(1)
  {
    cout<<endl;
    cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"1.客户订票"<<endl;
    cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"2.客户退票"<<endl;
    cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"3.客户查询航班信息"<<endl;
    cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"0.退出系统"<<endl;
    cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"选择功能:";
    cin>>choose;
    system("cls");
    if(!choose)
    {
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<endl;
      break;
    }
    else if(choose==1)//订票
    {
      cout<<endl<<endl;
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"航班信息如下"<<endl;
      show(l);
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"输入客户姓名,证件号:";
      cin>>k.name>>k.k_id;
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"输入客户选择的航班号,位置(行,列):";
      cin>>k.kp_id>>k.r>>k.c;
      if(judge(l,k))
      {
        creatk(L,k);
        alter(l,k,1);
      }
      else
      {
        cout<<endl;
        cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"座位已有人,不能订票,请重新选择!"<<endl;
      }
      getchar();
      getchar();
      system("cls");
    }
    else if(choose==2)//退票
    {
      cout<<endl<<endl;
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"输入客户信息(名字,证件号,航班)"<<endl;
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t';
      cin>>k.name>>k.k_id>>k.kp_id;
      int flag=delet(L,k);
      if(flag)
      {
        alter(l,k,0);
        cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"退票成功"<<endl;
      }
      else
      {
        cout<<endl;
        cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"查找失败,请重新输入"<<endl;
      }
      getchar();
      getchar();
      system("cls");
    }
    else if(choose==3)
    {
      cout<<endl<<endl;
      cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"输入客户信息(名字,证件号,航班):";
      cin>>k.name>>k.k_id>>k.kp_id;
      int flag=searchh(L,k);
      if(flag)
      {
        showone(l,k);
      }
      else
      {
        cout<<'\t'<<'\t'<<'\t'<<'\t'<<'\t'<<"查找失败,请重新输入"<<endl;
      }
      getchar();
      getchar();
      system("cls");
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/acmliying/article/details/78880982