-
Notifications
You must be signed in to change notification settings - Fork 1
/
Menu.c
686 lines (588 loc) · 13 KB
/
Menu.c
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#include "Console.h"
#include "Matrix.h"
#include "Menu.h"
#include "Utils.h"
#define EQ_ASSERT(X_COND, X_ERR) if(!(X_COND)) { setcolor(12); printf("Hata: %s", X_ERR); goto end; }
void loop_menu(Menu* menu, Memory* memory)
{
void (*func)(MENU_PARAM_DECL);
while (func = menu->functions[show_menu(menu)])
{
clear();
func(memory);
}
}
int show_menu(Menu* menu)
{
int opt, i, error = 0;
do
{
clear();
setcolor(7);
printf("%s", menu->title);
setcolor(11);
for (i = 1; i < 10; i++)
{
printf("\n");
if (!menu->options[i])
continue;
printf("%d. %s", i, menu->options[i]);
}
if (menu->options[0])
printf("\n0. %s", menu->options[0]);
setcolor(12);
printf("\n%s", error ? "Lutfen uygun bir secenek secin." : " ");
setcolor(7);
printf("\n> ");
scanl("%d", &opt);
} while (error = (opt < 0 || opt > 9 || !menu->options[opt]));
return opt;
}
void print_equterm(float value, char name, bool first, bool last)
{
value = froundf(value);
if (!value)
{
if (first && last)
printf("0");
return;
}
if (first)
{
if (last)
printf("%g", value);
else if (value == -1)
printf("-%c", name);
else if (value == 1)
printf("%c", name);
else
printf("%g%c", value, name);
}
else
{
printf(value > 0 ? " + " : " - ");
if (last)
printf("%g", fabsf(value));
else if (value == -1 || value == 1)
printf("%c", name);
else
printf("%g%c", fabsf(value), name);
}
}
void menu_matrices(MENU_PARAM_DECL)
{
loop_menu(&memory->menus[MATRIX_MENU], memory);
}
void menu_console(MENU_PARAM_DECL)
{
char buffer[CON_BUFFER_SIZE] = { 0 }, *endp;
bool newline = true;
PExpression* input;
printf("Islem Konsolu\n\n");
setcolor(8);
printf("Geri donmek icin return yazin.");
setcolor(7);
printf("\n> ");
while (true)
{
(void)readl(buffer, CON_BUFFER_SIZE);
input = parse_formula(memory, (char*)buffer, &endp);
newline = input;
if (*endp)
{
setcolor(12);
printf("Sozdizimi hatasi.\n");
}
else if (input && !run_command(memory, input, &newline))
{
free_formula(input);
break;
}
setcolor(7);
if (newline)
printf("\n");
printf("> ");
free_formula(input);
}
}
void menu_equation(MENU_PARAM_DECL)
{
// col
// 31 30 29 28 27 26 25 24 23 22 21 20 19 18 27 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
// z y x w v u t s r q p o n m l k j i h g f e d c b a
printf("Denklem Cozucu\n\n");
uint32_t flags = 0;
char buffer[CON_BUFFER_SIZE], var[MAX_MATRIX_SIZE], b;
int i = -1, state = 0, rows = 0, row, col, len, cols = 0, _ = 0;
Matrix *matrix1 = NULL, *matrix2 = NULL;
float* temp, number, con[MAX_MATRIX_SIZE];
bool negative, first = true;
setcolor(8);
printf("Denklem sisteminde bulunan degisken isimlerini boslukla ayirarak giriniz.\n");
printf("Degisken isimleri kucuk harf olmalidir. (maks. %d degisken)\n\n", MAX_MATRIX_SIZE);
setcolor(7);
printf("> ");
EQU_READ_LINE;
while (++i < len)
{
b = buffer[i];
if (isspace(b))
continue;
EQ_ASSERT(islower(b), "Degisken isimleri kucuk harf olmalidir.");
if (!(flags & 1 << (b - 'a')))
{
var[cols++] = b;
EQ_ASSERT(cols < MAX_MATRIX_SIZE, "Maksimum degisken sayisi asildi.");
flags |= 1 << (b - 'a');
}
}
EQ_ASSERT(flags, "Hicbir degisken belirtilmedi.");
setcolor(8);
printf("\nHer satira bir esitlik yazin. En fazla %d esitlik yazilabilir.\n", MAX_MATRIX_SIZE);
printf("Denklem sistemini cozmek icin bos satir girin.\n\n");
setcolor(7);
printf("> ");
while (EQU_READ_LINE)
{
EQ_ASSERT(rows < MAX_MATRIX_SIZE, "Maksimum esitlik sayisi asildi.");
if (!rows)
{
EQ_ASSERT(matrix1 = mx_create_all(1, cols, 0), "Hafiza hatasi.");
}
else
{
EQ_ASSERT(temp = realloc(matrix1->data, cols * ++matrix1->rows * sizeof(float)), "Hafiza hatasi.");
matrix1->data = temp;
memset(matrix1->data + cols * rows, 0, cols * sizeof(float));
}
i = 0;
while (i < len)
{
b = buffer[i];
if (isspace(b))
{
i++;
continue;
}
switch (state)
{
case -1: //son
setcolor(12);
printf("Hata: Esitligin sag tarafi sadece bir sayidan olusmalidir.\nKarakter: %c", b);
goto end;
case 0: //opsiyonel iþaret
negative = false;
if (b == '-' || b == '+')
state = 2;
else
state = 1;
break;
case 1: //sayý ya da deðiþken
number = 1;
if (isdigit(b))
state = 3;
else if (islower(b))
state = 4;
else
{
setcolor(12);
printf("Hata: Sayi ya da degisken adi bekleniyordu.\nKarakter: %c", b);
goto end;
}
break;
case 2: //zorunlu iþaret
if (b != '-' && b != '+')
{
printf("Hata: Isaret (+ ya da -) bekleniyordu.\nKarakter: %c", b);
goto end;
}
negative = b == '-';
i++;
state = 1;
break;
case 3: //sayý
if (!(_ = sscan_ufloat(buffer + i, &number)))
{
setcolor(12);
printf("Hata: Sayi bekleniyordu.");
goto end;
}
i += _;
state = 4;
break;
case 4: //degisken
if (!islower(b))
{
setcolor(12);
printf("Hata: Degisken bekleniyordu.\nKarakter: %c", b);
goto end;
}
_ = 0;
do
{
if (var[_] == b)
break;
} while (++_ < cols);
if (_ == cols)
{
setcolor(12);
printf("Hata: %c degiskeni basta tanimlanmamis.", b);
goto end;
}
temp = matrix1->data + (matrix1->cols * rows) + _;
if (negative)
*temp -= number;
else
*temp += number;
i++;
state = 5;
break;
case 5: //iþaret ya da eþittir
if (b == '=')
state = 6;
else if (b == '-' || b == '+')
state = 2;
else
{
setcolor(12);
printf("Hata: Isaret (+ ya da -) ya da esittir (=) bekleniyordu.\nKarakter: %c", b);
goto end;
}
break;
case 6: //eþittir
if (b != '=')
{
setcolor(12);
printf("Hata: Esittir (=) bekleniyordu.\nKarakter: %c", b);
goto end;
}
i++;
state = 7;
break;
case 7: //sabit: opsiyonel iþaret
negative = false;
if (b == '-' || b == '+')
{
negative = b == '-';
i++;
}
state = 8;
break;
case 8:
if (!(_ = sscan_ufloat(buffer + i, &con[rows])))
{
setcolor(12);
printf("Hata: Sayi bekleniyordu.");
goto end;
}
if (negative)
con[rows] *= -1;
i += _;
state = -1;
break;
default:
break;
}
}
EQ_ASSERT(state == -1, "Girdiginiz denklem anlasilamadi.");
state = 0;
rows++;
printf("> ");
}
EQ_ASSERT(rows, "Hicbir denklem belirtilmedi.");
printf("\n");
EQ_ASSERT(temp = malloc(rows * sizeof(float)), "Hafiza hatasi.");
for (i = 0; i < rows; i++)
temp[i] = con[i];
matrix2 = mx_new(rows, 1, temp);
printf("\n");
EQ_ASSERT(check_system(matrix1, matrix2), "Denklem sistemi tutarsiz oldugundan cozum yok.");
setcolor(11);
for (row = 0, col = 0; col < cols; col++)
{
number = *mx_get(matrix1, row, col);
if (iszero(number))
{
printf("%c: Herhangi bir deger.\n", var[col]);
continue;
}
printf("%c = ", var[col]);
first = true;
for (i = col + 1; i < cols; i++)
{
if (iszero(number = *mx_get(matrix1, row, i)))
continue;
print_equterm(-number, var[i], first, false);
first = false;
}
print_equterm(*mx_get(matrix2, row, 0), ' ', first, true);
printf("\n");
if (++row == rows)
{
for (; ++col < cols;)
printf("%c: Herhangi bir deger.\n", var[col]);
break;
}
}
end:
mx_free(matrix1);
mx_free(matrix2);
setcolor(7);
printf("\n");
get_char();
}
void menu_file(MENU_PARAM_DECL)
{
loop_menu(&memory->menus[FILE_MENU], memory);
}
void menu_about(MENU_PARAM_DECL)
{
printf("Hakkinda\n\n");
setcolor(11);
printf("Bu yazilim Mustafa Nesin ve Cem Ufuk Yilmaz tarafindan hazirlanmistir.\n");
printf("Temel Ozellikler\n");
printf("- Temel Matematik Islemleri\n");
printf("- Cesitli Matris Islemleri (transpoz, carpma, toplama, ters, eselon form vb.)\n");
printf("- Lineer Denklem Sistemi Cozme\n");
printf("Detayli bilgi: https://github.com/MustafaNesin/LinearC\n");
get_char();
}
void menu_define(MENU_PARAM_DECL)
{
int rows, cols;
char c, name;
float* data;
printf("Matrisler | Yeni\n\n");
// Matris adý al, ayný adda varsa üzerine yazýlýp yazýlmayacaðýný sor
{
printf("Matris Adi (Buyuk harf): ");
scanl("%c", &name);
if (name < 0 || !isupper(name))
{
setcolor(12);
printf("Matris adi buyuk harf olmalidir.");
get_char();
return;
}
Node* node;
if (node = mem_query(memory, name))
{
printf("Ayni adda bir matris zaten var. Uzerine yazilsin mi? (E/H): ");
scanl("%c", &c);
if ((c | 0x20) != 'e')
{
setcolor(12);
printf("Islem iptal edildi.");
get_char();
return;
}
mem_remove(memory, node);
}
}
// Satýr ve sütun sayýsý al
{
printf("Satir Sayisi: ");
scanl("%d", &rows);
if (rows < MIN_MATRIX_SIZE || rows > MAX_MATRIX_SIZE)
{
setcolor(12);
printf("Matris satir sayisi %d ile %d arasinda olmalidir.", MIN_MATRIX_SIZE, MAX_MATRIX_SIZE);
get_char();
return;
}
printf("Sutun Sayisi: ");
scanl("%d", &cols);
if (cols < MIN_MATRIX_SIZE || cols > MAX_MATRIX_SIZE)
{
setcolor(12);
printf("Matris sutun sayisi %d ile %d arasinda olmalidir.", MIN_MATRIX_SIZE, MAX_MATRIX_SIZE);
get_char();
return;
}
}
// Matris oluþtur
{
data = calloc(rows * cols, sizeof(float));
if (!data)
{
setcolor(12);
printf("Yeni matris verisi icin bellek yeri ayirilirken hata olustu.");
get_char();
return;
}
if (!mem_new(memory, name, rows, cols, data))
{
setcolor(12);
free(data);
printf("Yeni matris icin bellek yeri ayirilirken hata olustu.");
get_char();
return;
}
}
// Matris içeriðini al;
int p = 0;
for (uint8_t i = 0, j; i < rows; i++)
for (j = 0; j < cols; j++, p++)
{
setcolor(11);
printf("%c[%d, %d] = ", name, i + 1, j + 1);
scanl("%f", data + p);
}
}
void menu_list(MENU_PARAM_DECL)
{
uint8_t count = 0;
printf("Matrisler | Liste\n\n");
setcolor(11);
printf("\tAd\tSat\tSut");
Node* node = memory->tail;
while (node)
{
printf("\n%d.\t%c\t%d\t%d", ++count, node->name, node->matrix->rows, node->matrix->cols);
node = node->prev;
}
printf("\n\n%d adet matris bulundu.", count);
get_char();
}
void menu_show(MENU_PARAM_DECL)
{
printf("Matrisler | Goruntule\n\n");
int len;
char buffer[CON_BUFFER_SIZE], name;
Node* node;
setcolor(8);
printf("Goruntulemek istediginiz matrisin adini yazin.\nCikmak icin bos satir girin.");
while (true)
{
setcolor(7);
printf("\n\n> ");
if (!(len = readl(buffer, CON_BUFFER_SIZE)))
break;
if (len == 1)
{
name = buffer[0];
if (!isupper(name))
{
setcolor(12);
printf("Matris adi buyuk harf olmalidir.");
}
else if (node = mem_query(memory, name))
{
setcolor(11);
mx_print(node->matrix);
}
else
{
setcolor(12);
printf("Matris bulunamadi.");
}
}
else
{
setcolor(12);
printf("Gecersiz giris. Yalnizca matris adi girilmelidir.");
}
}
}
void menu_delete(MENU_PARAM_DECL)
{
printf("Matrisler | Sil\n\n");
char buffer[CON_BUFFER_SIZE], name, len;
Node* node;
setcolor(8);
printf("Silmek istediginiz matrisin adini yazin.\nCikmak icin bos satir girin.");
while (true)
{
setcolor(7);
printf("\n\n> ");
if (!(len = readl(buffer, CON_BUFFER_SIZE)))
break;
if (len == 1)
{
name = buffer[0];
if (!isupper(name))
{
setcolor(12);
printf("Matris adi buyuk harf olmalidir.");
}
else if (node = mem_query(memory, name))
{
setcolor(11);
mem_remove(memory, node);
printf("Matris basariyla silindi.");
}
else
{
setcolor(12);
printf("Matris bulunamadi.");
}
}
else
{
setcolor(12);
printf("Gecersiz giris. Yalnizca matris adi girilmelidir.");
}
}
}
void menu_clear(MENU_PARAM_DECL)
{
printf("Matrisler | Hafizayi Temizle\n\n");
char c;
printf("Tanimli olan tum matrisler silinecektir, devam edilsin mi? (E/H):");
scanl("%c", &c);
if ((c | 0x20) != 'e')
{
setcolor(12);
printf("Islem iptal edildi.");
get_char();
return;
}
mem_remove_all(memory);
printf("Hafizadaki tum matrisler basariyla silindi.\n");
get_char();
}
void menu_save(MENU_PARAM_DECL)
{
printf("Dosya Islemleri | Kaydet\n\n");
int count = mem_save(memory);
if (count >= 0)
{
setcolor(10);
printf("Tanimlanmis %d matris dosyaya basariyla kaydedildi.", count);
}
else
{
setcolor(12);
printf("Tanimlanmis matrisler dosyaya kaydedilemedi.");
}
get_char();
}
void menu_read(MENU_PARAM_DECL)
{
printf("Dosya Islemleri | Yukle\n\n");
if (memory->tail)
{
char c;
printf("Mevcut tanimli matrislerin uzerine yazilacaktir, devam edilsin mi? (E/H): ");
scanl("%c", &c);
if ((c | 0x20) != 'e')
{
setcolor(12);
printf("Islem iptal edildi.");
get_char();
return;
}
}
int count = mem_read(memory);
if (count >= 0)
{
setcolor(10);
printf("%d adet matris kayit dosyasindan basariyla yuklendi.", count);
}
else
{
setcolor(12);
printf("Kayit dosyasi yok veya bozuk, matrislerin tamami okunamadi.");
}
get_char();
}