home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_02 / numcvrt.c < prev    next >
Text File  |  1991-12-09  |  10KB  |  395 lines

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