home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06102b < prev    next >
Text File  |  1991-04-15  |  9KB  |  369 lines

  1. /*************************************************
  2. *  file d:\cips\intcvrt.c
  3. *
  4. *  Purpose: These functions convert a string of
  5. *           characters to their number value.
  6. *
  7. *   Modifications:
  8. *           Taken from Jamsa's software package
  9. *           and altered to fit into the computer
  10. *           vision programming 22 August 1986.
  11. **************************************************/
  12.  
  13. #include "d:\cips\numdefs.h"
  14.  
  15.  
  16. get_integer(n)
  17.    int *n;
  18. {
  19.    char string[80];
  20.  
  21.    read_string(string);
  22.    int_convert(string, n);
  23. }
  24.  
  25.  
  26. int_convert (ascii_val, result)
  27.     char *ascii_val;
  28.     int *result;
  29.   {
  30.     int sign = 1;  /* -1 if negative */
  31.     *result = 0;   /* value returned to 
  32.                      the calling routine */
  33.  
  34.     /* read passed blanks */
  35.     while (is_blank(*ascii_val))
  36.        ascii_val++;       /* get next letter */
  37.  
  38.     /* check for sign */
  39.     if (*ascii_val == '-' || *ascii_val == '+')
  40.        sign = (*ascii_val++ == '-') ? -1 : 1;  
  41.        /* find sign */
  42.  
  43. /*
  44. * convert the ASCII representation to the actual
  45. * decimal value by subtracting '0' from each 
  46. * character.
  47. *
  48. * for example, the ASCII '9' is equivalent to 57 
  49. * in decimal. By subtracting '0' (or 48 in decimal) 
  50. * we get the desired value.
  51. *
  52. * if we have already converted '9' to 9 and the 
  53. * next character is '3', we must first multiply 
  54. * 9 by 10 and then convert '3' to decimal and 
  55. * add it to the previous total yielding 93.
  56. */
  57.  
  58.     while (*ascii_val)
  59.      if (is_digit(*ascii_val))
  60.        *result = *result * 10 + 
  61.                   to_decimal(*ascii_val++);
  62.      else
  63.        return (IO_ERROR);
  64.  
  65.  
  66.     *result = *result * sign;
  67.     return (NO_ERROR);
  68.   }
  69.  
  70.  
  71. /************************************************
  72. *    Functions: This file contains
  73. *           get_short
  74. *           short_convert
  75. *
  76. *    Purpose: These functions convert a string of
  77. *             characters to their number value.
  78. *
  79. *    Modifications:
  80. *       Taken from Jamsa's software package
  81. *       and altered to fit into the computer
  82. *       vision programming 22 August 1986.
  83. *************************************************/
  84.  
  85.  
  86. get_short(n)
  87.    short *n;
  88. {
  89.    char string[80];
  90.  
  91.    read_string(string);
  92.    int_convert(string, n);
  93. }
  94.  
  95.  
  96. short_convert (ascii_val, result)
  97.     char *ascii_val;
  98.     short *result;
  99. {
  100.     int sign = 1;  /* -1 if negative */
  101.  
  102.     *result = 0;   /* value returned to the calling
  103.                       routine */
  104.  
  105.     /* read passed blanks */
  106.  
  107.     while (is_blank(*ascii_val))
  108.        ascii_val++;    /* get next letter */
  109.  
  110.     /* check for sign */
  111.     if (*ascii_val == '-' || *ascii_val == '+')
  112.        sign = (*ascii_val++ == '-') ? -1 : 1;
  113.  
  114. /*
  115. * convert the ASCII representation to the actual
  116. * decimal value by subtracting '0' from each 
  117. * character.
  118. *
  119. * for example, the ASCII '9' is equivalent to 57 
  120. * in decimal. By subtracting '0' (or 48 in decimal) 
  121. * we get the desired value.
  122. *
  123. * if we have already converted '9' to 9 and the 
  124. * next character is '3', we must first multiply 
  125. * 9 by 10 and then convert '3' to decimal and 
  126. * add it to the previous total yielding 93.
  127. */
  128.  
  129.     while (*ascii_val){
  130.      if (is_digit(*ascii_val)){
  131.        *result = *result * 10 +
  132.                  to_decimal(*ascii_val++);
  133.        if( (sign == -1) && (*result > 0)) *result =
  134.          *result * -1;
  135.      }
  136.      else
  137.        return (IO_ERROR);
  138.     }  /* ends while ascii_val  */
  139.  
  140.     return (NO_ERROR);
  141.   }
  142.  
  143. /***********************************************
  144. *   file d:\cips\locvrt.c
  145. *
  146. *   Functions: This file contains
  147. *      get_long
  148. *      long_convert
  149. *
  150. *   Purpose: These functions convert a string of
  151. *            characters to their number value.
  152. *
  153. *   Modifications:
  154. *    Taken from Jamsa's software package
  155. *    and altered to fit into the computer
  156. *    vision programming 22 August 1986.
  157. ***********************************************/
  158.  
  159.  
  160. get_long(n)
  161.    long *n;
  162. {
  163.    char string[80];
  164.  
  165.    read_string(string);
  166.    long_convert(string, n);
  167. }
  168.  
  169.  
  170.  
  171. long_convert (ascii_val, result)
  172.     char *ascii_val;
  173.     long *result;
  174. {
  175.     int sign = 1;  /* -1 if negative */
  176.     *result = 0;   /* value returned to the 
  177.                       calling routine */
  178.  
  179.     /* read passed blanks */
  180.     while (is_blank(*ascii_val))
  181.        ascii_val++;    /* get next letter */
  182.  
  183.     /* check for sign */
  184.     if (*ascii_val == '-' || *ascii_val == '+')
  185.        sign = (*ascii_val++ == '-') ? -1 : 1;
  186.  
  187. /*
  188.  * convert the ASCII representation to the actual
  189.  * decimal value by subtracting '0' from each 
  190.  * character.
  191.  *
  192.  * for example, the ASCII '9' is equivalent to 57 
  193.  * in decimal. by subtracting '0' (or 48 in decimal) 
  194.  * we get the desired value.
  195.  *
  196.  * if we have already converted '9' to 9 and the 
  197.  * next character is '3', we must first multiply 
  198.  * 9 by 10 and then convert '3' to decimal and 
  199.  * add it to the previous total yielding 93.
  200.  */
  201.  
  202.     while (*ascii_val)
  203.      if (is_digit(*ascii_val))
  204.        *result = *result * 10 +
  205.                  to_decimal(*ascii_val++);
  206.      else
  207.        return (IO_ERROR);
  208.  
  209.  
  210.     *result = *result * sign;
  211.     return (NO_ERROR);
  212.   }
  213.  
  214.  
  215. /***************************************************
  216. *  file d:\cips\flocvrt.c
  217. *
  218. *  Functions: This file contains
  219. *       get_float
  220. *       float_convert
  221. *       power
  222. *
  223. *  Purpose: This function converts a string of
  224. *           characters to its number value.
  225. *
  226. *   Modifications:
  227. *        This was taken from Jamsa's software
  228. *        packages and modified to work in the
  229. *        computer vision programs 22 August 1986.
  230. *
  231. *        16 June 1987 - the power function was not 
  232. *        working so Borland's Turbo C function 
  233. *        pow10 was substituted for it.
  234. ***************************************************/
  235.  
  236.  
  237. get_float(f)
  238.    float *f;
  239. {
  240.    char string[80];
  241.  
  242.    read_string(string);
  243.    float_convert(string, f);
  244. }
  245.  
  246. float_convert (ascii_val, result)
  247.     char *ascii_val;
  248.     float *result;
  249. {
  250.     int count;        /* # of digits to the right of
  251.                          the decimal point. */
  252.     int sign = 1;     /* -1 if negative */
  253.  
  254.     double pow10();   /* Turbo C function */
  255.     float power();    /* function returning a value
  256.                          raised to the power
  257.                          specified. */
  258.  
  259.     *result = 0.0;    /* value desired by the
  260.                          calling routine */
  261.  
  262.     /* read passed blanks */
  263.     while (is_blank(*ascii_val))
  264.        ascii_val++;       /* get the next letter */
  265.  
  266.     /* check for a sign */
  267.     if (*ascii_val == '-' || *ascii_val == '+')
  268.        sign = (*ascii_val++ == '-') ? -1 : 1;
  269.  
  270.  
  271. /*
  272.  * first convert the numbers on the left of the
  273.  * decimal point.
  274.  *
  275.  * if the number is 33.141592  this loop will
  276.  * convert 33
  277.  *
  278.  * convert ASCII representation to the  actual
  279.  * decimal value by subtracting '0' from
  280.  * each character.
  281.  *
  282.  * for example, the ASCII '9' is equivalent to 57
  283.  * in decimal by subtracting '0' (or 48 in
  284.  * decimal) we get the desired value.
  285.  *
  286.  * if we have already converted '9' to 9 and the
  287.  * next character is '3', we must first multiply
  288.  * 9 by 10 and then convert '3' to decimal
  289.  * and add it to the previous total yielding 93.
  290.  *
  291.  */
  292.  
  293.     while (*ascii_val)
  294.      if (is_digit(*ascii_val))
  295.        *result = *result * 10 +
  296.              to_decimal(*ascii_val++);
  297.  
  298.      else if (*ascii_val == '.')  /* start the
  299.                                      fractional
  300.                                      part */
  301.        break;
  302.  
  303.      else
  304.        return (IO_ERROR);
  305.  
  306. /*
  307.  * find number to the right of the decimal point.
  308.  *
  309.  * if the number is 33.141592 this portion will
  310.  * return 141592.
  311.  *
  312.  * by converting a character and then dividing it
  313.  * by 10 raised to the number of digits to the
  314.  * right of the decimal place the digits are
  315.  * placed in the correct locations.
  316.  *
  317.  *     4 / power (10, 2) ==> 0.04
  318.  */
  319.  
  320.     if (*ascii_val != NULL2)
  321.      {
  322.         ascii_val++;   /* past decimal point */
  323.  
  324.         for (count = 1; *ascii_val != NULL2; 
  325.              count++, ascii_val++)
  326.  
  327.     /*********************************************
  328.      *   The following change was made 16 June 1987.
  329.      *   For some reason the power function below
  330.      *   was not working. Borland's Turbo C pow10
  331.      *   was substituted.
  332.      ***********************************************/
  333.          if (is_digit(*ascii_val)){
  334.            *result = *result + to_decimal(*ascii_val)/
  335.                      power(10.0,count);
  336.         /***********
  337.         *result = *result + to_decimal(*ascii_val)/
  338.                      ((float)(pow10(count)));
  339.         ************/
  340.           }
  341.  
  342.          else
  343.           return (IO_ERROR);
  344.      }
  345.     *result = *result * sign; /* positive or
  346.                                  negative value */
  347.  
  348.     return (NO_ERROR);
  349.   }
  350.  
  351.  
  352. float power(value, n)
  353.    float value;
  354.    int n;
  355. {
  356.    int   count;
  357.    float result;
  358.  
  359.    if(n < 0)
  360.       return(-1.0);
  361.  
  362.    result = 1;
  363.    for(count=1; count<=n; count++){
  364.       result = result * value;
  365.    }
  366.  
  367.    return(result);
  368. }
  369.