By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,189 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,189 IT Pros & Developers. It's quick & easy.

How to parse float number

P: 1
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.

Expand|Select|Wrap|Line Numbers
  1. if (isdigit(ch))
  2.             {
  3.                 int num=0;
  4.                 do
  5.                 {
  6.                     //no checking for overflow
  7.                     num=10 * num + ch - '0';
  8.                 }while((ch = read_ch())!=EOF && isalnum (ch));
  9.                 put_back(ch);
  10.                 return num;
  11.             }
  12. }
  13.  
Nov 3 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
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
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     float num=0, decimal, divisor=10;;
  6.     char ch;
  7.     while(1)
  8.          if(isdigit(ch=getche())) num=10 * num + ch - '0';
  9.          else break;                   // assume a . here
  10.     while(1)
  11.           if(isdigit(ch=getche())) {num=num + (ch - '0')/divisor; divisor *= 10; }
  12.           else break;                   // non digit
  13.     printf("num = %f\n", num);
  14. }
  15.  
Nov 3 '06 #2

Expert 100+
P: 1,510
Simpler version of previous code would be
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int main()
  4. {
  5.     float num=0, decimal, divisor=10;;
  6.     char ch;
  7.     while(isdigit(ch=getchar())) num=10 * num + ch - '0';
  8.     // assume a . here
  9.     while(isdigit(ch=getchar())) {num=num + (ch - '0')/divisor; divisor *= 10; }
  10.     // non digit
  11.     printf("num = %f\n", num);
  12. }
  13.  
or in C you could use sscanf(), e.g.
Expand|Select|Wrap|Line Numbers
  1. // parse a decimal number
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. int main()
  5. {
  6.     char data[]=" 3.14159";
  7.     float fnum=0;
  8.     // attempt to read a float from data[]
  9.     if(sscanf(data, "%f", &fnum)) printf("float = %f\n",fnum);
  10.     else                          printf("input fail\n");
  11. }
  12.  
program output would be
float = 3.141590


or in C++ you could use strstream, e.g.
Expand|Select|Wrap|Line Numbers
  1. // parse a decimal number
  2. #include <iostream>
  3. #include <strstream>
  4. using namespace std; 
  5.  
  6. int main()
  7. {
  8.     char data[20]=" 3.1415926   2.718";
  9.     float fnum=0;
  10.     // attempt to read a float from data[]
  11.     istrstream str(data,20);
  12.     str >> fnum;
  13.     if(str.fail())  { cout << "input fail\n"; exit(1); } 
  14.     cout << "float = " << fnum << endl;
  15.     str >> fnum;
  16.     if(str.fail())  { cout << "input fail\n"; exit(1); } 
  17.     cout << "float = " << fnum << endl;
  18. }
  19.  
program output would be
float = 3.14159
float = 2.718
Nov 4 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.