Il programma è un risolutore di un sistema di 2/3 equazioni e 2/3 incognite, l'inserimento è già in forma semplice.
Ho applicato il metodo di Cramer ma non mi funziona, neanche mi identifica l'errore, provato a rimuovere i controlli per diminuire la possibilità di errori ma niente.
Se qualcuno gentilente mi puo' trovare l'errore ne son grato:
Ho applicato il metodo di Cramer ma non mi funziona, neanche mi identifica l'errore, provato a rimuovere i controlli per diminuire la possibilità di errori ma niente.
Se qualcuno gentilente mi puo' trovare l'errore ne son grato:
C++:
#include <iostream>
#include <cctype>
#define m 100
using namespace std;
//Prototipi
void input(int coeff[][m][m], char eq[], int dim[], int type[]); //
void insert(char eq[], int dim[], int d); //
bool ctrl(char eq[], int dim[], bool test, int d, int type[]); //
bool elab(int coeff[][m][m], int type[], int ris[], bool t); //
void concat(int coeff[][m][m]); //
void output(int ris[], bool t, int type[]); //
int main()
{
int coeff[m][m][m], dim[m], type[m], ris[m];
char eq[m];
bool t = true;
//type[0] = 2; // N incognite
cout << "[Risolutore sistemi di equazioni (2/3 incognite | 2/3 equazioni)]" << endl;
input(coeff, eq, dim, type);
t = elab(coeff, type, ris, t);
output(ris, t, type);
system("pause");
return 0;
}
void input(int coeff[][m][m], char eq[], int dim[], int type[])
{
int scelta;
int d = 0, n = 0, c = 0, x = 0;
bool test = true;
cout << "Inserimento equazioni: " << endl;
cout << "-No spazi" << endl;
cout << "-In questi due modi \n Ax+By+Cz=D\n Ax+By=C" << endl << endl;
cout << "2 equazioni & 2 incognite || 3 equazioni & 3 incognite? [1-2]";
do
cin >> scelta;
while (scelta < 1 || scelta > 2);
//do
//{
cout << "1) ";
insert(eq, dim, d);
test = ctrl(eq, dim, test, d, type);
//} while (test);
for (int i = 0; i < dim[d]; i++)
{
if (eq[i] >= 48 && eq[i] <= 57) //if(eq[i] >= '0' && eq[i] <= '9')
{
coeff[d][n][x] = eq[i] - 48;
x++;
}
else
{
coeff[d][n][x] = '\0';
n++;
x = 0;
}
//if (eq[i] >= 97 && eq[i] <= 122)
// c++;
}
//if (c == 3) type[0]++;
d++; n = 0; //c = 0;
//do
//{
cout << "2) ";
insert(eq, dim, d);
test = ctrl(eq, dim, test, d, type);
//for (int i = 0; i < dim[d]; i++)
// if (eq[i] >= 97 && eq[i] <= 122)
// c++;
//if (c == 3) test = true;
//c = 0;
//} while (test);
for (int i = 0; i < dim[d]; i++)
{
if (eq[i] >= 48 && eq[i] <= 57) //if(eq[i] >= '0' && eq[i] <= '9')
{
coeff[d][n][x] = eq[i] - 48;
x++;
}
else
{
coeff[d][n][x] = '\0';
n++;
x = 0;
}
}
d++; n = 0;
if (scelta == 2)
{
//do
//{
cout << "3) ";
insert(eq, dim, d);
test = ctrl(eq, dim, test, d, type);
//for (int i = 0; i < dim[d]; i++)
// if (eq[i] >= 97 && eq[i] <= 122)
// c++;
//if (c == 3) test = true;
//c = 0;
//} while (test);
for (int i = 0; i < dim[d]; i++)
{
if (eq[i] >= 48 && eq[i] <= 57) //if(eq[i] >= '0' && eq[i] <= '9')
{
coeff[d][n][x] = eq[i] - 48;
x++;
}
else
{
coeff[d][n][x] = '\0';
n++;
x = 0;
}
}
}
}
void insert(char eq[], int dim[], int d)
{
cin.getline(eq, m);
dim[d] = strlen(eq);
eq[dim[d] + 1] = '\0';
}
bool ctrl(char eq[], int dim[], bool test, int d, int type[])
{
for (int i = 0; i < dim[d]; i++)
if (eq[i] == 'X' || eq[i] == 'Y' || eq[i] == 'Z')
eq[i] = tolower(eq[i]);
/*
for (int i = 0; i < dim[d]; i++)
{
if (type[0] == 2)
{
if (eq[i] != '=' || eq[i] != '+' || eq[i] != '-' || eq[i] != 'x' || eq[i] != 'y')
test = false;
}
else
{
if (eq[i] != '=' || eq[i] != '+' || eq[i] != '-' || eq[i] != 'x' || eq[i] != 'y' || eq[i] != 'z')
test = false;
}
}
*/
return test;
}
bool elab(int coeff[][m][m], int type[], int ris[], bool t)
{
int c = 0;
int d = 0, dx = 0, dy = 0, dz = 0;
concat(coeff);
// ris[0] = x; ris[1] = y; ris[2] = z;
if (type[0] == 2)
{
d = (coeff[0][0][c] * coeff[1][1][c]) - (coeff[1][0][c] * coeff[0][1][c]);
if (d == 0)
{
t = false;
}
else
{
dx = (coeff[0][2][c] * coeff[1][1][c]) - (coeff[1][2][c] * coeff[0][1][c]);
dy = (coeff[0][0][c] * coeff[1][2][c]) - (coeff[1][0][c] * coeff[0][2][c]);
ris[0] = dx / d;
ris[1] = dy / d;
}
}
else
{
d = (coeff[0][0][c] * coeff[1][1][c] * coeff[2][2][c]) + (coeff[0][1][c] * coeff[1][2][c] * coeff[2][0][c]) +
(coeff[0][2][c] * coeff[1][0][c] * coeff[2][1][c]) - (coeff[0][2][c] * coeff[1][1][c] * coeff[2][0][c]) -
(coeff[0][1][c] * coeff[1][0][c] * coeff[2][2][c]) - (coeff[0][0][c] * coeff[1][2][c] * coeff[2][1][c]);
if (d == 0)
{
t = false;
}
else
{
dx = (coeff[0][3][c] * coeff[1][1][c] * coeff[2][2][c]) + (coeff[0][1][c] * coeff[1][2][c] * coeff[2][3][c]) +
(coeff[0][2][c] * coeff[1][3][c] * coeff[2][1][c]) - (coeff[0][2][c] * coeff[1][1][c] * coeff[2][3][c]) -
(coeff[0][1][c] * coeff[1][3][c] * coeff[2][2][c]) - (coeff[0][3][c] * coeff[1][2][c] * coeff[2][1][c]);
dy = (coeff[0][0][c] * coeff[1][3][c] * coeff[2][2][c]) + (coeff[0][3][c] * coeff[1][2][c] * coeff[2][0][c]) +
(coeff[0][2][c] * coeff[1][0][c] * coeff[2][3][c]) - (coeff[0][2][c] * coeff[1][3][c] * coeff[2][0][c]) -
(coeff[0][3][c] * coeff[1][0][c] * coeff[2][2][c]) - (coeff[0][0][c] * coeff[1][2][c] * coeff[2][3][c]);
dz = (coeff[0][0][c] * coeff[1][1][c] * coeff[2][3][c]) + (coeff[0][1][c] * coeff[1][3][c] * coeff[2][0][c]) +
(coeff[0][3][c] * coeff[1][0][c] * coeff[2][1][c]) - (coeff[0][3][c] * coeff[1][1][c] * coeff[2][0][c]) -
(coeff[0][1][c] * coeff[1][0][c] * coeff[2][3][c]) - (coeff[0][0][c] * coeff[1][3][c] * coeff[2][1][c]);
ris[0] = dx / d;
ris[1] = dy / d;
ris[2] = dz / d;
}
}
return t;
}
void concat(int coeff[][m][m])
{
int i = 0;
for (int z = 0, k = 0; coeff[z][k][i] != '\0'; z++)
{
k = 0;
i = 0;
for (; coeff[z][k][0] != '\0'; k++)
{
for (; coeff[z][k][i] != '\0'; i++);
for (int j = 0, p = i; j < i; j++)
coeff[z][k][0] += pow(coeff[z][k][j], p);
}
}
}
void output(int ris[], bool t, int type[])
{
if (t)
{
if (type[0] == 2)
cout << endl << "Risultato: (x ; y) => (" << ris[0] << " ; " << ris[1] << ")" << endl << endl;
else
cout << endl << "Risultato: (x ; y ; z) => (" << ris[0] << " ; " << ris[1] << " ; " << ris[2] << ")" << endl << endl;
}
else
{
cout << endl << "Sistema Indeterminato o Impossibile" << endl << endl;
}
}