Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Контрольная по ОАиП Скачков А.А.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
1.42 Mб
Скачать

Int main()

{

char * fileName = "info.txt";

FreePlace * FreePlaces = new FreePlace[20];

int count = OpenFreePlaces(FreePlaces, fileName);

bool isExit = true;

char destination[80];

while (isExit)

{

int i = Menu(); switch (i)

{

case 1:

WriteAllFreePlaces(FreePlaces, count);

break;

case 2:

NewFreePlace(FreePlaces, count);

count++;

break;

case 3:

int j;

WriteAllFreePlaces(FreePlaces, count);

cout << "Write number of FreePlace: ";

cin >> j;

if (j - 1 <= count)

{

DeleteFreePlace(FreePlaces, j-1, count);

count--;

}

system("cls");

break;

case 4:

char name[80];

system("cls");

cout << "Write name of file: ";

cin >> name;

cout << "Write number of destination: ";

cin >> destination;

WriteInfoInFile(FreePlaces, name, count, destination);

break;

case 5:

cout << "Write number of destination: ";

cin >> destination;

WriteInfoInScreen(FreePlaces, count, destination);

break;

case 6:

remove(fileName);

for (int i = 0; i <= count; i++)

SaveFreePlaces(FreePlaces[i], fileName); //Сохранить данные

system("cls");

break;

case 7:

isExit = false; //Вывод из цикла и из программы

break;

default:

system("cls");

break;

}

}

delete [] FreePlaces; //Очищаем память

return 0; //Выход из главной функции

}

void WriteAllFreePlaces(FreePlace * FreePlaces, int count)

{

system("cls");

for (int i = 0; i <= count; i++) //Выводим всю информация о свободных местах в файл

{

cout <<i+1<<". "<< FreePlaces[i].TrainNumber << endl;

cout << FreePlaces[i].Destination << endl;

cout << FreePlaces[i].Time << endl;

cout << FreePlaces[i].PlaceCount << endl<<endl;

}

}

int Menu()

{

cout <<"1. Write all FreePlaces"<<endl;

cout <<"2. New FreePlace"<<endl;

cout <<"3. Delete FreePlace"<<endl;

cout <<"4. Info in file"<<endl;

cout <<"5. Info in screen"<<endl;

cout <<"6. Save"<<endl;

cout <<"7. Exit"<<endl;

int i;

cin >> i;

return i;

}

void NewFreePlace(FreePlace * FreePlaces, int count)

{

system("cls");

count++;

FreePlaces[count].PlaceCount = new char[80];

FreePlaces[count].Destination = new char[80];

FreePlaces[count].Time = new char[80];

FreePlaces[count].TrainNumber = new char[80];

cout <<"Number Of Train: ";

cin >> FreePlaces[count].TrainNumber;

cout <<"Destination of Train: ";

cin >> FreePlaces[count].Destination;

cout <<"Time: ";

cin >> FreePlaces[count].Time;

cout <<"Count of free places: ";

cin >> FreePlaces[count].PlaceCount;

system("cls");

}

void DeleteFreePlace(FreePlace * FreePlaces, int j, int count)

{

for (int i = j; i<=count; i++)

{

if (i != count)

FreePlaces[i] = FreePlaces[i+1];

}

}

void WriteInfoInFile(FreePlace * FreePlaces, char * name, int count, char * destination)

{

for (int i = 0; i <= count; i++)

{

if (equals(FreePlaces[i].Destination, destination))

{

SaveFreePlaces(FreePlaces[i], name);

}

}

}

void WriteInfoInScreen(FreePlace * FreePlaces, int count, char * destination)

{

system("cls");

for (int i = 0; i <= count; i++)

{

if (equals(FreePlaces[i].Destination, destination))

{

cout <<i+1<<". "<< FreePlaces[i].TrainNumber << endl;

cout << FreePlaces[i].Destination << endl;

cout << FreePlaces[i].Time << endl;

cout << FreePlaces[i].PlaceCount << endl<<endl;

}

}

}

int OpenFreePlaces(FreePlace * FreePlaces, char * fileName)

{

FILE * file;

char * text = new char[80];

int count = -1;

if ((file = fopen(fileName, "r")) != NULL)

{

char symbol;

symbol = fgetc(file); //Для определения того, не пустой ли файл

while (!feof(file))

{

//Номер поезда

count++;

int index = 0;

while ((symbol = fgetc(file)) != '\n')

{

text[index] = symbol;

index++;

}

for (int i = index; i<80; i++)

text[i] = '\0';

FreePlaces[count].TrainNumber = text;

text = new char[80];

//Пункт назначения

index = 0;

while ((symbol = fgetc(file)) != '\n')

{

text[index] = symbol;

index++;

}

for (int i = index; i<80; i++)

text[i] = '\0';

FreePlaces[count].Destination = text;

text = new char[80];

//Время

index = 0;

while ((symbol = fgetc(file)) != '\n')

{

text[index] = symbol;

index++;

}

for (int i = index; i<80; i++)

text[i] = '\0';

FreePlaces[count].Time = text;

text = new char[80];

//Количество свободных мест

index = 0;

while ((symbol = fgetc(file)) != '\n')

{

text[index] = symbol;

index++;

}

for (int i = index; i<80; i++)

text[i] = '\0';

FreePlaces[count].PlaceCount = text;

text = new char[80];

symbol = fgetc(file); //Для определения того, не был ли последний символ последним в файле (предотвращение зацикливания)

}

fclose(file);

delete [] text;

}

else

{

cout << "Error with open file";

return -1;

}

return count;

}

void SaveFreePlaces(FreePlace FreePlace, char * fileName)

{

FILE * file;

char text[80];

if ((file = fopen(fileName, "a")) != NULL)

{

char symbol;

fputc('\n', file);

for (int j = 0; FreePlace.TrainNumber[j] != '\0'; j++)

{

fputc(FreePlace.TrainNumber[j], file);

}

fputc('\n', file);

for (int j = 0; FreePlace.Destination[j] != '\0'; j++)

{

fputc(FreePlace.Destination[j], file);

}

fputc('\n', file);

for (int j = 0; FreePlace.Time[j] != '\0'; j++)

{

fputc(FreePlace.Time[j], file);

}

fputc('\n', file);

for (int j = 0; FreePlace.PlaceCount[j] != '\0'; j++)

{

fputc(FreePlace.PlaceCount[j], file);

}

fputc('\n', file);

fclose(file);

system("cls");

}

else

{

system("cls");

cout << "Error with open file";

return;

}

return;

}

bool equals(char * a, char * b)

{

bool result = false;

int i = 0;

while ((a[i] != '\0' && b[i] != '\0') || (a[i] == '\0' && b[i] == '\0'))

{

if (a[i] == '\0' && b[i] == '\0')

{

result = true;

break;

}

else

if (a[i] != b[i])

{

break;

}

i++;

}

return result;

}

Результат выполнения программы