Advertisement
SUBANGKAR

13 14-2a

Aug 2nd, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <stdarg.h>
  5.  
  6. void putInt(int n)
  7. {
  8.     printf("%d", n);
  9. }
  10.  
  11. void putChar(char c)
  12. {
  13.     printf("%c", c);
  14. }
  15.  
  16. void putFloat(float f)
  17. {
  18.     printf("%.2f", f);
  19. }
  20.  
  21.  
  22.  
  23. int noOfVars(const char *str)
  24. {
  25.     int varNo = 0;
  26.  
  27.     while (*str)
  28.     {
  29.         if (*str++ == '%') varNo++;
  30.     }
  31.  
  32.     return varNo;
  33. }
  34.  
  35.  
  36.  
  37.  
  38. void print(const char *str, ...)
  39. {
  40.  
  41.     va_list list;
  42.  
  43.     va_start(list, str);
  44.  
  45.     while (*str)
  46.     {
  47.         if (*str == '%')
  48.         {
  49.             str++;
  50.  
  51.             if (*str == 'd') putInt((va_arg(list, int)));
  52.             else if (*str == 'c') putChar(va_arg(list, char));
  53.             else if (*str == 'f') putFloat(va_arg(list, float));
  54.  
  55.         }
  56.  
  57.         else
  58.             putChar(*str);
  59.         str++;
  60.     }
  61.  
  62.     va_end(list);
  63.  
  64. }
  65.  
  66.  
  67.  
  68. int main()
  69. {
  70.  
  71.     int Int = 5;
  72.     char Char = 'p';
  73.     print("%d %c ", Int, Char);
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement