Review: Homework and Labs [1]#

Homework#

h7#

Ex3. File I/O in C++#

  • File, string, standard input/output can all be converted into stream

  • Similar to FILE* in C

  • DO NOT forget to check whether the files are correctly opened

#include <fstream>

ofstream output("result.txt");
ifstream input("matrices.txt");
if(!output.is_open() || !input.is_open()) return;

Think

How to read a line that contains numbers separated by spaces?

One way is to take the advantages of:

  • std::getline

  • std::istringstream

// Read a line from input. input can be a file or standard input.

std::string line;
std::getline(input, line);
// Now line is one line
std::istringstream iss(line);

// Read numbers from iss
while(iss >> num) {
    // Do something with num
}

Think

How to detect input errors?

For example, if we use cin>>a; where a is an int, but the real input is not an integer.

Ex4. Basic Programming#

\[\begin{split}\begin{cases} u_0 = a\\ u_{i+1} = \begin{cases} \frac{1}{2}u_i, \quad &\text{if $u_i$ is even}\\ 3_{u_i}+1, &\text{if $u_i$ is odd} \end{cases} \end{cases}\end{split}\]
  • Basic logic questions

  • Think about Fibonacci sequence. How to find the \(n\)-th Fibonacci number?

Hint

How to optimize the following recursion function?

long findn(long a) {
    if (a == 1) return 0;
    if (a % 2) {
        return findn(3 * a + 1) + 1;
    } else {
        return findn(a >> 1) + 1;
    }
}

Solution. We can use tail recursion to optimize it.

long findn(long a, long cnt) {
    if (a == 1) return cnt;
    if (a % 2) {
        return findn(3 * a + 1, cnt + 1);
    } else {
        return findn(a >> 1, cnt + 1);
    }
}

Ex5. From C to C++#

Here are some questions you should think:

  • What is class in C++?

  • What is poly-morphism in C++?

  • What is OOP?

  • What are the three characteristics of OOP?

  • What is abstract class in C++?

  • What is virtual function in C++? What is virtual destructor and when to use it?

  • What are the common containers in STL? How to use them?

A Class is a user defined data-type which has data members and member functions.

ex5.cpp#
class Grade {
private:
    static const char GRADES[] = {...};
    // Or use string
    char                  ltr;
    int                   prct;

public:
    void setPrct(int prct) {
        this->prct = prct;
        this->ltr  = GRADES[prct / 10];
    }
    void setLtr(char ltr) {
        this->ltr  = ltr;
        this->prct = 100 - (ltr -'A') * 10 - 5;
    }
    void print() { std::cout << "Grade:" << this->prct << "->" << this->ltr << std::endl; }
};

h8#

Ex1. Containers#

  • Basic C++ Containers library & Data structure

    Make full use of cppreference.

  • Parameter T inside <> is called template parameter, deduced when compilation

  • std::array<T, N>: static contiguous array

  • std::vector<T>: dynamic contiguous array

  • std::stack<T>: adapts a container to provide stack (LIFO data structure)

  • std::queue<T>: adapts a container to provide queue (FIFO data structure)

You should know the basic operations of those containers.

ex1.cpp#
#include <string>
#include <array>
#include <vector>
#include <stack>
#include <queue>
#include <iostream>

using namespace std;

void ex1_reverse_array() {
    array<string, 10000> re;
    string               word;
    size_t               count = 0;
    while (cin >> word) re[count++] = word;
    for (auto iter = re.cbegin() + count - 1; iter != re.cbegin() - 1; --iter) {
    }
}
void ex1_reverse_vector() {
    vector<string> re;
    string         word;
    while (cin >> word) re.push_back(word);
    for (auto iter = re.crbegin(); iter != re.crend(); ++iter) {
    }
}
void ex1_reverse_stack() {
    stack<string> re;
    string        word;
    while (cin >> word) re.push(word);
    for (; !re.empty(); re.pop()) {
    };
}
void ex1_ordered_queue() {
    queue<string> re;
    string        word;
    while (cin >> word) re.push(word);
    for (; !re.empty(); re.pop()) {
    };
}

Ex2. Class Implementation#

  • Basic inheritance & polymorphism

  • Basic drawing in OpenGL

  • Draw hierarchy diagram

Ex3. Classes and OpenGL#

  • Basic animation in OpenGL

  • Draw different figures in OpenGL

    • Rectangle

    • Triangle

    • Circle

    • Line

    • Polynomial

  • Combination of classes

Lab#

l9#

  • What is stack?

  • What is Postfix Expression?

  • How to use stack and implement the Shunting Yard algorithm

    • stack.push()

    • stack.pop()

    • stack.top()

    • stack.empty()

l10#

  • Basic class design (mandatory)

    • Methods

    • Attributes

    • Hierarchy (public, protected, private)

    • Abstract class (virtual functions)

  • Some design patterns (advanced)

    • Singleton

    • Factory Method

    • Observer

    • Adapter

Warning

Avoid diamond structure in class design

(Solutions: See Solution 1)