缩短/ foreach循环

时间:2018-05-15 18:14:16

标签: c

我在网上找到了一个代码,它有这行我的编译器没有运行。我也不知道我怎么能正常写它。

G

G表示#define _sum_k(a, b, c, s) { s = 0; foreach(k, a, b) s+= c; }

我如何正常编写循环?

代码用于LU分解,这里是代码:

foreach(k, a, b)

编辑:我替换了每个宏,包括有问题的宏(使用for (int k = a; k < b; k++)),但它崩溃了,所以也许这不是它应该是的。

1 个答案:

答案 0 :(得分:2)

预处理器只是进行“搜索和替换”。

#define foreach(a, b, c) for (int a = b; a < c; a++)字面意思是foreach(x, y, z)之类的内容会被for (int x = y; x < z; x++)取代。等等。 它会为每个#define的文字执行此操作,直到不再有替换为止。

您只需通过预处理器运行程序,看看剩下的内容:

cpp myfile.c > myfile_preprocessed.c

// #include's omitted

typedef double **mat;

void mat_zero(mat x, int n) {
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
            x[i][j] = 0;
}

mat mat_new(int n) {
    mat x = malloc(sizeof(double*) * n);
    x[0] = malloc(sizeof(double) * n * n);

    for (int i = 0; i < n; i++)
        x[i] = x[0] + n * i;
    mat_zero(x, n);

    return x;
}

mat mat_copy(void *s, int n) {
    mat x = mat_new(n);
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
            x[i][j] = ((double(*)[n]) s)[i][j];
    return x;
}

void mat_del(mat x) {
    free(x[0]);
    free(x);
}

void mat_show(mat x, char *fmt, int n) {
    if (!fmt)
        fmt = "%8.4g";
    for (int i = 0; i < n; i++) {
        printf(i ? "      " : " [ ");
        for (int j = 0; j < n; j++) {
            printf(fmt, x[i][j]);
            printf(j < n - 1 ? "  " : i == n - 1 ? " ]\n" : "\n");
        }
    }
}


mat mat_mul(mat a, mat b, int n) {
    mat c = c = mat_new(n);
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++)
                c[i][j] += a[i][k] * b[k][j];
    return c;
}


void mat_pivot(mat a, mat p, int n) {
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
        {
            p[i][j] = (i == j);
        }
    for (int i = 0; i < n; i++) {
        int max_j = i;
        for (int j = i; j < n; j++)
            if (fabs(a[j][i]) > fabs(a[max_j][i]))
                max_j = j;

        if (max_j != i)
            for (int k = 0; k < n; k++) {
                { typeof(p[i][k]) tmp = p[i][k]; p[i][k] = p[max_j][k]; p[max_j][k] = tmp; };
            }
    }
}


void mat_LU(mat A, mat L, mat U, mat P, int n) {
    mat_zero(L, n);
    mat_zero(U, n);
    mat_pivot(A, P, n);

    mat Aprime = mat_mul(P, A, n);

    for (int i = 0; i < n; i++) {
        L[i][i] = 1;
    }
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
        {
            double s;
            if (j <= i) {
                { s = 0; for (int k = 0; k < j; k++) s+= L[j][k] * U[k][i]; }
                U[j][i] = Aprime[j][i] - s;
            }
            if (j >= i) {
                { s = 0; for (int k = 0; k < i; k++) s+= L[j][k] * U[k][i]; };
                L[j][i] = (Aprime[j][i] - s) / U[i][i];
            }
        }

    mat_del(Aprime);
}

double A3[][3] = { { 1, 3, 5 }, { 2, 4, 7 }, { 1, 1, 0 } };
double A4[][4] = { { 11, 9, 24, 2 }, { 1, 5, 2, 6 }, { 3, 17, 18, 1 }, { 2, 5,
        7, 1 } };

int main() {
    int n = 3;
    mat A, L, P, U;

    L = mat_new(n);
    P = mat_new(n);
    U = mat_new(n);
    A = mat_copy(A3, n);
    mat_LU(A, L, U, P, n);
    printf("A"" =");mat_show(A, 0, n);
    printf("L"" =");mat_show(L, 0, n);
    printf("U"" =");mat_show(U, 0, n);
    printf("P"" =");mat_show(P, 0, n);
    mat_del(A);
    mat_del(L);
    mat_del(U);
    mat_del(P);

    printf("\n");

    n = 4;

    L = mat_new(n);
    P = mat_new(n);
    U = mat_new(n);
    A = mat_copy(A4, n);
    mat_LU(A, L, U, P, n);
    printf("A"" =");mat_show(A, 0, n);
    printf("L"" =");mat_show(L, 0, n);
    printf("U"" =");mat_show(U, 0, n);
    printf("P"" =");mat_show(P, 0, n);
    mat_del(A);
    mat_del(L);
    mat_del(U);
    mat_del(P);

    return 0;
}

请注意用函数调用替换for循环体,结果代码可能变慢,这取决于优化器的质量和设置。