Vector Arithmetic

Lab C++ Intermediate 20 points

Write struct Vec2 { double x, y; } with +, -, scalar *, == and << (prints (x, y)). Read two vectors and a scalar; print a+b, a-b, a*k and a==b.

Input

Three lines: ax ay, bx by, k.

Output

Four lines.

Example

Input

3 4
1 2
2

Output

(4, 6)
(2, 2)
(6, 8)
false
Sample input available Sample output available
Editor
C++

Hints

An operator is just a function named operator+, operator== ...

Solution explanation

Overloading should follow the usual meaning of the operator so code stays readable.

Show solution
#include <iostream>
using namespace std;

struct Vec2 {
    double x, y;
};

Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }
Vec2 operator-(Vec2 a, Vec2 b) { return {a.x - b.x, a.y - b.y}; }
Vec2 operator*(Vec2 a, double k) { return {a.x * k, a.y * k}; }
bool operator==(Vec2 a, Vec2 b) { return a.x == b.x && a.y == b.y; }
ostream &operator<<(ostream &out, Vec2 v) { return out << "(" << v.x << ", " << v.y << ")"; }

int main() {
    Vec2 a, b;
    double k;
    cin >> a.x >> a.y >> b.x >> b.y >> k;
    cout << a + b << "\n" << a - b << "\n" << a * k << "\n" << boolalpha << (a == b) << "\n";
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD