home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / 2num / 2numbr.txt
Encoding:
Text File  |  1994-06-17  |  2.3 KB  |  103 lines

  1.  
  2.                                      G. M. HOOVER
  3.                                      COMPUSERVE I.D. - 71230,537
  4.  
  5.  
  6.  
  7.  
  8.             ......NUMBER.TXT REVISITED.....
  9.  
  10.  
  11.      2NUMBR.TXT (A MORE "precision based" RESOLUTION)
  12.  
  13.  
  14.  
  15.  
  16.      THE FOLLOWING CODE WAS PROVIDED ME BY A GENTLEMAN NAMED JOHN M. BILYJ,
  17.  
  18. ALSO OF COMPUSERVE WHO POINTED OUT SEVERAL ASPECTS OF "NUMBER.TXT" WHICH WERE
  19.  
  20. NOT UP TO PAR, ON SEVERAL ASPECTS OF THEIR USAGE.  
  21.  
  22.  
  23.  
  24.      THE MAJOR ASPECT OF "xnge" HAS TO DO WITH MODIFYING THE "getfloat"
  25.  
  26. FUNCTION WITH "double".  ALTHOUGH I'D READ THAT DOUBLE WAS MORE ACCURATE
  27.  
  28. THAN THE FLOAT FUNCTION, THE FOLLOWING "coding" VALIDATES THIS SUPPOSITION.
  29.  
  30.  
  31.      WHEN INPUTTING 123456789.00 WITH "NUMBER.TXT CODING", IT RETURNS SOMETHING 
  32.  
  33. LIKE "123456792.00".  THE FOLLOWING CODE ASSISTS WITH ALLEVIATING THAT, 
  34.  
  35. INCORPORATES "backspacing" TO RESOLVE AN ERROR, AND ALSO DISCONTINUES INPUT 
  36.  
  37. TO GO PAST THE END OF THE ARRAY.
  38.  
  39.      
  40.  
  41.       #include <stdio.h>
  42.       #include <math.h>
  43.       #include <ctype.h>
  44.       #define MAXDIGITS 15
  45.       #define TRUE (1==1)
  46.       #define FALSE (1==0)
  47.       #define END_OF_STRING '\0'
  48.       double GetNum(void);
  49.       
  50.       void main ()
  51.       {
  52.       double rate;
  53.       
  54.        printf("please enter the amount of rate for month\n");
  55.        rate = GetNum();
  56.       
  57.        printf("rate is %.2f", rate);
  58.        return;
  59.       }
  60.       
  61.       double GetNum(void)
  62.       {
  63.       char buf[MAXDIGITS+1];
  64.       int fdecimal = FALSE;
  65.       int ch;
  66.       int i=0;
  67.       
  68.        while((ch=getch())!='\r') {
  69.          if(ch==0x08 && i>0) {
  70.            // process a backspace key
  71.            putch(0x08) ; putch(0x20) ; putch(0x08) ;
  72.            if(buf[i-1]=='.')
  73.              fdecimal=FALSE ;
  74.            buf[i]=0x00 ;
  75.            i-- ;
  76.            continue ;
  77.          }
  78.          if(!isdigit(ch))
  79.            if((fdecimal==TRUE) || (ch!='.'))
  80.              continue;
  81.       
  82.          buf[i]=ch;
  83.          i++;
  84.          putch(ch);
  85.          if(ch == '.')
  86.            fdecimal = TRUE;
  87.          if((fdecimal)?(i>MAXDIGITS+1):(i>MAXDIGITS)) {
  88.            putch(0x08) ; putch(0x20) ; putch(0x08) ;
  89.            i-- ;
  90.          }
  91.        }
  92.        buf[i] = END_OF_STRING;
  93.        putch('\r');
  94.        putch('\n');
  95.        return(atof(buf));
  96.       }
  97.       
  98.      
  99.  
  100.  
  101. BEST REGARDS,
  102.  
  103.                     GARY