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. - 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: - 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;
-
}
-
}
5 3411
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.
What would I do once I set the flag: Like for example for outputing the number: - 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
-
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.
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).
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
...
|
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!
|
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!
...
|
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;
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |