Advertisement
Stoycho_KK

домашна работа март

Mar 9th, 2021
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.19 KB | None | 0 0
  1. #pragma warning(disable : 4996)
  2. #include<iostream>
  3. #include<cstring>
  4. #include<fstream>
  5.  
  6. const int COMMAND_SIZE = 10;
  7. const int FIELD_SIZE = 30;
  8.  
  9. #define open ">open"
  10. #define help ">help"
  11. #define closeloop ">close"
  12. #define print ">print"
  13. #define edit ">edit"
  14. #define save ">save"
  15. #define printall ">printall"
  16. #define clear ">clear"
  17.  
  18. void showHelp(bool& helper) {
  19.     helper = true;
  20.     std::cout << "Wellcome to help!" << std::endl <<
  21.         "Press " << open << " + valid directory to open a .csv file, containing students information" << std::endl <<
  22.         "Press " << closeloop << " to exit" << std::endl <<
  23.         "Press " << print << " + faculcy number to see student's detail" << std::endl <<
  24.         "Press " << edit << " + faculcy number and name to change someone's first name" << std::endl <<
  25.         "Press " << save << " + name of the new file to save the changes you made in .csv format" << std::endl <<
  26.         "Press " << printall << " to print the current file " << std::endl <<
  27.         "Press " << clear << " to clear the console" << std::endl <<
  28.         "Press " << help << " to see this text again" << std::endl << std::endl;
  29. }
  30.  
  31. struct Student {
  32. private:
  33.     char* firstName;
  34.     char* secondName;
  35.     char* email;
  36.     int FN;
  37.  
  38. public:
  39.     Student() : firstName(nullptr), secondName(nullptr), email(nullptr), FN(-1) {}
  40.  
  41.     Student(char* _first, char* _second, char* _email, const int& fNum) : firstName(_first), secondName(_second), email(_email), FN(fNum) {}
  42.  
  43.     inline void setFirstName(char* _first) { this->firstName = _first; }
  44.  
  45.     inline void setLastName(char* _second) { this->secondName = _second; }
  46.  
  47.     inline void setEmail(char* _mail) { this->email = _mail; }
  48.  
  49.     inline void setFacultyNumber(const int& fNum) { this->FN = fNum; }
  50.  
  51.     char* getFirstName()const { return this->firstName; }
  52.  
  53.     char* getFirstName() { return this->firstName; }
  54.  
  55.     char* getSecondName()const { return this->secondName; }
  56.  
  57.     char* getEmail()const { return this->email; }
  58.  
  59.     int getFaculcyNumber()const { return this->FN; }
  60.  
  61.     void deleteFirstName() {
  62.         delete firstName;
  63.     }
  64.  
  65.     ~Student() {
  66.         delete firstName;
  67.         delete secondName;
  68.         delete email;
  69.     }
  70. };
  71.  
  72. void showStudentInfo(const Student& entity) {
  73.     std::cout << "Name: " << entity.getFirstName() << " " << entity.getSecondName();
  74.     std::cout.width(5);
  75.     std::cout << "\temail: " << entity.getEmail();
  76.     std::cout << "\tFN:" << entity.getFaculcyNumber() << std::endl << std::endl;
  77. }
  78.  
  79. void initStudent(Student& _dest, char* _first, char* _second, char* _email, const int& _fNum) {
  80.     _dest.setFirstName(_first);
  81.     _dest.setLastName(_second);
  82.     _dest.setEmail(_email);
  83.     _dest.setFacultyNumber(_fNum);
  84. }
  85.  
  86. int countLines(const char* _dir) {
  87.     std::ifstream inFile(_dir);
  88.  
  89.     if (!inFile.is_open())
  90.         return -1;
  91.  
  92.     int counter = 0;
  93.  
  94.     while (!inFile.eof()) {
  95.         char trash[1024];
  96.         inFile.getline(trash, 1024);
  97.         counter++;
  98.     }
  99.  
  100.     inFile.close();
  101.  
  102.     return counter;
  103. }
  104.  
  105. void readStudent(const char* input, Student& entity) {
  106.     int ind = 0;
  107.     int border = strlen(input);
  108.  
  109.     int firstNameSymbols = 0;
  110.     int secondNameSymbols = 0;
  111.     int mailSymbols = 0;
  112.     int faculcyNumber = 0;
  113.  
  114.     while (input[ind++] != ',')
  115.         firstNameSymbols++;
  116.  
  117.     while (input[ind++] != ',')
  118.         secondNameSymbols++;
  119.  
  120.     while (input[ind++] != ',')
  121.         mailSymbols++;
  122.  
  123.     for (int i = ind; i < border; i++) {
  124.         faculcyNumber *= 10;
  125.         faculcyNumber += input[i] - '0';
  126.     }
  127.  
  128.     char* firstName = new char[firstNameSymbols + 1];
  129.     char* secondName = new char[secondNameSymbols + 1];
  130.     char* email = new char[mailSymbols + 1];
  131.  
  132.     for (int i = 0; i < firstNameSymbols; i++)
  133.         firstName[i] = input[i];
  134.  
  135.     firstName[firstNameSymbols] = '\0';
  136.  
  137.     int borderForSecondLoop = firstNameSymbols + secondNameSymbols + 1;
  138.  
  139.     for (int i = firstNameSymbols + 1; i < borderForSecondLoop; i++)
  140.         secondName[i - firstNameSymbols - 1] = input[i];
  141.  
  142.     secondName[secondNameSymbols] = '\0';
  143.  
  144.     int borderForThridLoop = firstNameSymbols + secondNameSymbols + mailSymbols + 2;
  145.  
  146.     for (int i = borderForSecondLoop + 1; i < borderForThridLoop; i++)
  147.         email[i - borderForSecondLoop - 1] = input[i];
  148.  
  149.     email[mailSymbols] = '\0';
  150.  
  151.     initStudent(entity, firstName, secondName, email, faculcyNumber);
  152. }
  153.  
  154. void readStudents(Student* group, const char* location) {
  155.     std::ifstream inFile(location);
  156.  
  157.     if (!inFile.is_open())
  158.         std::cout << "Error opening file!";
  159.  
  160.     bool firstLine = true;
  161.     int index = 0;
  162.  
  163.     while (!inFile.eof()) {
  164.         char dataStudents[1024];
  165.  
  166.         if (firstLine) {
  167.             firstLine = false;
  168.             inFile.getline(dataStudents, 1024);
  169.         }
  170.         else {
  171.             inFile.getline(dataStudents, 1024);
  172.             readStudent(dataStudents, group[index++]);
  173.         }
  174.     }
  175.  
  176.     inFile.close();
  177. }
  178.  
  179. void getStudentByFaculcyNumber(const int& faculcyNumber, const Student* group, const int& studentsCount) {
  180.     bool found = false;
  181.  
  182.     for (int i = 0; i < studentsCount; i++) {
  183.         if (group[i].getFaculcyNumber() == faculcyNumber) {
  184.             std::cout << "Found this: ";
  185.             showStudentInfo(group[i]);
  186.             found = true;
  187.         }
  188.     }
  189.     if (!found)
  190.         std::cout << "No such student!" << std::endl;
  191. }
  192.  
  193. void changeFirstNameUsingFaculcyNumber(const int& faculcyNumber, char* name, Student* group, const int& studentsCount) {
  194.     for (int i = 0; i < studentsCount; i++) {
  195.         if (group[i].getFaculcyNumber() == faculcyNumber) {
  196.             group[i].deleteFirstName();
  197.             group[i].setFirstName(name);
  198.             std::cout << "Changed successfully of student " << i + 1 << std::endl;
  199.             return; //Suppose that only one student has active faculcy number, which might not be the case when we use >print.
  200.         }
  201.     }
  202.  
  203.     std::cout << "No such student!" << std::endl;
  204. }
  205.  
  206. void saveStudentsToFile(const Student* group, const int& studentsCount, const char* fileName) {
  207.     int len = strlen(fileName) + 4;
  208.  
  209.     char* adress = new char[len];
  210.  
  211.     strcpy(adress, fileName);
  212.     adress[len - 4] = '.';
  213.     adress[len - 3] = 'c';
  214.     adress[len - 2] = 's';
  215.     adress[len - 1] = 'v';
  216.     adress[len] = '\0';
  217.  
  218.     std::ofstream outFile(adress);
  219.  
  220.     if (!outFile.is_open()) {
  221.         std::cout << "Failed to save file!" << std::endl;
  222.         return;
  223.     }
  224.  
  225.     outFile << "Име,Фамилия,email,факултетенномер" << std::endl;
  226.  
  227.     for (int i = 0; i < studentsCount; i++) {
  228.         outFile << group[i].getFirstName() << "," << group[i].getSecondName() << "," << group[i].getEmail() << "," << group[i].getFaculcyNumber();
  229.         if (i != studentsCount - 1)
  230.             outFile << std::endl;
  231.     }
  232.  
  233.     outFile.close();
  234.  
  235.     std::cout << "File savet successfully as " << adress << std::endl;
  236. }
  237.  
  238. int main() {
  239.     Student* databaseOfStudents = nullptr;
  240.     int studentsCount = 0;
  241.  
  242.     bool helpDialog = false;
  243.  
  244.     while (true) {
  245.         char command[COMMAND_SIZE];
  246.         std::cout << "Enter command";
  247.  
  248.         if (!helpDialog)
  249.             std::cout << " (try >help if you're stuck): ";
  250.         else
  251.             std::cout << ": ";
  252.  
  253.         std::cin >> command;
  254.  
  255.         if (strcmp(command, open) == 0) {
  256.             char dest[FIELD_SIZE];
  257.             std::cin.ignore();
  258.             std::cin >> dest;
  259.             std::ifstream inFile(dest);
  260.  
  261.             if (!inFile.is_open()) {
  262.                 std::cout << "Invalid input\n";
  263.                 continue;
  264.             }
  265.             studentsCount = countLines(dest) - 1;
  266.  
  267.             databaseOfStudents = new Student[studentsCount];
  268.  
  269.             readStudents(databaseOfStudents, dest);
  270.             std::cout << "Opened successfully!" << std::endl;
  271.             inFile.close();
  272.         }
  273.         else if (strcmp(command, print) == 0) {
  274.             int faculcyNumber = 0;
  275.             std::cin >> faculcyNumber;
  276.             getStudentByFaculcyNumber(faculcyNumber, databaseOfStudents, studentsCount);
  277.         }
  278.         else if (strcmp(command, edit) == 0) {
  279.             int faculcyNumber = 0;
  280.             std::cin >> faculcyNumber;
  281.             char* nName = new char[FIELD_SIZE];
  282.             std::cin >> nName;
  283.  
  284.             changeFirstNameUsingFaculcyNumber(faculcyNumber, nName, databaseOfStudents, studentsCount);
  285.         }
  286.         else if (strcmp(command, save) == 0) {
  287.             char nNameOfFile[FIELD_SIZE];
  288.             std::cin >> nNameOfFile;
  289.  
  290.             saveStudentsToFile(databaseOfStudents, studentsCount, nNameOfFile);
  291.         }
  292.         else if (strcmp(command, printall) == 0) {
  293.             for (int i = 0; i < studentsCount; i++)
  294.                 showStudentInfo(databaseOfStudents[i]);
  295.         }
  296.         else if (strcmp(command, closeloop) == 0)
  297.             break;
  298.         else if (strcmp(command, help) == 0)
  299.             showHelp(helpDialog);
  300.         else if (strcmp(command, clear) == 0)
  301.             system("CLS");
  302.         else
  303.             std::cout << "Invalid command!" << std::endl;
  304.     }
  305.  
  306.     std::cout << "Bye!";
  307.  
  308.     delete[] databaseOfStudents;
  309.  
  310.     return 0;
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement