主题:高手给看看到底哪错了,我都晕了
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
FILE *p3_in = NULL;
FILE *p3_out = NULL;//定义输入输出文件指针
int init()
{
p3_in=fopen("p3_in.txt","r");// 读入数据,若不能打开则返回错误
if(p3_in== NULL)
{
return -1;
}
p3_out = fopen("p3_out.txt", "w");
if(p3_out == NULL)
{
return -1;
}
}
int T,n,m;
int *stack[3];
int move(int from, int to )
{
int i,j;
for(i = 0; i < n; i++ )
{
if(stack[from][i] != n)
{
for(j = n-1; j >= 0; j-- )
{
if(stack[to][j] == n )
{
stack[to][j] = stack[from][i];
stack[from][i] = n;
return TRUE;
}//从栈from向栈to进行一次移动 //移动成功,返回TRUE,移动失败返回FALSE;
}
}
}
return FALSE;
}
int check()
{
int i;
for(i = 0; i < n; i++ )
{
if(stack[2][i] != i) //检查木桩3
break;
}
if( i == n )
return TRUE;
else
return FALSE;//如果移动成功,则stack[2]中,从下标为0的地方开始,到最后一个元素,为依次递增。
}
void search()
{
int i,j,T;
fscanf(p3_in,"%d\n",&T); //读入变量T
for(i=0;i<T;i++)
{
fscanf(p3_in,"%d %d",&n,&m);//读入 n个盘子,m次移动
int skip = FALSE;//判断是否跳过当前移动过程中是否跳过后面的步骤。
stack[0] = (int*)malloc(n*sizeof(int));
stack[1] = (int*)malloc(n*sizeof(int));
stack[2] = (int*)malloc(n*sizeof(int));
for(j=0;j<n;j++)
{
stack[0][j]=j;//初始化为n个盘子,j表示盘子的变量
stack[1][j]=stack[2][j]=n;//置栈上盘子为空
}
for(j = 0; j < m; j++ )
{
//读入移动步骤,from,to
int from,to;
fscanf(p3_in,"%d %d",&from,&to);
if(skip == TRUE )
continue;
if(move(from,to) == FALSE )
{//第p次移动失败,输出-p,并忽略后面的移动步骤
fprintf(p3_out,"%d\n",-(j+1));
skip = TRUE;
break;
}
else
{
if(check()==TRUE)
{
fprintf(p3_out,"%d\n",j+1);
skip = TRUE;
break; //输出p,并忽略后面的移动步骤。
}
}
skip=FALSE;
if(check()==FALSE)
fprintf(p3_out,"%d\n",0);
}
}
void Destroy()
{
free(stack[0]);//释放分配内存空间
free(stack[1]);
free(stack[2]);
fclose(p3_out);
fclose(p3_in);
}
int main()//主函数
{
if(Init()==-1)
return -1;
move(from,to);
check();
search();
Destroy();
}
错误
Compiling...
Cpp1.cpp
C:\Documents and Settings\Administrator\Cpp1.cpp(116) : error C2601: 'Destroy' : local function definitions are illegal
C:\Documents and Settings\Administrator\Cpp1.cpp(125) : error C2601: 'main' : local function definitions are illegal
C:\Documents and Settings\Administrator\Cpp1.cpp(133) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.
#include<stdlib.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
FILE *p3_in = NULL;
FILE *p3_out = NULL;//定义输入输出文件指针
int init()
{
p3_in=fopen("p3_in.txt","r");// 读入数据,若不能打开则返回错误
if(p3_in== NULL)
{
return -1;
}
p3_out = fopen("p3_out.txt", "w");
if(p3_out == NULL)
{
return -1;
}
}
int T,n,m;
int *stack[3];
int move(int from, int to )
{
int i,j;
for(i = 0; i < n; i++ )
{
if(stack[from][i] != n)
{
for(j = n-1; j >= 0; j-- )
{
if(stack[to][j] == n )
{
stack[to][j] = stack[from][i];
stack[from][i] = n;
return TRUE;
}//从栈from向栈to进行一次移动 //移动成功,返回TRUE,移动失败返回FALSE;
}
}
}
return FALSE;
}
int check()
{
int i;
for(i = 0; i < n; i++ )
{
if(stack[2][i] != i) //检查木桩3
break;
}
if( i == n )
return TRUE;
else
return FALSE;//如果移动成功,则stack[2]中,从下标为0的地方开始,到最后一个元素,为依次递增。
}
void search()
{
int i,j,T;
fscanf(p3_in,"%d\n",&T); //读入变量T
for(i=0;i<T;i++)
{
fscanf(p3_in,"%d %d",&n,&m);//读入 n个盘子,m次移动
int skip = FALSE;//判断是否跳过当前移动过程中是否跳过后面的步骤。
stack[0] = (int*)malloc(n*sizeof(int));
stack[1] = (int*)malloc(n*sizeof(int));
stack[2] = (int*)malloc(n*sizeof(int));
for(j=0;j<n;j++)
{
stack[0][j]=j;//初始化为n个盘子,j表示盘子的变量
stack[1][j]=stack[2][j]=n;//置栈上盘子为空
}
for(j = 0; j < m; j++ )
{
//读入移动步骤,from,to
int from,to;
fscanf(p3_in,"%d %d",&from,&to);
if(skip == TRUE )
continue;
if(move(from,to) == FALSE )
{//第p次移动失败,输出-p,并忽略后面的移动步骤
fprintf(p3_out,"%d\n",-(j+1));
skip = TRUE;
break;
}
else
{
if(check()==TRUE)
{
fprintf(p3_out,"%d\n",j+1);
skip = TRUE;
break; //输出p,并忽略后面的移动步骤。
}
}
skip=FALSE;
if(check()==FALSE)
fprintf(p3_out,"%d\n",0);
}
}
void Destroy()
{
free(stack[0]);//释放分配内存空间
free(stack[1]);
free(stack[2]);
fclose(p3_out);
fclose(p3_in);
}
int main()//主函数
{
if(Init()==-1)
return -1;
move(from,to);
check();
search();
Destroy();
}
错误
Compiling...
Cpp1.cpp
C:\Documents and Settings\Administrator\Cpp1.cpp(116) : error C2601: 'Destroy' : local function definitions are illegal
C:\Documents and Settings\Administrator\Cpp1.cpp(125) : error C2601: 'main' : local function definitions are illegal
C:\Documents and Settings\Administrator\Cpp1.cpp(133) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.