Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <stdlib.h>
- // целое число в строку
- char* IntStr(char *buf, int n, char flag, char flag3, int width);
- // вещественное число в строку
- char* DoubleStr(char *buf, double n, char flag);
- char* Int8Str(char *buf, int n);
- int LenInt(int n);
- int Len8Int(int n, int base);
- int LenDouble(double n);
- int subspecifier(char *str, int p);
- char *convert(unsigned int num, int base, int symb, char flag2, char flag3, int width);
- void f_b(char *str, ...)
- {
- va_list ap; /* point on next unnamed argument */
- va_start(ap, str); /* set 'ap' on 1-st unnamed argument */
- char *t = str;
- char output[100] ="";
- for (char *p = str; *p; p++)
- {
- //int curr = p - t;
- //printf("%d\n" , curr);
- if (*p !='%')
- {
- putchar(*p);
- //output[strlen(output)] = *p;
- output[strlen(output)] = *p;
- //printf("^^^%c^^^\n", t);
- continue;
- }
- char flag1 = -1;
- char flag2 = -1;
- char flag3 = -1;
- int width = -1;
- int prec = -1;
- /*
- // if ((*p == '-' || *p == '+' || *p == ' ' || *p == '#' || *p == '0') && *(p+1) == )
- if (*(p+1) != 'c' && *(p+1) != 'd' && *(p+1) != 'i' && *(p+1) != 'e' && *(p+1) != 'E' && *(p+1) != 'f' && *(p+1) != 'g' && *(p+1) != 'G' && *(p+1) != 'o' && *(p+1) != 's' && *(p+1) != 'u' && *(p+1) != 'x' && *(p+1) != 'X' && *(p+1) != 'p' && *(p+1) != 'n' && *(p+1) != '%') {
- ++p;
- char char1 = *p;
- printf("%c", char1);
- }
- */
- //printf("%c", *p);
- //if (subspecifier(str, *(p+1)) ==1) {
- // p++;
- //}
- ++p;
- while(1) {
- if (*p == '-' || *p == '+' || *p == ' ' || *p == '#' || *p == '0') {
- if (*p == '+' || *p == ' ') {
- flag1 = *p;
- printf("%c", flag1);
- }
- if (*p == '#'){
- flag2 = *p;
- printf("%c", flag2);
- }
- if (*p == '0' || *p == '-'){
- flag3 = *p;
- printf("%c", flag3);
- }
- ++p;
- } else {
- break;
- }
- }
- if (*p == '*') {
- width = va_arg(ap, int);
- printf("!%d!", width);
- ++p;
- }
- if(*p == '.' && *(p+1) == '*') {
- prec = va_arg(ap, int);
- printf("!%d!", prec);
- p+=2;
- }
- switch(*p)
- {
- case 'c':
- {
- char ival;
- ival = (char) va_arg(ap, int);
- printf ("%c", ival);
- //strcat(output, ival);
- if (width != -1){
- for (int i = 0; i < width - 1; ++i)
- output[strlen(output)] = ' ';
- }
- output[strlen(output)] = ival;
- break;
- }
- case 'd':
- {
- int ival = 0;
- ival = va_arg(ap, int);
- char buf[21]="";
- IntStr(buf, ival, flag1, flag3, width);
- printf("%s", buf);
- strcat(output, buf);
- break;
- }
- case 'i':
- {
- int ival = 0;
- ival = va_arg(ap, int);
- char buff[21]="";
- IntStr(buff, ival, flag1, flag3, width);
- printf("%s", buff);
- strcat(output, buff);
- break;
- }
- case 'f':
- {
- double dval = 0.;
- dval = va_arg(ap, double);
- char buf[21]="";
- DoubleStr(buf, dval, flag1);
- printf("%s", buf);
- strcat(output, buf);
- break;
- }
- case 'o':
- {
- int ival = 0;
- ival = va_arg(ap, int);
- int len = Len8Int(ival , 8);
- printf("%s", convert(ival, 8, 1, flag2, flag3, width));
- strcat(output, convert(ival, 8, 1, flag2, flag3, width));
- break;
- }
- case 'x':
- {
- int ival = 0;
- ival = va_arg(ap, int);
- int len = Len8Int(ival , 16);
- printf("%s", convert(ival, 16, 1, flag2, flag3, width));
- strcat(output, convert(ival, 16, 1, flag2, flag3, width));
- break;
- }
- case 'X':
- {
- int ival = 0;
- ival = va_arg(ap, int);
- int len = Len8Int(ival , 16);
- printf("%s", convert(ival, 16, 0, flag2, flag3, width));
- strcat(output, convert(ival, 16, 0, flag2, flag3, width));
- break;
- }
- case 's':
- {
- char *buf = malloc(sizeof(char));
- int k = 0;
- for(char *sval = va_arg(ap, char *); *sval; sval++) {
- k++;
- if (prec < k)
- continue;
- printf("%c", *sval);
- //strcat(output, *sval);
- buf = realloc(buf, k*sizeof(char));
- *(buf+k-1)=*sval;
- //output[strlen(output)] = *sval;
- }
- int l = strlen(buf);
- if (flag3 != '-') {
- for (int i = 0; i < width - l; ++i)
- strcat(output, " ");
- }
- printf("%s", buf);
- strcat(output, buf);
- free(buf);
- if (flag3 == '-') {
- for (int i = 0; i < width - l; ++i)
- strcat(output, " ");
- }
- // putchar(*sval);
- break;
- }
- case 'n':
- {
- break;
- }
- case '%':
- {
- printf("%%");
- strcat(output, "%");
- break;
- }
- default:
- {
- putchar(*p);
- output[strlen(output)] = *p;
- break;
- }
- }
- }
- va_end(ap); /* clean all */
- printf("\n");
- printf("%s", output);
- }
- int main ()
- {
- f_b("%*d %+*d % f %+-*d %% %n %*.*s %-#*.*o %.*i %*c %*x %#X\n",9, 28,9, -286, 4.0,6, 100,10,3, "Hello", 9, 666, 45674,333, 4, 8,'Q',7, 1023, 1023);
- //f_b("%0#*x %#X\n",8, 1023, 1023);
- //f_b("%+d %f", 55, 4.45);
- //f_b("Hello");
- //f_b("%+*d",9, 28);
- return 0;
- }
- char* IntStr(char *buf, int n, char flag, char flag3, int width) {
- //char *index = buf;
- int len = LenInt(n);
- if (prec != -1)
- len = prec;
- int full_len = len;
- int i = 0;
- int j = 0;
- //printf("@%d@", len);
- if (flag == ' ' && n >= 0 || flag == '+' && n > 0 || n < 0)
- full_len++;
- //printf("&%d&", full_len);
- if (flag3 == -1 && width != -1){
- for (j; j < width - full_len; ++j) {
- *(buf+j) = ' ';
- //printf("(%d)", buf+j);
- }
- }
- if (flag == ' ' && n >= 0) {
- buf+=j;
- *(buf++) = ' ';
- buf-=j;
- }
- if (flag == '+' && n > 0) {
- buf+=j;
- *(buf++) = '+';
- buf-=j;
- }
- if (n < 0) {
- buf+=j;
- *(buf++) = '-';
- buf-=j;
- n = -n;
- }
- if (n == 0) {
- buf+=j;
- *(buf++) ='0';
- buf-=j;
- }
- if (flag3 == '0' && width != -1) {
- for (j; j < width - full_len; ++j){
- *(buf+j) = '0';
- }
- }
- while (n > 0) {
- *(buf+j+len-i-1) = (n % 10) | '0';
- n /= 10;
- i++;
- }
- if (flag3 == '-' && width != -1) {
- for (j; j < width - full_len; ++j)
- *(buf+len-1+j+1) = ' ';
- }
- //*(buf+len) = '\0';
- return(buf);
- }
- int LenInt(int n) {
- int len = 0;
- if (n < 0)
- n = n * (-1);
- while(n > 0) {
- len++;
- n /= 10;
- }
- return len;
- }
- int Len8Int(int n, int base) {
- int len = 0;
- while(n > 0) {
- len++;
- n /= base;
- }
- return len;
- }
- char* DoubleStr(char *buf, double n, char flag) {
- int len = LenInt(n);
- IntStr(buf, (int)n, flag, -1, -1);
- strcat(buf, ".");
- if (n < 0) {
- n = n * (-1);
- }
- n = n - (int)n;
- for (int i = 0; i < 6; i++) {
- n *= 10;
- //char c = n + '0';
- //strcat(buf, &c);
- buf[strlen(buf)] = n + '0';
- n = n - (int)n;
- }
- return buf;
- }
- int LenDouble(double n) {
- }
- char* Int8Str(char *buf, int n) {
- //char *index = buf;
- int len = Len8Int(n, 8);
- int i = 0;
- if (n < 0) {
- *buf++ = '-';
- n = -n;
- }
- while (n > 0) {
- *(buf+len-i-1) = (n % 8) | '0';
- n /= 8;
- i++;
- }
- *(buf+len) = '\0';
- return(buf);
- }
- int subspecifier(char *str, int p) {
- int f = 0;
- if (p != 'c' && p != 'd' && p != 'i' && p != 'e' && p != 'E' && p != 'f' && p != 'g' && p != 'G' && p != 'o' && p != 's' && p != 'u' && p != 'x' && p != 'X' && p != 'p' && p != 'n' && p != '%') {
- f = 1;
- printf("%c", p);
- }
- return f;
- }
- char *convert(unsigned int num, int base, int symb, char flag2, char flag3, int width)
- {
- //printf("#%d#", width);
- printf("%d", flag3);
- static char Representation[] = "0123456789ABCDEF";
- static char Representation1[] = "0123456789abcdef";
- static char buffer[50];
- char *ptr;
- ptr = &buffer[49];
- *ptr = '\0';
- int len = Len8Int(num , base);
- printf("len=%d", len);
- if (flag2 == '#') {
- if (base == 8)
- len+=1;
- else if (base == 16)
- len+=2;
- }
- if (flag3 == '-' && width != -1) {
- printf("$%d$", width);
- for (int i = 0; i < width - len; ++i)
- *--ptr = ' ';
- }
- do
- {
- if (symb == 0)
- *--ptr = Representation[num%base];
- else
- *--ptr = Representation1[num%base];
- num /= base;
- }while(num != 0);
- if (flag3 == '0' && width != -1) {
- printf("$%d$", width);
- for (int i = 0; i < width - len; ++i)
- *--ptr = '0';
- }
- if (flag2 == '#') {
- if (base == 8) {
- *--ptr = '0';
- } else if (base == 16 && symb == 0){
- *--ptr = 'X';
- *--ptr = '0';
- } else if (base == 16 && symb == 1){
- *--ptr = 'x';
- *--ptr = '0';
- }
- }
- if (flag3 == -1 && width != -1) {
- printf("$%d$", width);
- for (int i = 0; i < width - len; ++i)
- *--ptr = ' ';
- }
- return(ptr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement