This repository contains laboratory assignments related to the course "Object-Oriented Programming" taught at Saint Petersburg Polytechnic University.
The ./stl
directory contains labs related to learning C++ Standard Template Library (STL) algorithms and concepts.
The ./mfc
directory contains labs focused on Microsoft Foundation Class Library (MFC), which is a C++ object-oriented library for Windows desktop application development.
To get started with the labs in this repository, follow these steps:
- Clone the repository to your local machine.
- Navigate to the specific lab directory (e.g.,
./stl/8/
). - Explore the source code.
Contributions to this repository are welcome! If you have suggestions for improvements or would like to add your own lab assignments, feel free to submit a pull request.
-
Task Description:
- Implement sorting algorithms for a vector of integers using different access methods.
- Read data from a text file into a vector.
- Read numbers from standard input, modify vector based on input, and print the result.
- Fill vectors with random values and sort them.
-
Implementation:
- Sorting Algorithms:
- Using
operator[]
(insort_brackets
function): Implements insertion sort algorithm using theoperator[]
for access. - Using
at()
Method (insort_at
function): Implements insertion sort algorithm using theat()
method for access. - Using Iterators Only (
insort_iter
function): Implements insertion sort algorithm using only iterators for access, without using any other operations besides getting the current element and advancing to the next one.
- Using
- Reading from Text File:
- Reads the contents of a text file into a C-style array and copies the data into a vector without using loops or STL algorithms.
- Processing Standard Input:
- Reads numbers from standard input and stores them in a vector. If the last number is 1, removes all numbers divisible by 2. If the last number is 2, inserts three 1s after each number divisible by 3.
- Filling Vectors with Random Values:
- Implements a function
fillRandom
to fill an array with random values in the range from -1.0 to 1.0. Uses this function to fill vectors of various sizes (5, 10, 25, 50, 100) with random values and sorts the contents using one of the sorting algorithms implemented earlier, adjusted for sorting real numbers.
- Implements a function
- Sorting Algorithms:
-
Source Code:
- The source code demonstrates the implementation of various vector manipulation tasks, including sorting, reading from file, processing standard input, and filling with random values.
- It uses algorithms such as insertion sort and iterators to perform sorting operations efficiently.
- The code efficiently handles input/output operations, demonstrating understanding of vector manipulation and standard library functionalities.
-
Task Description:
- Read the content of a text file, which may contain:
- Words composed of Latin lowercase and uppercase letters, as well as digits, with a maximum length of 20 characters.
- Punctuation marks: ".", ",", "!", "?", ":", ";".
- Whitespace characters: space, tab, newline.
- Format the text as follows:
- Remove whitespace characters other than space.
- Do not allow more than one consecutive space.
- Do not place a space between a word and a punctuation mark.
- Always place a space after a punctuation mark.
- Replace words longer than 10 characters with "Vau!!!".
- Convert the formatted text into a set of lines, each containing a whole number of words and not exceeding 40 characters in length.
- Read the content of a text file, which may contain:
-
Implementation:
- Text Formatting Functions:
remove_whitespaces
: Removes whitespace characters other than space.remove_consecutive_whitespaces
: Removes consecutive whitespace characters.insert_space_after_punctuation
: Inserts a space after each punctuation mark.remove_space_before_punctuation
: Removes space before punctuation marks.replace_long_words
: Replaces words longer than 10 characters with "Vau!!!".transform_into_lines
: Converts the formatted text into a set of lines not exceeding 40 characters in length.
- Main Functionality:
- Reads text from a file and applies formatting functions to it.
- Converts the formatted text into a set of lines and ensures that each line does not exceed 40 characters in length.
- Text Formatting Functions:
-
Source Code:
- The source code effectively implements the required text formatting tasks using various string manipulation techniques.
- It demonstrates understanding of string processing, including removal of whitespace characters, insertion/removal of spaces around punctuation marks, and replacement of long words.
- The code efficiently handles input/output operations and correctly formats the text as per the specified requirements.
- Description:
- Implemented a priority queue class with three priority levels: low, normal, and high.
- Elements are added to the queue with a specified priority and extracted based on their priority level and insertion order.
- Acceleration operation increases the priority of low priority elements to high, causing them to bypass normal priority elements.
- Implementation:
- Utilized
std::list
to store elements with each priority level represented by a separate list. - Implemented methods for adding elements with a given priority, extracting the top priority element, and accelerating low priority elements.
- The class efficiently manages elements according to their priority levels and ensures correct extraction order.
- Utilized
- Testing:
- Created a priority queue instance and tested various scenarios by adding elements with different priorities and performing acceleration.
- Checked the correctness of the queue operations using debug output, demonstrating the proper functioning of the class.
- Description:
- Developed a program to fill a list of integers with random values and output its elements in a zigzag order.
- Zigzag order involves printing the first, last, second, second to last, and so on elements of the list.
- Implementation:
- Utilized
std::list
to store random integer values. - Implemented the zigzag output by iterating through the list with two iterators, one starting from the beginning and the other from the end.
- Handled odd-sized lists by printing the middle element only once.
- Utilized
- Testing:
- Tested the program with lists of various sizes (0, 1, 2, 3, 4, 5, 7, 14) to ensure correct behavior in different scenarios.
- Verified the zigzag output by comparing it with the expected output for each list size.
- Description:
- Implemented a container class that stores factorial values from 1! to 10!.
- Implemented an iterator class for enumerating elements of this container.
- Access to the elements of this container is possible only through iterators returned by
begin()
andend()
functions. - The container does not store its elements in memory; they are computed when accessed through iterators.
- Implementation:
- Created
factorial_container
class with default constructor and functionsbegin()
andend()
returning iterators. - Implemented
factorial_iterator
class as a bidirectional iterator for enumerating factorial values. - Overloaded operators
*
,++
,--
,==
, and!=
for the iterator to enable iteration and comparison.
- Created
- Compatibility:
- Ensured compatibility with the STL by testing with algorithms like
copy
to copy container contents into astd::vector<int>
. - Verified compatibility by successfully copying the contents of the custom container into a
std::vector<int>
using thecopy
algorithm.
- Ensured compatibility with the STL by testing with algorithms like
- Description:
- Developed a program that performs the following actions:
- Fills a vector of
DataStruct
structures with random values:key1
andkey2
are generated randomly in the range from -5 to 5.str
is filled from a table of 10 arbitrary strings, and the index of the string is generated randomly.
- Prints the filled vector.
- Sorts the vector in the following order:
- Ascending order of
key1
. - If
key1
values are equal, sorts in ascending order ofkey2
. - If both
key1
andkey2
values are equal, sorts in ascending order of the length ofstr
.
- Ascending order of
- Prints the sorted vector.
- Fills a vector of
- Developed a program that performs the following actions:
- Implementation:
- Defined a
DataStruct
structure withkey1
,key2
, andstr
members. - Implemented a
fill_random
function to populate the vector with randomDataStruct
objects. - Utilized a lambda function as the sorting predicate in
std::sort
to define the custom sorting order based onkey1
,key2
, and the length ofstr
.
- Defined a
- Execution:
- Generated 10 random
DataStruct
objects and printed them before sorting. - Sorted the vector based on the specified criteria.
- Printed the sorted vector to demonstrate the sorting result.
- Generated 10 random
- Description:
- Developed a program that performs the following actions:
- Reads the content of a text file.
- Tokenizes the text into words, considering a word as a sequence of characters separated by spaces, tabs, and/or newline characters.
- Outputs the list of unique words present in the text without repetitions.
- Developed a program that performs the following actions:
- Implementation:
- Utilized standard algorithms and data structures such as
std::unordered_set
andstd::transform
. - Tokenized the text using
std::stringstream
andstd::istream_iterator
.
- Utilized standard algorithms and data structures such as
- Execution:
- Provided a command-line argument specifying the input file.
- Read the content of the file, tokenized it, and outputted the unique words found.
- Description:
- Developed a program that performs various operations on a vector of geometric shapes:
- Fills the vector with randomly generated geometric shapes such as triangles, squares, rectangles, and pentagons.
- Counts the total number of vertices of all shapes in the vector.
- Counts the number of triangles, squares, rectangles, and pentagons in the vector.
- Removes all pentagons from the vector.
- Creates a new vector containing the coordinates of one vertex of each shape.
- Reorders the shapes in the vector so that triangles come first, followed by squares, then rectangles, and finally pentagons.
- Utilized standard algorithms like
std::generate_n
,std::accumulate
,std::count_if
,std::remove_if
,std::transform
, andstd::stable_partition
.
- Developed a program that performs various operations on a vector of geometric shapes:
- Implementation:
- Defined structures for points and shapes and overloaded the necessary operators for printing.
- Implemented functions for generating random shapes, counting vertices, counting shapes, removing shapes, extracting vertices, and reordering shapes.
- Execution:
- Generated random shapes and added additional squares for testing purposes.
- Performed various operations on the vector of shapes and printed the results at each step to demonstrate the changes made.
- Description:
- Developed a functor that collects statistics about a sequence of integers.
- After processing the sequence with the
for_each
algorithm, the functor provides the following statistics:- Maximum number in the sequence
- Minimum number in the sequence
- Average number in the sequence
- Number of positive numbers
- Number of negative numbers
- Sum of odd elements in the sequence
- Sum of even elements in the sequence
- Whether the first and last elements of the sequence match.
- Tested the program on randomly generated sequences.
- Implementation:
- Defined a functor class
statistics
with overloadedoperator()
to process each element in the sequence. - Implemented methods to update maximum and minimum values, count positive and negative numbers, calculate the sum of odd and even numbers, and check the first and last elements of the sequence.
- Provided a method
printStatistics()
to print the collected statistics.
- Defined a functor class
- Execution:
- Generated a sequence of 30 random numbers in the range [-500, 500].
- Processed each element in the sequence using the functor to collect statistics.
- Printed the generated sequence and the collected statistics.
- Description:
- Developed a program that multiplies each element in a list of floating-point numbers by the value of PI using only standard algorithms and functors.
- Tested the program on randomly generated sequences of numbers.
- Implementation:
- Defined a functor
multiply_by_pi
that multiplies a number by PI. - Generated a sequence of random floating-point numbers.
- Applied the functor to each element in the sequence using the
transform
algorithm.
- Defined a functor
- Execution:
- Generated a sequence of random numbers in the range [0.0, 5.0].
- Multiplied each number by PI using the defined functor.
- Printed the original numbers and the resulting numbers after multiplication by PI.
- Description:
- Developed a program that implements an inheritance hierarchy of geometric shapes, including
Circle
,Triangle
, andSquare
classes derived from the base classShape
. - The
Shape
class contains information about the position of the center of the shape, methods to determine if a shape is more left or upper than another shape, and a pure virtual functiondraw
. - The program maintains a list of shape pointers and performs various operations using standard algorithms and adapters.
- Developed a program that implements an inheritance hierarchy of geometric shapes, including
- Implementation:
- Defined the base class
Shape
and derived classesCircle
,Triangle
, andSquare
. - Implemented methods to draw each shape and determine their position relative to other shapes.
- Generated random shapes and populated a list with pointers to these shapes.
- Used standard algorithms and adapters to draw the shapes, sort them based on different criteria, and then draw them again.
- Defined the base class
- Execution:
- Generated a list of random shapes.
- Drew all shapes and then sorted and drew them based on their position from left to right, right to left, top to bottom, and bottom to top.
I won't provide the logic for these labs as they are essentially step-by-step exercises from the teacher's manual.
However, I have made some bug fixes along the way. Below, I will list each lab title along with its corresponding path in the repository.
All laboratory assignments are compiled using a single Visual Studio Solution file: ./mfc/mfc.sln
.
Path: ./mfc/1/Hello/
Path: ./mfc/2/Test/
Path: ./mfc/3/Draw/
Path: ./mfc/4/MyKey/
Path: ./mfc/5/Speed/
Path: ./mfc/6/MyMsg/
Path: ./mfc/7/Graph/
Path: ./mfc/8/PaintORama/
Path: ./mfc/9/FourUp/
Path: ./mfc/10/BmpOpen/
Path: ./mfc/11/Tasks/