Ä£ÄâʵÏÖ printf ¸ñʽ»¯Êä³öµÄ»ù±¾¹¦ÄÜ¡¶Ð¡¼×Óã¿Îºó×÷Òµ
´óÀÐÃÇ Ð¡¼×Óã¿Îºó×÷ÒµÓÿɱä²ÎÊýʵÏÖ printf ¸ñʽ»¯Êä³öµÄ»ù±¾¹¦ÄÜÔÌâÔÚͼƬÀï
ÕâÊÇÎÒдµÄ´úÂ룺
#include <stdio.h>
#include<string.h>
#include<stdarg.h>
void printfint(int);
int numint(int );
void printfstr(char *str);
int numstr( char *str);
int my_itoa(char *p,...);
void printfint(int num)
{
//int count=0;//Ö»Ö´Ðгõʼһ´Î
if(num<0)
{
putchar('-');
num=-num;
}
int temp=num;
int mu=1;
while(temp>9)
{
temp/=10;
mu*=10;//±¶Êý
}
while(mu!=0)
{
putchar(num/mu+'0');
num%=mu;
mu/=10;
}
}
int numint(int num)
{
int i=0;
if (num<0)
{
i++;
num=-num;
}
while(num!=0)
{
num/=10;
i++;
}
return i;
}
void printfstr(char *str)
{
int i=0;
while(str)
{
putchar(str);
i++;
}
}
int numstr( char *str)
{
int i=0;
while(str)
{
i++;
}
return i;
}
int my_itoa(char *p,...)
{
int count=0,i=0;
int darg; char *sarg; char carg;//%d arg==darg
va_list (mst);
va_start(mst,p);
while(p)
{
if (p!='%')
{
putchar(p);
i++;
count++;
}
else
{
switch(p)
{
case'd':
{ darg=va_arg(mst, int);
printfint(darg);
count+= numint(darg);
break;
}
case 'c':
{ carg=va_arg(mst, int);
putchar(carg); //Ϊʲô²»ÄÜÊÇchar
count++;
break;
}
case 's':
{
sarg=va_arg(mst, char* );
printfstr(sarg);
count+=numstr(sarg);
break;
}
}
}
i+=2;
}
va_end(mst);
return count;
}
int main()
{
int i;
i=my_itoa("ÎÒÊÇС¼×Óã%d\n",1314);
my_itoa("Ò»¹²%d¸ö×Ö·û\n",i);
return 0;
}
ÇëÎÊΪʲô´òÓ¡³öÀ´ÊÇ £º
\346\346\345\347\3511314
\270\205d\252\227\246\344\34516\344\345\347
ÄÄÀï³ö´íÁËÄØ
»¹ÓÐ
va_arg(,)
µÚ¶þ¸ö²ÎÊýÊÇʲôÒâ˼¡£ intÊÇ»ñÈ¡¿É±ä²ÎÊýÐβÎÁбíÀïµÄ¶ÔÓ¦µÄ intÀàÐÍÂï
carg=va_arg(mst, int);¡£ÄÇÕâÀïΪʲô²»ÄÜÊÇ va_arg(mst, char)¡£ÄØ
{:5_104:} {:5_104:}
Öú¤±¤Æ¤¯¤À¤µ¤¤¡¡£¡¡¡
Õl¤«¤¤¤ë¤Î Íòǧֻcnm ·¢±íÓÚ 2021-1-15 20:01
Õl¤«¤¤¤ë¤Î
»Ø´ðÄãµÄÎÊÌ⣬»¹µÃÏÈѧÈÕÓ{:5_109:} ·ç¹ýÎÞºÛ1989 ·¢±íÓÚ 2021-1-15 21:52
»Ø´ðÄãµÄÎÊÌ⣬»¹µÃÏÈѧÈÕÓ
´óÀÐÄãÓÖÀ´À² °ïÎÒ¿´¿´ßÂ{:5_102:} ÄǸöÓ¦¸ÃÊÇÊ®Áù½øÖÆ×Ö½ÚÂë°É£¬Òª×Ö·û´®Êä³ö Íòǧֻcnm ·¢±íÓÚ 2021-1-15 22:42
´óÀÐÄãÓÖÀ´À² °ïÎÒ¿´¿´ßÂ
²»ºÃÒâ˼£¬ÎÒ½ñÄ껨Èý¸öÔµ½°ëÄêʱ¼ä£¬Ñ§Ï° windows ±à³Ì£¬Ã¿ÌìÖ»»Ø¸´Ê®¸öÌû×Ó¼°Ò»Ð©¼òµ¥µÄÎÊÌâÒÔά³ÖÉý¼¶ËùÐè»ý·Ö£¬²»ÔÙ»¨Ê±¼äÈ¥µ÷ÊÔ¡¢Ì½ÌÖÉÔ³¤»òÉÔ¸´ÔÓµÄÎÊÌ⣬ÇëÁ½⣡ ·ç¹ýÎÞºÛ1989 ·¢±íÓÚ 2021-1-16 00:18
²»ºÃÒâ˼£¬ÎÒ½ñÄ껨Èý¸öÔµ½°ëÄêʱ¼ä£¬Ñ§Ï° windows ±à³Ì£¬Ã¿ÌìÖ»»Ø¸´Ê®¸öÌû×Ó¼°Ò»Ð©¼òµ¥µÄÎÊÌâÒÔά³ÖÉý ...
ŶŶºÃµÄ Minecraft³ÌÐòÔ³ ·¢±íÓÚ 2021-1-15 22:45
ÄǸöÓ¦¸ÃÊÇÊ®Áù½øÖÆ×Ö½ÚÂë°É£¬Òª×Ö·û´®Êä³ö
ɶÒâ˼{:5_104:} Íòǧֻcnm ·¢±íÓÚ 2021-1-16 11:16
ɶÒâ˼
ÄãÊä³öµÄûÓб»×ªÎª×Ö·û£¬¶øÊÇÖ±½ÓÊä³öÁ˵ײã±àÂë
Ò³:
[1]