MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->frame_Mid->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); this->setWindowTitle("Board game"); InitSpace(); InitPlayer(); start(); }
void difference(SLinkList space,int &S) // 算法2.17 { // 依次输入集合A和B的元素,在一维数组space中建立表示集合(A-B)∪(B-A) // 的静态链表,S为其头指针。假设备用空间足够大,space[0].cur为备用空间的头指针 int r,p,m,n,i,j,k; ElemType b; InitSpace(space); // 初始化备用空间 S=Malloc(space); // 生成S的头结点 r=S; // r指向S的当前最后结点 printf("请输入集合A和B的元素个数m,n:"); scanf("%d,%d%*c",&m,&n); // %*c吃掉回车符 printf("请输入集合A的元素(共%d个):",m); for(j=1;j<=m;j++) // 建立集合A的链表 { i=Malloc(space); // 分配结点 scanf("%c",&space[i].data); // 输入A的元素值 space[r].cur=i; // 插入到表尾 r=i; } scanf("%*c"); // %*c吃掉回车符 space[r].cur=0; // 尾结点的指针为空 printf("请输入集合B的元素(共%d个):",n); for(j=1;j<=n;j++) { // 依次输入B的元素,若不在当前表中,则插入,否则删除 scanf("%c",&b); p=S; k=space[S].cur; // k指向集合A中的第一个结点 while(k!=space[r].cur&&space[k].data!=b) { // 在当前表中查找 p=k; k=space[k].cur; } if(k==space[r].cur) { // 当前表中不存在该元素,插入在r所指结点之后,且r的位置不变 i=Malloc(space); space[i].data=b; space[i].cur=space[r].cur; space[r].cur=i; } else // 该元素已在表中,删除之 { space[p].cur=space[k].cur; Free(space,k); if(r==k) r=p; // 若删除的是尾元素,则需修改尾指针 } } }
void difference(SLinkList space,int *S) /* 算法2.17 */ { /* 依次输入集合A和B的元素,在一维数组space中建立表示集合(A-B)∪(B-A) */ /* 的静态链表,S为其头指针。假设备用空间足够大,space[0].cur为备用空间的头指针 */ int r,p,m,n,i,j,k; ElemType b; InitSpace(space); /* 初始化备用空间 */ *S=Malloc(space); /* 生成S的头结点 */ r=*S; /* r指向S的当前最后结点 */ printf("请输入集合A和B的元素个数m,n:"); scanf("%d,%d%*c",&m,&n); /* %*c吃掉回车符 */ printf("请输入集合A的元素(共%d个):",m); for(j=1;j<=m;j++) /* 建立集合A的链表 */ { i=Malloc(space); /* 分配结点 */ scanf("%c",&space[i].data); /* 输入A的元素值 */ space[r].cur=i; /* 插入到表尾 */ r=i; } scanf("%*c"); /* %*c吃掉回车符 */ space[r].cur=0; /* 尾结点的指针为空 */ printf("请输入集合B的元素(共%d个):",n); for(j=1;j<=n;j++) { /* 依次输入B的元素,若不在当前表中,则插入,否则删除 */ scanf("%c",&b); p=*S; k=space[*S].cur; /* k指向集合A中的第一个结点 */ while(k!=space[r].cur&&space[k].data!=b) { /* 在当前表中查找 */ p=k; k=space[k].cur; } if(k==space[r].cur) { /* 当前表中不存在该元素,插入在r所指结点之后,且r的位置不变 */ i=Malloc(space); space[i].data=b; space[i].cur=space[r].cur; space[r].cur=i; } else /* 该元素已在表中,删除之 */ { space[p].cur=space[k].cur; Free(space,k); if(r==k) r=p; /* 若删除的是尾元素,则需修改尾指针 */ } } }
void CSimulator::Init() { /* General configuration */ InitFramework(GetNode(m_tConfigurationRoot, "framework")); /* Initialize controllers */ InitControllers(GetNode(m_tConfigurationRoot, "controllers")); /* Create loop functions */ if(NodeExists(m_tConfigurationRoot, "loop_functions")) { /* User specified a loop_functions section in the XML */ InitLoopFunctions(GetNode(m_tConfigurationRoot, "loop_functions")); } else { /* No loop_functions in the XML */ m_pcLoopFunctions = new CLoopFunctions; } /* Physics engines */ InitPhysics(GetNode(m_tConfigurationRoot, "physics_engines")); /* Media */ InitMedia(GetNode(m_tConfigurationRoot, "media")); /* Space */ InitSpace(GetNode(m_tConfigurationRoot, "arena")); /* Call user init function */ if(NodeExists(m_tConfigurationRoot, "loop_functions")) { m_pcLoopFunctions->Init(GetNode(m_tConfigurationRoot, "loop_functions")); } /* Physics engines */ InitPhysics2(); /* Media */ InitMedia2(); /* Initialise visualization */ TConfigurationNodeIterator itVisualization; if(NodeExists(m_tConfigurationRoot, "visualization") && ((itVisualization = itVisualization.begin(&GetNode(m_tConfigurationRoot, "visualization"))) != itVisualization.end())) { InitVisualization(GetNode(m_tConfigurationRoot, "visualization")); } else { LOG << "[INFO] No visualization selected." << std::endl; m_pcVisualization = new CDefaultVisualization(); } /* Start profiling, if needed */ if(IsProfiling()) { m_pcProfiler->Start(); } }
NewEnv() #endif /* Establish a new environment * On exit- * NewEnv=new environment ***/ { Environment e; if (!baseptr) InitSpace(); e = (Environment)obstack_alloc(&space, sizeof(struct _EnvImpl)); e->relate = NoBinding; e->parent = (Environment)0; e->key = NoKey; e->level = 0; e->classdescr = (_Class)0; e->nested = 1; e->access = (_Access)obstack_alloc(&space, sizeof(struct _AccessMechanism)); obstack_init(&(e->access->IdnTbl)); e->access->MaxIdn = 0; e->access->CurrEnv = e; e->access->ClassIdnTbl = (ObstackP)0; e->access->MaxClassIdn = 0; e->access->Classes = (_Class)0; e->access->NextClassNo = 0; return e; }
int main(void) { int n, m, i, j; Elemtype b;//from set B InitSpace(); Cursor head = mymalloc(); Cursor r = head, p, k; printf("enter elemnum of set A and B:"); scanf("%d%d", &m, &n); for (j = 1; j <= m; j++) { i = mymalloc(); scanf("%d", &space[i].data); space[r].next = i; r = i;//r direct to last of set A } space[r].next = 0; for (j = 1; j <= n; j++) { scanf("%d", &b); p = head; k = space[head].next; while (k != space[r].next && space[k].data != b) { p = k; k = space[k].next;//similar to p = p->next } if (k == space[r].next)// 如果space中没有b,则插入之 { i = mymalloc(); space[i].data = b; space[i].next = space[r].next; space[r].next = i; } else//如果space中有b,则删除之 { space[p].next = space[k].next; myfree(k); if (r == k) r = p; } } return 0; }
SIZE InitShips (void) { SIZE num_ships; InitSpace (); SetContext (StatusContext); SetContext (SpaceContext); InitDisplayList (); InitGalaxy (); if (inHQSpace ()) { ReinitQueue (&race_q[0]); ReinitQueue (&race_q[1]); BuildSIS (); LoadHyperspace (); num_ships = 1; } else { COUNT i; RECT r; SetContextFGFrame (Screen); r.corner.x = SAFE_X; r.corner.y = SAFE_Y; r.extent.width = SPACE_WIDTH; r.extent.height = SPACE_HEIGHT; SetContextClipRect (&r); SetContextBackGroundColor (BLACK_COLOR); { CONTEXT OldContext; OldContext = SetContext (ScreenContext); SetContextBackGroundColor (BLACK_COLOR); ClearDrawable (); SetContext (OldContext); } if (LOBYTE (GLOBAL (CurrentActivity)) == IN_LAST_BATTLE) free_gravity_well (); else { #define NUM_ASTEROIDS 5 for (i = 0; i < NUM_ASTEROIDS; ++i) spawn_asteroid (NULL); #define NUM_PLANETS 1 for (i = 0; i < NUM_PLANETS; ++i) spawn_planet (); } num_ships = NUM_SIDES; } // FlushInput (); return (num_ships); }
void MergeList2(SLinkList L) { int na,nb; int i; int n; ElemType e; InitSpace(L); InitList(L,na); InitList(L,nb); printf("请输入A中数据的个数:"); scanf("%d",&i); if(i>0) n=i; for(i=0;i<n;i++) { loop1: scanf("%d",&e); if(!LocateElem(L,na,e,compare)) ListInsert(L,na,i+1,e); else { printf("已存在,请重新输入:"); goto loop1; } } printf("遍历链表"); TraverseList(L,na,visit); printf("\n"); printf("请输入B中数据的个数:"); scanf("%d",&i); if(i>0) n=i; for(i=0;i<n;i++) { loop2: scanf("%d",&e); if(!LocateElem(L,nb,e,compare)) ListInsert(L,nb,i+1,e); else { printf("已存在,请重新输入:"); goto loop2; } } printf("遍历链表"); TraverseList(L,nb,visit); printf("\n"); for(int j=0;j<ListLength(L,nb);j++) { if(GetElem(L,nb,j+1,e)) { if(!(i=LocateElem(L,na,e,compare))) { i=ListLength(L,na); ListInsert(L,na,++i,e); } else ListDelete(L,na,i,e); } } printf("遍历链表"); TraverseList(L,na,visit); printf("\n"); }
//expand void test2() { SLinkList L; SLinkList Lk; ElemType e; int i=0; int N=10; int n; InitSpace(L); InitList(L,n); printf("表是否为空(1:非空,0:空),结果:%d\n",!ListEmpty(L,n)); printf("表长%d\n",ListLength(L,n)); for(i=1;i<=5;i++) { ListInsert(L,n,i,2*i); } printf("插入5个元素(2,4,6,8,10),结果:"); TraverseList(L,n,visit); printf("\n"); i=3; if(PriorElem(L,n,3,e)) printf("%d的前驱为%d\n",i,e); if(NextElem(L,n,3,e)) printf("%d的后继为%d\n",i,e); for(i=1;i<=5;i++) { ListInsert(L,n,2*i-1,2*i-1); } printf("插入5个元素(1,3,5,7,9)后结果:"); TraverseList(L,n,visit); printf("\n"); printf("表是否为空(1:非空,0:空),结果:%d\n",!ListEmpty(L,n)); printf("表长%d\n",ListLength(L,n)); for(i=9;i<=10;i++) { printf("删除%d个元素为",i); if(ListDelete(L,n,i,e)) printf("%d\n",e); else printf("删除失败\n"); } int j=0; for(i=-1;i<=1;i++) { printf("查找%d所在位置",i); j=LocateElem(L,n,i,compare); if(j) printf("%d\n",j); else printf("查找失败\n"); } for(i=-1;i<=N+5;i+=3) { if(PriorElem(L,n,i,j)) printf("查找到%d的前驱%d\n",i,j); else printf("查找%d的前驱失败\n",i); if(NextElem(L,n,i,j)) printf("查找到%d的后继%d\n",i,j); else printf("查找%d的后继失败\n",i); } j=ClearList(L,n); if(j) printf("清空链表成功"); else printf("清空失败"); printf("表是否为空(1:非空,0:空),结果:%d\n",!ListEmpty(L,n)); printf("表长%d\n",ListLength(L,n)); MergeList2(Lk); printf("\n"); }
int main(int argc, char *argv[]) { int i, OutputNumber = 0, d; int ni, ntot; char ParameterFile[MAXLINELENGTH]; if (argc == 1) PrintUsage (argv[0]); strcpy (ParameterFile, ""); for (i = 1; i < argc; i+=d) { d=1; if (*(argv[i]) == '+') { if (strspn (argv[i], \ "+S#D") \ != strlen (argv[i])) PrintUsage (argv[0]); if (strchr (argv[i], '#')) { d=2; ArrayNb = atoi(argv[i+1]); EarlyOutputRename = YES; if (ArrayNb <= 0) { masterprint ("Incorrect Array number after +# flag\n"); PrintUsage (argv[0]); } } if (strchr (argv[i], 'D')) { d=2; strcpy (DeviceFile, argv[i+1]); DeviceFileSpecified = YES; } if (strchr (argv[i], 'S')) { d=2; StretchNumber = atoi(argv[i+1]); StretchOldOutput = YES; } } if (*(argv[i]) == '-') { if (strspn (argv[i], \ "-tofCmkspSVBD0#") \ != strlen (argv[i])) PrintUsage (argv[0]); if (strchr (argv[i], 't')) TimeInfo = YES; if (strchr (argv[i], 'f')) ForwardOneStep = YES; if (strchr (argv[i], '0')) OnlyInit = YES; if (strchr (argv[i], 'C')) { EverythingOnCPU = YES; #ifdef GPU mastererr ("WARNING: Forcing execution of all functions on CPU\n"); #else mastererr ("WARNING: Flag -C meaningless for a CPU built\n"); #endif } if (strchr (argv[i], 'm')) { Merge = YES; } if (strchr (argv[i], 'k')) { Merge = NO; } if (strchr (argv[i], 'o')) { RedefineOptions = YES; ParseRedefinedOptions (argv[i+1]) ; d=2; } if (strchr (argv[i], 's')) { Restart = YES; d=2; NbRestart = atoi(argv[i+1]); if ((NbRestart < 0)) { masterprint ("Incorrect restart number\n"); PrintUsage (argv[0]); } } if (strchr (argv[i], '#')) { d=2; ArrayNb = atoi(argv[i+1]); if (ArrayNb <= 0) { masterprint ("Incorrect Array number after -# flag\n"); PrintUsage (argv[0]); } } if (strchr (argv[i], 'p')) { PostRestart = YES; } if (strchr (argv[i], 'S')) { Restart_Full = YES; d=2; NbRestart = atoi(argv[i+1]); if ((NbRestart < 0)) { masterprint ("Incorrect restart number\n"); PrintUsage (argv[0]); } } if (strchr (argv[i], 'V')) { Dat2vtk = YES; Restart_Full = YES; d=2; NbRestart = atoi(argv[i+1]); if ((NbRestart < 0)) { masterprint ("Incorrect output number\n"); PrintUsage (argv[0]); } } if (strchr (argv[i], 'B')) { Vtk2dat = YES; Restart_Full = YES; d=2; NbRestart = atoi(argv[i+1]); if ((NbRestart < 0)) { masterprint ("Incorrect output number\n"); PrintUsage (argv[0]); } } if (strchr (argv[i], 'D')) { d=2; DeviceManualSelection = atoi(argv[i+1]); } } else strcpy (ParameterFile, argv[i]); } #ifdef WRITEGHOSTS if (Merge == YES) { mastererr ("Cannot merge outputs when dumping ghost values.\n"); mastererr ("'make nofulldebug' could fix this problem.\n"); mastererr ("Using the -k flag could be another solution.\n"); prs_exit (1); } #endif if (ParameterFile[0] == 0) PrintUsage (argv[0]); #ifdef MPICUDA EarlyDeviceSelection(); #endif MPI_Init (&argc, &argv); MPI_Comm_rank (MPI_COMM_WORLD, &CPU_Rank); MPI_Comm_size (MPI_COMM_WORLD, &CPU_Number); CPU_Master = (CPU_Rank == 0 ? 1 : 0); #ifndef MPICUDA SelectDevice(CPU_Rank); #endif InitVariables (); MPI_Barrier(MPI_COMM_WORLD); ReadDefaultOut (); ReadVarFile (ParameterFile); if (strcmp (PLANETCONFIG, "NONE") != 0) ThereArePlanets = YES; if (ORBITALRADIUS > 1.0e-30){ YMIN *= ORBITALRADIUS; YMAX *= ORBITALRADIUS; DT *= sqrt(ORBITALRADIUS*ORBITALRADIUS*ORBITALRADIUS); } SubsDef (OUTPUTDIR, DefaultOut); /* This must be placed ***BEFORE*** reading the input files in case of a restart */ if ((ArrayNb) && (EarlyOutputRename == YES)) { i = strlen(OUTPUTDIR); if (OUTPUTDIR[i-1] == '/') OUTPUTDIR[i-1] = 0;//Remove trailing slash if any sprintf (OUTPUTDIR, "%s%06d/", OUTPUTDIR, ArrayNb); //Append numerical suffix /* There is no need to perform the wildcard (@) substitution. This has already been done */ printf ("\n\n***\n\nNew Output Directory is %s\n\n***\n\n", OUTPUTDIR); MakeDir(OUTPUTDIR); /*Create the output directory*/ } MakeDir(OUTPUTDIR); /*Create the output directory*/ #if !defined(X) NX = 1; #endif #if !defined(Y) NY = 1; #endif #if !defined(Z) NZ = 1; #endif SelectWriteMethod(); #if !defined(Y) && !defined(Z) if (CPU_Rank==1){ prs_error ("You cannot split a 1D mesh in x. Sequential runs only!"); } if (CPU_Number > 1) { MPI_Finalize(); prs_exit(EXIT_FAILURE); } #endif ListVariables ("variables.par"); //Writes all variables defined in set up ListVariablesIDL ("IDL.var"); ChangeArch(); /*Changes the name of the main functions ChangeArch adds _cpu or _gpu if GPU is activated.*/ split(&Gridd); /*Split mesh over PEs*/ InitSpace(); WriteDim(); InitSurfaces(); LightGlobalDev(); /* Copy light arrays to the device global memory */ CreateFields(); // Allocate all fields. Sys = InitPlanetarySystem(PLANETCONFIG); ListPlanets(); if(Corotating) OMEGAFRAME = GetPsysInfo(FREQUENCY); OMEGAFRAME0 = OMEGAFRAME; /* We need to keep track of initial azimuthal velocity to correct the target velocity in Stockholm's damping prescription. We copy the value above *after* rescaling, and after any initial correction to OMEGAFRAME (which is used afterwards to build the initial Vx field. */ if(Restart == YES || Restart_Full == YES) { CondInit (); //Needed even for restarts: some setups have custom //definitions (eg potential for setup MRI) or custom //scaling laws (eg. setup planetesimalsRT). begin_i = RestartSimulation(NbRestart); if (ThereArePlanets) { PhysicalTime = GetfromPlanetFile (NbRestart, 9, 0); OMEGAFRAME = GetfromPlanetFile (NbRestart, 10, 0); RestartPlanetarySystem (NbRestart, Sys); } } else { if (ThereArePlanets) EmptyPlanetSystemFiles (); CondInit(); // Initialize set up // Note: CondInit () must be called only ONCE (otherwise some // custom scaling laws may be applied several times). } if (StretchOldOutput == YES) { StretchOutput (StretchNumber); } FARGO_SAFE(comm(ENERGY)); //Very important for isothermal cases! /* This must be placed ***after*** reading the input files in case of a restart */ if ((ArrayNb) && (EarlyOutputRename == NO)) { i = strlen(OUTPUTDIR); if (OUTPUTDIR[i-1] == '/') OUTPUTDIR[i-1] = 0;//Remove trailing slash if any sprintf (OUTPUTDIR, "%s%06d/", OUTPUTDIR, ArrayNb); //Append numerical suffix /* There is no need to perform the wildcard (@) substitution. This has already been done */ printf ("\n\n***\n\nNew Output Directory is %s\n\n***\n\n", OUTPUTDIR); MakeDir(OUTPUTDIR); /*Create the output directory*/ ListVariables ("variables.par"); //Writes all variables defined in set up ListVariablesIDL ("IDL.var"); InitSpace(); WriteDim (); } DumpToFargo3drc(argc, argv); FillGhosts(PrimitiveVariables()); #ifdef STOCKHOLM FARGO_SAFE(init_stockholm()); #ifdef STOCKHOLMACC FARGO_SAFE(ComputeVymed(Vy)); FARGO_SAFE(ComputeRhomed(Density)); Write2D(Density0_avg, "density0_2d_avg.dat", OUTPUTDIR, GHOSTINC); Write2D(Vy0_avg, "vy0_2d_avg.dat", OUTPUTDIR, GHOSTINC); #endif #endif #ifdef GHOSTSX masterprint ("\n\nNew version with ghost zones in X activated\n"); #else masterprint ("Standard version with no ghost zones in X\n"); #endif #ifdef TIMER clock_t begin_timer_time, end_timer_time; real timer_time_elapsed; #endif ntot = NTOTINIT; for (ni = 0; ni<NITER; ni++) { // Iteration loop ntot = (ni == 0) ? NTOTINIT : NTOT; masterprint ("Start of %d iteration\n", ni); masterprint ("Evolving waves for %d DT (DT = %lg)\n", ntot,DT); for (i = begin_i; i<=ntot; i++) { // MAIN LOOP #ifdef TIMER if (i==begin_i) { begin_timer_time = clock(); } #endif if (NINTERM * (TimeStep = (i / NINTERM)) == i) { TimeStepIter = ni; #if defined(MHD) && defined(DEBUG) FARGO_SAFE(ComputeDivergence(Bx, By, Bz)); #endif if (ThereArePlanets) WritePlanetSystemFile(TimeStep, NO); #ifndef NOOUTPUTS WriteOutputsAndDisplay(ALL); if(CPU_Master) printf("OUTPUTS %d at date t = %f OK\n", TimeStep, PhysicalTime); #endif if (TimeInfo == YES) GiveTimeInfo (TimeStep); } if (NSNAP != 0) { if (NSNAP * (TimeStep = (i / NSNAP)) == i) { WriteOutputsAndDisplay(SPECIFIC); } } AlgoGas(FALSE); MonitorGlobal (MONITOR2D | MONITORY | MONITORY_RAW| \ MONITORSCALAR | MONITORZ | MONITORZ_RAW); if (ThereArePlanets) { WriteTorqueAndWork(TimeStep, 0); WritePlanetSystemFile(TimeStep, YES); SolveOrbits (Sys); } #ifdef TIMER if (i==begin_i) { end_timer_time = clock(); timer_time_elapsed =( (double)(end_timer_time-begin_timer_time))/CLOCKS_PER_SEC; masterprint("time for time_step was %g s\n",timer_time_elapsed); } #endif } masterprint ("End of %d iteration\n", ni); AlgoGas(TRUE); masterprint ("Computing steady state\n"); compute_steady_state(); add_avgs(); output_steady_state(ni); clear_averages(); } MPI_Finalize(); printf("End of simulation!\n"); return 0; }