More actions
imported>Unknown No edit summary |
(Repair batch-0003 pages from live compare) |
||
| Line 29: | Line 29: | ||
if(*n=='d') | if(*n=='d') | ||
{ | { | ||
char st | char st[IntSize]; | ||
itoa(va_arg(l,int), st, 10); | itoa(va_arg(l,int), st, 10); | ||
if((number-48)>strlen(st)) | if((number-48)>strlen(st)) | ||
| Line 64: | Line 64: | ||
if(*n=='d') | if(*n=='d') | ||
{ | { | ||
char st | char st[IntSize]; | ||
itoa(va_arg(l,int), st, 10); | itoa(va_arg(l,int), st, 10); | ||
fputs(st, stdout); | fputs(st, stdout); | ||
| Line 102: | Line 102: | ||
for(int i=0; i<count; i++) | for(int i=0; i<count; i++) | ||
{ | { | ||
char st | char st[IntSize]; | ||
itoa(number | itoa(number[i], st, 10); | ||
fputs(st, stdout); | fputs(st, stdout); | ||
if(i!=(count-1)) | if(i!=(count-1)) | ||
| Line 117: | Line 117: | ||
for(int i=0; i<count; i++) | for(int i=0; i<count; i++) | ||
{ | { | ||
fputs(ch | fputs(ch[i], stdout); | ||
if(i!=(count-1)) | if(i!=(count-1)) | ||
putchar(','); | putchar(','); | ||
| Line 130: | Line 130: | ||
for(int i=0; i<count; i++) | for(int i=0; i<count; i++) | ||
{ | { | ||
char *st=_fcvt(number | char *st=_fcvt(number[i], limit, &decimal, &sign); | ||
if(sign==1) | if(sign==1) | ||
putchar('-'); | putchar('-'); | ||
| Line 165: | Line 165: | ||
print("정수 : %d, 실수 : %f, 문자열 : %s\n", d, f, s); | print("정수 : %d, 실수 : %f, 문자열 : %s\n", d, f, s); | ||
int a | int a[3] = { 1, 2, 3 }; | ||
char *b | char *b[2] = { "abc", "def" }; | ||
double c | double c[2] = { -1.22342, 43.232 }; | ||
print("array: @d\n", a, 3); | print("array: @d\n", a, 3); | ||
print("array: @s\n", b, 2); | print("array: @s\n", b, 2); | ||
| Line 188: | Line 188: | ||
* 허준수 - | * 허준수 - | ||
* 위키의 메뉴중 '과거'를 누르면 과거의 상태를 보거나 복구할 수 있습니다. 제가 복구했습니다.^^ - [[조현태]] | * 위키의 메뉴중 '과거'를 누르면 과거의 상태를 보거나 복구할 수 있습니다. 제가 복구했습니다.^^ - [[조현태]] | ||
* 내가 다시 올려놨삼~ 중간에 없어진게 좀 있어서~ - | * 내가 다시 올려놨삼~ 중간에 없어진게 좀 있어서~ - 하기웅 | ||
---- | ---- | ||
[[OurMajorLangIsCAndCPlusPlus/print]] | [[OurMajorLangIsCAndCPlusPlus/print]] | ||
Latest revision as of 00:29, 27 March 2026
소감
puts를 통해 글을 찍는 방법을 배웠고 fputs, putchar, _fcvt등에 대해서 알게 되었다. 코드가 지져분하게 만들어졌지만 앞의 내용과 약간의 C++ 공부했던 기억을 되살리면 목적을 달성하는데는 많은 어려움은 없었다. 그러나 코드가 깔끔하지 못해 아쉽다.
코드
#include <iostream>
#include <stdarg.h>
#include <stdlib.h>
using namespace std;
int decimal, sign;
#define limit 5
#define IntSize 12
void print(char *n, ...)
{
va_list l;
va_start(l,n);
while(*n)
{
if(*n=='%')
{
n++;
if(isdigit(*n))
{
char number = *n;
n++;
if(*n=='d')
{
char st[IntSize];
itoa(va_arg(l,int), st, 10);
if((number-48)>strlen(st))
for(int i=0; i<(number-48-strlen(st)); i++)
putchar(' ');
fputs(st, stdout);
}
else if(*n=='s')
fputs(va_arg(l,char*), stdout);
else if(*n=='f')
{
char *st=_fcvt(va_arg(l,double), limit, &decimal, &sign);
if(sign==1)
putchar('-');
for(int k=decimal+limit; k>=0; k--)
{
if(decimal==0)
putchar('.');
else
putchar(*(st++));
decimal--;
}
}
else
{
putchar('%');
putchar(number);
putchar(*n);
}
n++;
}
else
{
if(*n=='d')
{
char st[IntSize];
itoa(va_arg(l,int), st, 10);
fputs(st, stdout);
}
else if(*n=='s')
fputs(va_arg(l,char*), stdout);
else if(*n=='f')
{
char *st=_fcvt(va_arg(l,double), limit, &decimal, &sign);
if(sign==1)
putchar('-');
for(int k=decimal+limit; k>=0; k--)
{
if(decimal==0)
putchar('.');
else
putchar(*(st++));
decimal--;
}
}
else
{
putchar('%');
putchar(*n);
}
n++;
}
}
else if(*n=='@')
{
n++;
if(*n=='d')
{
int *number = va_arg(l,int*);
int count = va_arg(l, int);
putchar('{');
for(int i=0; i<count; i++)
{
char st[IntSize];
itoa(number[i], st, 10);
fputs(st, stdout);
if(i!=(count-1))
putchar(',');
}
putchar('}');
}
else if(*n=='s')
{
char **ch= va_arg(l,char**);
int count = va_arg(l, int);
putchar('{');
for(int i=0; i<count; i++)
{
fputs(ch[i], stdout);
if(i!=(count-1))
putchar(',');
}
putchar('}');
}
else if(*n=='f')
{
double *number = va_arg(l, double*);
int count = va_arg(l, int);
putchar('{');
for(int i=0; i<count; i++)
{
char *st=_fcvt(number[i], limit, &decimal, &sign);
if(sign==1)
putchar('-');
for(int k=decimal+limit; k>=0; k--)
{
if(decimal==0)
putchar('.');
else
putchar(*(st++));
decimal--;
}
if(i!=(count-1))
putchar(',');
}
putchar('}');
}
else
{
putchar('@');
putchar(*n);
}
n++;
}
else
putchar(*(n++));
}
}
int main()
{
int d=1234;
double f=-2.12321;
char *s="String";
print("정수 : %d, 실수 : %f, 문자열 : %s\n", d, f, s);
int a[3] = { 1, 2, 3 };
char *b[2] = { "abc", "def" };
double c[2] = { -1.22342, 43.232 };
print("array: @d\n", a, 3);
print("array: @s\n", b, 2);
print("array: @f\n", c, 2);
int n1 = 123;
int n2 = 12345;
int n3 = 12345678;
print("number: %7d\n", n1);
print("number: %7d\n", n2);
print("number: %7d\n", n3);
return 0;
}
헉!기웅아.. 내가 위키 사용법을 잘 몰라서 올리는 법좀 붙여쓰기해서 사용했더니.. 니꺼 다 날라갔다...-..ㅡ;; 미안.... ^^;;
- 허준수 -
- 위키의 메뉴중 '과거'를 누르면 과거의 상태를 보거나 복구할 수 있습니다. 제가 복구했습니다.^^ - 조현태
- 내가 다시 올려놨삼~ 중간에 없어진게 좀 있어서~ - 하기웅