gpt4 book ai didi

c - 在没有任何可以理解的原因的情况下中止 C 中的陷阱(对我来说;))

转载 作者:行者123 更新时间:2023-11-30 20:28:14 25 4
gpt4 key购买 nike

使用命令“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")”句子?

感谢您的帮助!!

最佳答案

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

关于c - 在没有任何可以理解的原因的情况下中止 C 中的陷阱(对我来说;)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10324957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com