U E D R , A S I H C RSS

Our Major Lang IsCAndCPlus Plus/print/허준수

#include <iostream>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>

using namespace std;

void print(const char* first,...)
{
	va_list l;
	va_start(l, first);

	for(int i = 0; i<strlen(first); i++) {
		if(first[i] != '%' && first[i] != '@')
			putchar(first[i]);
		else if(first[i] == '%')
		{
			i++;
			if(first[i] == 'd')
			{
				int d = va_arg(l, int);
				char str[100];
				itoa(d, str, 10);
				fputs(str, stdout);
			}
			else if (first[i] == 's')
			{
				fputs(va_arg(l, char*), stdout);
			}
			else if(first[i] == 'f') {
				double f = va_arg(l, double);
				char* strf;				
				int decimal, sign;
				strf = _fcvt(f, 5, &decimal, &sign);
				if(sign == 1) putchar('-');
				for(int j = 0; j<decimal; j++) 
					putchar(*(strf+j));
				if(decimal == 0) putchar('0');
					
				putchar('.');

				for(int k = j; k<5; k++)
					putchar(*(strf+k));
			}
			else {
				char temp[100]; 
				int itemp = va_arg(l, int);
				itoa(itemp, temp, 10);
				int len = strlen(temp);
				int num_underBar = atoi(&first[i]);
				for(int m = 0; m<num_underBar; m++) {
					if(len < num_underBar) {
						putchar(' ');
						num_underBar--;
						continue;
					} else if(len >= num_underBar) {
						fputs(temp, stdout);				
						break;
					}				
				}
				i++;
			}
		}
		else if(first[i] == '@') {
			i++;
			if(first[i] == 'd') {
				int *i_array = va_arg(l, int*);
				int index = va_arg(l, int);
				char str[100];
				putchar('{');
				for(int x = 0; x<index; x++) {
					itoa(*(i_array + x), str, 10);
					fputs(str, stdout);
					if(x != index -1) putchar(',');
				}
				putchar('}');
			}	
			else if(first[i] == 's') {
				char** s_array = va_arg(l, char**);
				int index = va_arg(l, int);
				putchar('{');
				for(int y = 0; y<index; y++){
					fputs(*(s_array + y), stdout);
					if(y != index -1) putchar(',');
				}
				putchar('}');
			}
			else if(first[i] == 'f') {
				double* d_array = va_arg(l, double*);
				int index = va_arg(l, int);
				int decimal, sign;
				char* strf;
				putchar('{');
				for(int z=0; z< index; z++) {
					strf = _fcvt(*(d_array + z), 5, &decimal, &sign);
					if(sign == 1) putchar('-');
					if(decimal == 0) putchar('0');
					for(int j = 0; j<decimal; j++) 					
						putchar(*(strf+j));
					
					putchar('.');

					for(int k = j; k<5; k++)
						putchar(*(strf+k));
					
					if(z != index -1) putchar(',');
				}
				putchar('}');
			}
		}	
	
	}

	putchar('\n');

	va_end(l);
}

int main()
{
	{
		int a = 10; 
		char *b = "example"; 
		double c = 10.5;
		print("number: %d, string: %s, real number: %f\n", a, b, c); 
	}

	{
		int a[3] = { 1, 2, 3 }; 
		char *b[2] = { "abc", "def" }; 
		print("array: @d\n", a, 3); 
		print("array: @s\n", b, 2); 
	}

	{
		int a = 123; 
		int b = 12345; 
		int c = 12345678; 
		print("number: %5d\n", a); 
		print("number: %5d\n", b); 
		print("number: %5d\n", c); 
	}
	
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:57
Processing time 0.0157 sec