Navigation
Page categories
Outline:
Last modification
2025-09-04
This is smol web. If you don't like it, fix your browser.

Infopage about the C++ lab of NPRG041.

Lab requirements#

To get the credit, you have to attend the course reasonably (at least accordingly to your knowledge of the topic), do the homework (see below) and finish an individual project (also below).

You will be assigned several points for finishing the homework. These will be added to the points from the final test, therefore improving your final grade for the whole course.

Depending on many factors, students from Erasmus programs may need a completely different set of rules to fit into different time limits. If you are an ERASMUS student, contact us!

Homework#

Assignment A — CString#

Wrap a C++-style string functionality in a STL-style string container. You are not allowed to use STL though: All memory management and required data structures need to be implemented by you.

The string class should support:

Hints:

Save your solution to a .h file and (optionally) accompanying .cpp file, and submit them using the SIS study group interface — corresponding fields are marked DU1 (czech) or HW1 (english). Your score and comments to the solution will be available at the same place.

DEADLINE: Sunday November 19th, 23:59:59.999

UPDATE: If you are not sure whether your solution really works as expected, you can test it with the test program. (I have assumed that your class is called CString and is available in file CString.h, change that if required.)

UPDATE OF UPDATE: There was a mistake in the original test program on line 22 (there was = instead of originally intended +=). The result was that the program printed out lots of non-ascii garbage instead of what I originally intended (randomly looking strings of numbers and letters).

Assignment B — Simple Grep#

You certainly know the grep utility from UNIX systems. The assignment is to write a simple reimplementation of that. Let’s call it sgrep.

sgrep receives a single command-line argument, which is a regular expression. Then it processes the input line-by-line and prints out the lines that match the regex.

Your program should accept a simple subset of the regular expression patterns:

After parsing the regular expression from the argument (argv argument of main() ), your programm should read the standard input line-by-line, decide which lines match the given regular expression (WHOLE line must match the expression, unlike grep), and print them out of they do.

Example usage input:

$ ./sgrep 'aba*cd'       --on windows, it would be:  # sgrep.exe 'aba*cd'
xxx
abcd
abaacd
xabcdx
abd

Corresponding output:

abcd
abaacd

If there are parentheses in the input, the program should instead print the matched content of the parentheses, one by one, separated by a newline:

Example input:

$ ./sgrep 'a([bc]([de]*))'
ab
acdedddeee
ade

Corresponding output:

b           --first line, first paren
            --first line, second paren is empty!
cdedddeee   --second line, first paren
dedddeee    --second line, second paren

The main requirement of assignment B is to never use manual memory management. Preferably not even raw pointers nor anything that can get broken easily. Exploit STL as much as you can, preferably smart pointers and other data structures, to get the safest and cleanest code possible. Except for the STL regex library, of course.

Your program should also behave as a reasonable command-line utility:

On the other hand, you may ignore several practical and efficiency-related measures that are present in traditional grep implementations:

Points for solution will also be derived from the usual criteria:

Submit your solution to SIS using the study group interface (as usual). The corresponding field for the .cpp file (no headers this time!) is DU2 - sgrep.cpp for Czech group, and HW2 - sgrep.cpp for English group.

DEADLINE: 2017/12/17

UPDATE: Because this is a C++ lab (not a formal grammars lab), recommended structure of tokens/parsers/matchers is provided here.

UPDATE 2: checking your solution. To verify that your solution works as expected, you can compare its output to the output of standard UNIX utilities grep and sed:

UPDATE 3: You may submit the assignment solution after the deadline, with appropriate score penalization. The penalization for delay of n full days after 2017/12/17 is computed as floor(n*n/8) (so a few days delay is mostly OK).

Project#

Each student has to create an individual project and submit it to get the credit. Topic is variable, but should be discussed and agreed upon before the end of November. Size of the project is not an issue and largely depends on the topic, around 500 lines of (neat) C++ code is a good guideline (on the other hand, packing the same functionality into a smaller program using e.g. advanced language features is even better).

Bad topics:

Good topics:

Deadlines:

Submission guidelines:

Lab timeline#

Week 1 (2017/10/3,4)#

Introduction to (and necessity of) C and low-level programming in general. How to compile C to assembly, how does linker link the symbols to produce executables.

Source code here. (Source from the labs will always appear there. Also, if you are new to programming on UNIX, you will certainly want to examine the makefile from 01/asm2/Makefile !)

Week 2 (2017/10/10,11)#

Plain C programming practice (with pointers).

Sad face about how modern CPUs won’t ever process strings automagically.

Source code can be found on the usual place.

Week 3 (2017/10/17,18)#

C++ leap: Encapsulation of various stuff in nice interfaces, a word about references.

Week 4 (2017/10/24,25)#

Constructors and destructors, RAII (do NOT use new if you really don’t want heap memory), a tiny hint of resource ownership. We have applied the Rule of Three (copy constructor+copy assignment+destructor) to make sure that our list class will be handled as we expect in all cases, which makes it perfectly encapsulated (and basically as safe as we want to). Source code is on the usual place.

Week 5 (2017/10/31, 2017/11/01)#

A bit about templates, STL containers and iterators.

Week 6 (2017/11/07,08)#

A bit of I/O. Iterators on your own class.

Czech class was cancelled due to a sport event. The material from english class is available for study as always, I suggest you take a look at it. We’ll eventually run through it at the lab.

Week 7 (2017/11/14,15)#

Run-time polymorphism, virtual, vtables, sneaky peek on smart pointers.

Czech class will be taught by Vlastimil Dort this week.

Week 8 (2017/11/20,21)#

Conversion operators, proxy classes and their usage: get/set-style properties, safe array indexing.

Week 9 (2017/11/28,29)#

Recursive Descent Parsing —- a straight-forward way to convert a formal grammar to a tokenizers and parsers. AST as a really vital data structure.

Week 10 (2017/12/5,6)#

Using the previous approach to write an interpreter. Using preprocessor to generate repetitive code.

Week 11 (2017/12/12,13)#

UNIXifying the interpreter, smart pointers and move references.

Week 12 (2016/12/19,20)#

A bit of practice —- implementing the postfix calc, various goodies from STL iterator/algorithm library.

We still have 2 labs left now, so if you want to practice something specific, send me an e-mail.

Week 13,14#

Practice for the exam.

Extra stuff we managed to fit in the czech lab: Computing with templates, low-level program loading (dynamically linked libraries), simple HTTP-like server.

Bonus material#