~cpp #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <ctype.h> #include <string.h> void print_d(int temp, int sort) { char buffer[20]; itoa(temp,buffer,10); if(strlen(buffer) < sort) { for(int i = 0; i < sort - strlen(buffer); i++) fputchar(' '); } fputs(buffer,stdout); } void print_d_a(int * temp, int num) { fputs("{ ", stdout); for(int i =0; i < num; i++) { print_d(*(temp+i), 0); if(i != num -1) fputs(", ", stdout); else fputs(" }", stdout); } } void print_s(const char * temp, int sort) { if(strlen(temp) < sort) { for(int i = 0; i < sort - strlen(temp); i++) fputchar(' '); } fputs(temp, stdout); } void print_s_a(const char ** temp, int num) { fputs("{ ", stdout); for(int i =0; i < num; i++) { print_s(*(temp+i), 0); if(i != num -1) fputs(", ", stdout); else fputs(" }", stdout); } } void print_f(double temp, int sort) { int decimal, sign; char *buffer; buffer = _fcvt( temp, 7, &decimal, &sign ); if(sign) fputchar('-'); for(int i = 0; i < decimal;i++) fputchar(*buffer++); fputchar('.'); fputs(buffer, stdout); } void print_f_a(double * temp, int num) { fputs("{ ", stdout); for(int i =0; i < num; i++) { print_f(*(temp+i), 0); if(i != num -1) fputs(", ", stdout); else fputs(" }", stdout); } } void print(const char * list, ...) { va_list args; va_start(args, list); while(*list != '\0') { if(*list =='%') { int sort = 0; if(isdigit(*++list)) { sort = *list - 48; } else list--; if(*++list == 'd') { print_d(va_arg(args, int), sort); } else if(*list == 's') { print_s(va_arg(args, const char *), sort); } else if(*list == 'f') { print_f(va_arg(args, double), sort); } list++; } else if(*list == '@') { int num; if(*++list == 'd') { int * temp = va_arg(args, int *); num = va_arg(args, int); print_d_a(temp, num); } else if(*list == 's') { const char ** temp = va_arg(args, const char **); num = va_arg(args, int); print_s_a(temp, num); } else if(*list == 'f') { double * temp = va_arg(args, double *); num = va_arg(args, int); print_f_a(temp, num); } list ++; } else if(*list == '\n') { putchar(*list++); } else putchar(*list++); } va_end(args); } 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 aa[3] = { 1, 2, 3 }; char *bb[2] = { "abc", "def" }; print("array: @d\n", aa, 3); print("array: @s\n", bb, 2); int aaa = 123; int bbb = 12345; int ccc = 12345678; print("number: %5d\n", aaa); print("number: %5d\n", bbb); print("number: %5d\n", ccc); return 0; }