-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
206 lines (189 loc) · 5.83 KB
/
Program.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections;
using System.Collections.Generic;
namespace Stack_Assignment
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Bai 1:");
Console.Write("Nhap chuoi ky tu: ");
var a = Console.ReadLine();
var stak = TaoChuoiKyTu(a);
Console.WriteLine(stak.ToArray());
Console.WriteLine("Bai 2:");
Console.Write("Nhap mot cau bat ky: ");
string[] tk = Console.ReadLine().Split();
string chuoi = tk[0];
var capacity = tk.Length;
var x = TaoChuoiTu(tk);
while (capacity != 0)
{
var i = x.Pop();
Console.Write($"{i} ");
capacity--;
}
Console.WriteLine("Bai 3: ");
Console.Write("Mang co bao nhieu phan tu?: ");
int n = int.Parse(Console.ReadLine());
int[] data = new int[n];
string[] tk1 = Console.ReadLine().Split();
for (int i = 0; i < n; i++)
{
data[i] = int.Parse(tk1[i]);
}
QuickSortStack(ref data);
for (int i = 0; i < n; i++)
{
Console.Write(data[i] + " ");
}
Console.WriteLine("Bai 4: ");
Console.Write("Nhap infix: ");
string infix = Console.ReadLine();
Console.WriteLine(infixToPostfix(infix));
}
#region Reverse a word
static Stack<char> TaoChuoiKyTu(string a)
{
var stak = new Stack<char>();
for (int i = 0; i < a.Length; i++)
{
stak.Push(a[i]);
}
return stak;
}
#endregion
#region Reverse a sentence
static Stack<string> TaoChuoiTu(string[] a)
{
var stak = new Stack<string>();
for (int i = 0; i < a.Length; i++)
{
stak.Push(a[i]);
}
return stak;
}
#endregion
#region Iterative QuickSort
public static void QuickSortStack(ref int[] data)
{
int startIndex = 0;
int endIndex = data.Length - 1;
int top = -1;
int[] stack = new int[data.Length];
stack[++top] = startIndex;
stack[++top] = endIndex;
while (top >= 0)
{
endIndex = stack[top--];
startIndex = stack[top--];
int p = Partition(ref data, startIndex, endIndex);
if (p - 1 > startIndex)
{
stack[++top] = startIndex;
stack[++top] = p - 1;
}
if (p + 1 < endIndex)
{
stack[++top] = p + 1;
stack[++top] = endIndex;
}
}
}
private static int Partition(ref int[] data, int left, int right)
{
int x = data[right];
int i = (left - 1);
for (int j = left; j <= right - 1; ++j)
{
if (data[j] <= x)
{
++i;
Swap(ref data[i], ref data[j]);
}
}
Swap(ref data[i + 1], ref data[right]);
return (i + 1);
}
private static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
#endregion
#region Infix to Postfix
public static string infixToPostfix(string expn)
{
Stack<char> stak = new Stack<char>();
string output = "";
char temp;
foreach (char ch in expn)
{
if (ch >= '0' && ch <= '9')
{
output += ch;
}
else
{
switch (ch)
{
case '+':
case '-':
case '*':
case '/':
case '%':
if (stak.Count == 0)
{
stak.Push(ch);
output += "";
}
else if (stak.Count != 0)
{
if (precedence(stak.Peek()) >= precedence(ch))
{
output += stak.Pop();
stak.Push(ch);
}
else
{
stak.Push(ch);
}
}
break;
case '(':
stak.Push(ch);
break;
case ')':
while (stak.Count != 0 && (temp = stak.Pop()) != '(')
{
output = output + temp;
}
break;
}
}
}
while (stak.Count != 0)
{
output += stak.Pop();
}
return output;
}
public static int precedence(char ch)
{
switch (ch)
{
case '+':
case '-':
return 12;
case '*':
case '/':
case '%':
return 13;
}
return -1;
}
#endregion
}
}