This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
为了物理项目,我试图在Mac上的Xcode中的C ++中使用fprintf输出。以前,我在PC上使用过Turbo C ++,并且在输出类似以下的内容时没有问题,如下所述:我试图将数据发送到.csv文件,以便在xcel中创建图形/图表,但是调试器很麻烦我尝试的输出。有人可以帮助我,告诉我如何将输出从xcode发送到xcel或mac的数字程序中吗?我的代码有什么问题?谢谢我的等待!

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

int main ()
{
    int ia,ja,ib=1,jb=1,n=1,t=1;
    float Ia[25],Ja[25],C[25][25],Wbb[25][25];
    float xl[25][25],xr[25][25],Pr[25][25],Nr[25][25];
    float max,r1,H=0;

    max=RAND_MAX;
    FILE * Project1;
    Project1 = fopen ("Users/Dave/Desktop/Project1.csv","w");

    Ia[n]=3;
    Ja[n]=3;
    for(t=1;t<100;++t) //time step counter
    {
        n=1;
        for(n=1;n<26;++n) //# of states to look at
        {
            ia=Ia[n];
            ja=Ja[n];
            ib=1;

            for(ib=1;ib<6;++ib) //determine I[n]'s C[ia][ja] values (initially will be the same due to ICs)
            {
                jb=1;
                for(jb=1;jb<6;++jb)
                {
                    C[ia][ja]=100*(ia-ib)^2+100*(ja-jb)^2+1;
                }
            }
            fprintf(Project1, "%d,%d,%d,%f\n",n,ia,ja,C[ia][ja]);

            ib=1;
            for(ib=1;ib<6;++ib) //determine Wsrs and delta x's for each system I[n] {
            {
                jb=1;
                for(jb=1;jb<6;++jb)
                {
                    Wbb[ib][jb]=C[ia][ja]/((100)*(ia-ib)^2+100*(ja-jb)^2+1);
                    xl[1][1]=0;
                    xr[ib][jb]=xl[ib][jb]+Wbb[ib][jb];
                    fprintf(Project1,"%d,%d,%f,%f,%f",ib,jb,Wbb[ib][jb],xl[ib][jb],xr[ib][jb]);
                    xl[ib][jb+1]=xr[ib][jb];
                }
            }

            r1=rand()/max; //randomly select new state and determine it's ib,jb value and reassign to new ia, ja at next time step
            ib=1;
            for(ib=1;ib<6;++ib)
            {
                jb=1;
                for(jb=1;jb<6;++jb)
                {
                    if(r1<xr[ib][jb] & r1>xl[ib][jb])
                        Nr[ib][jb]++;
                    Ia[n]=ib;
                    Ja[n]=jb;
                }
            }
        }

        ib=1;
        for(ib=1;ib<6;++ib)
        {
            jb=1;
            for(jb=1;jb<6;++jb)
            {
                Pr[ib][jb]=Nr[ib][jb]/25;
                H=Pr[ib][jb]*log(Pr[ib][jb])+H; //Compute Total Entropy Function of all systems by adding each systems entropy
            }
        }

        fprintf(Project1,"%f",H); //print out the Entropy at each time step
    }
}

最佳答案

因此,我在这里看到一些问题,在此行中您使用了错误的引号:

Project1 = fopen (“Users/Dave/Desktop/Project1.csv”,”w”);


应该:

Project1 = fopen ("Users/Dave/Desktop/Project1.csv","w");


正如有人指出C ++没有power运算符,^bitwise exclusive or,您需要改用pow函数,因此此行:

C[ia][ja]=100*(ia-ib)^2+100*(ja-jb)^2+1;


将会:

C[ia][ja]=100*pow((ia-ib),2)+100*pow((ja-jb),2)+1;


此外,&运算符也是bitwise and,在以下情况下,我认为您的意思是&&这是logical and,但在这种情况下,它们将执行相同的操作:

 if(r1<xr[ib][jb] & r1>xl[ib][jb])


另外,如果您使用C ++开发,我将使用头文件的C ++版本:

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

关于c++ - XCode中的fprintf C有问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15468198/

10-11 22:56