Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. //prototypes
  7. int scnf(char* format, ...);
  8. int getl(char*);
  9. int getch();
  10. int ungetch(int);
  11. char * allofc(int n);
  12. void freef(int n);
  13. int conv2i(char*);
  14. double conv2d(char*);
  15.  
  16. void main() {
  17.     double* a = (double *) malloc(200);
  18.     int* d = (int *) malloc(200);
  19.     //char* l = allofc(5);
  20.    
  21.     scnf("%f%d", a, d);
  22.     printf("\n%f %d", *a, *d);
  23. }
  24.  
  25. int scnf(char* format, ...) {
  26.     va_list ap;
  27.     char* b4conv = allofc(100);
  28.     int ival;
  29.     char* stringv;
  30.     int i;
  31.  
  32.     char* il = allofc(1000);
  33.    
  34.     va_start(ap, format);
  35.    
  36.     getl(il);  //getline
  37.        
  38.     for( ; isspace(*++format); )      //skip spaces
  39.         ;
  40.     --format;
  41.         while(*format != '\0'){   //parse format
  42.            
  43.             if(*format++ == '%'){
  44.                
  45.                 while(*format != '%' && *format!= '\0' && !isspace(*format)){
  46.                    
  47.                     switch(*format++){
  48.                            
  49.                         case 'd':
  50.                             i = 0;
  51.                             while(isdigit(*il))
  52.                                 b4conv[i] = il[i++];
  53.                             if(isspace(*il))
  54.                                 il++;
  55.                             *(va_arg(ap, int*)) = conv2i(b4conv); //hurrayy less spaghetti code woohoo
  56.                             freef(i);
  57.                             i = 0;
  58.                             break;
  59.                         case 'f':
  60.                             i = 0;
  61.                             while(isdigit(*il))
  62.                                 b4conv[i] = il[i++];
  63.                             *(va_arg(ap, double*)) = conv2d(b4conv);
  64.                             freef(i);
  65.                             i = 0;
  66.                             break;
  67.                            
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.    
  73. }
  74.  
  75. int getl(char* line) {
  76.     int c, i;
  77.     for(i = 0; (c = getch()) != EOF && c!= '\n'; i++)
  78.         *line++ = c;
  79.     if(c == '\n'){
  80.         *line++ = c;
  81.         *line = '\0';
  82.     } else
  83.         *line = '\0';
  84.     return i;
  85. }
  86.    
  87. #define MAXBUF 4000
  88. char buffer[MAXBUF];
  89. int buffp = 0;
  90.  
  91. int getch() {
  92.     return (buffp > 0) ? buffer[buffp--] : getchar();
  93. }
  94. int ungetch(int a) {
  95.     buffer[buffp++] = a;
  96. }
  97.  
  98. char allocbuf[MAXBUF];
  99. char* allocp = allocbuf;
  100.  
  101. char* allofc(int n) {
  102.     if(allocbuf - allocp + MAXBUF >= n) {
  103.         allocp += n;
  104.         return allocp - n;
  105.     }
  106.     return NULL;
  107. }
  108. void freef(int n) {
  109.     if(allocp - allocbuf - n > 0)
  110.         allocp -= n;
  111. }
  112.  
  113. int conv2i(char* s) {
  114.     int val = 0;
  115.    
  116.     for(int i = 0; s[i] >= '0' && s[i] <= '9'; i++)
  117.         val = 10 * val + (s[i] - '0');
  118.    
  119.     return val;
  120. }
  121.  
  122. double conv2d(char* s) {
  123.     double val = 0;
  124.     double power;
  125.     while(isspace(*s))
  126.         s++;
  127.    
  128.     int i = 0;
  129.     for(; s[i] >= '0' && s[i] <= '9' && s[i] != '\0' && s[i] != '.'; i++)
  130.         val = 10.0 * val + (s[i] - '0');
  131.    
  132.     if(s[i] == '.'){
  133.         int exp = 1.0;
  134.         for(power = 1.0; s[++i] >= '0' && s[i] <= '9' && s[i] != '\0'; ){
  135.             val = val * 10.0 + (s[i] - '0');
  136.             power *= 10.0;
  137.         }
  138.         return val / power;
  139.     }
  140.     return (double) val;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement