-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalkulator.cpp
230 lines (211 loc) · 4.74 KB
/
kalkulator.cpp
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
//Jan Wolski
//02.01.2021
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <cstdlib>
using std::endl;
using std::cin;
using std::cout;
auto pow(int x, int n) -> int//potegowanie
{
int wynik = 1;
for(int i = 1; i <= n; i++)
{
wynik *= x;
}
return wynik;
}
int **create()//tworzenie macierzy
{
int **matrix;
matrix = new int *[3];
for(int j=0; j < 3; j++)
{
matrix[j] = new int [3];
}
return matrix;
}
auto fillMatrix(int **matrix) -> void // wczytywanie macierzy z klawiatury
{
char it[25];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout << "Podaj: [" << i+1 << "][" << j+1 <<"] = ";
cin >> it;
matrix[i][j] = atoi(it); //atof zamienia stringa na int
}
}
}
auto freemem(int **matrix) -> void //czyszczenie pamieci po macierzach
{
for(int i = 0; i < 3; i++)
{
delete[] matrix[i];
}
delete[] matrix;
}
int **adding(int **matrixA, int **matrixB) //dodawanie
{
int **matrixC = create();
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
return matrixC;
}
auto show(int **matrix) -> void //pokazywanie macierzy
{
cout << endl;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
auto add() -> void //dodawanie
{
cout << "--Dodawanie macierzy--" <<endl;
int **A = create();
int **B = create();
cout << "Podaj liczby do macierzy A: " << endl;
fillMatrix(A);
cout << "Podaj liczby do macierzy B: " << endl;
fillMatrix(B);
int **C = adding(A, B);
show(A);
cout << " + " << endl;
show(B);
cout << " = " << endl;
show(C);
freemem(A);
freemem(B);
}
int **substraction(int **matrixA, int **matrixB) //odejmowanie macierzy
{
int **matrixC = create();
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
matrixC[i][j] = matrixA[i][j] - matrixB[i][j];
}
}
return matrixC;
}
auto sub() -> void //odejmowanie
{
cout << "--Odejmowanie macierzy--" <<endl;
int **A = create();
int **B = create();
cout << "Podaj liczby do macierzy A: " << endl;
fillMatrix(A);
cout << "Podaj liczby do macierzy B: " << endl;
fillMatrix(B);
int **C = substraction(A, B);
show(A);
cout << " - " << endl;
show(B);
cout << " = " << endl;
show(C);
freemem(A);
freemem(B);
}
int **multiplication(int **A, int **B) //mnozenie macierzy
{
int wynik = 0;
int **C = create();
for(int i = 0; i < 3; i++)//iterowanie dla m macierzy c
{
for(int g = 0; g < 3; g++)//iterowanie dla n macierzy c
{
wynik = 0;
for(int j = 0; j < 3; j++)
{
wynik += A[i][j]*B[j][g];
}
C[i][g] = wynik;
}
}
return C;
}
auto multi() -> void //mnozenie
{
int **A = create();
int **B = create();
cout << "Podaj liczyby do macierzy A:" << endl;
fillMatrix(A);
cout << "Podaj liczyby do macierzy B:" << endl;
fillMatrix(B);
cout << "------ Mnozenie macierzy --------" << endl;
show(A);
cout << endl << " * " << endl;
show(B);
int **C = multiplication(A, B);
cout << endl << " = " << endl;
show(C);
}
int **transpozycja(int **matrix) //transponowanie macierzy
{
int **trans = create();
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
trans[j][i] = matrix[i][j];
}
}
return trans;
}
auto tran() -> void //transpozycja
{
int **matrix = create();
fillMatrix(matrix);
cout << "----- Transponowanie macierzy -------" << endl;
matrix = transpozycja(matrix);
show(matrix);
}
auto main() -> int
{
short ch = 0;
do
{
cout << " KALKULATOR MACIERZY" << endl;
cout << "----------------------------------" << endl;
cout << "1 - dodawanie" << endl;
cout << "2 - odejmowanie" << endl;
cout << "3 - mnozenie" << endl;
cout << "4 - Transpozycja" << endl;
cout << "5 - koniec" << endl;
cout << "----------------------------------" << endl;
cin >> ch;
switch(ch)
{
case 1:
add();
break;
case 2:
sub(); break;
case 3:
multi();
break;
case 4:
tran();
break;
case 5:
break;
default:
cout << "Zly znak!" << endl;
}
}
while(ch!=5);
return 0;
}