-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
73 lines (66 loc) · 1.7 KB
/
Stack.java
File metadata and controls
73 lines (66 loc) · 1.7 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
/**
* Створити стек символів. Порахувати чого більше у стеці – літер «а» чи «о».
* Stack
*/
public class Stack {
char [] array;
int index;
Stack(){index = 0;}
Stack(char start){
push(start);
}
public void push(char elem){
char [] temp = array;
array = new char[index + 1];
for (int i = 0; i < index; i++) {
array[i] = temp[i];
}
array[index] = Character.toLowerCase(elem);
index++;
}
public char pop(){
return array[index--];
}
public char get(){
return array[index];
}
public char get(int i){
return array[i];
}
public void print(){
for (int i = index - 1; i > -1; i--) {
System.out.println(array[i] + " ");
}
}
public void checkChars(){
int c1 = 0, c2 = 0;
for (int i = 0; i < index; i++) {
if (array[i] == 'a') {
c1++;
}
if(array[i] == 'o'){
c2++;
}
}
if (c1 > c2) {
System.out.println("char 'a' is presented more times than 'o'");
}
else if(c1 == c2){
System.out.println("char 'a' equals times than 'o'");
}
else{
System.out.println("char 'o' is presented more times than 'a'");
}
}
public static void main(String[] args) {
Stack stack = new Stack();
for (int i = 0; i < 26; i++) {
if(i == 10){
stack.push('a');
}
stack.push((char)(i+65));
}
stack.print();
stack.checkChars();
}
}