#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
const int SIZE = 6;
void Equation(string line, double coefRow[], double& coefB, string& coefVal){
int len = int(line.length());
bool firstItem = true;
int j=0, m=0;
coefVal = " ";
string eq = "", item="";
for (int i = 0; i < len; i++){
if (line[i] == '+' || line[i] == '-'){
if (isalpha(line[i + 1])){
coefVal += line[i + 1];
coefRow[j++] = (line[i] == '+') ? 1. : -1.;
i++;
continue;
}
}
if (line[i] == '='){
coefRow[j++] = atof(item.c_str()); item = "";
for (int l = i + 1; l < len; l++)
item += line[l];
coefB = atof(item.c_str()); break;
}
if (isalpha(line[i])){
if (firstItem){ firstItem = false;
coefRow[j++] = 1.;
}
else coefRow[j++] = atof(item.c_str()); item = "";
m = 0;
coefVal += line[i];
firstItem = false;
continue;
}
if (m>8)cerr << "The maximum length of coefficients >8\n";
item += line[i];
m++;
firstItem = false;
}
}
double Determinant(double A[SIZE][SIZE], int size){
double res = 0;
if (size == 2)return A[1][1]; if (size == 3)return A[1][1] * A[2][2] - A[2][1] * A[1][2];
int newSize = size - 1;
double NewA[SIZE][SIZE];
for (int i = 1; i<size;> for (int j = 1; j < newSize; j++){
for (int k = 1; k < newSize; k++)
if (k >= i) NewA[j][k] = A[j+ 1][k + 1];
else NewA[j][k] = A[j + 1][k ];
}
int p = (i % 2) ? 1:-1;
res += p * A[1][i] * Determinant(NewA, newSize); }
return res;
}
void Solve(double A[SIZE][SIZE], double b[SIZE], double sol[SIZE], int size){
double temp[SIZE][SIZE], det;
det = Determinant(A, size);
for (int j = 1; j < size; j++){
for (int k = 1; k < size; k++)
for (int l = 1; l < size; l++)
temp[k][l] = A[k][l];
for (int i = 1; i < size; i++)
temp[i][j] = b[i];
sol[j] = Determinant(temp, size) / det;
}
}
int main(){
string row, coefVals;
double coefRow[SIZE], coefB;
int i, j, size;
double det;
double A[SIZE][SIZE], b[SIZE], sol[SIZE];
do{
cout << "Number of Input equations (minimum 1 ~ maximum 5): ";
cin >> size;
} while (size < 1 || size > 5);
cout << "Please, enter the " << size++ << " equations \n"
<< "(Equations to be entered as strings, without spaces)\n";
for (i = 1; i < size; i++){
cin >> row; Equation(row, coefRow, coefB, coefVals);
for (j = 1; j < size; j++)
A[i][j] = coefRow[j - 1];
b[i] = coefB;
}
cout << "\nA: \n";
for (i = 1; i < size; i++){
for (j = 1; j < size; j++)
cout << A[i][j] << " ";
cout << endl;
}
cout << "\nb: \n";
for (i = 1; i < size; i++)
cout << b[i] << endl;
cout << "\nVariables: \n";
for (i = 1; i < size; i++)
cout << coefVals[i] << endl;
cout << "\nDeterminant= ";
det = Determinant(A, size);
cout << det;
if (det != 0){
Solve(A, b, sol, size);
cout << "\nSolution: \n";
for (i = 1; i < size; i++)
cout << sol[i] << endl;
}
else
cout << "\nSystem has not solution ";
cin.get();
cin.get();
return 0;
}
What I have tried:
Its my final project and i couldn't change it to c its so difficult so if any one can help