Use the kramo law to solve the n-element linear equations.

Source: Internet
Author: User

Use the kramo law to solve the n-element linear equations.

Next, use the kramo rule to solve the n-element linear equations.

Code:

1 # include <iostream> 2 # include <cmath> 3 using namespace std; 4 void appDescribe (); // program description 5 double valDet (double * detPtr, int rank ); // solve the determinant 6 class LinearEquations 7 {8 public: 9 LinearEquations (); // construct the equations 10 void sol (); // solve 11 void showAns (); // display solution 12 private: 13 int rank; // Order 14 double * parPtr; // coefficient determinant 15 double * conPtr; // constant item 16 double * ansPtr; // solution 17}; 18 LinearEquations: LinearEquatio Ns () 19 {20 cout <"Number of input equations:"; 21 cin> rank; 22 int rank2 = rank * rank; 23 parPtr = new double [rank2]; 24 conPtr = new double [rank]; 25 cout <endl <"determinant of input coefficient:" <endl; 26 for (int I = 0; I <rank2; I ++) 27 cin> parPtr [I]; 28 cout <endl <"input constant:" <endl; 29 for (int I = 0; I <rank; I ++) 30 cin> conPtr [I]; 31} 32 void LinearEquations: sol () 33 {34 int rank2 = rank * rank; 35 double det = valDet (parPtr, rank); 36 ansPtr = new doub Le [rank]; 37 double * tempParptr = new double [rank2]; 38 double tempDet; 39 if (det = 0) 40 {41 ansPtr = NULL; 42 return; 43} 44 for (int I = 0; I <rank; I ++) 45 {46 for (int j = 0; j <rank2; j ++) 47 tempParptr [j] = parPtr [j]; 48 for (int j = 0; j <rank; j ++) 49 tempParptr [I + j * rank] = conPtr [j]; 50 tempDet = valDet (tempParptr, rank); 51 ansPtr [I] = tempDet/det; 52} 53} 54 void LinearEquations: showAns () 55 {56 if (ansPtr = NUL L) 57 {58 cout <"the linear equations have no or countless solutions" <endl; 59 return; 60} 61 cout <endl <"the solution of the linear equations is:" <endl; 62 for (int I = 0; I <rank; I ++) 63 cout <'X' <I + 1 <':' <ansPtr [I] <endl; 64} 65 int main () 66 {67 appDescribe (); 68 LinearEquations l1; 69 l1.sol (); 70 l1.showAns (); 71 system ("pause"); 72 return 0; 73} 74 void appDescribe () 75 {76 cout <"********************************* * ***** "<endl 77 <" this program uses the kramo law to solve the n-element linear equation. Group. "<Endl 78 <" case: "<endl 79 <" number of elements of the input equation: 4 "<endl 80 <" coefficient of input: "<endl 81 <" 1 1 1 1 "<endl 82 <" 1 2-1 4 "<endl 83 <" 2-3-1-5 "<endl 84 <" 3 1 2 11 "<endl 85 <" input constant: "<endl 86 <" 5-2-2 0 "<endl 87 <" the solution of the linear equations is: "<endl 88 <" x1: 1 "<endl 89 <" x2: 2 "<endl 90 <" x3: 3 "<endl 91 <" x4: -1 "<endl 92 <"****************************** * ******** "<endl; 93} 94 double valDet (double * detPtr, int rank) 95 {96 double val = 0; 97 if (rank = 1) return detPtr [0]; 98 for (int I = 0; I <rank; I ++) // The formula for calculating the remainder is stored in 99 {100 double * nextDetPtr = new double [(rank-1) * (rank-1)] In nextDetPtr []; 101 for (int j = 0; j <rank-1; j ++) 102 for (int k = 0; k <I; k ++) 103 nextDetPtr [j * (rank-1) + k] = detPtr [(j + 1) * rank + k]; 104 for (int j = 0; j <rank-1; j ++) 105 for (int k = I; k <rank-1; k ++) 106 nextDetPtr [j * (rank-1) + k] = detPtr [(j + 1) * rank + k + 1]; 107 val + = detPtr [I] * valDet (nextDetPtr, rank-1) * pow (-1.0, I); 108} 109 return val; 110} 111/* 112 data 2114 115 1 2116 2 1117 118 3 119 data 2: 120 4121 122 1 1 1123 1 2-1 4124 2-3-1-5125 3 1 2 11126 127 5-2 0128 */

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.