Додаток с
Лістинг програми:
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>
struct TRANSP {
char marsh[20];
char vid[20];
};
struct other {
TRANSP transp;
char ost1[20];
long int vremya;
char ost2[20];
};
void resetPosition(fstream &f) {
f.clear();
f.sync();
f.seekg(0, f.beg);
}
int readRecord(fstream &f, other *record) {
f >> record->ost1;
f >> record->transp.marsh;
f >> record->transp.vid;
f >> record->ost2;
f >> record->vremya;
return !f.fail();
}
void printRecord(other *record) {
cout << "Nachalnaya ostanovka: " << record->ost1 << endl;
cout << "Marshrut: " << record->transp.marsh << endl;
cout << "Vid transporta: " << record->transp.vid << endl;
cout << "Konechnaya ostanovka : " << record->ost2 << endl;
cout << "Vremya poezdki(min): " << record->vremya << endl;
cout << endl;
}
int search(fstream &f) {
clrscr();
other data;
char x;
TRANSP transp;
char ost1[20];
char ost2[20];
long int vremya;
int found = 0;
cout << "Transportnoe depo" << endl << endl;
cout << "Poisk za vidom transporta i marshrutom\n\t\t\t\t[t]" << endl;
cout << "Poisk za nachalnoi ostanovkoi\n\t\t\t\t[n]" << endl;
cout << "Poisk za konechnoi ostanovkoi\n\t\t\t\t[k]" << endl;
cout << "Poisk za vremenem v puti\n\t\t\t\t[v]" << endl;
cout << "\t\t\t\t\t\tExit {q}" << endl;
cout << "Vvedite bukvu: ";
cin >> x;
cout << endl;
switch (x) {
case 't':
cout << "Napishite marshrut: ";
cin >> transp.marsh;
cout << "Napishite vid transporta: ";
cin >> transp.vid;
break;
case 'n':
cout << "Napishite nachalnuiy ostanovku: ";
cin >> ost1;
break;
case 'k':
cout << "Napishite konechnuiy ostanovku: ";
cin >> ost2;
break;
case 'v':
cout << "Napishite vremya poezdki: ";
cin >> vremya;
break;
case 'q':
return 0;
}
cout << endl;
resetPosition(f);
while (readRecord(f, &data)) {
int ok = 1;
switch (x) {
case 't':
if (strcmp(data.transp.vid, transp.vid) ||
strcmp(data.transp.marsh, transp.marsh)) ok = 0;
break;
case 'n':
if (strcmp(data.ost1, ost1)) ok = 0;
break;
case 'k':
if (strcmp(data.ost2, ost2)) ok = 0;
break;
case 'v':
if (data.vremya != vremya) ok = 0;
break;
}
if (ok) {
printRecord(&data);
found++;
}
}
if (!found)
cout << "Error_404" << endl;
getch();
return 1;
}
int main() {
clrscr();
fstream file("depo.txt", ios::in | ios::out);
while (search(file)) ;
file.close();
return 0;
}
