⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stdiop_f.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
          return -1;
        ++printed_bytes;
      }
      if (send_chars(stream, spc, width - num_digits))
        return -1;
      printed_bytes += (width - num_digits);
      width = num_digits;
    }
  }

  /*-------------------------------------------------------------------*/
  /* If the sign is not already printed, print it first.               */
  /*-------------------------------------------------------------------*/
  if (!start && ((real_buf[0] == '-') || (real_buf[0] == 32) ||
                 (real_buf[0] == '+')) && added_sign)
  {
    if (send_char(stream, real_buf[0]))
      return -1;
    ++printed_bytes;
  }

  /*-------------------------------------------------------------------*/
  /* Check the specifier the function was called with.                 */
  /*-------------------------------------------------------------------*/
  if (spec == 'f')
  {
    /*-----------------------------------------------------------------*/
    /* Function called with the 'f' specifier.                         */
    /*-----------------------------------------------------------------*/
    for (; exponent < 0; ++times, ++exponent)
    {
      /*---------------------------------------------------------------*/
      /* Put 0s in front and increment exponent till it becomes 0.     */
      /*---------------------------------------------------------------*/
      if (times == 1)
      {
        /*-------------------------------------------------------------*/
        /* Put the decimal point after the first 0.                    */
        /*-------------------------------------------------------------*/
        start_count = 1;
        if (send_char(stream, '.'))
          return -1;
        ++printed_bytes;
      }
      if (start_count)
        ++d_digits;

      /*---------------------------------------------------------------*/
      /* If exponent exceeded the precision, and is negative, stop.    */
      /*---------------------------------------------------------------*/
      if (d_digits > precision)
        return printed_bytes;

      if (send_char(stream, '0'))
        return -1;
      ++printed_bytes;
    }

    if (times == 1)
    {
      /*---------------------------------------------------------------*/
      /* The exponent was -1, so put the decimal point now.            */
      /*---------------------------------------------------------------*/
      start_count = 1;
      if (send_char(stream, '.'))
        return -1;
      ++printed_bytes;
    }

    if (start_count)
    {
      /*---------------------------------------------------------------*/
      /* A decimal point was already printed, followed by 0s.          */
      /*---------------------------------------------------------------*/
      precision = precision - (d_digits + 1);

      /*---------------------------------------------------------------*/
      /* Finish printing decimal digits until precision.               */
      /*---------------------------------------------------------------*/
      d_digits = round_up(real_buf, precision, zeros, &exponent);
      for (j = 1; j <= d_digits; ++j)
      {
        if (j != 2)
        {
          if (send_char(stream, real_buf[j]))
            return -1;
          ++printed_bytes;
        }
      }

      /*---------------------------------------------------------------*/
      /* Pad, if necessary, with spaces at the end.                    */
      /*---------------------------------------------------------------*/
      if (send_chars(stream, ' ', spaces))
        return -1;
      printed_bytes += spaces;

      return printed_bytes;
    }

    /*-----------------------------------------------------------------*/
    /* If exponent is positive, find out where the decimal digit       */
    /* should be in the buffer.                                        */
    /*-----------------------------------------------------------------*/
    if (exponent > 0)
    {
      change_point = 1;
      when = 2 + exponent;
    }

    /*-----------------------------------------------------------------*/
    /* If precision or # flag specified, print the decimal point.      */
    /*-----------------------------------------------------------------*/
    if (precision || (flags & NUM_FLAG))
    {
      if (precision + exponent < 14)
        d_digits = round_up(real_buf, precision + exponent, zeros,
                            &exponent);
      else
        d_digits = round_up(real_buf, 13, zeros, &exponent);
      for (i = 1; i <= d_digits; ++i)
      {
        /*-------------------------------------------------------------*/
        /* If there are 0s in front, skip the decimal point.           */
        /*-------------------------------------------------------------*/
        if (!((times || change_point) && (real_buf[i] == '.')))
        {
          exponent = (change_point && (i > 2)) ? exponent - 1 :
                                                 exponent;

          /*-----------------------------------------------------------*/
          /* If we haven't reached the decimal point, send digits.     */
          /*-----------------------------------------------------------*/
          if (real_buf[i] != '.')
          {
            if (send_char(stream, real_buf[i]))
              return -1;
            ++printed_bytes;
          }

          /*-----------------------------------------------------------*/
          /* If we've reached decimal point, or its place, write it.   */
          /*-----------------------------------------------------------*/
          if ((change_point && (i == when)) || (real_buf[i] == '.'))
          {
            if ((i < d_digits) || (flags & NUM_FLAG))
            {
              if (send_char(stream, '.'))
                return -1;
              ++printed_bytes;
            }

            for (j = 0; (j < precision) && (i < d_digits); ++j)
            {
              /*-------------------------------------------------------*/
              /* Send precision digits to be printed after decimal pt. */
              /*-------------------------------------------------------*/
              ++i;

              /*-------------------------------------------------------*/
              /* If i < 23 send decimal digit, else send '0'.          */
              /*-------------------------------------------------------*/
              if (i < 23)
              {
                if (send_char(stream, real_buf[i]))
                  return -1;
              }
              else
              {
                if (send_char(stream, '0'))
                  return -1;
              }
              ++printed_bytes;
            }

            /*---------------------------------------------------------*/
            /* Pad with spaces if necessary.                           */
            /*---------------------------------------------------------*/
            if (send_chars(stream, ' ', spaces))
              return -1;
            printed_bytes += spaces;

            return printed_bytes;
          }
        }
      }
    }

    /*-----------------------------------------------------------------*/
    /* Else precision was 0, so send only digit before the decimal pt. */
    /*-----------------------------------------------------------------*/
    else
    {
      if (send_char(stream, real_buf[1]))
        return -1;
      ++printed_bytes;
    }

    /*-----------------------------------------------------------------*/
    /* Put 0s at the back and decrement exponent till it becomes 0.    */
    /*-----------------------------------------------------------------*/
    if (send_chars(stream, '0', exponent))
      return -1;
    printed_bytes += exponent;
    exponent = 0;

    if (precision && zeros)
    {
      if (send_char(stream, '.'))
        return -1;
      ++printed_bytes;

      /*---------------------------------------------------------------*/
      /* Fill in with 0s until precision digits if necessary.          */
      /*---------------------------------------------------------------*/
      if (d_digits < precision)
      {
        if (send_chars(stream, '0', precision - d_digits))
          return -1;
        printed_bytes += (precision - d_digits);
      }
    }
  }

  /*-------------------------------------------------------------------*/
  /* Else function called with the 'e' or 'E' specifier.               */
  /*-------------------------------------------------------------------*/
  else
  {
    /*-----------------------------------------------------------------*/
    /* Print the decimal point if precision or the # flag specified.   */
    /*-----------------------------------------------------------------*/
    if ((d_digits == 2) && (flags & NUM_FLAG))
    {
      if (send_buf(stream, &real_buf[1], 2))
        return -1;
      printed_bytes += 2;
    }
    else if (d_digits == 2)
    {
      if (send_char(stream, real_buf[1]))
        return -1;
      ++printed_bytes;
    }
    else
    {
      if (send_buf(stream, &real_buf[1], d_digits))
        return -1;
      printed_bytes += d_digits;
    }

    /*-----------------------------------------------------------------*/
    /* Print exponent in the form (e/E) (+/-) xxx.                     */
    /*-----------------------------------------------------------------*/
    if (send_char(stream, spec))
      return -1;
    ++printed_bytes;

    for (i = 24; i < 29; ++i)
      if (!(((i == 25) && (real_buf[i] == '0')) ||
         ((i == 26) && (real_buf[25] == '0') && (real_buf[i] == '0'))))
      {
        if (send_char(stream, real_buf[i]))
          return -1;
        ++printed_bytes;
      }
  }

  /*-------------------------------------------------------------------*/
  /* Pad, if necessary, with spaces at the end.                        */
  /*-------------------------------------------------------------------*/
  if (send_chars(stream, ' ', spaces))
    return -1;
  printed_bytes += spaces;

  return printed_bytes;
}

/***********************************************************************/
/*   store_buf: Store the value of the real into a string              */
/*                                                                     */
/*      Inputs: val = the real value to be stored                      */
/*              buf = string in which value is stored                  */
/*                                                                     */
/***********************************************************************/
static void store_buf(double val, char *buf)
{
  int exponent = 0, index;
  double mantissa, f, i, decimal;
  char temp_buf[4];

  /*-------------------------------------------------------------------*/
  /* If val is negative, put '-' sign, else put '+' sign.              */
  /*-------------------------------------------------------------------*/
  if (val < 0)
  {
    buf[0] = '-';
    val = -val;
  }
  else
    buf[0] = '+';

  f = val;

  /*-------------------------------------------------------------------*/
  /* Force val between 10 and 1 and store exponent.                    */
  /*-------------------------------------------------------------------*/
  if (val > 10 || val <= 1)
    exponent = get_exponent(&f);

  mantissa = f;
  for (index = 1; index < 17; ++index)
  {
    /*-----------------------------------------------------------------*/
    /* Get the mantissa.                                               */
    /*-----------------------------------------------------------------*/
    if (index != 2)
    {
      decimal = modf(mantissa, &i);
      mantissa = 10 * decimal;
      buf[index] = (char)(i + '0');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -