#include <iostream>
#include <limits.h>
#include <iomanip>
#include <fstream>
using namespace std;
struct MARSH
{
char NameStart [16];
char NameEnd [16];
int Number;
};
int Count = 0;
const int MaxEl = 8;
MARSH El[MaxEl];
char ShowMenu (void)
{
cout<<
"-----------------------"<<endl<<
" MENU "<<endl<<
"-----------------------"<<endl<<
"<1> - Input Marshrut"<<endl<<
"<2> - Print All Marshrut"<<endl<<
"<3> - Print Marshrut of Number"<<endl<<
"<4> - Quit"<<endl<<endl<<
"<5> - Save To File"<<endl<<
"<6> - Load From File"<<endl;
cout<<": ";
return getchar();
};
bool Add()
{
MARSH Temp;
cin.sync();
cout<<endl<<
"----------------------------------------"<<endl<<
" Input new Marshrut"<<endl<<
"----------------------------------------"<<endl;
cout<<"Enter Start Marshrut: ";
cin.getline(Temp.NameStart,16);
cout<<"Enter End Marshrut: ";
cin.getline(Temp.NameEnd,16);
cout<<"Enter Number (#) Marshrut: ";
cin>>Temp.Number;
cout<<endl;
if (Count < MaxEl)
{
El[Count] = Temp;
Count ++;
} else {
cout<<endl<<"False :Massive is FULL"<<endl;
cin.sync();
return false;
}
cin.sync();
return true;
}
void Sort (void)
{
for (int i = 0; i< Count; i++)
{
int Min = INT_MAX;
int minPos = -1;
for (int j=i; j<Count;j++)
{
if (El[j].Number<Min)
{
Min = El[j].Number;
minPos = j;
}
MARSH Temp;
Temp = El[i];
El[i] = El[minPos];
El[minPos] = Temp;
}
}
}
void Print(void)
{
cout<<setw(3)<<" "<<setw(6)<<"# "<<setw(17)<<"Start Marshrut "<<setw(17)<<"End Marshrut "<<endl<<
"------------------------------------------"<<endl;
cout.setf(ios::left);
for (int i=0; i < Count; i++)
{
cout<<setw(3)<<i+1<<setw(6)<<El[i].Number<<setw(17)<<El[i].NameStart<<setw(17)<<El[i].NameEnd<<endl;
}
cout.unsetf(ios::left);
cout<<"------------------------------------------"<<endl<<endl;
cin.sync();
cin.get();
}
void PrintOfNumMars(void)
{
int NumMarsh;
cin.sync();
cout<<endl<<"Input No Marshruta : ";
cin>>NumMarsh;
cout<<endl;
for (int i=0; i < Count; i++)
{
if (El[i].Number == NumMarsh)
{
cout<<setw(3)<<" "<<setw(6)<<"# "<<setw(17)<<"Start Marshrut "<<setw(17)<<"End Marshrut "<<endl<<
"------------------------------------------"<<endl;
cout<<setw(3)<<i+1<<setw(6)<<El[i].Number<<setw(17)<<El[i].NameStart<<setw(17)<<El[i].NameEnd<<endl;
cin.sync();
cin.get();
return;
}
}
cout<<"Marshrut "<<NumMarsh<<" NOT FOUND"<<endl;
cin.sync();
cin.get();
}
void SaveToFile(void)
{
char filename[256];
cout<<endl<<"Input Filename to Save Date : ";
cin.sync();
cin.get(filename,256);
ofstream OutFile(filename,ios::binary);
for (int i=0; i < Count; i++)
{
OutFile.write(reinterpret_cast<char*>(&El[i]),sizeof(El[i]));
}
OutFile.close();
}
void LoadFromFile(void)
{
char filename[256];
cout<<endl<<"Input Filename to Load Date : ";
cin.sync();
cin.get(filename,256);
ifstream InFile(filename,ios::binary);
Count = 0;
InFile.read(reinterpret_cast<char*>(&El[Count]),sizeof(El[Count]));
while (!InFile.eof())
{
InFile.read(reinterpret_cast<char*>(&El[Count]),sizeof(El[Count]));
Count++;
}
InFile.close();
}
int main (void)
{
char ch=0;
while (ch !='4')
{
cin.sync();
ch = ShowMenu();
switch (ch)
{
case '1': Add();Sort();break;
case '2': Print();break;
case '3': PrintOfNumMars();break;
case '5': SaveToFile();break;
case '6': LoadFromFile();break;
}
}
return 0;
}