Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 结构变量打印_C_Printing_Struct - Fatal编程技术网

C 结构变量打印

C 结构变量打印,c,printing,struct,C,Printing,Struct,我正在写一个遗传算法,我在打印一些结构变量时遇到了一个问题。我知道代码能够正确地读取它们,因为我得到了一个结果,但当我试图在屏幕上或文件中打印它们时,结果是零 这是我正在使用的代码,你能帮我一下吗 #include <stdio.h> #include <stdlib.h> #include <math.h> #define POPSIZE 50 #define MAXGENS 1000 #define NVARS 3 #define PXOVER 0.8

我正在写一个遗传算法,我在打印一些结构变量时遇到了一个问题。我知道代码能够正确地读取它们,因为我得到了一个结果,但当我试图在屏幕上或文件中打印它们时,结果是零

这是我正在使用的代码,你能帮我一下吗

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

#define POPSIZE 50
#define MAXGENS 1000
#define NVARS 3
#define PXOVER 0.8
#define PMUTATION 0.15
#define B 2
#define TOURNAMENT_SIZE 2
#define TRUE 1
#define FALSE 0

int generation ;
int cur_best;
FILE *galog_tournament;

struct genotype
{
    double gene[NVARS];
    double fitness;
    double upper[NVARS];
    double lower[NVARS];
    double cfitness;
    double rfitness;
};

struct genotype population[POPSIZE+1];
struct genotype newpopulation[POPSIZE+1];


void initialize(void);
double randval(double,double);
void evaluate(void);
void keep_the_best(void);
void elitist(void);
void tournament_selection(void);
void crossover(void);
void random_Xover(int,int);
void swap(double *,double *);
void non_uniform_mutation(void);
void report(void);
double delta2(int,int);


