VC6冒泡排序、回文判断的一种实现
/*****************************************************************************
** 函数 int SortInt32Number(char *szInputFile,char *szOutputFile)
**
** 目的 输入文件中的数字排序输出到输出文件
**
** 关联 --
**
** 参数 szInputFile 输入文件名
** szOutputFile 输出文件名
**
** 返回值 成功返回 0; 失败返回 其他值
**
** 版本号 1.0-2004.10.26 9:18
****************************************************************************/
int SortInt32Number(char *szInputFile,char *szOutputFile)
{
int i = 0;
int j = 0;
int k = 0;
int iReadDataNumber = 0; //读入整数的数目
int iArrayInput[128]; //读入整数的缓存区
char szInput[128][16]; //读入整数的字符型缓存区
FILE *fptrLog; //文件指针
CString strMessage; //排序完成后显示的信息
CString strTmp; //临时变量,格式化整数为字符时使用
if (szInputFile == NULL)
{
return -1;
}
if (szOutputFile == NULL)
{
return -2;
}
for (i=0; i<128; i++) //参数初始化
{
iArrayInput[i] = 0;
}
fptrLog = fopen(szInputFile, "r" );
if(fptrLog == NULL) //对文件存在性做判断
{
AfxMessageBox("File Not Exist!");
return -3;
}
i = 0;
while (!feof(fptrLog)) //读入数据,不判断数据是否有效,最多128行
{
try
{
fscanf(fptrLog, "%s /n",szInput[i]);
iArrayInput[i] = atoi(szInput[i]); //读入的字符串行均被转化成整型,字符串被转化为0
}
catch(CException err)
{
TCHAR szErrorDetail[255];
CString strFormatted;
err.GetErrorMessage(szErrorDetail, 255);
strFormatted = _T("The error occure because of this error: ");
strFormatted += szErrorDetail;
AfxMessageBox(strFormatted);
i = 0;
break;
}
i += 1;
if (i >= 128)
{
break;
}
if (ferror(fptrLog))
{
AfxMessageBox("Read Input File Error!");
break;
}
}
iReadDataNumber = i;
if (iReadDataNumber < 1)
{
return -4;
}
else
{
//保存输入完整信息,用于最后显示
strMessage = _T("The Sort Detail Information: ");
strTmp.Format("/n Input Data Number = %d /n",iReadDataNumber);
strMessage += strTmp;
strMessage += _T("Input Data: ");
for (i=0; i<iReadDataNumber; i++)
{
strTmp.Format(" %d",iArrayInput[i]);
strMessage += strTmp;
}
strMessage += "/n";
for (i=0; i<iReadDataNumber; i++) //冒泡排序过程
{
for (j=0; j<iReadDataNumber-1-i; j++)
{
if (iArrayInput[j] > iArrayInput[j+1])
{
k = iArrayInput[j+1];
iArrayInput[j+1] = iArrayInput[j];
iArrayInput[j] = k;
}
}
}
//排序结果输出到当前路径 Out.txt 文件
fptrLog = fopen(szOutputFile, "w+" );
if(fptrLog == NULL)
{
AfxMessageBox("Create File Error!");
return -5;
}
for (i=0; i<iReadDataNumber; i++)
{
fprintf(fptrLog, "%d /n",iArrayInput[i]);
}
fclose(fptrLog);
//保存排序后完整信息,用于最后显示
strMessage += _T("After Sort: ");
for (i=0; i<iReadDataNumber; i++)
{
strTmp.Format(" %d",iArrayInput[i]);
strMessage += strTmp;
}
strMessage += "/n";
strMessage = _T("Sort Complete... /n") + strMessage;
//显示排序前后完整信息
AfxMessageBox(strMessage,MB_OK);
return 0;
}
}
int IsPalindrome(char *szJudgeString)
{
if (szJudgeString == NULL)
{
return -1;
}
else if(strlen(szJudgeString) == 0)
{
return -2;
}
else
{
char *szHead = szJudgeString;
char *szEnd = szJudgeString + (strlen(szJudgeString) - 1);
while(*szHead++ == *szEnd--)
;
if (szHead >= szEnd)
{
return 1;
}
else
{
return 0;
}
}
}
// 调用冒泡
SortInt32Number(_T(".//Input.txt"),_T(".//Output.txt"));
///|///
// - - //
( @ @ )
+----------------------oOOo-(_)-oOOo---------------------+
| |
| |
| |
| |
| |
| Oooo |
+---------------------- oooO---( )---------------------+
( ) ) /
/ ( (_/
/_)