1.模拟汉字书写操作,模拟动作分:
(1)书写一个汉字;
(2)书写一个笔画;
(3)生成一个点坐标;
为了表示一个完整的汉字,需要对每一个笔画进行标记,此坐标定为(-1,0);整个汉字结束时采用(-1,-1)进行表示。
生成汉字的代码:
int GenIntData(int nNum,int* pData,int nMin,int nMax);
int _tmain(int argc, _TCHAR* argv[])
{
#define MAX_SCRIPTS 100
#define MAX_POINTS 96
int nScript = 0;
int* pIntData = new int[MAX_POINTS * MAX_SCRIPTS];
memset(pIntData,0,MAX_SCRIPTS* MAX_POINTS*sizeof(int));
//生成笔画数在10到1000之间
GenIntData(1,&nScript,1,MAX_SCRIPTS);
int* p = pIntData;
for (int i = 0; i < nScript;i++)
{
int nCount = 0;
//生成当前笔画的最大点数
GenIntData(1,&nCount,10,MAX_POINTS);
//圆整为偶数
nCount = nCount / 2 * 2;
GenIntData(nCount,p,0,800);
p += nCount;
*(p++) = -1;
*(p++) = 0;
}
//最后一点坐标
*(p++) = -1;
*(p++) = -1;
//将数据写入文本文件
//整数之间以','分割
FILE* fpIntData = fopen("./data.txt","w");
if (fpIntData == NULL)
{
return -1;
}
for (int i = 0; i < p - pIntData;i++)
{
fprintf(fpIntData,"%d,",*(pIntData + i));
if ((i + 1) % 10 == 0)
{
fprintf(fpIntData,"\n");
}
}
fclose(fpIntData);
delete[] pIntData;
//将存为逗号分隔的文本转换为整数数组
FILE* fpReadData = fopen("./data.txt","r");
if (fpReadData == NULL)
{
return -1;
}
char szLine[260] = {0};
char* pTail = NULL;
vector viData;
while (fgets(szLine,250,fpReadData) != NULL)
{
char* pHead = szLine;
while(pTail = strchr(pHead,','))
{
*pTail = 0;
viData.push_back(atoi(pHead));
pHead = ++pTail;
}
}
//转换完成,显示转换后的数值
for (int i = 0; i < viData.size();i++)
{
fprintf(stdout,"%d,",viData[i]);
if ((i+ 1) % 10 == 0)
{
fprintf(stdout,"\n");
}
}
fclose(fpReadData);
return 0;
}
/************************************************************************/
/* 生成nNum个随机数,最大值为nMax,最小值为nMin */
/************************************************************************/
int GenIntData(int nNum,int* pData,int nMin,int nMax)
{
if (nNum <= 0 || pData == NULL || nMin < 0 || nMax < 0 || nMax < nMin)
{
return -1;
}
srand(time(NULL));
int i;
for ( i = 0; i < nNum; i++ )
{
int u = (double)rand() / (RAND_MAX + 1) * (nMax - nMin);
*pData++ = u;
}
return 0;
}
//绘制到屏幕输出设备
void DrawLine(int* pData, int nLen,HDC hdc)
{
int nCount = 0;
boolean bNewLine = true;
while (nCount < nLen)
{
int x = pData[nCount++];
int y = pData[nCount++];
if (x == -1 && y == 0)
{
bNewLine = true;
//stop draw and move to next point
}else if (x == -1 && y == -1)
{
//stop draw and exit
}
if (bNewLine)
{
MoveToEx(hdc,x,y,NULL);
}
LineTo(hdc,x,y);
bNewLine = false;
}
}
阅读(1453) | 评论(0) | 转发(0) |