Q4: N元一次方程组(矩阵)

#include <stdio.h>
#include <math.h>
#define MAXROW 30
#define MAXCOL 31

void Print(int row, int col, double a[MAXROW][MAXCOL]){
    for (int i = 0; i < row; ++i){
        for (int j = 0; j < col; ++j){
            printf("%6.2f\t", a[i][j]);
        }
        putchar('\n');
    }
}

int Solve(int row, int col, double a[MAXROW][MAXCOL], double x[MAXROW]){
    int curRow = 0;
    while (curRow < row) {
        int i;
        for (i = curRow; i < row; i++) {
            double dividend = 0;
            int j;
            for (j = curRow; j < row; j++) {
                if (fabs(a[i][j]) < 1e-6) j++;
                else {
                    dividend = a[i][j];
                    break;
                }
            }
            for (int k = 0; k < row+1; k++) {
                if (fabs(dividend) < 1e-6) {
                    break;
                }
                else a[i][k] /= dividend;
                if (i != curRow) {
                    a[i][k] -= a[j][k];
                }
            }
        }
        curRow++;
    }
    curRow = row-1;
    while (curRow > 0) {
        for (int i = curRow-1; i >= 0; i--) {
            a[i][col-1] -= a[i][curRow] * a[curRow][col-1];
            if (!(fabs(a[curRow][curRow]) < 1e-6))a[i][curRow] = 0;
        }
        curRow--;
    }

    int zeroTimes = 0;
    for (int i = 0; i < row; i++) {
        int j = i;
        while (j < col && fabs(a[i][j]) < 1e-6) {
            j++;
        }
        if (j != i && j == col-1) {
            return 2;
        }
        else if (j == col) {
            zeroTimes++;
        }
    }
    if (zeroTimes != 0) {
        return 3;
    }
    for (int i = 0; i < row; ++i){
        x[i] = a[i][col-1];
    }
    return 1;
}

void PrintX(int row, const double x[], FILE *fp){
    fprintf(fp, "\nSolutions:\n");
    int k = 0;
    for (k = 0; k < row; ++k){
        fprintf(fp, "x%d = %.2f  ", k+1, x[k]);
    }
}

int main(){
    double a[MAXROW][MAXCOL] = {0};
    FILE *fp;

    char fname[100];
    while (1) {
        printf("Please enter path of file:");
        scanf("%s", fname);
        if ((fp = fopen(fname, "r+")) != NULL) break;
        else printf("Wrong path!\n");
    }

    int rank;
    fscanf(fp, "%d", &rank);
    fgetc(fp);
    int row = rank, col = rank+1;
    for (int i = 0; i < row; ++i){
        for (int j = 0; j < col; ++j){
            fscanf(fp, "%lf", &a[i][j]);
            if (j != col-1) fgetc(fp);
        }
    }
    double x[MAXROW];
    int result;
    result = Solve(row, col, a, x);
    if (result == 1){
        Print(row, col, a);
        PrintX(row, x, fp);
    }
    else if (result == 2) fprintf(fp, "\nNo solution.\n");
    else if (result == 3) fprintf(fp, "\nInfinite solutions.\n");
    fclose(fp);
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值