void initialize(void)
{
    FILE *infile;
    FILE *bounds;
    FILE *popu;
    int i,j;
    double lbound,ubound;

    bounds = fopen("bounds.txt","w");
    popu = fopen("population.txt","w");


    if((infile = fopen("gadata.txt","r"))== NULL)
    {
        fprintf(galog_tournament,"\n Cannot open input file\n");
        exit(1);
    }


    for(i = 0; i < NVARS; i++)
    {
        fscanf(infile, "%lf",&lbound);
        fscanf(infile, "%lf",&ubound);

        printf("%lf  %lf\n",lbound,ubound);

        for(j = 0; j < NVARS; j++)
            {
                population[j].fitness = 0;
                population[j].rfitness = 0;
                population[j].cfitness = 0;
                population[j].lower[i] = lbound;
                population[j].upper[i] = ubound;

                fprintf(bounds,"%lf  %lf\n",&population[j].lower[i],&population[j].upper[i]);

                population[j].gene[i] = randval(population[j].lower[i],population[j].upper[i]);

                fprintf(popu,"%lf\n",population[j].gene[i]);
            }

    }
    fclose(infile);
    fclose(bounds);
    fclose(popu);
}
#包括
#包括
#包括
#定义POPSIZE 50
#定义MAXGENS 1000
#定义NVARS 3
#定义PXOVER 0.8
#定义置换0.15
#定义b2
#定义尺寸2
#定义真1
#定义FALSE 0
整数代;
int cur_最佳;
文件*galog_锦标赛;
结构基因型
{
双基因;
双重健身;
双上[NVAR];
双下[NVAR];
双重性;
双重性;
};
结构基因型群体[POPSIZE+1];
结构基因型新群体[POPSIZE+1];
无效初始化(void);
双兰德瓦尔(双,双);
无效评估(void);
虚空保持最佳状态(虚空);
虚空精英(void);
无效选择(无效);
无效交叉(void);
无效随机变量(int,int);
无效掉期(双倍*,双倍*);
void非均匀变异(void);
作废报告(作废);
双delta2(int,int);
void初始化(void)
{
文件*填充;
文件*边界;
文件*popu;
int i,j;
双耳束,双耳束;
bounds=fopen(“bounds.txt”,“w”);
popu=fopen(“population.txt”,“w”);
如果((infle=fopen(“gadata.txt”,“r”))==NULL)
{
fprintf(galog_锦标赛,“\n无法打开输入文件\n”);
出口(1);
}
对于(i=0;i
伙计们,非常感谢你们的帮助,实际上我试着调试代码并对其进行一些修改,以便在屏幕上打印值。。不幸的是,我仍然得到零分。。。!!这是带有上下限的代码和我的输入文件

void initialize(void)
{
    FILE *infile;
    FILE *test;
    int i,j;
    double lbound,ubound;

    test = fopen("test.txt","w");



    if((infile = fopen("gadata.txt","r"))== NULL)
    {
        fprintf(galog_tournament,"\n Cannot open input file\n");
        exit(1);
    }


    for(i = 0; i < NVARS; i++)
    {
        fscanf(infile, "%lf",&lbound);
        fscanf(infile, "%lf",&ubound);

         printf("%lf  %lf\n",lbound,ubound);


    for (j=0; j < POPSIZE; j++) {
        population[j].fitness = 0;
        population[j].rfitness = 0;
        population[j].cfitness = 0;
        population[j].lower[i] = lbound;
        population[j].upper[i] = ubound;
        population[j].gene[i] = randval(population[j].lower[i],
                                        population[j].upper[i]);






     }


  }

    fclose(infile);
    fclose(test);

}






   0.000000 0.000000
    0.000514    0.010514
    0.011307    0.021307
    0.021876    0.031876
    0.033994    0.043994
    0.043272    0.053272
    0.050229    0.060229
    0.053976    0.063976
    0.053803    0.063803
    0.046640    0.056640
    0.029907    0.039907
    0.017619    0.027619
    0.002316    0.012316
    -0.005428   0.004572
    -0.00683    0.00317
    -0.009743   0.000257
    -0.10582    -0.09582
    -0.026304   -0.016304
    -0.027064   -0.017064
    -0.025243   -0.015243
    -0.022386   -0.012386
    -0.019108   -0.009108
    -0.015788   -0.005788
    -0.012185   -0.002185
    -0.009452   0.000548
    -0.052133   0.002867
    -0.006128   0.003872
    0.000000    0.000000
void初始化(void)
{
文件*填充;
文件*测试;
int i,j;
双耳束,双耳束;
test=fopen(“test.txt”、“w”);
如果((infle=fopen(“gadata.txt”,“r”))==NULL)
{
fprintf(galog_锦标赛,“\n无法打开输入文件\n”);
出口(1);
}
对于(i=0;i

fprintf(bounds,"%lf  %lf\n",&population[j].lower[i],&population[j].upper[i]);
因此,它变成:

fprintf(bounds,"%lf  %lf\n",population[j].lower[i],population[j].upper[i]);
您希望打印变量的值,而不是该变量的地址


将编译器设置为显示警告,在这种情况下,它会告诉您有关问题的信息。

从中删除
&
s

fprintf(bounds,"%lf  %lf\n",&population[j].lower[i],&population[j].upper[i]);
因此,它变成:

fprintf(bounds,"%lf  %lf\n",population[j].lower[i],population[j].upper[i]);
您希望打印变量的值,而不是该变量的地址


将编译器设置为显示警告,在这种情况下,它会告诉您有关问题的信息。

如果fScanf没有读取任何内容并返回0,则可能不希望代码运行。你为什么不改成这样

while( 0 != fscanf(infile, "%lf",&ubound))
{
    if(0 == fscanf(infile, "%lf",&lbound) || i >= NVARS)      
       {
       return 0; 
       }
    else
       {
       printf("%lf  %lf\n",lbound,ubound);
       i++
       }
}

然后,如果它没有打印您知道的任何内容,您就没有使用fscanf扫描任何内容。我猜如果你的scanf没有读取任何内容,你就不想继续用0填充数组,而且它可能会指向错误的方向

如果fScanf没有读取任何内容并返回0,你可能不想让代码运行。你为什么不改成这样

while( 0 != fscanf(infile, "%lf",&ubound))
{
    if(0 == fscanf(infile, "%lf",&lbound) || i >= NVARS)      
       {
       return 0; 
       }
    else
       {
       printf("%lf  %lf\n",lbound,ubound);
       i++
       }
}

然后,如果它没有打印您知道的任何内容,您就没有使用fscanf扫描任何内容。我猜如果您的扫描程序没有读取任何内容,并且可能会指向错误的方向,您不想继续用0填充数组

尝试调试代码并隔离问题。尝试调试代码并隔离问题。可能更容易记住:如果%s,则传递指针,else传递值。可能更容易记住:如果是%s,则传递指针,否则传递值。