mortezasadeghib
چهارشنبه 20 خرداد 1388, 12:37 عصر
syntax برنامه ام را چگونه رفع کنم؟ فوریییییییییییییییییییییی یییییییییییییییییییییییی.. ....
#include <iostream>
using namespace std;
int n;
double **eq;
// this function gets the equations from user
void readEquations()
{
// read the number of equations
cout << "n (number of unknowns/equations): ";
cin >> n;
// declare the equations matrix
eq = new double*[n];
for (int i = 0; i < n; i++)
eq[i] = new double[n+1];
// read the equations
for (i = 0; i < n; i++)
{
cout << "- Equation " << i+1 << ": " << "\n";
for (int j = 0; j < n; j++)
{
cout << "\t x" << j << ": ";
cin >> eq[i][j];
}
cout << "\t equals to: ";
cin >> eq[i][n];
}
}
// this function prints out the created matrix
void printMatrix()
{
for (int i = 0; i < n; i++)
{
cout << "\t";
for (int j = 0; j < n; j++)
cout << eq[i][j] << "\t";
cout << "|\t" << eq[i][n] << endl;
}
cout << "\n";
}
// this method solves the system
bool gauss(int index=0)
{
int i;
// make sure this is not a sign of unsolvable system
bool zero = true;
for (i = 0; i < n; i++)
if (eq[index][i] != 0)
{
zero = false;
break;
}
// if it was unsolvabe, return false
if (zero)
{
cout << "Ooops! The " << index+1 << "th equation is invalid!\n";
return false;
}
// make sure the index x in this row is not zero
if (eq[index][index] == 0)
{
// find an equation in which the index is not zero
for (i = index+1; i < n; i++)
if (eq[i][index] != 0)
{
// swip the equations
for (int j = 0; j < n+1; j++)
{
double temp = eq[index][j];
eq[index][j] = eq[i][j];
eq[i][j] = temp;
}
break;
}
// if the problem still insists, then the system cannot be solved
if (eq[index][index] == 0)
return false;
cout << "Swapping two rows...\n";
printMatrix();
}
// make index 1
if (eq[index][index] != 1)
{
for (i = n; i >= index; i--)
eq[index][i] /= eq[index][index];
cout << "Making index of the " << index+1 << "th equation to be 1...\n";
printMatrix();
}
// make remaining underneath x's to be 0
if (index+1 < n)
{
for (i = index+1; i < n; i++)
{
double coef = eq[i][index];
for (int j = index; j <= n; j++)
eq[i][j] -= coef*eq[index][j];
}
cout << "Making underneath x's to be 0...\n";
printMatrix();
}
// go one level further
if (index+1 < n)
if (!gauss(index+1))
return false;
// now, make all remaining non-zero x's to be 0
if (index+1 < n)
{
for (i = index+1; i < n; i++)
if (eq[index][i] != 0)
{
eq[index][n] -= eq[index][i]*eq[i][n];
eq[index][i] -= eq[index][i]*eq[i][i];
}
cout << "Substituting for the solved ones...\n";
printMatrix();
}
return true;
}
// the main method
void main()
{
// read the equations
readEquations();
// print out the matrix
cout << "\n";
cout << "Matrix of the given equation is:\n";
printMatrix();
// solve the system
bool solved = gauss();
// print out the results
cout << "\n";
cout << "The calculation result is:\n";
if (!solved)
cout << "THE SYSTEM DOES NOT HAVE A SOLUTION!\n";
else
for (int i = 0; i < n; i++)
cout << "\t x" << i << ": " << eq[i][n] << "\n";
cout << "\n";
}
#include <iostream>
using namespace std;
int n;
double **eq;
// this function gets the equations from user
void readEquations()
{
// read the number of equations
cout << "n (number of unknowns/equations): ";
cin >> n;
// declare the equations matrix
eq = new double*[n];
for (int i = 0; i < n; i++)
eq[i] = new double[n+1];
// read the equations
for (i = 0; i < n; i++)
{
cout << "- Equation " << i+1 << ": " << "\n";
for (int j = 0; j < n; j++)
{
cout << "\t x" << j << ": ";
cin >> eq[i][j];
}
cout << "\t equals to: ";
cin >> eq[i][n];
}
}
// this function prints out the created matrix
void printMatrix()
{
for (int i = 0; i < n; i++)
{
cout << "\t";
for (int j = 0; j < n; j++)
cout << eq[i][j] << "\t";
cout << "|\t" << eq[i][n] << endl;
}
cout << "\n";
}
// this method solves the system
bool gauss(int index=0)
{
int i;
// make sure this is not a sign of unsolvable system
bool zero = true;
for (i = 0; i < n; i++)
if (eq[index][i] != 0)
{
zero = false;
break;
}
// if it was unsolvabe, return false
if (zero)
{
cout << "Ooops! The " << index+1 << "th equation is invalid!\n";
return false;
}
// make sure the index x in this row is not zero
if (eq[index][index] == 0)
{
// find an equation in which the index is not zero
for (i = index+1; i < n; i++)
if (eq[i][index] != 0)
{
// swip the equations
for (int j = 0; j < n+1; j++)
{
double temp = eq[index][j];
eq[index][j] = eq[i][j];
eq[i][j] = temp;
}
break;
}
// if the problem still insists, then the system cannot be solved
if (eq[index][index] == 0)
return false;
cout << "Swapping two rows...\n";
printMatrix();
}
// make index 1
if (eq[index][index] != 1)
{
for (i = n; i >= index; i--)
eq[index][i] /= eq[index][index];
cout << "Making index of the " << index+1 << "th equation to be 1...\n";
printMatrix();
}
// make remaining underneath x's to be 0
if (index+1 < n)
{
for (i = index+1; i < n; i++)
{
double coef = eq[i][index];
for (int j = index; j <= n; j++)
eq[i][j] -= coef*eq[index][j];
}
cout << "Making underneath x's to be 0...\n";
printMatrix();
}
// go one level further
if (index+1 < n)
if (!gauss(index+1))
return false;
// now, make all remaining non-zero x's to be 0
if (index+1 < n)
{
for (i = index+1; i < n; i++)
if (eq[index][i] != 0)
{
eq[index][n] -= eq[index][i]*eq[i][n];
eq[index][i] -= eq[index][i]*eq[i][i];
}
cout << "Substituting for the solved ones...\n";
printMatrix();
}
return true;
}
// the main method
void main()
{
// read the equations
readEquations();
// print out the matrix
cout << "\n";
cout << "Matrix of the given equation is:\n";
printMatrix();
// solve the system
bool solved = gauss();
// print out the results
cout << "\n";
cout << "The calculation result is:\n";
if (!solved)
cout << "THE SYSTEM DOES NOT HAVE A SOLUTION!\n";
else
for (int i = 0; i < n; i++)
cout << "\t x" << i << ": " << eq[i][n] << "\n";
cout << "\n";
}