printfの自作
Posted feedbacks - C
printf関数を自作してください。
printfの説明は不要だと思います。とりあえずWikiPediaのリンクをはっておきます。
実際にはsprintf関数を作ってください。
注意事項
- 標準でついているprintf系関数の使用禁止
- 標準でついているライブラリ以外の使用禁止
- 引数・返り値等の仕様はできるだけ似せればよい
可変長引数など、言語によっては難しい/不可能な仕様もありますが、いろいろ工夫して本物に近づくようにしてみてください。
1 2 3 4 5 6 7 | #include <string.h>
// なにもフォーマットしてない
int mysprintf(char *str, const char *format, ... ){
strcpy(str, format);
return strlen(str);
}
|
超簡易板です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | /*-
* The MIT License
*
* Copyright (c) 2008 虹原いんく
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdarg.h>
char *itoa( int num ,char* buf, const int step)
{
const char table[] = "0123456789abcdef";
char *p = buf;
int tmp;
if( num < 0)
{
*p++ = '-';
num = - num;
}
for(tmp=num;tmp>0;tmp /= step) p++;
*p='\0';
for(tmp=num;tmp>0;tmp /= step) *--p=table[tmp%step];
return buf;
}
int mysprintf(char *str, const char *format, ... )
{
char buf[64];
char *s, *b;
const char *fp, *np;
char *cp;
int num;
va_list va;
va_start(va, format);
fp= format;
s= str;
b= str;
for(; *fp!='\0';) {
if( *fp != '%' ) {
*s++ = *fp++;
}
else {
*fp++; /* skip % */
switch((int)(unsigned char) *fp)
{
case '%':
*s++ = *fp++;
continue;
break;
case 's':
np = (char *)va_arg( va, char * );
break;
case 'b':
num = (int)va_arg( va, int );
np = itoa( num , buf, 2 );
break;
case 'o':
num = (int)va_arg( va, int );
np = itoa( num , buf, 8 );
break;
case 'x':
num = (int)va_arg( va, int );
np = itoa( num , buf, 16 );
break;
case 'd':
num = (int)va_arg( va, int );
np = itoa( num , buf, 10 );
break;
}
*fp++; /* skip %V */
for(; *np!='\0';*s++ = *np++);
}
}
*s = '\0';
va_end(va);
return (s - str);
}
/* ------------------------------------------------------------------ */
int main()
{
char str[256];
int ret;
ret = mysprintf(str, "%s %d%%%s%%", "ABCD", -12345678, "EFGHIJK" );
printf("len = %d [%s]", ret, str );
return 0;
}
|


yappy
#4119()
[
C
]
Rating-4/18=-0.22
Rating-4/18=-0.22-0+
[ reply ]