Last active
August 29, 2015 14:10
-
-
Save kuzemkon/a3fa673a2feec0436004 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 0 | |
| 1 | |
| 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef KRAMER | |
| #define KRAMER | |
| enum SolutionResult { OK, NO_SOLUTION, INFINITY_SOLUTION }; | |
| inline double* cofactor(double* C, int k, int m) { | |
| int s = m - 1; | |
| double* result = new double[s*s]; | |
| for (int i = 1; i < m; i++) | |
| for (int j = 0; j < m; j++) | |
| if (j < k) | |
| result[(i - 1) * s + j] = C[i*m + j]; | |
| else if (j > k) | |
| result[i*s + j] = C[i*m + j]; | |
| return result; | |
| } | |
| inline double det(double* C, int m) | |
| { | |
| double deter = 0; | |
| for (int i = 0; i < m; i++) | |
| deter += (i % 2 ? -1 : 1) + C[m + i] * det(cofactor(C, i, m), m - 1); | |
| return deter; | |
| } | |
| inline double* substitute(double* A, double* B, int k, int m) { | |
| double* C = new double[m*m]; | |
| for (int i = 0; i < m; i++) | |
| for (int j = 0; j < m; j++) | |
| C[i*m + j] = j == k ? B[j] : A[i*m + j]; | |
| return C; | |
| } | |
| inline SolutionResult kramer(double* A, double* B, double* x) { | |
| double d, detx, dety, detz; | |
| d = det(A, 3); | |
| detx = det(substitute(A, B, 0, 3), 3); | |
| dety = det(substitute(A, B, 1, 3), 3); | |
| detz = det(substitute(A, B, 2, 3), 3); | |
| if (det == 0 && detx == 0 && dety == 0 && detz == 0) | |
| return INFINITY_SOLUTION; | |
| else if (det == 0) | |
| return NO_SOLUTION; | |
| else { | |
| x[0] = detx / d; | |
| x[1] = dety / d; | |
| x[2] = detz / d; | |
| return OK; | |
| } | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include "kramer.cpp" | |
| using namespace std; | |
| int main() { | |
| double* a = new double[3 * 3]; | |
| double* b = new double[3 * 3]; | |
| double* x = new double[3]; | |
| int i, j; | |
| for (i = 0; i < 3; i++){ | |
| for (j = 0; j < 3; j++) { | |
| cout << "Enter left side coeficient\n"; | |
| cout << "a[" << i + 1 << "," << j + 1 << "]="; | |
| cin >> a[i*3 + j]; | |
| } | |
| cout << "Enter right side coeficient\n"; | |
| cout << "b[" << i + 1 << "]="; | |
| cin >> b[i]; | |
| } | |
| cout << endl; | |
| switch (kramer(a, b, x)) | |
| { | |
| case NO_SOLUTION: | |
| cout << "The system doesn't have solution" << endl; | |
| case INFINITY_SOLUTION: | |
| cout << "The system have infinity number of solution" << endl; | |
| default: | |
| cout << endl; | |
| for (i = 0; i < 3; i++) | |
| cout << "x[" << i + 1 << "]=" << x[i] << endl; | |
| break; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment