2012-04-25 67 views
-2

下面的代碼編譯而不與命令「的gcc -lm」任何問題:中止陷阱用C沒有任何可理解原因(我;))

/* 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(‘打印’)」一句?

謝謝您的幫助!

+0

爲什麼你使用'%lf'讀取輸入,但用'%f'打印相同的變量? 'printf()','%f'和'%lf'等於 – sarnold 2012-04-25 23:01:34

+0

是等價的(對於varags,'float'被提升爲'double')。 'scanf()'需要'%lf',因爲它需要一個指針。 – FatalError 2012-04-25 23:05:11

+0

這個問題似乎在'sprintf'句子上。事情是,它可以在與我的不同的計算機上運行... – Benjamin 2012-04-26 00:19:46

回答

1

你看不到Print this因爲你沒有包括它換行,因此它坐在線緩衝。我建議使用調試器來追蹤陷阱。

+0

或者你可以看看明顯的代碼。 – 2012-04-25 23:06:54

0

數組在C的0指數的,所以XN指數[N](對等)從0 ... N-1,但你從1循環.. N.