Skip to content

Instantly share code, notes, and snippets.

@Pyroarsonist
Created February 25, 2018 15:09
Show Gist options
  • Select an option

  • Save Pyroarsonist/08212238d8b2eeb42df695aafcee277d to your computer and use it in GitHub Desktop.

Select an option

Save Pyroarsonist/08212238d8b2eeb42df695aafcee277d to your computer and use it in GitHub Desktop.
2 лаба по ОП, 2 семестр
// Лабораторна #2, 2 семестр
// Створити масив структур.
// Кожна структура складається з таких елементів: місто, інститут, факультет.
// Для інституту задається план прийому на перший курс. Для факультету задається список спеціальностей.
// Створений масив записати до бінарного файла.
// Передбачити можливість доповнити файл, замінити дані в файлі, видалити дані з файлу.
// Реалізувати запити, визначивши: 1) список спеціальностей, що містять у своїй назві слова "комп'ютерний" або "автоматизований";
// 2) факультети й інститути, де є задана спеціальність;
// 3) факультет, інститут і місто, де на вказану користувачем спеціальність виділено найбільше місць.
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
const string FILENAME = "database.txt";
const int MAX_SYMB = 120; // const var with max saved symbols
const int SET_PRES = 30;// const var with max presicion symbols
struct faculty {
char name[MAX_SYMB];
char specialties[3][MAX_SYMB];
} fict;
struct institute {
char name[MAX_SYMB], timetable[MAX_SYMB];
faculty instFaculty;
} kpi;
struct department {
char city[MAX_SYMB];
institute depInstitute;
};
void newDepartment(); // set new faculty
void printAllDeps(); // print faculties from binary file
void printIT(); // print faculties with it words
void printCertainSpec(); // print faculties with entered words
char initMenu(); // get entered symb for menu
int main() {
setlocale(LC_CTYPE, "ukr");
bool inMenu = true;
char symb;
while (inMenu) {
symb = initMenu();
switch (symb) {
case '1': {
newDepartment();
break;
}
case '2': {
printAllDeps();
break;
}
case '3': {
printIT();
break;
}
case '4': {
printCertainSpec();
break;
}
case '5': {
inMenu = false;
break;
}
default: {
break;
}
}
}
return 0;
}
char initMenu() {
printf(" %-*s \n %-*s %d \n %-*s %d \n %-*s %d \n %-*s %d \n %-*s %d \n", SET_PRES, "Menu", SET_PRES,
"Write new department",
1, SET_PRES, "Print all departments", 2,
SET_PRES, "List with IT", 3, SET_PRES, "Find institute and city", 4, SET_PRES, "Exit", 5);
char input;
cin >> input;
return input;
}
void newDepartment() {
string temp;
department dep;
cout << "Enter city: \n";
cin.get();
getline(cin, temp);
memcpy(dep.city, temp.c_str(), temp.length() + 1);
cout << "Enter name of institute: \n";
getline(cin, temp);
memcpy(dep.depInstitute.name, temp.c_str(), temp.length() + 1);
cout << "Enter timetable: \n";
getline(cin, temp);
memcpy(dep.depInstitute.timetable, temp.c_str(), temp.length() + 1);
cout << "Enter name of faculty: \n";
getline(cin, temp);
memcpy(dep.depInstitute.instFaculty.name, temp.c_str(), temp.length() + 1);
cout << "Enter 3 specialties: \n";
for (int i = 0; i < 3; ++i) {
getline(cin, temp);
memcpy(dep.depInstitute.instFaculty.specialties[i], temp.c_str(), temp.length() + 1);
}
ofstream fo(FILENAME, ios::binary | ios::app);
fo.write((char *) &dep, sizeof(dep));
fo.close();
}
void printAllDeps() {
ifstream fi;
fi.open(FILENAME, ios::binary);
department dep;
printf("%-*s %-*s %-*s %-*s %-*s \n", SET_PRES, "City", SET_PRES, "Institute",
SET_PRES, "Timetable", SET_PRES, "Faculty", SET_PRES, "Specialties");
while (fi.good()) {
fi.read((char *) &dep, sizeof(dep));
if (fi.good()) {
printf("%-*s %-*s %-*s %-*s %-*s %-*s %-*s \n", SET_PRES, dep.city, SET_PRES, dep.depInstitute.name,
SET_PRES, dep.depInstitute.timetable, SET_PRES, dep.depInstitute.instFaculty.name, SET_PRES,
dep.depInstitute.instFaculty.specialties[0], SET_PRES, dep.depInstitute.instFaculty.specialties[1],
SET_PRES, dep.depInstitute.instFaculty.specialties[2]);
}
}
fi.close();
};
void printIT() {
ifstream fi;
fi.open(FILENAME, ios::binary);
department dep;
string str1 = "комп'ютерний";
string str2 = "автоматизований";
cout << "List of specs: " << endl;
bool found = false;
while (fi.good()) {
fi.read((char *) &dep, sizeof(dep));
if (fi.good()) {
for (int i = 0; i < 3; ++i) {
string temp = dep.depInstitute.instFaculty.specialties[i];
int pos1 = temp.find(str1);
int pos2 = temp.find(str2);
if (pos1 != -1 || pos2 != -1) {
found = true;
cout << temp << endl;
}
}
}
}
fi.close();
if (!found)
cout << "Not found" << endl;
};
void printCertainSpec() {
ifstream fi;
fi.open(FILENAME, ios::binary);
department dep;
cout << "Enter faculty: " << endl;
string fac;
cin.get();
getline(cin, fac);
cout << "List of foundings: " << endl;
bool found = false;
while (fi.good()) {
fi.read((char *) &dep, sizeof(dep));
if (fi.good()) {
for (int i = 0; i < 3; ++i) {
string temp = dep.depInstitute.instFaculty.specialties[i];
int pos1 = temp.find(fac);
int pos2 = temp.find(fac);
if (pos1 != -1 || pos2 != -1) {
found = true;
cout << dep.depInstitute.name << " " << dep.city << endl;
}
}
}
}
fi.close();
if (!found)
cout << "Not found" << endl;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment