From 51f517bf92ce0aa616de37dd73074ce3e3579875 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Fri, 5 Mar 2021 14:39:17 +0200 Subject: sem 2 --- Sem. 02/README.md | 52 ++++++++++++++++++ Sem. 02/notes_02.pdf | Bin 0 -> 75732 bytes Sem. 02/selfPrintingTask.cpp | 26 +++++++++ Sem. 02/studentsDatabase.cpp | 124 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+) create mode 100644 Sem. 02/README.md create mode 100644 Sem. 02/notes_02.pdf create mode 100644 Sem. 02/selfPrintingTask.cpp create mode 100644 Sem. 02/studentsDatabase.cpp diff --git a/Sem. 02/README.md b/Sem. 02/README.md new file mode 100644 index 0000000..b59d8ac --- /dev/null +++ b/Sem. 02/README.md @@ -0,0 +1,52 @@ + + +## Сруктури + +Структурите в C++ се използват за групиране на елементи. Елементите, наричани още членове, могат да бъдат от различен тип и с различна дължина. + + struct Box + { + int height; + int weight; + } + + +### Деклариране на обекти от новия тип + + Box b; //default values to height and weight + + Box b2 = {3, 4} // height = 3, weight = 3; + + Box b3; + b3.height = 13; + b3.weight = 14; + + +### Подаване във функции +Ако няма да променяме обекта го подаваме по **константна референция.** + + void calculcateArea(const Box& b) + { + return b.height * b.weight; + } + Може и само по **референция**, но тогава промените ще се отразят върху подадения аргумент. + + + void readBox(Box& b) + { + cin >> b.height >> b.weight; + } +.Може и да го подаваме по **копие**. + + Box revertBox(Box b) + { + int temp = b.height; + b.height = b.weight; + b.weight = temp; + + return b; + } +**Задача**: +Вход: Въвежда се цяло число **N** и после **N** тригъгълника в равнината, определени от 3 точки ( 6 координати). + +Отпечатайте тригълниците **сортирани по лицата им.** \ No newline at end of file diff --git a/Sem. 02/notes_02.pdf b/Sem. 02/notes_02.pdf new file mode 100644 index 0000000..a7372af Binary files /dev/null and b/Sem. 02/notes_02.pdf differ diff --git a/Sem. 02/selfPrintingTask.cpp b/Sem. 02/selfPrintingTask.cpp new file mode 100644 index 0000000..e269707 --- /dev/null +++ b/Sem. 02/selfPrintingTask.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +const int MAX_SIZE = 1024; +int main() +{ + ifstream inputFile("selfPrintingTask.cpp"); + + if (inputFile.fail()) + { + cout << "Error!" << endl; + return -1; + } + + while (!inputFile.eof()) + { + char buff[MAX_SIZE]; + inputFile.getline(buff, MAX_SIZE); + cout << buff << endl; + } + + + return 0; +} \ No newline at end of file diff --git a/Sem. 02/studentsDatabase.cpp b/Sem. 02/studentsDatabase.cpp new file mode 100644 index 0000000..ba3e420 --- /dev/null +++ b/Sem. 02/studentsDatabase.cpp @@ -0,0 +1,124 @@ +#include +#include + +using namespace std; + +const int FIELD_MAX_SIZE = 50; +const int BUFFER_LINE_SIZE = 1024; + +struct OOPstudent +{ + char firstName[FIELD_MAX_SIZE]; + char lastName[FIELD_MAX_SIZE]; + char email[FIELD_MAX_SIZE]; + int fn; + +}; + +int countLines(const char* fileName) +{ + ifstream f(fileName); + if (!f.is_open()) + return -1; + int linesCount = 0; + + while (!f.eof()) + { + char buff[1024]; + f.getline(buff, 1024); + linesCount++; + } + f.close(); + + return linesCount; + +} +// abc,bcd,ab@Abv.bg,fn +void readStudent(const char* input, OOPstudent& student) +{ + int ind = 0; + + // first name + while (input[ind] != ',') + { + student.firstName[ind] = input[ind]; + ind++; + } + student.firstName[ind] = '\0'; + ind++; + + //last name + int lastNameInd = 0; + while (input[ind] != ',') + student.lastName[lastNameInd++] = input[ind++]; + student.lastName[lastNameInd] = '\0'; + ind++; + + //email + int emailInd = 0; + + while (input[ind] != ',') + student.email[emailInd++] = input[ind++]; + student.email[emailInd] = '\0'; + + //fn + //?? +} + +void saveStudentsToFile(const OOPstudent* studentsCollection,int len, const char* fileName) +{ + ofstream outFile(fileName); + + if (!outFile.is_open()) + return; + + for (int i = 0; i < len; i++) + outFile << studentsCollection[i].firstName << " " << studentsCollection[i].lastName << " " << studentsCollection[i].email << endl; + + outFile.close(); +} + +int main() +{ + + int studentsCount = countLines("students.csv") - 1; // We ignore the first line + + OOPstudent* studentsCollection = new OOPstudent[studentsCount]; + + ifstream f("students.csv"); + + if (!f.is_open()) + { + cout << "Error!" << endl; + delete[] studentsCollection; + return -1; + } + + int line = 1; + while (!f.eof()) + { + char buff[BUFFER_LINE_SIZE]; + f.getline(buff, BUFFER_LINE_SIZE); + + if (line == 1) + { + line++; + continue; + } + + readStudent(buff, studentsCollection[line - 2]); + line++; + } + f.close(); + + + //Do stuff + + saveStudentsToFile(studentsCollection, studentsCount, "result.txt"); + + + + + delete[] studentsCollection; + return 0; +} \ No newline at end of file -- cgit v1.3.1-23-gd82d