С Новым годом! Форум программистов, компьютерный форум, киберфорум
C++ Builder
Войти
Регистрация
Восстановить пароль
Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 5.00/6: Рейтинг темы: голосов - 6, средняя оценка - 5.00
0 / 0 / 0
Регистрация: 17.03.2019
Сообщений: 14

Не правильно считает произведение матриц

25.03.2019, 20:45. Показов 1138. Ответов 2
Метки нет (Все метки)

Студворк — интернет-сервис помощи студентам
Здравствуйте. Моя программа должна считать произведение и сложение 2-ух матриц. Сложение считает правильно, а с произведением что-то не то. По какой-то не ведомой мне причине, результат вычислений записывается не в нужные ячейки, подскажите пожалуйста, как это исправить.

Прикреплю код программы:
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//---------------------------------------------------------------------------
 
#include <vcl.h>
#pragma hdrstop
 
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
int i,j,n,s,**k;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button5Click(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------
 
 
//---------------------------------------------------------------------------
 
void __fastcall TForm1::ComboBox2Change(TObject *Sender)
{
    switch (ComboBox2->ItemIndex)
    {
        case 0:
        StringGrid1->RowCount = 3;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 1:
        StringGrid1->RowCount = 4;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 2:
        StringGrid1->RowCount = 5;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 3:
        StringGrid1->RowCount = 6;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }break;
 
        case 4:
        StringGrid1->RowCount = 7;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
        
        case 5:
        StringGrid1->RowCount = 8;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
    }
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::ComboBox3Change(TObject *Sender)
{
    switch (ComboBox3->ItemIndex)
    {
        case 0:
        StringGrid1->ColCount = 3;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 1:
        StringGrid1->ColCount = 4;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 2:
        StringGrid1->ColCount = 5;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 3:
        StringGrid1->ColCount = 6;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 4:
        StringGrid1->ColCount = 7;
        for(i = 1;i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 5:
        StringGrid1->ColCount = 8;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
    }
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::ComboBox4Change(TObject *Sender)
{
    switch (ComboBox4->ItemIndex)
    {
        case 0:
        StringGrid2->RowCount = 3;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 1:
        StringGrid2->RowCount = 4;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 2:
        StringGrid2->RowCount = 5;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 3:
        StringGrid2->RowCount = 6;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 4:
        StringGrid2->RowCount = 7;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
 
        case 5:
        StringGrid2->RowCount = 8;
        for(i = 1; i<8; i++)
        {
            StringGrid1->Cells[0][i] = i;
            StringGrid1->Cells[i][0] = i;
            StringGrid2->Cells[0][i] = i;
            StringGrid2->Cells[i][0] = i;
            StringGrid3->Cells[0][i] = i;
            StringGrid3->Cells[i][0] = i;
        }
        break;
    }
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::ComboBox5Change(TObject *Sender)
{
switch (ComboBox5->ItemIndex)
{
    case 0:
    StringGrid2->ColCount = 3;
    for(i = 1; i<8; i++)
    {
        StringGrid1->Cells[0][i] = i;
        StringGrid1->Cells[i][0] = i;
        StringGrid2->Cells[0][i] = i;
        StringGrid2->Cells[i][0] = i;
        StringGrid3->Cells[0][i] = i;
        StringGrid3->Cells[i][0] = i;
    }
    break;
 
    case 1:
    StringGrid2->ColCount = 4;
    for(i = 1; i<8; i++)
    {
        StringGrid1->Cells[0][i] = i;
        StringGrid1->Cells[i][0] = i;
        StringGrid2->Cells[0][i] = i;
        StringGrid2->Cells[i][0] = i;
        StringGrid3->Cells[0][i] = i;
        StringGrid3->Cells[i][0] = i;
    }
    break;
 
    case 2:
    StringGrid2->ColCount = 5;
    for(i = 1; i<8; i++)
    {
        StringGrid1->Cells[0][i] = i;
        StringGrid1->Cells[i][0] = i;
        StringGrid2->Cells[0][i] = i;
        StringGrid2->Cells[i][0] = i;
        StringGrid3->Cells[0][i] = i;
        StringGrid3->Cells[i][0] = i;
    }
    break;
 
    case 3:
    StringGrid2->ColCount = 6;
    for(i = 1; i<8; i++)
    {
        StringGrid1->Cells[0][i] = i;
        StringGrid1->Cells[i][0] = i;
        StringGrid2->Cells[0][i] = i;
        StringGrid2->Cells[i][0] = i;
        StringGrid3->Cells[0][i] = i;
        StringGrid3->Cells[i][0] = i;
    }
    break;
 
    case 4:
    StringGrid2->ColCount = 7;
    for(i = 1; i<8; i++)
    {
        StringGrid1->Cells[0][i] = i;
        StringGrid1->Cells[i][0] = i;
        StringGrid2->Cells[0][i] = i;
        StringGrid2->Cells[i][0] = i;
        StringGrid3->Cells[0][i] = i;
        StringGrid3->Cells[i][0] = i;
    }
    break;
 
    case 5:
    StringGrid2->ColCount = 8;
    for(i = 1; i<8; i++)
    {
        StringGrid1->Cells[0][i] = i;
        StringGrid1->Cells[i][0] = i;
        StringGrid2->Cells[0][i] = i;
        StringGrid2->Cells[i][0] = i;
        StringGrid3->Cells[0][i] = i;
        StringGrid3->Cells[i][0] = i;
    }
    break;
}
}
//---------------------------------------------------------------------------
 
 
void __fastcall TForm1::Button4Click(TObject *Sender)
{
    if (SaveDialog1->Execute())
    {
        int f;
        f = FileCreate(SaveDialog1->FileName);
        if (f != -1)
        {
            for (int i = 0; i < StringGrid2->RowCount; i++)
            {
                AnsiString st = StringGrid2->Rows[i]->DelimitedText + "\r\n";
                FileWrite(f, st.c_str(), st.Length());
            }
            FileClose(f);
        }
        else
        {
            ShowMessage("Ошибка доступа");
        }
    }
}
//---------------------------------------------------------------------------
 
 
void __fastcall TForm1::Button6Click(TObject *Sender)
{
    for(i = 1; i<StringGrid1->ColCount; i++)
        for(j = 1; j<StringGrid1->RowCount; j++)
            StringGrid1->Cells[i][j] = ' ';
 
    for(i = 1; i<8; i++)
    {
        StringGrid1->Cells[0][i] = i;
        StringGrid1->Cells[i][0] = i;
        StringGrid2->Cells[0][i] = i;
        StringGrid2->Cells[i][0] = i;
        StringGrid3->Cells[0][i] = i;
        StringGrid3->Cells[i][0] = i;
    }
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button7Click(TObject *Sender)
{
                for(i = 1; i<StringGrid2->ColCount; i++)
        for(j = 1; j<StringGrid2->RowCount; j++)
            StringGrid2->Cells[i][j] = ' ';
 
    for(i = 1;i<8;i++)
    {
        StringGrid1->Cells[0][i] = i;
        StringGrid1->Cells[i][0] = i;
        StringGrid2->Cells[0][i] = i;
        StringGrid2->Cells[i][0] = i;
        StringGrid3->Cells[0][i] = i;
        StringGrid3->Cells[i][0] = i;
    }
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    if (SaveDialog1->Execute())
    {
        int f;
        f = FileCreate(SaveDialog1->FileName);
        if (f != -1)
        {
            for (int i = 0; i < StringGrid1->RowCount; i++)
            {
                AnsiString st = StringGrid1->Rows[i]->DelimitedText + "\r\n";
                FileWrite(f, st.c_str(), st.Length());
            }
            FileClose(f);
        }
        else
        {
            ShowMessage("Ошибка доступа");
        }
    }
}
//---------------------------------------------------------------------------
 
int GetLine(int f, AnsiString *st)
{
    unsigned char buf [256];
    unsigned char *p = buf;
    int n;
    int len=0;
    n = FileRead(f, p, 1);
    while(n != 0)
    {
        if (*p == '\r')
        {
            n = FileRead(f, p, 1);
            break;
        }
        len++;
        p++;
        n = FileRead(f, p, 1);
    }
    *p = '\0';
    if (len != 0 )
    st->printf("%s", buf);
    return len;
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    for (int i = 0; i<StringGrid1->RowCount; i++)
    StringGrid1->Rows[i]->Clear();
    StringGrid1->RowCount = 0;
    StringGrid1->ColCount = 0;
    if (OpenDialog1->Execute())
    {
        int f;
        AnsiString st;
        bool fl = true;
        f = FileOpen(OpenDialog1->FileName, fmOpenRead);
        if(f == -1) exit;
        while(GetLine(f, &st) != 0)
        {
            if (fl == true)
            {
                for (int i = 1; i<st.Length(); i++)
                if (st[i] == ',') StringGrid1->ColCount++;
                StringGrid1->Rows[StringGrid1->Row]->DelimitedText = st;
                fl = false;
                n = StringGrid1->ColCount;
                k = new int*[n];
                for(i = 0;i<n;i++)
                    k[i] = new int[n];
            }
            else
            {
                StringGrid1->RowCount++;
                StringGrid1->Row = StringGrid1->RowCount-1;
                StringGrid1->Rows[StringGrid1->Row]->DelimitedText = st;
            }
        }
        FileClose(f);
    }
    StringGrid1->FixedCols = 1;
    StringGrid1->FixedRows = 1;
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    for (int i = 0; i<StringGrid1->RowCount; i++)
    StringGrid2->Rows[i]->Clear();
    StringGrid2->RowCount = 0;
    StringGrid2->ColCount = 0;
    if (OpenDialog1->Execute())
    {
        int f;
        AnsiString st;
        bool fl = true;
        f = FileOpen(OpenDialog1->FileName, fmOpenRead);
        if(f == -1) exit;
        while(GetLine(f,&st) != 0)
        {
            if (fl == true)
            {
                for (i = 1;i<st.Length();i++)
                if (st[i] == ',') StringGrid2->ColCount++;
                StringGrid2->Rows[StringGrid2->Row]->DelimitedText = st;
                fl = false;
                n = StringGrid2->ColCount;
                k = new int*[n];
                for(i = 0;i<n;i++)
                k[i] = new int[n];
            }
            else
            {
                StringGrid2->RowCount++;
                StringGrid2->Row = StringGrid2->RowCount-1;
                StringGrid2->Rows[StringGrid2->Row]->DelimitedText = st;
            }
        }
        FileClose(f);
    }
    StringGrid1->FixedCols = 1;
    StringGrid1->FixedRows = 1;
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::ComboBox6Change(TObject *Sender)
{
    int a1[8][8],a2[8][8],a3[8][8],h,s;
 
    switch (ComboBox6->ItemIndex)
    {
        case 0:
        {
            for(i = 1; i<StringGrid1->ColCount; i++)
            {
                for(j = 1; j<StringGrid1->RowCount; j++)
                {
                    try
                    {
                        a1[i][j] = StrToInt(StringGrid1->Cells[i][j]);
                    }
                    catch (EConvertError&)
                    {
                        ShowMessage("Вы ввели неверный символ в первую матрицу он будет заменён на сумму строки и столбца");
                        StringGrid1->Cells[i][j] = i+j;
                    }
                }
            }
 
            for(i = 1; i<StringGrid2->ColCount; i++)
            {
                for(j = 1; j<StringGrid2->RowCount; j++)
                {
                    try
                    {
                        a2[i][j] = StrToInt(StringGrid2->Cells[i][j]);
                    }
                    catch (EConvertError&)
                    {
                        ShowMessage("Вы ввели неверный символ во вторую матрицу он будет заменён на сумму строки и столбца");
                        StringGrid2->Cells[i][j] = i+j;
                    }
                }
            }
 
            {
                if (StringGrid2->ColCount != StringGrid1->ColCount
                    || StringGrid2->RowCount != StringGrid1->RowCount)
                {
                    ShowMessage("Размер матриц не совпадает");
                }
                else
                {
                    StringGrid3->ColCount = StringGrid1->ColCount;
                    StringGrid3->RowCount = StringGrid1->RowCount;
                    for(i = 1; i<StringGrid3->ColCount; i++)
                    for(j = 1; j<StringGrid3->RowCount; j++)
                    StringGrid3->Cells[i][j] = IntToStr
                    (StrToInt(StringGrid1->Cells[i][j])+StrToInt(StringGrid2->Cells[i][j]));
                }
            }
        }
        break;
 
        case 1:
        {
            for(i = 1; i<StringGrid1->ColCount; i++)
            {
                for(j = 1; j<StringGrid1->RowCount; j++)
                {
                    try
                    {
                        a1[i][j] = StrToInt(StringGrid1->Cells[i][j]);
                    }
                    catch (EConvertError&)
                    {
                        ShowMessage("Вы ввели неверный символ в первую матрицу он будет заменён на разность строки и столбца");
                        StringGrid1->Cells[i][j] = i-j;
                    }
                }
            }
 
            for(i = 1; i<StringGrid2->ColCount; i++)
            {
                for(j = 1; j<StringGrid2->RowCount; j++)
                {
                    try
                    {
                        a2[i][j] = StrToInt(StringGrid2->Cells[i][j]);
                    }
                    catch (EConvertError&)
                    {
                        ShowMessage("Вы ввели неверный символ во вторую матрицу он будет заменён на разность строки и столбца");
                        StringGrid2->Cells[i][j] = i-j;
                    }
                }
            }
 
            {
                if(StringGrid2->ColCount != StringGrid1->ColCount
                   || StringGrid2->RowCount != StringGrid1->RowCount)
                {
                    ShowMessage("Размер матриц не совпадает");
                }
                else
                {
                    StringGrid3->ColCount = StringGrid1->ColCount;
                    StringGrid3->RowCount = StringGrid1->RowCount;
                    for(i = 1; i<StringGrid3->ColCount; i++)
                        for(j = 1; j<StringGrid3->RowCount; j++)
                            StringGrid3->Cells[i][j] = IntToStr(StrToInt(StringGrid1->Cells[i][j])+(-1)*
                                                                StrToInt(StringGrid2->Cells[i][j]));
                }
            }
        }
        break;
 
        case 2:
        {
            for(i = 1;i<StringGrid1->ColCount;i++)
            {
                for(j = 1;j<StringGrid1->RowCount;j++)
                {
                    try
                    {
                        a1[i][j] = StrToInt(StringGrid1->Cells[i][j]);
                    }
                    catch (EConvertError&)
                    {
                        ShowMessage("Вы ввели неверный символ в первую матрицу он будет заменён на произведение строки и столбца");
                        StringGrid1->Cells[i][j] = i*j;
                    }
                }
            }
 
            for(i = 1; i<StringGrid2->ColCount; i++)
            {
                for(j = 1; j<StringGrid2->RowCount; j++)
                {
                    try
                    {
                        a2[i][j] = StrToInt(StringGrid2->Cells[i][j]);
                    }
                    catch (EConvertError&)
                    {
                        ShowMessage("Вы ввели неверный символ во вторую матрицу он будет заменён на произведение строки и столбца");
                        StringGrid2->Cells[i][j] = i*j;
                    }
                }
            }
 
            {
                if(StringGrid1->ColCount != StringGrid2->RowCount)
                {
                    ShowMessage("Размер матриц не совпадает");
                }
                else
                {
                    StringGrid3->ColCount = StringGrid2->ColCount;
                    StringGrid3->RowCount = StringGrid1->RowCount;
                    for(i = 1; i<StringGrid3->ColCount; i++)
                    {
                        for(j = 1; j<StringGrid3->RowCount; j++)
                        {
                            s = 0;
                            for(h = 1; h<StringGrid2->RowCount; h++)
                                s = s + StrToInt(StringGrid1->Cells[h][i])*
                                    StrToInt(StringGrid2->Cells[j][h]);
                            StringGrid3->Cells[i][j] = IntToStr(s);
                        }
                    }
                }
            }
        }
        break;
    }
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::N2Click(TObject *Sender)
{
   // Открыть текстовый файл с инфой
   ShellExecute(Handle, "open","Інструкція розробника.docx",NULL,NULL,SW_RESTORE);
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::N3Click(TObject *Sender)
{
       // Открыть текстовый файл с инфой
   ShellExecute(Handle, "open","Інструкція користувача.docx",NULL,NULL,SW_RESTORE);        
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::N1Click(TObject *Sender)
{
// Открыть текстовый файл с инфой
   ShellExecute(Handle, "open","Опис методу.docx",NULL,NULL,SW_RESTORE);
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button8Click(TObject *Sender)
{
  for(i = 1; i<StringGrid3->ColCount; i++)
        for(j = 1; j<StringGrid3->RowCount; j++)
            StringGrid3->Cells[i][j] = ' ';
 
    for(i = 1;i<8;i++)
    {
        StringGrid1->Cells[0][i] = i;
        StringGrid1->Cells[i][0] = i;
        StringGrid2->Cells[0][i] = i;
        StringGrid2->Cells[i][0] = i;
        StringGrid3->Cells[0][i] = i;
        StringGrid3->Cells[i][0] = i;
    }
}
//---------------------------------------------------------------------------
Миниатюры
Не правильно считает произведение матриц   Не правильно считает произведение матриц  
0
Лучшие ответы (1)
cpp_developer
Эксперт
20123 / 5690 / 1417
Регистрация: 09.04.2010
Сообщений: 22,546
Блог
25.03.2019, 20:45
Ответы с готовыми решениями:

C++ Не правильно считает
Помогите ничего не могу понять чтобы возвести число в степень есть же функция pow(x,y) ну вот считаем void __fastcall...

Не правильно считает тригонометрию
Не правильный вывод результата при выполнении функций. math.h - подключен void __fastcall TForm1::Button25Click(TObject *Sender) ...

Не правильно считает умножение
int palind = 0; int pr_som = 0; int vt_som = 0; for (int i = 0; i &lt; kol_pros_chis; i++) { for (int j = 0; j &lt; kol_pros_chis;...

2
place status here
 Аватар для gunslinger
3186 / 2220 / 640
Регистрация: 20.07.2013
Сообщений: 6,013
26.03.2019, 10:15
Лучший ответ Сообщение было отмечено Max12e3 как решение

Решение

Если я не путаю, в StringGrid-е идут первым индексом столбцы, а вторым строки, а в твоем массиве сначала строки, потом столбцы (или, возможно, наоборот, но мысль должна быть ясна). По сути ты транспонируешь результирующую матрицу. Поменяй индексы местами при выводе в StringGrid.
1
0 / 0 / 0
Регистрация: 17.03.2019
Сообщений: 14
26.03.2019, 13:42  [ТС]
Цитата Сообщение от gunslinger Посмотреть сообщение
Поменяй индексы местами при выводе в StringGrid.
Спасибо. Все получилось

Это
C++
1
2
3
4
for(h = 1; h<StringGrid2->RowCount; h++)
                                s = s + StrToInt(StringGrid1->Cells[h][i])*
                                    StrToInt(StringGrid2->Cells[j][h]);
                            StringGrid3->Cells[i][j] = IntToStr(s);
Поменял на
C++
1
2
3
4
for(h = 1; h<StringGrid2->RowCount; h++)
                                s = s + StrToInt(StringGrid1->Cells[h][i])*
                                    StrToInt(StringGrid2->Cells[j][h]);
                            StringGrid3->Cells[j][i] = IntToStr(s);
0
Надоела реклама? Зарегистрируйтесь и она исчезнет полностью.
raxper
Эксперт
30234 / 6612 / 1498
Регистрация: 28.12.2010
Сообщений: 21,154
Блог
26.03.2019, 13:42
Помогаю со студенческими работами здесь

Не выводит результат(. Считает все правильно проверял в консоле
//--------------------------------------------------------------------------- #include &lt;vcl.h&gt; #pragma hdrstop #include...

Не правильно считает :-(
Всем доброго времени суток!!! Неправильный подсчет суммы в Sql, что не так делаю? Если писать запрос с одно таблицей то сумма 519405,...

Не правильно считает произведения матриц
Вообщем есть код полностью готовый, только вот не правильно считает произведения матриц 2х4 * 4х3. Должно быть , а выводит: ...

Не считает произведение матриц через указатель
Суть в том, что нужно умножить две матрицы, записанные из файла, сначала обычным способом, а потом через указатели, но при умножении через...

Используя функцию произведения двух матриц, найдите произведение трех матриц А(3,4) В(4,3) С(3,3)
Используя функцию произведения двух матриц, найдите произведение трех матриц А(3,4) В(4,3) С(3,3).


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
3
Ответ Создать тему
Новые блоги и статьи
Восстановить юзерскрипты Greasemonkey из бэкапа браузера
damix 15.01.2026
Если восстановить из бэкапа профиль Firefox после переустановки винды, то список юзерскриптов в Greasemonkey будет пустым. Но восстановить их можно так. Для этого понадобится консольная утилита. . .
Изучаю kubernetes
lagorue 13.01.2026
А пригодятся-ли мне знания kubernetes в России?
Сукцессия микоризы: основная теория в виде двух уравнений.
anaschu 11.01.2026
https:/ / rutube. ru/ video/ 7a537f578d808e67a3c6fd818a44a5c4/
WordPad для Windows 11
Jel 10.01.2026
WordPad для Windows 11 — это приложение, которое восстанавливает классический текстовый редактор WordPad в операционной системе Windows 11. После того как Microsoft исключила WordPad из. . .
Classic Notepad for Windows 11
Jel 10.01.2026
Old Classic Notepad for Windows 11 Приложение для Windows 11, позволяющее пользователям вернуть классическую версию текстового редактора «Блокнот» из Windows 10. Программа предоставляет более. . .
Почему дизайн решает?
Neotwalker 09.01.2026
В современном мире, где конкуренция за внимание потребителя достигла пика, дизайн становится мощным инструментом для успеха бренда. Это не просто красивый внешний вид продукта или сайта — это. . .
Модель микоризы: классовый агентный подход 3
anaschu 06.01.2026
aa0a7f55b50dd51c5ec569d2d10c54f6/ O1rJuneU_ls https:/ / vkvideo. ru/ video-115721503_456239114
Owen Logic: О недопустимости использования связки «аналоговый ПИД» + RegKZR
ФедосеевПавел 06.01.2026
Owen Logic: О недопустимости использования связки «аналоговый ПИД» + RegKZR ВВЕДЕНИЕ Введу сокращения: аналоговый ПИД — ПИД регулятор с управляющим выходом в виде числа в диапазоне от 0% до. . .
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2026, CyberForum.ru