-2

使用命令“gcc -lm”编译以下代码没有任何问题:

/*  The code was considerable shortened for a clearer understanding     */

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h>
#include <string.h> 
#include <complex.h>

#define N       (21)            /*  Number of positions of the chain        */

double Tleft, Tright, gama, J, k, Ji, C, omega;


void rk4(double h, complex a[N], complex da[N], double b[N],double db[N],double c[N], double dc[N])
{;}

int main()
{
    double tmax, dt;                //max time and lenth of the step
    double xn[N], dxn[N], pn[N], dpn[N];
    complex alfan[N], dalfan[N];
    double tempo;
    char fileName[100];         
    FILE *outputFile;
    int i, j;
    int printEvery;


    //Asking for input

    printf("tmax = ");              //These variables are either used in main() or in rk4()
    scanf("%lf", &tmax);
    printf("%f\n", tmax);
    printf("dt = ");
    scanf("%lf", &dt);
    printf("%f\n", dt);
    printf("Tleft = ");
    scanf("%lf", &Tleft);
    printf("%f\n", Tleft);
    printf("Tright = ");
    scanf("%lf", &Tright);
    printf("%f\n", Tright);
    printf("gamma = ");
    scanf("%lf", &gama);
    printf("%f\n", gama);
    printf("J = ");
    scanf("%lf", &J);
    printf("%f\n", J);
    printf("k = ");
    scanf("%lf", &k);
    printf("%f\n", k);
    printf("Ji = ");
    scanf("%lf", &Ji);
    printf("%f\n", Ji);
    printf("C = ");
    scanf("%lf", &C);
    printf("%f\n", C);
    printf("omega = ");
    scanf("%lf", &omega);
    printf("%f\n", omega);
    printf("print every ");
    scanf("%d", &printEvery);
    printf("%d\n", printEvery);
printf("Print this");

    for (i = 1; i<=N; i++)          //Initializing vectors
    {
        alfan[i] = 0.0+0.0i;
        dalfan[i] = 0.0+0.0i;
        xn[i] = 0.0;
        dxn[i] = 0.0;
        pn[i] = 0.0;
        dpn[i] = 0.0;
    }

    tempo = 0.0;                    //Initializing time

    //Initializing the file and printing its column titles

    sprintf(fileName, "tmax=%.4f, dt=%.4f, Tleft=%.4f, Tright=%.4f, gamma=%.4f, J=%.4f, k=%.4f, Ji=%.4f, C=%.4f, omega=%.4f, every%d.dat", tmax, dt, Tleft, Tright, gama, J, k, Ji, C, omega, printEvery);
    outputFile = fopen(fileName, "w");
    fprintf(outputFile, "tempo\t\t");
    for (i = 1; i<=N; i++)                              //alfas
    {
        fprintf(outputFile, "realfa[%d]\timalfa[%d]\t", i, i);
    }
    for (i = 1; i<=N; i++)                              //x's
    {
        fprintf(outputFile, "x[%d]\t\t", i);
    }
    for (i = 1; i<=N-1; i++)                            //p's
    {
        fprintf(outputFile, "p[%d]\t\t", i);
    }
    fprintf(outputFile, "p[%d]\n\n", i);

    j = 0;

    while (tempo < tmax)            
    {
        if (j % imprimirCada == 0)
        {
            fprintf(outputFile, "%f\t", tempo);                     //tempo
            for (i = 1; i<=N; i++)                          //alfas
            {
                fprintf(outputFile, "%f\t%f\t", creal(alfan[i]), cimag(alfan[i]));
            }
            for (i = 1; i<=N; i++)                          //x's
            {
                fprintf(outputFile, "%f\t", xn[i]);
            }
            for (i = 1; i<=N-1; i++)                        //p's
            {
                fprintf(outputFile, "%f\t", pn[i]);
            }
            fprintf(outputFile, "%f\n", pn[i]);

        }

        j++;
        rk4(dt, alfan, dalfan, xn, dxn, pn, dpn);
        tempo += dt;
    }

    printf("\n");

    return 0;       
}

当我运行它时,我得到:

tmax = 1 
1.000000
dt = 0.1
0.100000
Tleft = 0.1
0.100000
Tright = 0.2
0.200000
gamma = 0.5
0.500000
J = 0.001
0.001000
k = 0.001
0.001000
Ji = 0.001
0.001000
C = 0.001
0.001000
omega = 0.01
0.010000
print every 1
1
Abort trap

是什么导致了“中止陷阱”,为什么我没有得到最后一个 'printf("Print this")' 句子?

谢谢您的帮助!!

4

2 回答 2

1

您看不到 ,Print this因为您没有在其上包含换行符,因此它位于行缓冲区中。我建议使用调试器来追踪陷阱。

于 2012-04-25T23:04:30.197 回答
0

C 中的数组是 0 索引的,因此 xn[N] (除其他外)的索引从 0 .. N-1 运行,但您从 1 .. N 循环。

于 2012-04-25T23:06:31.310 回答