How to parse float number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amarendrajoshi
    New Member
    • Nov 2006
    • 1

    #1

    How to parse float number

    Hi, my name is Amarendra..
    I am building a parser for CAD translator.. My parser right now can parse integer numbers easily. But it has a problem while reading float number. Could anybode please help me.
    The code is as follows.

    Code:
    if (isdigit(ch))
    			{
    				int num=0;
    				do
    				{
    					//no checking for overflow
    					num=10 * num + ch - '0';
    				}while((ch = read_ch())!=EOF && isalnum (ch));
    				put_back(ch);
    				return num;
    			}
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    It depends how sophisticated you want to be. You could
    1.loop reading the whole number part – exit on a .
    2.loop reading the fractional part exit on a non digit

    For example the following assumes the first non digit is a . so you would want to add error checking, etc
    Code:
    #include <stdio.h>
    
    int main()
    {
        float num=0, decimal, divisor=10;;
        char ch;
        while(1)
             if(isdigit(ch=getche())) num=10 * num + ch - '0';
             else break;                   // assume a . here
        while(1)
              if(isdigit(ch=getche())) {num=num + (ch - '0')/divisor; divisor *= 10; }
              else break;                   // non digit
        printf("num = %f\n", num);
    }

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      Simpler version of previous code would be
      Code:
      #include <stdio.h>
      #include <ctype.h>
      int main()
      {
          float num=0, decimal, divisor=10;;
          char ch;
          while(isdigit(ch=getchar())) num=10 * num + ch - '0';
          // assume a . here
          while(isdigit(ch=getchar())) {num=num + (ch - '0')/divisor; divisor *= 10; }
          // non digit
          printf("num = %f\n", num);
      }
      or in C you could use sscanf(), e.g.
      Code:
      // parse a decimal number
      #include <stdio.h>
      #include <ctype.h>
      int main()
      {
          char data[]=" 3.14159";
          float fnum=0;
          // attempt to read a float from data[]
          if(sscanf(data, "%f", &fnum)) printf("float = %f\n",fnum);
          else                          printf("input fail\n");
      }
      program output would be
      float = 3.141590


      or in C++ you could use strstream, e.g.
      Code:
      // parse a decimal number
      #include <iostream>
      #include <strstream>
      using namespace std; 
      
      int main()
      {
          char data[20]=" 3.1415926   2.718";
          float fnum=0;
          // attempt to read a float from data[]
          istrstream str(data,20);
          str >> fnum;
          if(str.fail())  { cout << "input fail\n"; exit(1); } 
          cout << "float = " << fnum << endl;
          str >> fnum;
          if(str.fail())  { cout << "input fail\n"; exit(1); } 
          cout << "float = " << fnum << endl;
      }
      program output would be
      float = 3.14159
      float = 2.718

      Comment

      Working...