-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserInput.cs
444 lines (387 loc) · 21.9 KB
/
UserInput.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace CalMat
{
class UserInput
{
// defini des modeles de commande
private static Regex cal_rgx = new Regex(@"^(\w+)\=(\w)([\+\-\*])(\w)$"); // A=B+C
private static Regex init_rgx = new Regex(@"^(\w+)\=\[(\w+),(\w+)\]$"); //A=[2,2]
private static Regex showCal_rgx = new Regex(@"^(\w+)([\+\-\*])(\w+)$"); //A*B
private static Regex exit_rgx = new Regex(@"(exit)"); //exit
private static Regex meth_rgx = new Regex(@"^(\w+)\((\w)\)$"); // trans(A)
private static Regex methaff_rgx = new Regex(@"^(\w+)\=(\w+)\((\w)\)$"); //B=trans(A)
private static Regex affMat_rgx = new Regex(@"^(\w+)$"); //N
private static Regex power_rgx = new Regex(@"^(\w+)\^(\w+)$"); //A^3
private static Regex powerAssigne_rgx = new Regex(@"^(\w+)\=(\w+)\^(\w+)$"); //B=A^5
static MatchCollection matches;
private static void Create( string operande1, string operande2, string Mtrx_assign_name) // méthode pour crée une matrice "principal"
{
string mtrx_operande = Calculatrice.listMatrix.ContainsKey(operande1) ? operande1 : operande2; // si l'un des deux matrices existe
if (Calculatrice.listMatrix[mtrx_operande].Lines == Calculatrice.listMatrix[mtrx_operande].Columns)
{
SquareMatrix matrix_assign = new SquareMatrix(Calculatrice.listMatrix[mtrx_operande].Lines, Mtrx_assign_name); //si c'est une matrice carrée
}
else
{
Calculatrice.listMatrix.Add(Mtrx_assign_name, null); //si c'est une matrice classique
}
}
public static String AssigneCalcul () // méthode pour affecter une valeur venant d'un calcul matriciel à une autre matrice
{
//defini les paramètres donnés par l'utilisateur
string operateur = matches[0].Groups[3].ToString();
string operande1 = matches[0].Groups[2].ToString();
string operande2 = matches[0].Groups[4].ToString();
string Mtrx_assign_name = matches[0].Groups[1].ToString();
switch (operateur)
{
case "+": // cas si l'opérateur est +
if (Calculatrice.listMatrix.ContainsKey(operande1) && Calculatrice.listMatrix.ContainsKey(operande2))
{
if (!Calculatrice.listMatrix.ContainsKey(Mtrx_assign_name))
{
Create(operande1, operande2, Mtrx_assign_name); // si la matrice de gauche n'existe pas on la créer
}
Calculatrice.listMatrix[Mtrx_assign_name] = Calculatrice.listMatrix[operande1] + Calculatrice.listMatrix[operande2]; // on lui affecte la valeur du calcul
return Calculatrice.listMatrix[Mtrx_assign_name].ToString(); // on retourne la valeur sous forme de string
}
break;
case "-": // cas si l'opérateur est -
if (Calculatrice.listMatrix.ContainsKey(operande1) && Calculatrice.listMatrix.ContainsKey(operande2))
{
if (!Calculatrice.listMatrix.ContainsKey(Mtrx_assign_name))
{
Create(operande1, operande2, Mtrx_assign_name); // si la matrice de gauche n'existe pas on la créer
}
Calculatrice.listMatrix[Mtrx_assign_name] = Calculatrice.listMatrix[operande1] - Calculatrice.listMatrix[operande2]; // on lui affecte la valeur du calcul
return Calculatrice.listMatrix[Mtrx_assign_name].ToString(); // on retourne la valeur sous forme de string
}
break;
case "*": // cas si l'opérateur est *
if (Calculatrice.listMatrix.ContainsKey(operande1) || Calculatrice.listMatrix.ContainsKey(operande2)) // si au moins l'un des deux existe
{
if (!Calculatrice.listMatrix.ContainsKey(Mtrx_assign_name))
{
Create(operande1, operande2, Mtrx_assign_name); // si la matrice de gauche n'existe pas on la créer
}
if (!Calculatrice.listMatrix.ContainsKey(operande1)) //si c'est l'opérande 1 qui n'est pas dans le dictionnaire
{
Calculatrice.listMatrix[Mtrx_assign_name] = operande1 * Calculatrice.listMatrix[operande2]; // on affecte la valeur (on considère que l'opérande 1 est un nombre)
}
else if (!Calculatrice.listMatrix.ContainsKey(operande2)) //si c'est l'opérande 2 qui n'est pas dans le dictionnaire
{
Calculatrice.listMatrix[Mtrx_assign_name] = operande2 * Calculatrice.listMatrix[operande1]; // on affecte la valeur (on considère que l'opérande 2 est un nombre)
}
else // si les deux y sont
{
Calculatrice.listMatrix[Mtrx_assign_name] = Calculatrice.listMatrix[operande1] * Calculatrice.listMatrix[operande2]; //on affecte la valeur du produit des deux matrices
}
return Calculatrice.listMatrix[Mtrx_assign_name].ToString();
}
break;
}
throw new Exception("Au moins une des deux matrices n'existe pas\n"); // sinon on affiche l'erreur
}
public static String Init () //méthode pour créer et initialiser une matrice
{
//defini les paramètres donnés par l'utilisateur
string Mtrx_assign_name = matches[0].Groups[1].ToString();
string dim1 = matches[0].Groups[2].ToString();
string dim2 = matches[0].Groups[3].ToString();
if (!Calculatrice.listMatrix.ContainsKey(Mtrx_assign_name)) //si la matrice n'existe pas
{
if (dim1 == dim2)
{
int dim;
if (int.TryParse(dim1, out dim))
{
SquareMatrix matrix_creat = new SquareMatrix(dim, Mtrx_assign_name); // on crée une matrice carrée si les deux dimension son égale
}
else
{
throw new Exception ("Les dimentions données ne sont pas valide\n"); // si on arrvie pas à convertir les dimensions en int, on affiche l'erreur
}
}
else
{
int dim_1, dim_2;
if (int.TryParse(dim1, out dim_1) && int.TryParse(dim2, out dim_2))
{
Matrix matrix_creat = new Matrix(dim_1, dim_2, Mtrx_assign_name); //sinon on crée une matrice classique
}
else
{
return "Les dimentions données ne sont pas valide\n"; // si on arrvie pas à convertir les dimensions en int, on affiche l'erreur
}
}
Calculatrice.listMatrix[Mtrx_assign_name].Input(); // on appelle la méthode Input dans la classe Matrix pour initialiser la matrice
return ""; // sert juste pour que le programme compile
}
else
{
throw new Exception("Il existe deja une matrice " + Mtrx_assign_name + "\n"); // sinon on affiche l'erreur
}
}
public static String PrintCal () // méthode pour affichier les résultats d'un calcul matriciel
{
//defini les paramètres donnés par l'utilisateur
string operande1 = matches[0].Groups[1].ToString();
string operateur = matches[0].Groups[2].ToString();
string operande2 = matches[0].Groups[3].ToString();
switch (operateur)
{
case "+": // cas si l'opérateur est +
if (Calculatrice.listMatrix.ContainsKey(operande1) && Calculatrice.listMatrix.ContainsKey(operande2))
{
return (Calculatrice.listMatrix[operande1] + Calculatrice.listMatrix[operande2]).ToString(); // si les deux matrices existent, on retourne le résultat sous forme de string
}
break;
case "-": // cas si l'opérateur est -
if (Calculatrice.listMatrix.ContainsKey(operande1) && Calculatrice.listMatrix.ContainsKey(operande2))
{
return (Calculatrice.listMatrix[operande1] - Calculatrice.listMatrix[operande2]).ToString(); // si les deux matrices existent, on retourne le résultat sous forme de string
}
break;
case "*": // cas si l'opérateur est *
if (Calculatrice.listMatrix.ContainsKey(operande1) || Calculatrice.listMatrix.ContainsKey(operande2)) // si au moins l'un des deux existe
{
if (!Calculatrice.listMatrix.ContainsKey(operande1)) //si c'est l'opérande 1 qui n'est pas dans le dictionnaire
{
return (operande1 * Calculatrice.listMatrix[operande2]).ToString(); // on retourne la valeur sous forme de string (on considère que l'opérande 1 est un nombre)
}
else if (!Calculatrice.listMatrix.ContainsKey(operande2)) //si c'est l'opérande 2 qui n'est pas dans le dictionnaire
{
return (operande2 * Calculatrice.listMatrix[operande1]).ToString();// on retourne la valeur sous forme de string (on considère que l'opérande 2 est un nombre)
}
else // si les deux y sont
{
return (Calculatrice.listMatrix[operande1] * Calculatrice.listMatrix[operande2]).ToString(); //on retourne la valeur du produit des deux matrices sous forme de string
}
}
break;
}
throw new Exception("Au moins une des deux matrices n'existe pas\n"); // sinon on affiche l'erreur
}
public static String Method() // méthode pour affiher les valeurs de méthode de calcul
{
//defini les paramètres donnés par l'utilisateur
string method = matches[0].Groups[1].ToString();
string matrix = matches[0].Groups[2].ToString();
if (Calculatrice.listMatrix.ContainsKey(matrix)) // on verifie que la matrice existe
{
switch (method)
{
case "trans": // cas si la methode est trans
return (Calculatrice.listMatrix[matrix].Trans()).ToString(); //retourne la valeur de la transposée sour forme de string
case "det": // cas si la methode est det
if (Calculatrice.listMatrix[matrix].Lines == Calculatrice.listMatrix[matrix].Columns)
{
return (((SquareMatrix)Calculatrice.listMatrix[matrix]).Det()).ToString(); //si la matrice est carré, retourne la valeur du determinant sous forme de string
}
else
{
throw new Exception("La matrice " + matrix + " n'est pas une matrice carre\n"); // sinon retourne l'erreur
}
case "trace": // cas si la methode est trace
if (Calculatrice.listMatrix[matrix].Lines == Calculatrice.listMatrix[matrix].Columns)
{
return (((SquareMatrix)Calculatrice.listMatrix[matrix]).Trace()).ToString(); //si la matrice est carré, retourne la valeur de la trace sous forme de string
}
else
{
throw new Exception("La matrice " + matrix + " n'est pas une matrice carre\n"); // sinon retourne l'erreur
}
case "norme": // cas si la methode est norme
if (Calculatrice.listMatrix[matrix].Lines == Calculatrice.listMatrix[matrix].Columns)
{
return (((SquareMatrix)Calculatrice.listMatrix[matrix]).Norme()).ToString(); //si la matrice est carré, retourne la valeur de la norme infini sous forme de string
}
else
{
throw new Exception("La matrice " + matrix + " n'est pas une matrice carre\n"); // sinon retourne l'erreur
}
default:
throw new Exception("Cette fonction n'est pas definie.\n"); //sinon affiche l'erreur
}
}
throw new Exception("La matrice " + matrix + " n'existe pas\n"); // sinon retourne l'erreur
}
public static String MethodAffectation() //méthode pour affecter à une matrice une valeur avec une methode
{
//defini les paramètres donnés par l'utilisateur
string Mtrx_assign_name = matches[0].Groups[1].ToString();
string method = matches[0].Groups[2].ToString();
string matrix = matches[0].Groups[3].ToString();
if (Calculatrice.listMatrix.ContainsKey(matrix))
{
if (method == "trans") //si la méthode est trans
{
if (!Calculatrice.listMatrix.ContainsKey(Mtrx_assign_name)) // si la matrice d'assignation n'existe pas
{
Create(matrix, matrix, Mtrx_assign_name);
}
Calculatrice.listMatrix[Mtrx_assign_name] = Calculatrice.listMatrix[matrix].Trans(); // on donne les valeurs à la matrice de gauche
}
return (Calculatrice.listMatrix[Mtrx_assign_name]).ToString(); // on retourne la valeur sous string
}
throw new Exception("La matrice " + matrix + " n'existe pas\n"); // sinon retourne l'erreur
}
public static String Print () // méthode pour afficher la valeur de la matrice
{
//defini les paramètres donnés par l'utilisateur
string matrix = matches[0].Groups[1].ToString();
if (Calculatrice.listMatrix.ContainsKey(matrix))
{
return Calculatrice.listMatrix[matrix].ToString(); //si la matrice existe, on retourne sa valeur sous forme de string
}
else
{
throw new Exception("La matrice n'existe pas\n"); //sinon on retourne l'erreur
}
}
public static String Power() //méthode pour faire la puissance d'une matrix
{
//defini les paramètres donnés par l'utilisateur
string matrix = matches[0].Groups[1].ToString();
string power = matches[0].Groups[2].ToString();
if (Calculatrice.listMatrix.ContainsKey(matrix))
{
if (Calculatrice.listMatrix[matrix].Columns == Calculatrice.listMatrix[matrix].Lines) //on vérifie si la matrice est bien carrée
{
return (Calculatrice.listMatrix[matrix]).Pow(power).ToString(); // si la matrice existe, on retourne la valeur de matrix^power sous forme de string
}
throw new Exception("La matrice n'est pas carrée"); //si elle passe pas le test "carrée"
}
throw new Exception("La matrice n'existe pas"); // sinon, on affiche l'erreur
}
public static String AssignePower() // methode pour faire un puissance d'une matrice et affecter la valeur
{
//defini les paramètres donnés par l'utilisateur
string Mtrx_assign_name = matches[0].Groups[1].ToString();
string matrix = matches[0].Groups[2].ToString();
string power = matches[0].Groups[3].ToString();
Create(matrix, matrix, Mtrx_assign_name); //on crée la matrice
if (Calculatrice.listMatrix[matrix].Columns == Calculatrice.listMatrix[matrix].Lines) //on vérifie si la matrice est bien carrée
{
Calculatrice.listMatrix[Mtrx_assign_name] = ((SquareMatrix)Calculatrice.listMatrix[matrix]).Pow(power); // on iniialise la matrice avec le calcul matrix^power
return Calculatrice.listMatrix[Mtrx_assign_name].ToString(); // on retourne la valeur de matrix^power sous forme de string
}
throw new Exception("La matrice n'est pas carrée"); //si elle passe pas le test "carrée"
}
public static String Parse(string commande, ref string error) // procédure de parsing
{
commande = commande.Replace(" ", ""); // on enlève tout les espace
matches = cal_rgx.Matches(commande); // on regarde si la commande est de la forme A=B+C
if (matches.Count > 0)
{
try
{
return AssigneCalcul(); //si oui on essaye la méthode AssignCalcul
}
catch (Exception e)
{
return error = e.Message; // si il y a un problème, on récupère l'erreur et on l'affiche
}
}
matches = init_rgx.Matches(commande); // on regarde si la commande est de la forme A=[2,2]
if (matches.Count > 0)
{
try
{
return Init(); //si oui on essaye la méthode Init
}
catch (Exception e)
{
return error = e.Message; // si il y a un problème, on récupère l'erreur et on l'affiche
}
}
matches = showCal_rgx.Matches(commande); // on regarde si la commande est de la forme B+C
if (matches.Count > 0)
{
try
{
return PrintCal(); //si oui on essaye la méthode PrintCal
}
catch (Exception e)
{
return error = e.Message; // si il y a un problème, on récupère l'erreur et on l'affiche
}
}
matches = exit_rgx.Matches(commande); // on regarde si la commande est de la forme exit
if (matches.Count>0)
{
System.Diagnostics.Process.GetCurrentProcess().Kill(); // on ferme le programme
}
matches = meth_rgx.Matches(commande); // on regarde si la commande est de la forme trans(A)
if(matches.Count > 0)
{
try
{
return Method(); //si oui on essaye la méthode Method
}
catch (Exception e)
{
return error = e.Message; // si il y a un problème, on récupère l'erreur et on l'affiche
}
}
matches = methaff_rgx.Matches(commande); // on regarde si la commande est de la forme B=trans(A)
if(matches.Count >0)
{
try
{
return MethodAffectation(); //si oui on essaye la méthode MethodAffectation
}
catch (Exception e)
{
return error = e.Message; // si il y a un problème, on récupère l'erreur et on l'affiche
}
}
matches = affMat_rgx.Matches(commande); // on regarde si la commande est de la forme A
if (matches.Count > 0)
{
try
{
return Print(); //si oui on essaye la méthode Print
}
catch(Exception e)
{
return error = e.Message; // si il y a un problème, on récupère l'erreur et on l'affiche
}
}
matches = power_rgx.Matches(commande); // on regarde si la commande est de la forme A^2
if (matches.Count > 0)
{
try
{
return Power(); //si oui on essaye la méthode Power
}
catch (Exception e)
{
return error = e.Message; // si il y a un problème, on récupère l'erreur et on l'affiche
}
}
matches = powerAssigne_rgx.Matches(commande); // on regarde si la commande est de la forme A=B^2
if (matches.Count > 0)
{
try
{
return AssignePower(); //si oui on essaye la méthode PowerAssigne
}
catch (Exception e)
{
return error = e.Message; // si il y a un problème, on récupère l'erreur et on l'affiche
}
}
if (commande.Length == 0) // on regarde si la commande a une longueur null
{
return error = "\n";
}
return error = "L'expression n'est pas valable.\n"; // si la commande ressemble à rien
}
}
}