Website Summary Files Commits Branches @commit @diff @stats
path: root / k_1_SI
diff options
context:
space:
mode:
Commit5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-05-10 20:06:53 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-05-10 20:06:53 +0300
Files52f913ec9532fd07c8f679ccf1e68876d1fc9ff2 /k_1_SI
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.tar
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.tar.gz
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.zip
Parent459d3ac280aafdba703930455f3200b6e7b0f31b (diff)
sem 11
3 files changed, 0 insertions, 277 deletions
-rw-r--r--k_1_SI/task1.cpp138
-rw-r--r--k_1_SI/task2_read.cpp55
-rw-r--r--k_1_SI/task2_write.cpp84
diff --git a/k_1_SI/task1.cpp b/k_1_SI/task1.cpp
deleted file mode 100644
index 77ad5f5..0000000
--- a/k_1_SI/task1.cpp
+++ /dev/null
@@ -1,138 +0,0 @@
-#include <iostream>
-#include <fstream>
-#include <cmath>
-
-using namespace std;
-
-struct Point
-{
- int x;
- int y;
- Point() : Point(0,0){}
-
- Point(int x, int y)
- {
- this->x = x;
- this->y = y;
- }
-
- double getDistance(const Point& other) const
- {
- int dx = x - other.x;
- int dy = y - other.y;
- return sqrt(dx * dx + dy * dy);
- }
-};
-
-void print(const Point& p)
-{
- std::cout << "[ x: " << p.x << ", y: " << p.y <<" ]";
-}
-
-struct Rectangle
-{
-private:
- Point points[4];
-
-public:
- Rectangle() = default; //the same as Rectangle() {}
-
- void setPoint(size_t index, int x, int y)
- {
- if(index > 3)
- return;
-
- points[index].x = x;
- points[index].y = y;
-
- }
- const Point& getPoint(size_t index) const
- {
- if(index > 3)
- return {}; //some Error. This is a refference to a temporary memory, so a better solution would be to throw an exception.
- return points[index];
- }
- double getPer() const
- {
- return 2 * points[0].getDistance(points[1]) + 2 * points[1].getDistance(points[2]);
- }
-};
-
-void print(const Rectangle& rect)
-{
- for(int i = 0; i < 4; i++)
- {
- print(rect.getPoint(i));
- }
-}
-
-void swap(Rectangle& tr1, Rectangle& tr2)
-{
- Rectangle temp = tr1;
- tr1 = tr2;
- tr2 = temp;
-}
-
-void selectionSort(Rectangle* triangles, double* perimeters, unsigned size)
-{
- for (unsigned i = 0; i < size - 1; i++)
- {
- //findMin
- int minElementIndex = i;
- for (unsigned j = i + 1; j < size; j++)
- {
- if (perimeters[j] < perimeters[minElementIndex])
- minElementIndex = j;
- }
-
- if (minElementIndex != i)
- {
- swap(perimeters[i], perimeters[minElementIndex]);
- swap(triangles[i], triangles[minElementIndex]);
- }
- }
-}
-
-int main()
-{
- ifstream file("rects.txt");
-
- if(!file.is_open())
- {
- std::cout << "Error while opening the file!";
- return -1;
- }
-
- int n;
- file >> n;
-
- Rectangle* rects = new Rectangle[n];
- double* perimeters = new double[n]; //this should be size_t, but we are using a double returning function for getDist.
-
- for(size_t i = 0; i < n; i++)
- {
- int x1, y1, x3, y3;
- file >> x1 >> y1 >> x3 >> y3;
-
- rects[i].setPoint(0, x1, y1);
- rects[i].setPoint(1, x1, y3);
- rects[i].setPoint(2, x3, y3);
- rects[i].setPoint(3, x3, y1);
-
- perimeters[i] = rects[i].getPer();
-
- }
-
- selectionSort(rects, perimeters, n); //selection sort, because we want min number of copies (or swaps)
-
- for(size_t i = 0; i < n; i++)
- {
- print(rects[i]);
- std::cout <<", Per: " << perimeters[i] << std::endl;
- }
-
-
- delete[] rects, perimeters;
- return 0;
-}
-
diff --git a/k_1_SI/task2_read.cpp b/k_1_SI/task2_read.cpp
deleted file mode 100644
index e6a8312..0000000
--- a/k_1_SI/task2_read.cpp
+++ /dev/null
@@ -1,55 +0,0 @@
-#include <iostream>
-#include <fstream>
-#include <cstring>
-using namespace std;
-
-const size_t MAX_NAME_SIZE = 22;
-struct Student
-{
- //gender should be BEFORE fn so the struct would be 28 bytes.
- char name[MAX_NAME_SIZE + 1];
- bool isMale;
- size_t fn;
-};
-
-
-size_t getFileSize(ifstream& f)
-{
- size_t currentPos = f.tellg();
- f.seekg(0, ios::end);
- size_t size = f.tellg();
-
- f.seekg(currentPos);
- return size;
-}
-
-void readFromFile(Student*& ptr, size_t& studentsCount, ifstream& f)
-{
- size_t sizeOfFile = getFileSize(f);
- studentsCount = sizeOfFile / sizeof(Student);
- ptr = new Student[studentsCount];
- f.read((char*)ptr, sizeOfFile);
-}
-
-
-int main()
-{
- Student* arr;
- size_t count;
- ifstream file("students.dat");
-
- if(!file.is_open())
- {
- std::cout << "Error while opening the file!" << std::endl;
- return -1;
- }
-
- readFromFile(arr, count, file);
-
- for (int i = 0; i < count; i++)
- std::cout << "Name: " << arr[i].name << ", fn: " << arr[i].fn << ", is male: " << arr[i].isMale << std::endl;
-
- delete[] arr;
-
- return 0;
-}
diff --git a/k_1_SI/task2_write.cpp b/k_1_SI/task2_write.cpp
deleted file mode 100644
index f320557..0000000
--- a/k_1_SI/task2_write.cpp
+++ /dev/null
@@ -1,84 +0,0 @@
-#include <iostream>
-#include <fstream>
-#include <cstring>
-using namespace std;
-
-const size_t MAX_NAME_SIZE = 22;
-struct Student
-{
- //gender should be BEFORE fn so the struct would be 28 bytes.
- char name[MAX_NAME_SIZE + 1];
- bool isMale;
- size_t fn;
-};
-
-bool isLower(char ch)
-{
- return ch >= 'a' && ch <= 'z';
-}
-
-bool areAllLowerCase(const char* str)
-{
- while(*str)
- {
- if(!isLower(*str))
- return false;
- str++;
- }
- return true;
-}
-
-bool isUpper(char ch)
-{
- return ch >= 'A' && ch <= 'Z';
-}
-
-bool isNameValid(const char* name)
-{
- size_t len = strlen(name);
- if(len == 0 || len > MAX_NAME_SIZE)
- return false;
- return isUpper(*name) && areAllLowerCase(name + 1);
-}
-
-void initStudent(Student& st, const char* name, size_t fn, bool isMale)
-{
- if(!isNameValid(name))
- strcpy(st.name, "Unknown");
- else
- strcpy(st.name, name);
-
- st.isMale = isMale;
- st.fn = fn;
-}
-
-void saveToFile(const Student* students, size_t count, ofstream& file)
-{
- file.write((const char*)students, count * sizeof(Student));
-}
-
-
-int main()
-{
- const size_t studentsCount = 4;
- Student* arr = new Student[studentsCount];
- initStudent(arr[0], "Ivan", 1, true);
- initStudent(arr[1], "Petur", 2, true);
- initStudent(arr[2], "Alex", 3, true);
- initStudent(arr[3], "Katerina", 4, false);
-
- ofstream file("students.dat", ios::binary);
-
- if(!file.is_open())
- {
- std::cout << "Error while opening the file!" << std::endl;
- delete[] arr; //!
- return -1;
- }
-
- saveToFile(arr, studentsCount, file);
-
- delete[] arr;
-
- return 0;
-}