0

I am working on a programming assignment in which we are making our own BigNum class. One of the constructors needs to be set up so that it can take a number from a string (i.e. 342567) and reads it into an array. However if the number were 0000000342567 it would have to be able to skip over the 0s and just read 342567.

Where is what i have so far but am lost on trimming the 0s

BigNum::BigNum(const char strin[])
{
    size_t size = strlen(strin);
    positive = true;
    capacity = size;
    digits = new size_t[capacity];
    used=0;

    while(used<size)
    {
        if(strin[size - used -1] =='-')
        {
            positive = false;
            size --;
        }
        else if(strin[size - used -1] =='+')
        {
            size --;
        }
        else
        {
            digits[used] = strin[size - used -1] - '0';
            used++;
        }
    }
}

Here is the assignment description if it helps http://csel.cs.colorado.edu/%7Eekwhite/CSCI2270Fall2011/hw2/Homework2.pdf

3 Answers 3

3

Here's a hint:

Write a separate loop at the beginning that skips over all the zeros.

Sign up to request clarification or add additional context in comments.

Comments

1

Add this just before your while loop:

for (int i=0; i < size; i++)
{
    if (strin[i] >= '1' && strin[i] <= '9')
    {
        used = i;
        break;
    }
}

This way, your while loop begins reading the string only from the index where the number actually begins, skipping over all leading 0s.

This should handle the leading sign as well:

BigNum::BigNum(const char strin[])
{
    size_t size = strlen(strin);
    positive = true;        
    used=0;

    if (strin[0] == '+' || strin[0] == '-')
    {
        //set positive or negative
        used++;
    }
    while (used < size)
    {
        if (strin[used] != '0')     
            break;

        used++; //used will only increment if above if condition failed.
    }

    int digitIndex = 0;
    digits = new size_t[size-used]; //create digits array here so it isn't larger than needed

    while(used<size)
    {        
         digits[digitIndex++] = strin[used++];      
    }
}

6 Comments

does this also take into account if the number starts with + or -. since i check it in the while loop
@Sean: You shouldn't actually check for the sign in the while loop, I told you why in my answer below.
is the new while loop in the above code testing for the leading zeros
the first if condition is for the case where there is a sign. The first while is for leading zeroes. Once you've consumed the sign and the leading zeroes, create your digits array then so its not larger than required. Then iterate through all the digits in the strin array.
if i wanted to read the remaining numbers in backward (i.e. the number 345 is actual in the array like [5,4,3] what should the index be for each number
|
0

You just need to add another while loop before the one you have.

But just some other hints:

You can't change the sign of the number at any digit, the sign depends only on the very first charachter. So if you had a string like -2345, that'll be ok, but if you had something other like: 234-88 then this should be invalid, what will you do with this then?

Also the digits array shouldn't really be equal to size, but rather should drop the sign digit if it did exist, so how will you deal with capacity?

Hope that's helpful!

2 Comments

but i need to have the sign check in there. i can see it should be in the while loop but am confused how the digits array is gonna change as you say. should i have a if statement that makes a new capacity and size
capacity should be equal to size at the very beginning, then, with an if statement that checks the first digit, you should change the capacity and also the positive (which I suppose should be sign), after that comes memory allocation, and then the while loop that skips zeros, and then the other while loop that only reads digits without any if statements, because there is a relationship between every X digit and it's associated 'X' character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.