/** * Funkce printf * Material pro cviceni z c/c++ */ #include #include #include #include #include /** * Funkce na vyplneni mezerami */ void putSpaces(unsigned int total, unsigned int numlen) { unsigned int j; if (total > numlen) for (j = 0; j < total - numlen; ++j) putchar(' '); } /** * precte ze zadaneho vstupu cele cislo * vraci pocet cislic cisla */ unsigned readNum(char *src, unsigned *out) { unsigned int i = 0; *out = 0; while ((src[i] >= '0') && (src[i] <= '9')) *out = *out * 10 + src[i++]; return i; } /** * funkce nacte potrebne formatovaci parametry a vrati pocet * znaku, ze kterych se formatovaci retezec skladal */ int getType(char *fmt, char *type, unsigned *whole, unsigned *decimal) { unsigned i = 0; if (isalpha(fmt[i])) { *whole = *decimal = 0; *type = fmt[i]; return 1; } else { i += readNum(fmt, whole); if (fmt[i] == '.') { ++i; i += readNum(&fmt[i], decimal); } else *decimal = 0; *type = fmt[i]; ++i; return i; } } /** * zjednodusena implementace funkce printf ze stdio.h */ int myprintf(char *_format, ...) { va_list format; unsigned cnt = 0; size_t fmtsize; unsigned i, j; size_t numsize; // promenne pro data z argumentu int intdata; long longdata; unsigned uintdata; unsigned long ulongdata; char chardata; char *strdata; //formatovaci parametry unsigned decimal; unsigned whole; char type; char buffer[256]; //nejprve inicializujeme pomocne promene a va_list fmtsize = strlen(_format); va_start(format, _format); //tiskneme jednotlive znaky a v pripade, ze narazime // na '%', vezmeme dalsi argument a vytiskneme ho odpovidajicim // zpusobem for(i = 0; i < fmtsize; ++i) { //vytiskneme znak beznym zpusobem if (_format[i] != '%') { putchar(_format[i]); ++cnt; } //musime precist typovana data z argumentu else { ++i; i += getType(&_format[i], &type, &whole, &decimal) - 1; switch(type) { case 'd': intdata = va_arg(format, int); itoa(intdata, buffer, 10); numsize = strlen(buffer); putSpaces(whole, (unsigned)numsize); for (j = 0; j < numsize; ++j) putchar(buffer[j]); cnt += (unsigned)numsize; break; case 'u': uintdata = va_arg(format, unsigned int); ultoa(uintdata, buffer, 10); numsize = strlen(buffer); putSpaces(whole, (unsigned)numsize); for (j = 0; j < numsize; ++j) putchar(buffer[j]); cnt += (unsigned)numsize; break; case 'D': longdata = va_arg(format, long); ltoa(longdata, buffer, 10); numsize = strlen(buffer); putSpaces(whole, (unsigned)numsize); for (j = 0; j < numsize; ++j) putchar(buffer[j]); cnt += (unsigned)numsize; break; case 'U': ulongdata = va_arg(format, unsigned long); ultoa(ulongdata, buffer, 10); numsize = strlen(buffer); putSpaces(whole, (unsigned)numsize); for (j = 0; j < numsize; ++j) putchar(buffer[j]); cnt += (unsigned)numsize; break; case 'c': chardata = va_arg(format, char); putSpaces(whole, 1); putchar(chardata); cnt += 1; break; case 's': strdata = va_arg(format, char *); numsize = strlen(strdata); putSpaces(whole, (unsigned)numsize); for (j = 0; j < numsize; ++j) putchar(strdata[j]); cnt += (unsigned)numsize; break; } } } return cnt; } int main() { int a = 5; myprintf("cislo %D\n\tpismeno %c\ntext: %s\n", -1, 'w', "a sample text line"); return 0; }