abc to decimal

This is a discussion on abc to decimal within the C++ Programming forums, part of the General Programming Boards category; I already made my version of atoi.. Only one thing I want to upgrade it to take decimals... ex 1.25 ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    abc to decimal

    I already made my version of atoi..
    Only one thing I want to upgrade it to take decimals... ex 1.25
    Im not sure on how to change my function to accept that kind of value..
    any sugestions?

    Code:
    double power(double pow, double from)	// x power y
    {
          double temp =  from;
          if(pow != 0)
          {
    	  --pow;				// x power 1 means x 1 time not 2 times !
    	  for(int i = 0; i < pow; i++)	  temp *= from;	// temp = from x times
          }
          else if (pow == 0) temp = 1;	// x power 0 == 1
          return temp;
    }
    
    double into()
    {
          const int ten = 10;		// with this 2 will become 20!
          double temp = 0, answer = 0;
          int j = 0;
          unsigned int u = (temp_string.size()-1);
          int y = n;
          
          while((isdigit(temp_string[n]))&&(n<u))	++n;
          if(!(isdigit(temp_string[n]))) --n;
          for(int h = n; h>(y-1); --h, ++j)
          {
    	  temp = ((temp_string[h] - '0')*(power(j, ten)));
    	  answer += temp;
          }
          return answer;
    }
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  2. #2
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    Ok dont answer this I finally made it myself..

    here it is just in case someone is interested..

    Code:
    double power(double pow, double from)	// x power y
    {
          double temp =  from;
          if(pow > 0)
          {
    	  --pow;				// x power 1 means x 1 time not 2 times !
    	  for(int i = 0; i < pow; i++)	  temp *= from;	// temp = from x times
          }
          else if(pow < 0)	  
          {
    	  --pow;				//10/10 = 1 so must add 1..
    	  for(int i = 0; i > pow; --i)	  temp /= from;	// temp = from divided x times
          }
          else if (pow == 0) temp = 1;			// x power 0 == 1
          return temp;
    }
    
    double into() //upgrade this!
    {
          const int ten = 10;		// with this 2 will become 20!
          double temp = 0, answer = 0;
          int j = 0, z = 0;
          unsigned int u = (temp_string.size()-1);
          int y = n;
          
          while((isdigit(temp_string[n]))&&(n<u))	++n;
          if(temp_string[n] == '.') z = 666;
          if(!(isdigit(temp_string[n]))) --n;
          for(int h = n; h>(y-1); --h, ++j)
          {
    	  temp = ((temp_string[h] - '0')*(power(j, ten)));
    	  answer += temp;
          }
          if(z==666)
          {
    	  n +=2;
    	  for(j=-1; isdigit(temp_string[n]); n++, j--)
    	  {
    	  temp = ((temp_string[n] - '0')*(power(j, ten)));
    	  answer += temp;
    	  }
          }
          --n;
          return answer;
    }
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 04:52 AM
  3. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 08:52 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21