Advertisement
Guest User

Untitled

a guest
Oct 29th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #ifndef PRINTF_SIMULATOR_H_INCLUDED
  2. #define PRINTF_SIMULATOR_H_INCLUDED
  3.  
  4. #include<iostream>
  5. #include<cstdarg>
  6. #include<cstdlib>
  7. #include<cstring>
  8. using namespace std;
  9.  
  10. void move_forward(char y[], int position);
  11.  
  12. void print(char x[], ...)
  13. {
  14.     va_list contor;
  15.     char number[15], y[1000];
  16.  
  17.  
  18.     int aux, i, k=0;
  19.  
  20.     for(int i=0; x[i]; i++)
  21.     {
  22.         y[i] = x[i];
  23.     }
  24.     y[strlen(x)] = '\0';
  25.  
  26.     va_start(contor,x);
  27.  
  28.     for(i=0; y[i]!='\0'; i++)
  29.     {
  30.  
  31.         if((y[i] == '%')&&(y[i+1] == 'd'))
  32.         {
  33.  
  34.               aux  = va_arg(contor, int);
  35.  
  36.               itoa(aux, number, 10);
  37.  
  38.               if(strlen(number) > 1)
  39.               {
  40.                   for(int p=1; p<=strlen(number)-2 ; p++)
  41.                   {
  42.                        move_forward(y, i);
  43.                   }
  44.                   k = i;
  45.                   for(int q = 0; number[q]; q++)
  46.                   {
  47.                      y[k] = number[q];
  48.                      k++;
  49.                   }
  50.                   i = i+strlen(number)-2;
  51.  
  52.              }
  53.              else if(strlen(number) == 1)
  54.              {
  55.                  y[i] = number[0];
  56.  
  57.                  for(int p = i+1; y[p]; p++)
  58.                  {
  59.                      y[p] = y[p+1];
  60.                  }
  61.  
  62.              }
  63.  
  64.  
  65.         }
  66.  
  67.      }
  68.      cout<<y;
  69.  
  70.  
  71.         va_end(contor);
  72. }
  73.  
  74. void move_forward(char y[], int position)
  75. {
  76.         int n;
  77.         n = strlen(y);
  78.         for(int l = n; l>=position; l--)
  79.         {
  80.                 y[l+1] = y[l];
  81.         }
  82.  
  83. }
  84. #endif // PRINTF_SIMULATOR_H_INCLUDED
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. /*   Code::Example::
  93.  
  94. #include<iostream>
  95. #include"printf_simulator.h"
  96. using namespace std;
  97. int main()
  98. {
  99.       int a, b;
  100.       a = 5;
  101.       b = 2;
  102.       print("This is the value %d and this is %d", a, b);
  103.       return 0;
  104. }
  105.  
  106. It will print on the screen: This is the value 5 and this is 2
  107.  
  108.  
  109.  
  110. _________Designed, Developped, Created by [[<<<Ionita Cosmin>>>]]_________(http://www.kosvip.webs.com)  -> For more information (including contact)______*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement