Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning(disable : 4996)
- #include<iostream>
- #include<cstring>
- #include<fstream>
- const int COMMAND_SIZE = 10;
- const int FIELD_SIZE = 30;
- #define open ">open"
- #define help ">help"
- #define closeloop ">close"
- #define print ">print"
- #define edit ">edit"
- #define save ">save"
- #define printall ">printall"
- #define clear ">clear"
- void showHelp(bool& helper) {
- helper = true;
- std::cout << "Wellcome to help!" << std::endl <<
- "Press " << open << " + valid directory to open a .csv file, containing students information" << std::endl <<
- "Press " << closeloop << " to exit" << std::endl <<
- "Press " << print << " + faculcy number to see student's detail" << std::endl <<
- "Press " << edit << " + faculcy number and name to change someone's first name" << std::endl <<
- "Press " << save << " + name of the new file to save the changes you made in .csv format" << std::endl <<
- "Press " << printall << " to print the current file " << std::endl <<
- "Press " << clear << " to clear the console" << std::endl <<
- "Press " << help << " to see this text again" << std::endl << std::endl;
- }
- struct Student {
- private:
- char* firstName;
- char* secondName;
- char* email;
- int FN;
- public:
- Student() : firstName(nullptr), secondName(nullptr), email(nullptr), FN(-1) {}
- Student(char* _first, char* _second, char* _email, const int& fNum) : firstName(_first), secondName(_second), email(_email), FN(fNum) {}
- inline void setFirstName(char* _first) { this->firstName = _first; }
- inline void setLastName(char* _second) { this->secondName = _second; }
- inline void setEmail(char* _mail) { this->email = _mail; }
- inline void setFacultyNumber(const int& fNum) { this->FN = fNum; }
- char* getFirstName()const { return this->firstName; }
- char* getFirstName() { return this->firstName; }
- char* getSecondName()const { return this->secondName; }
- char* getEmail()const { return this->email; }
- int getFaculcyNumber()const { return this->FN; }
- void deleteFirstName() {
- delete firstName;
- }
- ~Student() {
- delete firstName;
- delete secondName;
- delete email;
- }
- };
- void showStudentInfo(const Student& entity) {
- std::cout << "Name: " << entity.getFirstName() << " " << entity.getSecondName();
- std::cout.width(5);
- std::cout << "\temail: " << entity.getEmail();
- std::cout << "\tFN:" << entity.getFaculcyNumber() << std::endl << std::endl;
- }
- void initStudent(Student& _dest, char* _first, char* _second, char* _email, const int& _fNum) {
- _dest.setFirstName(_first);
- _dest.setLastName(_second);
- _dest.setEmail(_email);
- _dest.setFacultyNumber(_fNum);
- }
- int countLines(const char* _dir) {
- std::ifstream inFile(_dir);
- if (!inFile.is_open())
- return -1;
- int counter = 0;
- while (!inFile.eof()) {
- char trash[1024];
- inFile.getline(trash, 1024);
- counter++;
- }
- inFile.close();
- return counter;
- }
- void readStudent(const char* input, Student& entity) {
- int ind = 0;
- int border = strlen(input);
- int firstNameSymbols = 0;
- int secondNameSymbols = 0;
- int mailSymbols = 0;
- int faculcyNumber = 0;
- while (input[ind++] != ',')
- firstNameSymbols++;
- while (input[ind++] != ',')
- secondNameSymbols++;
- while (input[ind++] != ',')
- mailSymbols++;
- for (int i = ind; i < border; i++) {
- faculcyNumber *= 10;
- faculcyNumber += input[i] - '0';
- }
- char* firstName = new char[firstNameSymbols + 1];
- char* secondName = new char[secondNameSymbols + 1];
- char* email = new char[mailSymbols + 1];
- for (int i = 0; i < firstNameSymbols; i++)
- firstName[i] = input[i];
- firstName[firstNameSymbols] = '\0';
- int borderForSecondLoop = firstNameSymbols + secondNameSymbols + 1;
- for (int i = firstNameSymbols + 1; i < borderForSecondLoop; i++)
- secondName[i - firstNameSymbols - 1] = input[i];
- secondName[secondNameSymbols] = '\0';
- int borderForThridLoop = firstNameSymbols + secondNameSymbols + mailSymbols + 2;
- for (int i = borderForSecondLoop + 1; i < borderForThridLoop; i++)
- email[i - borderForSecondLoop - 1] = input[i];
- email[mailSymbols] = '\0';
- initStudent(entity, firstName, secondName, email, faculcyNumber);
- }
- void readStudents(Student* group, const char* location) {
- std::ifstream inFile(location);
- if (!inFile.is_open())
- std::cout << "Error opening file!";
- bool firstLine = true;
- int index = 0;
- while (!inFile.eof()) {
- char dataStudents[1024];
- if (firstLine) {
- firstLine = false;
- inFile.getline(dataStudents, 1024);
- }
- else {
- inFile.getline(dataStudents, 1024);
- readStudent(dataStudents, group[index++]);
- }
- }
- inFile.close();
- }
- void getStudentByFaculcyNumber(const int& faculcyNumber, const Student* group, const int& studentsCount) {
- bool found = false;
- for (int i = 0; i < studentsCount; i++) {
- if (group[i].getFaculcyNumber() == faculcyNumber) {
- std::cout << "Found this: ";
- showStudentInfo(group[i]);
- found = true;
- }
- }
- if (!found)
- std::cout << "No such student!" << std::endl;
- }
- void changeFirstNameUsingFaculcyNumber(const int& faculcyNumber, char* name, Student* group, const int& studentsCount) {
- for (int i = 0; i < studentsCount; i++) {
- if (group[i].getFaculcyNumber() == faculcyNumber) {
- group[i].deleteFirstName();
- group[i].setFirstName(name);
- std::cout << "Changed successfully of student " << i + 1 << std::endl;
- return; //Suppose that only one student has active faculcy number, which might not be the case when we use >print.
- }
- }
- std::cout << "No such student!" << std::endl;
- }
- void saveStudentsToFile(const Student* group, const int& studentsCount, const char* fileName) {
- int len = strlen(fileName) + 4;
- char* adress = new char[len];
- strcpy(adress, fileName);
- adress[len - 4] = '.';
- adress[len - 3] = 'c';
- adress[len - 2] = 's';
- adress[len - 1] = 'v';
- adress[len] = '\0';
- std::ofstream outFile(adress);
- if (!outFile.is_open()) {
- std::cout << "Failed to save file!" << std::endl;
- return;
- }
- outFile << "Име,Фамилия,email,факултетенномер" << std::endl;
- for (int i = 0; i < studentsCount; i++) {
- outFile << group[i].getFirstName() << "," << group[i].getSecondName() << "," << group[i].getEmail() << "," << group[i].getFaculcyNumber();
- if (i != studentsCount - 1)
- outFile << std::endl;
- }
- outFile.close();
- std::cout << "File savet successfully as " << adress << std::endl;
- }
- int main() {
- Student* databaseOfStudents = nullptr;
- int studentsCount = 0;
- bool helpDialog = false;
- while (true) {
- char command[COMMAND_SIZE];
- std::cout << "Enter command";
- if (!helpDialog)
- std::cout << " (try >help if you're stuck): ";
- else
- std::cout << ": ";
- std::cin >> command;
- if (strcmp(command, open) == 0) {
- char dest[FIELD_SIZE];
- std::cin.ignore();
- std::cin >> dest;
- std::ifstream inFile(dest);
- if (!inFile.is_open()) {
- std::cout << "Invalid input\n";
- continue;
- }
- studentsCount = countLines(dest) - 1;
- databaseOfStudents = new Student[studentsCount];
- readStudents(databaseOfStudents, dest);
- std::cout << "Opened successfully!" << std::endl;
- inFile.close();
- }
- else if (strcmp(command, print) == 0) {
- int faculcyNumber = 0;
- std::cin >> faculcyNumber;
- getStudentByFaculcyNumber(faculcyNumber, databaseOfStudents, studentsCount);
- }
- else if (strcmp(command, edit) == 0) {
- int faculcyNumber = 0;
- std::cin >> faculcyNumber;
- char* nName = new char[FIELD_SIZE];
- std::cin >> nName;
- changeFirstNameUsingFaculcyNumber(faculcyNumber, nName, databaseOfStudents, studentsCount);
- }
- else if (strcmp(command, save) == 0) {
- char nNameOfFile[FIELD_SIZE];
- std::cin >> nNameOfFile;
- saveStudentsToFile(databaseOfStudents, studentsCount, nNameOfFile);
- }
- else if (strcmp(command, printall) == 0) {
- for (int i = 0; i < studentsCount; i++)
- showStudentInfo(databaseOfStudents[i]);
- }
- else if (strcmp(command, closeloop) == 0)
- break;
- else if (strcmp(command, help) == 0)
- showHelp(helpDialog);
- else if (strcmp(command, clear) == 0)
- system("CLS");
- else
- std::cout << "Invalid command!" << std::endl;
- }
- std::cout << "Bye!";
- delete[] databaseOfStudents;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement