473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. void HugeInteger::input()
  2. {
  3.       char line[41];      //characters read in to hold integer values
  4.  
  5.       cout << "\nEnter Huge Integer (Max: 40 digits): ";
  6.       cin.getline(line, sizeof line);
  7.  
  8.       inputSize = strlen(line);      //determine size of users integer
  9.  
  10.       if (inputSize > arraySize)      //make sure no more than 40 digits where entered
  11.             cout << "\nThe integer entered was to long.  Array not initialized." << endl;
  12.       else
  13.             for (int loopSize = 0; loopSize < inputSize; loopSize++)
  14.                   //subtract ASCII value for '0' to get raw value
  15.                   //store digits at end of 40 digit array all non initialized parts of array stay a 0
  16.                   bigNumber[(arraySize - 1) - loopSize] = line[(inputSize- 1) - loopSize] - '0';
  17.  
  18. }
and the second design is:
Expand|Select|Wrap|Line Numbers
  1. void HugeInteger::input()
  2. {
  3.     char line[41];      //characters read in to hold integer values
  4.  
  5.     cout << "\nEnter Huge Integer (Max: 40 digits): ";
  6.     cin.getline(line, sizeof line);
  7.  
  8.     inputSize = strlen(line);      //determine size of users integer
  9.  
  10.     if (inputSize > arraySize)      //make sure no more than 40 digits where entered
  11.         cout << "\nThe integer entered was to long.  Array not initialized." << endl;
  12.     else
  13.         for (int loopSize = 0; loopSize < inputSize; loopSize++)
  14.         //subtract ASCII value for '0' to get raw value
  15.         //store digits at end of 40 digit array all non initialized parts of array stay a 0
  16.         bigNumber[(arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize];
  17.  
  18.         for (int digitIndex = 0; digitIndex < arraySize; digitIndex++)
  19.             bigNumber[digitIndex] = intConvert(bigNumber[digitIndex]);
  20.  
  21. }//end input function
  22.  
  23.  
  24. int HugeInteger::intConvert(char toConvert)
  25. {
  26.     switch(toConvert)
  27.     {
  28.         case '1': return 1;
  29.         case '2': return 2;
  30.         case '3': return 3;
  31.         case '4': return 4;
  32.         case '5': return 5;
  33.         case '6': return 6;
  34.         case '7': return 7;
  35.         case '8': return 8;
  36.         case '9': return 9;
  37.         case '0': return 0;
  38.         default: return 0;
  39.     }
  40. }
Apr 22 '08 #1
5 3411
Ganon11
3,652 Expert 2GB
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.
Apr 22 '08 #2
What would I do once I set the flag: Like for example for outputing the number:
Expand|Select|Wrap|Line Numbers
  1. HugeInteger::HugeInteger()    //default constructor const arraySize = 40
  2. :arraySize(40)
  3. {
  4.     for (int loopSize = 0; loopSize < arraySize; loopSize++)
  5.         bigNumber[loopSize] = 0;    //initialize array with 0's
  6.  
  7.     isNegative = false;
  8.  
  9. }//end constructor
  10.  
  11.  
  12. void HugeInteger::input()
  13. {
  14.     char line[41];      //characters read in to hold integer values
  15.  
  16.     cout << "\nEnter Huge Integer (Max: 40 digits): ";
  17.     cin.getline(line, sizeof line);
  18.  
  19.     inputSize = strlen(line);      //determine size of users integer
  20.  
  21.     if (isdigit(line[0]))
  22.         isNegative = true;
  23.  
  24.     if (inputSize > arraySize)      //make sure no more than 40 digits where entered
  25.         cout << "\nThe integer entered was to long.  Array not initialized." << endl;
  26.     else
  27.         if (isNegative)//if number is positive
  28.             for (int loopSize = 0; loopSize < inputSize; loopSize++)
  29.             //subtract ASCII value for '0' to get raw value store digits at 
  30.             //end of 40 digit array all non initialized parts of array stay a 0
  31.             bigNumber[(arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize] - '0';
  32.  
  33.  
  34. }//end input function
  35.  
  36. void HugeInteger::output() const
  37. {
  38.     bool leadingZero = true;
  39.  
  40.     //avoid printing all leading zero's
  41.     for (int digitIndex = 0; digitIndex < arraySize; digitIndex++)
  42.     {
  43.         int digit = bigNumber[digitIndex];
  44.             //if it is not a zero, print it, and set leadingZero to false;
  45.             if (digit != 0)
  46.             {
  47.                 leadingZero = false;
  48.                 cout << digit;
  49.             }//end if
  50.             else
  51.                 //else if digit is zero, print only if not leading
  52.                 if (!leadingZero)
  53.                     cout << digit;
  54.     }//end for
  55.  
  56. }//end output function
  57.  
Apr 22 '08 #3
Laharl
849 Expert 512MB
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.
Apr 23 '08 #4
Ganon11
3,652 Expert 2GB
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).
Apr 23 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 23 '08 #6

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

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
16
by: x | last post by:
converting 1944 to '1','9','4','4' how can I convert a number such as 1944 to a character array? thanks!
8
by: Ramiro Barbosa, Jr. | last post by:
All, Any ideas on how to convert the first 8 bytes of raw uninterpreted sequence of bytes from 'char array;' (populated with _binary_ data read from a socket), into a 'long id'? Thank you! ...
18
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.