自编scanf函数

<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;
		  }
	   }	
	  }
	}
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值