2016-12-01 7 views
0

Vor kurzem habe ich mich mit Programmierung beschäftigt. In meiner Schule wurde gesagt, ein Programm zu schreiben, um Systeme der linearen Gleichungen Gauß-Methode zu lösen, das ist, was ich getan habe, aber ich einen Fehler "'ABS' kann nicht als Funktion verwendet werden", bitte sagen Sie mir, wie zu beheben.Typ 'abs' kann nicht als Funktion verwendet werden

#include <iostream> 
#include <stdlib.h> 
#include <cstdlib> 
using namespace std; 
// Вывод системы уравнений 
void sysout(double **a, double *y, int n) { 
    for (int i = 0; i < n; i++) { 
    for (int j = 0; j < n; j++){ 
     cout << a[i][j] << "*x" << j; 
     if (j < n - 1) { 
     cout << " + "; 
     } 
    } 
    cout << " = " << y[i] << endl; 
    } 
    return; 
} 
double * gauss(double **a, double *y, int n) { 
    double *x, max; 
    int k, index; 
    const double eps = 0.00001; // точность 
    x = new double[n]; 
    k = 0; 
    while (k < n) { 
    // Поиск строки с максимальным a[i][k] 
     int abs; 
    max = abs(a[k][k]); 
    index = k; 
    for (int i = k + 1; i < n; i++) { 
     if (abs(a[i][k]) > max) { 
     max = abs(a[i][k]); 
     index = i; 
     } 
    } 
    // Перестановка строк 
    if (max < eps) { 
    // нет ненулевых диагональных элементов 
     cout << "Решение получить невозможно из-за нулевого столбца " ; 
     cout << index << " матрицы A" << endl; 
     return 0; 
    } 
    for (int j = 0; j < n; j++) { 
     double temp = a[k][j]; 
     a[k][j] = a[index][j]; 
     a[index][j] = temp; 
    } 
    double temp = y[k]; 
    y[k] = y[index]; 
    y[index] = temp; 
    // Нормализация уравнений 
    for (int i = k; i < n; i++) { 
     double temp = a[i][k]; 
     if (abs(temp) < eps) continue; // для нулевого коэффициента пропустить 
     for (int j = 0; j < n; j++) { 
     a[i][j] = a[i][j]/temp; 
     } 
     y[i] = y[i]/temp; 
     if (i == k) continue; // уравнение не вычитать само из себя 
     for (int j = 0; j < n; j++) { 
     a[i][j] = a[i][j] - a[k][j]; 
     } 
     y[i] = y[i] - y[k]; 
    } 
    k++; 
    } 
    // обратная подстановка 
    for (k = n - 1; k >= 0; k--) { 
    x[k] = y[k]; 
    for (int i = 0; i < k; i++) { 
     y[i] = y[i] - a[i][k] * x[k]; 
    } 
    } 
    return x; 
} 
int main() { 
    double **a, *y, *x; 
    int n; 
    system("chcp 1251>nul"); 
    system("cls"); 
    cout << "Введите количество уравнений: "; 
    cin >> n; 
    a = new double*[n]; 
    y = new double[n]; 
    for (int i = 0; i < n; i++) { 
    a[i] = new double[n]; 
    for (int j = 0; j < n; j++) { 
     cout << "a[" << i << "][" << j << "]= "; 
     cin >> a[i][j]; 
    } 
    } 
    for (int i = 0; i < n; i++) { 
    cout << "y[" << i << "]= "; 
    cin >> y[i]; 
    } 
    sysout(a, y, n); 
    x = gauss(a, y, n); 
    for (int i = 0; i < n; i++){ 
    cout << "x[" << i << "]=" << x[i] << endl; 
    } 
    cin.get(); cin.get(); 
    return 0; 
} 

Ändern Sie die Variable in "fabs" versucht, auf "std :: abs" zu ändern versucht. Compiler MiGW.

+4

Warum wollen Sie 'int abs haben,' zu Beginn der while-Schleife? – NathanOliver

+3

Entfernen Sie 'using namespace std;'. Diese Art von Fehler ist der Grund, dass Namensräume erfunden wurden. Wenn Sie 'std' wegblasen, sind Sie allein damit beschäftigt, mit den Problemen umzugehen, die Sie geschaffen haben. –

+0

Sie möchten nicht sowohl "stdlib.h" als auch "cstdlib" einschließen - sie haben die gleichen Funktionen, aber "cstdlib" hat sie im 'std'-Namespace. Siehe [diese Antwort] (http://stackoverflow.com/a/1374361/2449857) für weitere Informationen. –

Antwort

1

Wenn Sie #include <cmath> statt stdlib.h und cstdlib dann funktioniert es:

#include <iostream> 
#include <cmath> 
using namespace std; 

// Вывод системы уравнений 
void sysout(double **a, double *y, int n) { 
    ... 

Auch sollten Sie die int abs; in der while Schleife entfernen.

Ich bin mir nicht sicher, warum #include <cstdlib> hier Probleme verursachen sollte - kann jemand erklären?

Here's an online demo of the code compiling.

Verwandte Themen