<pre name="code" class="html">#include "stdafx.h"
#include <stdio.h>
#include "ZRScanf.h"
int Atoi(char *pStr)
{
int Res = 0;
if( NULL != pStr)
{
if( pStr[0] != '-')
{
for(int nCount = 0; '\0' != pStr[nCount]; nCount++)
{
if( pStr[nCount] < '0' || pStr[nCount] > '9')
{
return 0;
}
}
for(int i = nCount-1, k =1; i >= 0; i--)
{
Res = Res + (pStr[i] - '0')*k;
k*=10;
}
return Res;
}
else
{
for(int nCount = 1; '\0' != pStr[nCount]; nCount++)
{
if( pStr[nCount] < '0' || pStr[nCount] > '9')
{
return 0;
}
}
for(int i = nCount -1, k = 1; i > 0; i--)
{
Res = Res + (pStr[i] - '0')*k;
k*=10;
}
return -Res;
}
}
return Res;
}
void Scanf(const char* pFormate,...)
{
char *pTemp = (char*)&pFormate;
pTemp += 4;
if( NULL != pFormate)
{
while(*pFormate)
{
if( '%' != *pFormate)
{
putchar(*pFormate);
pFormate++;
}
if( '%' == *pFormate)
{
pFormate++;
switch(*pFormate)
{
case 'c':
case 'C':
{
*(*(char**)pTemp) = getchar();
char c = '0';
while( (c = getchar()) != EOF)
{
if( c == ' ' || c == '\n')
{
break;
}
}
pFormate++;
pTemp+=4;
}
break;
case 's':
case 'S':
{
char *pChar = *(char**)pTemp;
for(int i = 0; ;i++)
{
pChar[i] = getchar();
if( ' ' == pChar[i] || '\n' == pChar[i])
{
break;
}
}
pChar[i] = '\0';
pFormate++;
pTemp+=4;
}
break;
case 'd':
case 'D':
{
char szBuf[256] = "";
for(int i =0; ; i++)
{
szBuf[i] = getchar();
if( ' ' == szBuf[i] || '\n' == szBuf[i])
{
break;
}
}
szBuf[i] = '\0';
*(*(int**)pTemp) = Atoi(szBuf);
pFormate++;
pTemp+=4;
}
break;
}
}
}
}
}
自编scanf函数
最新推荐文章于 2024-04-08 11:32:32 发布