当前位置:编程学习 > C#/ASP.NET >>

刚学C#求助

/*KMC code simulating the movement and binding of vacancies and alloy element
  after solid solution heat treatments. A 3D fcc Lattice with 50x50x50 lattice 
  points was set up
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mersenne_inline.h"

/*initial setting*/
/*PI (not sure if it is neccesary for the furture use*/
#define PI 3.14159265359
/*Boltzman constant in [eV/K]*/
#define kB 8.617343e-5
/*the size of simulated lattice*/
#define Nx 10
#define Ny 10
#define Nz 10
/*the lattice parameter of Al FCC strcutre at room temperature(unit A)*/
#define A 4.0496
/*the number of vacancies at t=0*/
#define N_Vac 4
/*the number of Si in the simulated Al alloy*/
#define N_Si 8
/*the number of Mg in the simulated Al alloy*/
#define N_Mg 6
/*a data structure for describing the atom in the lattice*/


void create_initial_distributions(short *atomtype[2*Nx][2*Ny][2*Nz]);

/* body of the program*/


int main ()
{
int i, j, k ;
/*decleartion of for the distributions of atom*/
/*0=aluminiuam, 1=Vacancies, 2=Si, 3=Mg*/
short atomtype[2*Nx][2*Ny][2*Nz];
FILE *output;
seed_mersenne(5516761);



/*print out test of the matrix setting*/
/*open file for output*/
output=fopen("matrix.txt","w");

for (i=0;i<=2*Nx;i++)
{
for(j=0;j<=2*Ny;j++)
{
for(k=0;k<=2*Nz;k++)
{
if (atomtype[i][j][k]==0)
fprintf(output,"Al %8d %8.4d  %8.4d  %8.4d \n",
atomtype[i][j][k], i, j, k);
}
}
}

for (i=0;i<=2*Nx;i++)
{
for(j=0;j<=2*Ny;j++)
{
for(k=0;k<=2*Nz;k++)
{
if (atomtype[i][j][k]==1)
fprintf(output,"Vac %8d %8.4d  %8.4d  %8.4d \n",
atomtype[i][j][k], i, j, k);
}
}
}
for (i=0;i<=2*Nx;i++)
{
for(j=0;j<=2*Ny;j++)
{
for(k=0;k<=2*Nz;k++)
{
if (atomtype[i][j][k]==2)
fprintf(output,"Si %8d %8.4d  %8.4d  %8.4d \n",
atomtype[i][j][k], i*A, j*A, k*A);
}
}
}
for (i=0;i<=2*Nx;i++)
{
for(j=0;j<=2*Ny;j++)
{
for(k=0;k<=2*Nz;k++)
{
if (atomtype[i][j][k]==3)
fprintf(output,"Mg %8d %8.4d  %8.4d  %8.4d \n",
atomtype[i][j][k], i, j, k);

}
}
}

return 0;
}

/*******************************************************************/
/*******************************************************************/
/************create the initial distribution of the atoms***********/
/*******************************************************************/
/*******************************************************************/


void create_initial_distributions(short *atomtype[2*Nx][2*Ny][2*Nz])
/*****************create the pure Al FCC lattice********************/
{int i,j,k,l;
for (i=1;i<=2*Nx;i++)
{
for (j=1;j<=2*Ny;j++)
{
for (k=1;k<=2*Nz;k++)
{
*atomtype[i][j][k]=10;

}
}
}

for (i=0;i<=2*Nx;i++)
{
for (j=1;j<=2*Ny;j++)
{
for (k=1;k<=2*Nz;k++)
{
if ((i%2==0 && j%2==0 && k%2==0)||((i%2)+(j%2)+(k%2)==2))
{*atomtype[i][j][k]=0; }
}
}
}
/*****************create Si, Mg and vacancies in the lattice********************/
/****Si*****/
for (l=0;l<N_Si;)
{
i=(int)mersenne()*2*Nx+1;
j=(int) mersenne()*2*Ny+1;
k=(int)(mersenne()*2*Nz)+1;
{
if ((i%2==0 && j%2==0 && k%2==0)||((i%2)+(j%2)+(k%2)==2))
{
*atomtype[i][j][k]=2;
l=l+1;
}
else 
continue;
}

}
/****Mg*****/
for (l=0;l<N_Mg;)
{
i=(int)(mersenne()*2*Nx)+1;
j=(int)(mersenne()*2*Ny)+1;
k=(int)(mersenne()*2*Nz)+1;
if ((i%2==0 && j%2==0 && k%2==0)||((i%2)+(j%2)+(k%2)==2))
{   
if (*atomtype[i][j][k]!=2)

*atomtype[i][j][k]=3;
l=l+1;
}
else 
continue;
}
}
/****Vacancy*****/
for (l=0;l<N_Vac;)
{
i=(int)(mersenne()*2*Nx)+1;
j=(int)(mersenne()*2*Ny)+1;
k=(int)(mersenne()*2*Nz)+1;
if ((i%2==0 && j%2==0 && k%2==0)||((i%2)+(j%2)+(k%2)==2))
{   
if (*atomtype[i][j][k]==0)

*atomtype[i][j][k]=1;
l=l+1;
}
else 
continue;
}
}
}





第二部分
/* A C-program for MT19937: Real number version  (1998/4/6)    */
/*   genrand() generates one pseudorandom real number (double) */
/* which is uniformly distributed on [0,1]-interval, for each  */
/* call. sgenrand(seed) set initial values to the working area */
/* of 624 words. Before genrand(), sgenrand(seed) must be      */
/* called once. (seed is any 32-bit integer except for 0).     */
/* Integer generator is obtained by modifying two lines.       */
/*   Coded by Takuji Nishimura, considering the suggestions by */
/* Topher Cooper and Marc Rieffel in July-Aug. 1997.           */

