Advertisement
JewishCat

va_arg_23v

Jan 14th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <cstdlib>
  4. #include <stdarg.h>
  5. #include <iostream>
  6. #include <vector>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. void sc(char *format, ...);
  12.  
  13. struct complex
  14. {
  15.     int real, img;
  16. };
  17. int main() {
  18.     sc("%d%f%r%i", 9, 8.5, 1, -3);
  19.     system("pause");
  20.     return 0;
  21. }
  22.  
  23. void sc(char *format, ...) {
  24.     int i_val;
  25.     double d_val;
  26.     struct complex a;
  27.     va_list ap;
  28.     va_start(ap, format);
  29.     for (char *p = format; *p; p++)
  30.     {
  31.         if (*p == '%')
  32.         {
  33.             switch (*++p)
  34.             {
  35.             case 'd': i_val = va_arg(ap, int);
  36.                 cout << i_val << " "; break;
  37.             case 'f': d_val = va_arg(ap, double);
  38.                 cout << d_val << " "; break;
  39.             case 'r': a.real = va_arg(ap, int);
  40.                 cout << a.real; break;
  41.             case 'i': a.img = va_arg(ap, int);
  42.                 cout << a.img <<"i"; break;
  43.             default: cout << "ERROR"; break;
  44.            
  45.             }
  46.         }
  47.         else cout << *p << " ";
  48.     }
  49.     va_end(ap);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement