home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / pcmag / pp704.arc / TRYXCVT.C < prev    next >
C/C++ Source or Header  |  1987-12-15  |  2KB  |  60 lines

  1. /*
  2.         TRYXCVT.C --- Demonstrates the C formatting 
  3.                       routines ecvt, fcvt, and gcvt
  4.  
  5.         Ray Duncan, October 1987
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. main (int argc, char *argv[])
  12. {       
  13.    double value;                /* value to be converted */
  14.    int digits;                  /* number of digits stored */   
  15.    char buff[80];               /* buffer to hold ASCIIZ string */
  16.    int decptr;                  /* receives position of decimal point */
  17.    int signptr;                 /* receives flag specifying sign */
  18.    char *strptr;                /* receives pointer to string */
  19.  
  20.    while(1)
  21.    {  
  22.                                 /* prompt for number of digits */
  23.         printf("\n\nEnter digits (Q to quit):  ");
  24.  
  25.         gets(buff);             /* read string from keyboard */
  26.  
  27.                                 /* exit if 'Q' or 'q' entered */
  28.         if( buff[0] == 'Q' || buff[0] == 'q') break;     
  29.  
  30.         digits=atoi(buff);      /* convert digits to format */
  31.  
  32.                                 /* prompt for value to convert */
  33.         printf("\nEnter value: ");
  34.  
  35.         gets(buff);             /* read string from keyboard */
  36.  
  37.         value=atof(buff);       /* convert string to 8-byte real value */
  38.  
  39.                                 /* convert value to string with ecvt */                 
  40.         strptr = ecvt(value,digits,&decptr,&signptr);
  41.  
  42.                                 /* display results of conversion */
  43.         printf("\necvt(%f) = %s, decimal position = %d, sign = %d",
  44.                                 value,strptr,decptr,signptr);
  45.  
  46.                                 /* convert value to string with fcvt */
  47.         strptr = fcvt(value,digits,&decptr,&signptr);
  48.  
  49.                                 /* display results of conversion */
  50.         printf("\n\nfcvt(%f) = %s, decimal position = %d, sign = %d",
  51.                                 value,strptr,decptr,signptr);
  52.  
  53.                                 /* convert value to string with gcvt */
  54.         strptr = gcvt(value,digits,buff);
  55.  
  56.                                 /* display results of conversion */
  57.         printf("\n\ngcvt(%f) = %s",value,strptr);
  58.    }
  59. }
  60.