【发布时间】:2021-09-14 06:55:43
【问题描述】:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct airlines{
char name[50];
char origin[50];
char destination[50];
char dtime[50];
char atime[50];
char age[50];
}
struct airlines *p;
typedef struct airlines lines[50];
void reserve(struct airlines *p);
void edit(struct airlines *p, int newent);
void search(lines *p, int newent);
void AgeS();
void OriginS();
void DestinationS();
void deletecus(lines *p, int newent, int del);
void display(struct airlines *p, int newent);
void displayall(lines *p, int newent);
void saveFile(struct airlines *p);
int main(){
int sel;
int newent=0 , editcust, del;
lines airlinesnew;
begin = stream = NULL;
do {
printf("\n\n\t*******************************************************************");
printf("\n\t BREEZE AIRLINES ONLINE BOOKING ");
printf("\n\t*******************************************************************");
printf("\n\n\t\t1. Reserve a seat");
printf("\n\n\t\t2. Edit a reservation");
printf("\n\n\t\t3. Search existing reservations");
printf("\n\n\t\t4. Delete existing reservations");
printf("\n\n\t\t5. Display existing reservations");
printf("\n\n\t\t6. Exit program");
printf("\n\n\t\tWhich option would you like to choose?: ", &sel);
scanf("%d", &sel);
system("cls");
switch (sel){
case 1:
reserve(&airlinesnew[newent]);
newent++;
break;
case 2:
displayall(&airlinesnew, newent);
printf("\nWhich entry would you like to edit?: ");
scanf("%d", &editcust);
edit(&airlinesnew[editcust-1], newent);
break;
case 3:
search(&airlinesnew, newent);
break;
case 4:
displayall(&airlinesnew, newent);
printf("Which entry do you want to delete?: ");
scanf("%d", &del);
deletecus(&airlinesnew, newent, del);
newent--;
break;
case 5:
displayall(&airlinesnew, newent);
break;
case 6:
saveFile(&airlinesnew[newent]);
break;
default:
printf("\nInvalid Choice ");
printf("\nPlease select only from 1-6 ");
}
} while (sel != 6);
return 0;
}
void reserve(struct airlines *p){
printf("\n\tReservation Details");
printf("\n\tName: ");
scanf(" %[^\n]s", &(*p).name);
printf("\n\tAge: ");
scanf(" %[^\n]s", &(*p).age);
printf("\n\tOrigin: ");
scanf(" %[^\n]s", &(*p).origin);
printf("\n\tDestination: ");
scanf(" %[^\n]s", &(*p).destination);
printf("\n\tTime of departure (use military time): ");
scanf(" %[^\n]s", &(*p).dtime);
printf("\n\tTime of arrival (use military time): ");
scanf(" %[^\n]s", &(*p).atime);
system("cls");
}
void edit(struct airlines *p, int newent){
char nname[50];
char nage[50];
char norigin[50];
char ndestination[50];
char ndtime[50];
char natime[50];
display( &(*p), newent-1 );
printf("\nEditing the entry: ");
printf("\n\t New Name: ");
scanf(" %[^\n]s", &nname );
strcpy( (*p).name, nname );
printf("\n\t New age: ");
scanf(" %d", &nage );
strcpy( (*p).age, nage );
printf("\n\t New Origin: ");
scanf(" %[^\n]s", &norigin );
strcpy( (*p).origin, norigin );
printf("\n\t New Destination: ");
scanf(" %[^\n]s", &ndestination );
strcpy( (*p).destination, ndestination );
printf("\n\t New time of departure (use military time): ");
scanf(" %[^\n]s", &ndtime );
strcpy( (*p).dtime, ndtime );
printf("\n\t New time of arrival (use military time): ");
scanf(" %[^\n]s", &natime );
strcpy( (*p).atime, natime );
system("cls");
}
void search(lines *p, int newent){
int sel1;
char Ageint[50];
char Originint[50];
char Destinationint[50];
do {
printf ("\nWhich heading would you like to search:");
printf ("\n1. Age");
printf ("\n2. Origin");
printf ("\n3. Destination");
printf ("\n4. Return");
printf ("\nHeading number: ");
scanf ("%d", &sel1);
switch (sel1)
{
case 1:
printf ("\nEnter age: ");
scanf ("%d", &Ageint);
for (int i = 0; i < newent; i++){
if( strcmp( (*p)[i].age, Ageint ) == 0 ){
display( &(*p)[i] , i );
}
}
break;
case 2:
printf ("\nEnter origin: ");
scanf(" %[^\n]s", &Originint );
for (int i = 0; i < newent; i++){
if( strcmp( (*p)[i].origin, Originint ) == 0 ){
display( &(*p)[i] , i );
}
}
break;
case 3:
printf ("\nEnter destination: ");
scanf(" %[^\n]s", &Destinationint );
for (int i = 0; i < newent; i++){
if( strcmp( (*p)[i].destination, Destinationint ) == 0 ){
display( &(*p)[i] , i );
}
}
break;
case 4:
main();
default:
printf("\nInvalid Choice ");
printf("\nPlease select only from 1-4 ");
}
} while (sel1 != 4);
}
void deletecus(lines *p, int newent, int del){
int i;
for( i = del-1; i < newent; i++ ){
strcpy( (*p)[i].name, (*p)[i+1].name );
strcpy( (*p)[i].age, (*p)[i+1].age );
strcpy( (*p)[i].origin, (*p)[i+1].origin );
strcpy( (*p)[i].destination,(*p)[i+1].destination);
strcpy( (*p)[i].dtime, (*p)[i+1].dtime );
strcpy( (*p)[i].atime, (*p)[i+1].atime );
}
}
void display(struct airlines *p, int newent){
printf("\nReservation list:");
printf("\n");
printf("Passport No. %d\n", newent+1);
printf("Name: %s", (*p).name);
printf("\n");
printf("Age: %s", (*p).age);
printf("\n");
printf("Origin: %s", (*p).origin);
printf("\n");
printf("Destination: %s", (*p).destination);
printf("\n");
printf("Time of departure: %s", (*p).dtime);
printf("\n");
printf("Time of Arrival: %s", (*p).atime);
printf("\n");
}
void displayall(lines *p, int newent){
int i;
for (i=0;i<newent;i++){
display(&(*p)[i], i);
}
}
void saveFile(struct airlines *p)
{
FILE *fptr;
fptr = fopen("reserved_seats", "a+");
if(!fptr)
{
printf("Error\n");
return;
}
fprintf(fptr, "%15s", (*p).name);
fprintf(fptr, "%15s", (*p).age);
fprintf(fptr, "%15s", (*p).origin);
fprintf(fptr, "%15s", (*p).destination);
fprintf(fptr, "%15s", (*p).dtime);
fprintf(fptr, "%15s", (*p).atime);
fprintf(fptr, "\n");
printf("\nSaved successfully");
fclose(fptr);
}
这是我当前的航空公司预订系统代码。所需的功能之一是保存输入的数据,这样当程序关闭时,它仍然会显示保存的数据,因为它将访问保存的文件。但是在执行保存功能时,程序确实创建了一个文件,但是它没有输入数据,它只是一个没有任何内容的文件。请帮忙
【问题讨论】:
-
无关:
(*p).member也可以写成p->member。