-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3_bonus_stack.cpp
More file actions
84 lines (78 loc) · 1.49 KB
/
3_bonus_stack.cpp
File metadata and controls
84 lines (78 loc) · 1.49 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
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
void push_one(vector<int> *stack)
{
char buffer[11] = "smth";
cout << "enter number" << endl;
cin.getline(buffer, 11);
int data = atoi(buffer);
(*stack).push_back(data);
}
int pop_one(vector<int> *stack)
{
if ((*stack).size() == 0)
{
cout << "nothing to return from buffer. 0 returned" << endl;
return (0);
}
int returned = (*stack)[(*stack).size() - 1];
(*stack).pop_back();
return (returned);
}
void show_stack(vector<int> *stack)
{
cout << "your stack: " << endl;
for (int i = (*stack).size() - 1; i >= 0; i--)
{
cout << (*stack).size() - i - 1 << " " << (*stack)[i] << endl;
}
}
int main()
{
char buffer[11] = "smth";
vector<int> stack;
while (buffer[0] != 4)
{
cout << "choose next operation or ctrl + D to end" << endl;
cout << "1 - push item" << endl;
cout << "2 - pop and operation plus for 2 elements in stack" << endl;
cout << "3 - delete one element" << endl;
cout << "4 - show current stack" << endl;
cin.getline(buffer, 11);
int choose = atoi(buffer);
switch(choose)
{
case 1:
{
push_one(&stack);
break;
}
case 2:
{
int a = pop_one(&stack);
int b = pop_one(&stack);
cout << "rezult is " << a + b << endl;
break;
}
case 3:
{
pop_one(&stack);
break;
}
case 4:
{
show_stack(&stack);
break;
}
default:
{
if (buffer[0] == 4)
break;
cout << "wrong operation. Retry" << endl;
break;
}
}
}
}