-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
289 lines (248 loc) · 10.4 KB
/
Form1.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Linq;
using System.Windows.Forms;
namespace ClaseNotaciones
{
public partial class Form1 : Form
{
//Instanciamos las clases NotacionInfija y NotacionPosfija
NInfija infija;
NPosfija posfija;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) { }
/// <summary>
/// Evento que se ejecuta al presionar la tecla "Enter" en cualquier textbox que lo tenga asignado.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void KeyEnter(object sender, KeyPressEventArgs e)
{
//Se activará unicamente al ser "Enter"
if (e.KeyChar == (char)Keys.Enter)
{
// Verificar mezcla de letras y números
if (!VerificarSinMezclaLetrasYNumeros(this.txtInfija.Text))
{
MessageBox.Show("Error: No se permite la mezcla de letras y números.");
return; // Salir del método si hay mezcla de letras y números
}
// Limpiamos la memoria de objetos ya no usados. Con el fin de optimizar el programa
GC.Collect();
try
{
infija = new NInfija(txtInfija.Text);
txtPosfija.Text = infija.APosfija().ToString();
}
//En caso de dar algun error, es previsto que fue por la notación mal ingresada.
catch (Exception ex) { MessageBox.Show("Error, notación ingresada incorrecta\n" + ex.ToString()); }
}
}
/// <summary>
/// Permite que al dar "foco" a un textbox especifico, los demás se borrarán el texto almacenado.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Focus_Enter(object sender, EventArgs e)
{
txtPosfija.Text = "";
txtRespuesta.Text = string.Empty;
}
private void btnTransformar_Click(object sender, EventArgs e)
{
// Verificar si hay operadores consecutivos
if (VerificarOperadoresConsecutivos(this.txtInfija.Text))
{
MessageBox.Show("Error: Se encontraron operadores consecutivos.");
return; // Salir del método si hay operadores consecutivos
}
// Verificar si hay mezcla de letras y números
if (!VerificarSinMezclaLetrasYNumeros(this.txtInfija.Text))
{
MessageBox.Show("Error: No se permite la mezcla de letras y números.");
return; // Salir del método si hay mezcla de letras y números
}
// Verificar si la cantidad de paréntesis es correcta
if (!verificarParentesis(this.txtInfija.Text))
{
MessageBox.Show("Error en paréntesis\nPor favor complete los paréntesis.");
return; // Salir del método si hay error en los paréntesis
}
// Si todas las validaciones pasan, procedemos a realizar la transformación
// Limpiamos la memoria de objetos ya no usados con el fin de optimizar el programa
GC.Collect();
try
{
infija = new NInfija(txtInfija.Text);
txtPosfija.Text = infija.APosfija() + "";
}
// En caso de dar algún error, es previsto que fue por la notación mal ingresada.
catch (Exception ex)
{
MessageBox.Show("Error, notación ingresada incorrecta\n" + ex.ToString());
}
}
/// <summary>
/// Es un metodo que verifica si hay la misma cantidad de '(' y ')' para su correcto uso
/// </summary>
/// <param name="cadena"></param>
/// <returns></returns>
public static bool verificarParentesis(string cadena)
{
int contador = 0;
for (int i = 0; i < cadena.Length; i++)
{
char caracter = cadena[i];
if (caracter == '(')
{
contador++;
}
else if (caracter == ')')
{
contador--;
}
// Si el contador es negativo en algún momento, significa que hay más paréntesis de cierre que de apertura
if (contador < 0)
{
return false;
}
}
// Si el contador es igual a 0 al final, significa que hay la misma cantidad de paréntesis de apertura y cierre
return contador == 0;
}
/// <summary>
/// Metodo para comprobar mediante metodos char si hay letras o numero en el mismo txt
/// </summary>
/// <param name="cadena"></param>
/// <returns></returns>
public static bool VerificarSinMezclaLetrasYNumeros(string cadena)
{
bool tieneLetras = false;
bool tieneNumeros = false;
foreach (char c in cadena)
{
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
{
tieneLetras = true;
}
else if (char.IsDigit(c))
{
tieneNumeros = true;
}
// Si encontramos tanto letras como números, devolvemos false
if (tieneLetras && tieneNumeros)
{
return false;
}
}
// Si solo hay letras o solo hay números, devolvemos true
return true;
}
private bool VerificarOperadoresConsecutivos(string input)
{
// Lista de operadores comunes
char[] operadores = { '+', '-', '*', '/', '^' };
for (int i = 0; i < input.Length - 1; i++)
{
if (operadores.Contains(input[i]) && operadores.Contains(input[i + 1]))
{
return true;
}
}
return false;
}
private void btnValidar_Click(object sender, EventArgs e)
{
// Para que cuando se de click en el botón y no se encuentre nada, se muestre mensaje
if (string.IsNullOrWhiteSpace(this.txtInfija.Text))
{
MessageBox.Show("No hay contenido que validar");
}
else
{
// Verificar mezcla de letras y números
if (!VerificarSinMezclaLetrasYNumeros(this.txtInfija.Text))
{
MessageBox.Show("Error: No se permite la mezcla de letras y números.");
return; // Salir del método si hay mezcla de letras y números
}
// Verificar operadores consecutivos
if (VerificarOperadoresConsecutivos(this.txtInfija.Text))
{
MessageBox.Show("Error: Se encontraron operadores consecutivos.");
return; // Salir del método si hay operadores consecutivos
}
if (verificarParentesis(this.txtInfija.Text))
{
try
{
infija = new NInfija(txtInfija.Text);
}
// En caso de dar algún error, es previsto que fue por la notación mal ingresada.
catch (Exception ex)
{
MessageBox.Show("Error, notación ingresada incorrecta\n" + ex.ToString());
}
finally // estructura try-catch-finally: finally sirve aunque no hay una excepción
{
MessageBox.Show("Notación Correcta"); // Mostrará la notación correcta
}
}
if (VerificarOperadoresConsecutivos(txtInfija.Text))
{
MessageBox.Show("Error: No se permite operadores conjuntos.");
return; // Salir del método si hay mezcla de letras y números
}
}
}
private void btnEvaluar_Click(object sender, EventArgs e)
{
string infijaInput = this.txtInfija.Text;
// Verificar que no haya mezcla de letras y números
if (!VerificarSinMezclaLetrasYNumeros(infijaInput))
{
MessageBox.Show("Error: No deben estar mezclados números y letras.");
return;
}
// Verificar que no haya operadores consecutivos
if (VerificarOperadoresConsecutivos(infijaInput))
{
MessageBox.Show("Error: No debe haber operadores consecutivos.");
return;
}
// Verificar paréntesis balanceados
if (!verificarParentesis(infijaInput))
{
MessageBox.Show("Error: Los paréntesis no están balanceados.");
return;
}
// Convertir de infija a posfija y evaluar
if (txtPosfija.Text != "")
{
//Primero va convertir de infija a posfija, si ya fue transformada continua con la resolución.
if (txtPosfija.Text != "") posfija = new NPosfija(txtPosfija.Text);
else
{
infija = new NInfija(txtInfija.Text);
posfija = infija.APosfija();
txtPosfija.Text = posfija.ToString().Replace(" ", "");
}
string not = posfija.ToString();
string not2 = not.Replace('.', ',');
double result = -999999999; // inicializo valores
try //compruebo que los tokens me regresen un double y si no un mensaje de error
{
result = posfija.CalcularResultado(not2);
}
catch (Exception ex) { MessageBox.Show("Error, notación ingresada incorrecta\n" + ex.ToString()); }
//ahora si asigno a r lo que contiene result.ToString()
string r = result.ToString();
//finalmente lo imprime
txtRespuesta.Text = r;
}
else MessageBox.Show("Error: No deben estar mezcla de numeros y letras");
}
}
}