converting '-' char to int array for hugeInteger

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AZRebelCowgirl73
    New Member
    • Nov 2006
    • 47

    #1

    converting '-' char to int array for hugeInteger

    I am trying to accomodate for negative numbers, and as long as the negative is not being used, I have no problems with my input() ouput() and add() functions. I have tried this two different ways, one with and one without a switch statement to convert negatives. I will show my input() both ways and the char convert() for the latter of the two. With the first -1234 prints -31234 because subtracting '0' results in a -3 for '-', the second way deletes the - completely and -1234 prints 1234.
    Code:
    void HugeInteger::input()
    {
          char line[41];      //characters read in to hold integer values
          
          cout << "\nEnter Huge Integer (Max: 40 digits): ";
          cin.getline(line, sizeof line);
    
          inputSize = strlen(line);      //determine size of users integer
    
          if (inputSize > arraySize)      //make sure no more than 40 digits where entered
                cout << "\nThe integer entered was to long.  Array not initialized." << endl;
          else
                for (int loopSize = 0; loopSize < inputSize; loopSize++)
                      //subtract ASCII value for '0' to get raw value
    				  //store digits at end of 40 digit array all non initialized parts of array stay a 0
                      bigNumber[(arraySize - 1) - loopSize] = line[(inputSize- 1) - loopSize] - '0';
    
    }
    and the second design is:
    Code:
    void HugeInteger::input()
    {
        char line[41];      //characters read in to hold integer values
          
        cout << "\nEnter Huge Integer (Max: 40 digits): ";
        cin.getline(line, sizeof line);
    
        inputSize = strlen(line);      //determine size of users integer
    
        if (inputSize > arraySize)      //make sure no more than 40 digits where entered
    		cout << "\nThe integer entered was to long.  Array not initialized." << endl;
    	else
            for (int loopSize = 0; loopSize < inputSize; loopSize++)
            //subtract ASCII value for '0' to get raw value
    		//store digits at end of 40 digit array all non initialized parts of array stay a 0
            bigNumber[(arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize];
    
    		for (int digitIndex = 0; digitIndex < arraySize; digitIndex++)
    			bigNumber[digitIndex] = intConvert(bigNumber[digitIndex]);
    
    }//end input function
    
    
    int HugeInteger::intConvert(char toConvert)
    {
    	switch(toConvert)
    	{
    		case '1': return 1;
    		case '2': return 2;
    		case '3': return 3;
    		case '4': return 4;
    		case '5': return 5;
    		case '6': return 6;
    		case '7': return 7;
    		case '8': return 8;
    		case '9': return 9;
    		case '0': return 0;
    		default: return 0;
    	}
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    When inputting a number, the negative sign can only be in front of the number, right? Then all you need to do is check the first character to see if it is '-' or not. If it is '-', set some flag to indicate the hugeInteger is negative. If not, it's a number, and you proceed as normal.

    Comment

    • AZRebelCowgirl73
      New Member
      • Nov 2006
      • 47

      #3
      What would I do once I set the flag: Like for example for outputing the number:
      Code:
      HugeInteger::HugeInteger()	//default constructor const arraySize = 40
      :arraySize(40)
      {
      	for (int loopSize = 0; loopSize < arraySize; loopSize++)
      		bigNumber[loopSize] = 0;	//initialize array with 0's
      	
      	isNegative = false;
      
      }//end constructor
      
      
      void HugeInteger::input()
      {
          char line[41];      //characters read in to hold integer values
            
          cout << "\nEnter Huge Integer (Max: 40 digits): ";
          cin.getline(line, sizeof line);
      
          inputSize = strlen(line);      //determine size of users integer
      
      	if (isdigit(line[0]))
      		isNegative = true;
      
          if (inputSize > arraySize)      //make sure no more than 40 digits where entered
      		cout << "\nThe integer entered was to long.  Array not initialized." << endl;
      	else
              if (isNegative)//if number is positive
      			for (int loopSize = 0; loopSize < inputSize; loopSize++)
      			//subtract ASCII value for '0' to get raw value store digits at 
      			//end of 40 digit array all non initialized parts of array stay a 0
      			bigNumber[(arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize] - '0';
      		
      
      }//end input function
      
      void HugeInteger::output() const
      {
      	bool leadingZero = true;
      
      	//avoid printing all leading zero's
      	for (int digitIndex = 0; digitIndex < arraySize; digitIndex++)
      	{
      		int digit = bigNumber[digitIndex];
      			//if it is not a zero, print it, and set leadingZero to false;
      			if (digit != 0)
      			{
      				leadingZero = false;
      				cout << digit;
      			}//end if
      			else
      				//else if digit is zero, print only if not leading
      				if (!leadingZero)
      					cout << digit;
      	}//end for
           
      }//end output function

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        When converting a negative number, you'll need an extra slot in the array. That said, just set the first element to '-', then pass the address of the second element (arrayname[1]) to the function when setting it and you don't have to change any other code at all to create it.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          When outputting, if the number is negative (isNegative == true), output a '-' first - otherwise, don't print anything before the digits.

          The entire point of having this isNegative variable is to let you, the programmer, think of the number as in one of two possible states either it is negative (and thus you treat it one way: adding a '-' to the output, multiplying the result by -1) or not negative (and thus you treat it another way: it's a normal, positive number).

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Er, HugeInteger has a 40 byte array for the ineger value. This is 320 bits. That is indeed a huge integer.

            However, what you do is use the array at the bit level. That is, as one 320-bit integer with the left bit set when the number is negative. You would normally do this by converting the integer value as a positive value to a 2's-complement value.

            The HugeInteger operator+, operator-, etc handle the bit operations.

            So for a string with the value, convert to a 320-bit number and if the vlaue was negative, convert that to 2's-complement.

            All of your application code works with HugeInteger and you will provide all necessary code to use numbers of this size.

            Comment

            Working...