-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_3.cpp
More file actions
102 lines (84 loc) · 1.99 KB
/
5_3.cpp
File metadata and controls
102 lines (84 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) {}
Point operator-() const {
return Point(-x, -y);
}
Point operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
void display() const {
cout << "(" << x << ", " << y << ")" << endl;
}
};
class PointStack {
private:
Point stack[30];
int top;
public:
PointStack() : top(-1) {}
bool isEmpty() const {
return top == -1;
}
bool isFull() const {
return top == 29;
}
void push(const Point& p) {
if (!isFull()) {
stack[++top] = p;
} else {
cout << "Stack overflow!" << endl;
}
}
Point pop() {
if (!isEmpty()) {
return stack[top--];
} else {
cout << "Stack underflow!" << endl;
return Point();
}
}
Point peek() const {
if (!isEmpty()) {
return stack[top];
} else {
cout << "Stack is empty!" << endl;
return Point();
}
}
};
int main() {
PointStack history;
Point p1(4, 5);
Point p2(1, 3);
Point result = p1 + p2;
cout << "Result of p1 + p2 is: ";
result.display();
history.push(result);
result = -p1;
cout << "Result of -p1: ";
result.display();
history.push(result);
cout << "\nUndo last operation:" << endl;
Point undone = history.pop();
undone.display();
cout << "Previous state: ";
history.peek().display();
Point p3(3, 4);
cout << "\nIs p1 equal to p3? ";
if (p1 == p3) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
cout<<"24e052_pushti kansara";
return 0;
}