程序代码:
比较有意思,俺也来一个.
#include <stdio.h>
#include <string.h>
#define MIN 4
#define MAX 16
#define IN
#define OUT
#define INOUT
#define OUTPUTFILE "d:\\1.txt"
const char SPECK = ' ';
const short int ENTER = 0x0a0d;
static int CreateDraw(IN int nNum);
static int GetInput(void);
int main(int argc,char* argv[])
{
int InputNum;
while(!(InputNum = GetInput()))
{
printf("输入数值越界或非法!请再次尝试。\n");
fflush(stdin);
}
return CreateDraw(InputNum);
}
int GetInput(void)
{
int Input = 0;
printf("请输入一个介于%d到%d的整数:\n",MIN,MAX);
scanf("%d",&Input);
return (Input >=MIN && Input <= MAX)?Input:0;
}
int CreateDraw(int nNum)
{
int DrawArry[MAX][MAX];
int Y,Y0,i,YY,XX,j;
FILE* pOutPutF;
char Fttxt[8];
int CrrentValue = 1;
Y=0; Y0 = nNum - 1;
for (i = 0;i <= nNum / 2; i++)
{
j = i;
for (YY = Y; YY <= Y0; YY++)
{
DrawArry[j][YY]=CrrentValue;
CrrentValue++;
};
for (XX = j+1;XX <= Y0; XX++)
{
DrawArry[XX][Y0]=CrrentValue;
CrrentValue++;
};
for (YY = Y0-1; YY >= Y; YY--)
{
DrawArry[Y0][YY]=CrrentValue;
CrrentValue++;
};
for (XX = Y0-1;XX >= Y+1; XX--)
{
DrawArry[XX][Y]=CrrentValue;
CrrentValue++;
};
++Y;
--Y0;
}
if(!(pOutPutF = fopen(OUTPUTFILE,"wb+")))
{
printf("创建输出文件失败,I/O错误!");
return -1;
}
for (YY = 0;YY < nNum;YY++)
{
for (XX = 0;XX < nNum; XX++)
{
sprintf(Fttxt,"%3d",DrawArry[XX][YY]);
printf("%s ",Fttxt);
fwrite(Fttxt,sizeof(char),(size_t)strlen(Fttxt),pOutPutF);
fwrite(&SPECK,sizeof(char),1,pOutPutF);
}
printf("\n");
fwrite(&ENTER,sizeof(short int),1,pOutPutF);
}
fclose(pOutPutF);
return 0;
}
[
本帖最后由 东海一鱼 于 2009-8-11 17:30 编辑 ]