当我写:

printf("%f; ", massive[i]);


哪里:

double** InputSystemOfLinearEquationsByFile(FILE* file) {
    char* inputc = (char*)malloc(quiteenoughelements * sizeof(char));
    int thenumberofvaribles = 0;
    int thenumberofequations = 0;
    if (fscanf(file, "%s", inputc) == EOF) {
        return NULL;
    }
    thenumberofvaribles = atoi(inputc);
    if (thenumberofvaribles <= 0) {
        return NULL;
    }
    if (fscanf(file, "%s", inputc) == EOF) {
        return NULL;
    }
    thenumberofequations = atoi(inputc);
    if (thenumberofequations <= 0) {
        return NULL;
    }
double** answer = (double**)malloc(thenumberofequations*sizeof(double*));
for (int i = 0; i < thenumberofequations; i++) {
    answer[i] = (double*)malloc((thenumberofvaribles + 1)*sizeof(double));
}
for (int i = 0; i < thenumberofequations; i++) {
    for (int j = 0; j < thenumberofvaribles; j++) {
        if (fscanf(file, "%s", inputc) == EOF) {
            return NULL;
        }
        answer[i][j] = atof(inputc);
    }
}
for (int i = 0; i < thenumberofequations; i++) {
    if (fscanf(file, "%s", inputc) == EOF) {
        return NULL;
    }
    answer[i][thenumberofvaribles] = atof(inputc);
}
free(inputc);
return answer;
}

    void SwapStrokes(double** matrix, const unsigned int stroke1index, const unsigned int stroke2index) {
    double* temp = matrix[stroke1index];
    matrix[stroke1index] = matrix[stroke2index];
    matrix[stroke2index] = temp;
    temp = NULL;
}

unsigned char ZeroCheck(double** matrix, const unsigned int currentstroke, const unsigned int currentcolumn, const unsigned int numberofequations) {
    int numberofnotzerocolumn = currentstroke;
    while ((fabs(matrix[numberofnotzerocolumn][currentcolumn]) < accuracy) || (matrix[numberofnotzerocolumn][currentcolumn] != matrix[numberofnotzerocolumn][currentcolumn])) {
        numberofnotzerocolumn++;
        if (numberofnotzerocolumn == numberofequations) {
            return 1;
        }
    }
    if (numberofnotzerocolumn != currentstroke) {
        SwapStrokes(matrix, currentstroke, numberofnotzerocolumn);
    }
    return 0;
}

void JordanException(double** matrix, const unsigned int currentstroke, const unsigned int currentcolumn, const unsigned int numberofvars, const unsigned int numberofequations) {
    int optionalcolumn = 0;
    int optionalstroke = 0;
    for (optionalstroke = 0; optionalstroke < currentstroke; optionalstroke++) {
        for (optionalcolumn = 0; optionalcolumn < currentcolumn; optionalcolumn++) {
            matrix[optionalstroke][optionalcolumn] -= matrix[optionalstroke][currentcolumn] * matrix[currentstroke][optionalcolumn] / matrix[currentstroke][currentcolumn];
        }
        optionalcolumn++;
        for (optionalcolumn; optionalcolumn <= numberofvars; optionalcolumn++) {
            matrix[optionalstroke][optionalcolumn] -= matrix[optionalstroke][currentcolumn] * matrix[currentstroke][optionalcolumn] / matrix[currentstroke][currentcolumn];
        }
    }
    for (optionalstroke = currentstroke + 1; optionalstroke < numberofequations; optionalstroke++) {
        for (optionalcolumn = 0; optionalcolumn < currentcolumn; optionalcolumn++) {
            matrix[optionalstroke][optionalcolumn] -= matrix[optionalstroke][currentcolumn] * matrix[currentstroke][optionalcolumn] / matrix[currentstroke][currentcolumn];
        }
        optionalcolumn++;
        for (optionalcolumn; optionalcolumn <= numberofvars; optionalcolumn++) {
            matrix[optionalstroke][optionalcolumn] -= matrix[optionalstroke][currentcolumn] * matrix[currentstroke][optionalcolumn] / matrix[currentstroke][currentcolumn];
        }
    }
    for (optionalcolumn = 0; optionalcolumn < currentcolumn; optionalcolumn++) {
        matrix[currentstroke][optionalcolumn] /= matrix[currentstroke][currentcolumn];
    }
    optionalcolumn++;
    for (optionalcolumn; optionalcolumn <= numberofvars; optionalcolumn++) {
        matrix[currentstroke][optionalcolumn] /= matrix[currentstroke][currentcolumn];
    }
    for (optionalstroke = 0; optionalstroke < numberofequations; optionalstroke++) {
        matrix[optionalstroke][currentcolumn] = 0.;
    }
    matrix[currentstroke][currentcolumn] = 1.;
}

double* JordanMethod(double** matrix, const unsigned int numberofvars, const unsigned int numberofequations) {
    if (numberofvars > numberofequations) {
        return NULL;
    }
    else {
        unsigned int currentcolumn = 0;
        unsigned int currentstroke = 0;
        while ((currentcolumn < numberofvars) && (currentstroke < numberofequations)) {
            if (ZeroCheck(matrix, currentstroke, currentcolumn, numberofequations) == 1) {
                return NULL;
            }
            JordanException(matrix, currentstroke, currentcolumn, numberofvars, numberofequations);
            currentstroke++;
            currentcolumn++;
        }
        double* answer = (double*)malloc(numberofvars * sizeof(double));
        for (currentstroke = 0; currentstroke < numberofvars; currentstroke++) {
            if ((fabs(matrix[currentstroke][numberofvars]) < accuracy) || (matrix[currentstroke][numberofvars] != matrix[currentstroke][numberofvars])){
                matrix[currentstroke][numberofvars] = 0.;
            }
            answer[currentstroke] = matrix[currentstroke][numberofvars];
        }
        return answer;
    }
}

    void WriteMassive(double* massive, unsigned int numberofelements, FILE* file) {
    if ((massive != NULL) && (file != NULL)){
        for (unsigned int i = 0; i < numberofelements; i++) {
            fprintf(file, "%f; ", massive[i]);
        }
        fprintf(file, "\n");
    }
}

int main(int argc, char **argv)
{
    double* matrixa = NULL;
    double** matrix = NULL;
    FILE* file;
    FILE* output;
    file = fopen("File.txt", "r");
    matrix = InputSystemOfLinearEquationsByFile(file);
    matrixa = JordanMethod(matrix, 4, 4);
    WriteMassive(matrixa, 4, output);
    fclose(file);
    system("pause");
    return 0;
}


我的文件有:

4
4
2 -1 5 7
3 3,5 4 -5
-7 -3 7,2 5,3
4 3 2,1 -3,5


当我= 0时,我遇到这个错误:

> Debug Assertion Failed! Program: ...ConsoleApplication1.exe File:
> minkernel\crts\ucrt\src\appcrt\convert\cfout.cpp Line: 126 Expression:
> ("unexpected input value; log10 failed, 0)


我该怎么办?
P.s.在调试时,我大骂着进入Mass [i],Visual Studio证明它具有正常价值(大约为6.29 ...)。

最佳答案

我已经在VS 2013(而不是2015)上试用了我的代码,并且...行得通!知道它是如何工作的,也许这只是我的计算机出现问题。

关于c - 意外的输入值; log10失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36500316/

10-10 11:15