/* This library is free software; you can redistribute it and/or   */
/* modify it under the terms of the GNU Library General Public     */
/* License as published by the Free Software Foundation; either    */
/* version 2 of the License, or (at your option) any later         */
/* version.                                                        */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of  */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.            */
/* See the GNU Library General Public License for more details.    */
/* You should have received a copy of the GNU Library General      */
/* Public License along with this library; if not, write to the    */
/* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA   */ 
/* 02111-1307  USA                                                 */

/* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.       */
/* When you use this, send an email to: matumoto@math.keio.ac.jp   */
/* with an appropriate reference to your work.                     */

/* REFERENCE                                                       */
/* M. Matsumoto and T. Nishimura,                                  */
/* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform  */
/* Pseudo-Random Number Generator",                                */
/* ACM Transactions on Modeling and Computer Simulation,           */
/* Vol. 8, No. 1, January 1998, pp 3--30.                          */

#include <stdio.h>
#include <stdlib.h>
#include "mersenne_inline.h"


/* Period parameters */  
#define N 624
#define M 397
#define MATRIX_A 0x9908b0df   /* constant vector a */
#define UPPER_MASK 0x80000000 /* most significant w-r bits */
#define LOWER_MASK 0x7fffffff /* least significant r bits */

/* Tempering parameters */   
#define TEMPERING_MASK_B 0x9d2c5680
#define TEMPERING_MASK_C 0xefc60000
#define TEMPERING_SHIFT_U(y)  (y >> 11)
#define TEMPERING_SHIFT_S(y)  (y << 7)
#define TEMPERING_SHIFT_T(y)  (y << 15)
#define TEMPERING_SHIFT_L(y)  (y >> 18)

static unsigned int mt[N]; /* the array for the state vector  */
int mersenne_i = -1; /*  < 0 means mt[N] is not initialized */
double mersenne_array[N];

/* initializing the array with a NONZERO seed */
void
seed_mersenne(long seed)
{
  int mti;
  mt[0]= seed & 0xffffffffUL;
  for (mti=1; mti<N; mti++) {
    mt[mti] =
      (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
    /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
    /* In the previous versions, MSBs of the seed affect   */
    /* only MSBs of the array mt[].                        */
    /* 2002/01/09 modified by Makoto Matsumoto             */
    mt[mti] &= 0xffffffffUL;
    /* for >32 bit machines */
  }
  mersenne_i = 0;
}

double /* generating reals */
/* unsigned int */ /* for integer generation */
mersenne_generate()
{
  register unsigned int y;
  register int kk;
  static unsigned int mag01[2]={0x0, MATRIX_A};
  /* mag01[x] = x * MATRIX_A  for x=0,1 */

  if (mersenne_i < 0) {  /* if sgenrand() has not been called, */
    printf("DUMMY: you did not seed the generator!\n");
    exit(0);
  }
  
  /* generate N words at one time */

  for (kk=0;kk<N-M;kk++) {
    y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
    mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
  }
  for (;kk<N-1;kk++) {
    y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
    mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
  }
  y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
  mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];

  for (kk=0; kk<N; kk++) {
    y = mt[kk];
    y ^= TEMPERING_SHIFT_U(y);
    y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
    y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
    y ^= TEMPERING_SHIFT_L(y);
    mersenne_array[kk] = (double)y * 2.3283064365386963e-10;  /* reals: interval [0,1) */
  }
  
  mersenne_i = N;
  return ( mersenne_array[--mersenne_i] );
    /* return y; */ /* for integer generation */
}

第三部分
/***************************************************************
 *  mersenne.h   
 *  for the inline version of the mersenne generator
 */

#define MERSENNE_N 624

extern int mersenne_i;
extern double mersenne_array[MERSENNE_N];

#define mersenne() ( mersenne_i > 0 ? mersenne_array[--mersenne_i] : mersenne_generate() )

void seed_mersenne(long seed);
double mersenne_generate();



问题在与,我像让那些数按照条件语句生成。按道理 在Al那里的 i=0 j=0 k=3这些在条件语句那里应该是没有赋值不会出现的,我不知道为什么最后输出的时候输出了这些多余的数据。还有就是那些 Vac, Si 和Mg的数目 和我在#define里面定义的个数不一样。我找不到问题的原因。如果有时间帮我改改。我在编一个很简单的模拟程序,开始设计那个矩阵就搞成这样了,很急呢 
--------------------编程问答-------------------- 这个好像是C++的吧,不是C#的。 --------------------编程问答-------------------- 啊那你能帮我看下哪里出错了么 --------------------编程问答-------------------- 是啊,C++的头文件,不是C# --------------------编程问答-------------------- 发错版面了 --------------------编程问答-------------------- 恩,错了,不是c# --------------------编程问答-------------------- C#的代码可以这样写了?微软怎么没通知过? --------------------编程问答-------------------- 这个,明显的C++啊 --------------------编程问答-------------------- 这是C++代码嘛

啥时候成C#了 难道是C#5.0?
我3.5都不会 loser了 --------------------编程问答-------------------- 代码要用ubb括起来。你这样代码太乱了。
最好的做法还是调试。 --------------------编程问答-------------------- C++ Primary 精读. --------------------编程问答-------------------- 不懂帮顶 --------------------编程问答-------------------- --------------------编程问答-------------------- 你确定是C#?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,