Question

In: Computer Science

I have a BigInt class I created that works correctly. I would like to add a...

I have a BigInt class I created that works correctly. I would like to add a constructor

BigInt(string s, int base)

that creates a BigInt operating in the given base. The newly extended class should work with any base between 2 and 36, using the digits 0, 1, …, 9, A, B, C, …, Z. How do I do this from what I have? (you may add new data members and such of course)

#include <string>
#include <vector>

class BigInt {
private:
   std::string m_String = "";
  
public:
   BigInt(std::string s)   // convert string to BigInt
   {
      for(int i = s.length() - 1; i >= 0; i--) {
        m_BigInt.push_back((int)s[i] - 48);
      }
   }

   std::string to_string() // get string representation
   {
       m_String = ""; // clear before print
       for (auto it = m_BigInt.rbegin(); it != m_BigInt.rend(); it++)
       {
           m_String += std::to_string(*it);
       }
       return m_String;
   }


   void add(BigInt b) // add another BigInt to this one with carry function
   {
       for (int digit = 0; digit != b.getVect().size(); digit++)
       {
      int temp = m_BigInt[digit] + b.getVect()[digit];
           if(temp >= 10) {
        if(digit + 1 != m_BigInt.size()) {
          m_BigInt[digit] = temp % 10;
          m_BigInt[digit + 1] = m_BigInt[digit + 1] + temp / 10;
        } else {
          m_BigInt[digit] = temp % 10;
          m_BigInt.push_back(temp / 10);
        }
      } else
        m_BigInt[digit] = temp;
     }
   }


   std::vector<int> getVect() // get BigInt vector
   {
       return m_BigInt;
   }
};

Solutions

Expert Solution

Program Code Screenshot

Program Sample Input/Output Screenshot

Program Code to Copy

#include <string>
#include <vector>
#include <iostream>

class BigInt
{
private:
   std::string m_String = "";
   std::vector<int> m_BigInt;
   int base;

public:
   BigInt(std::string s) // convert string to BigInt
   {
      for (int i = s.length() - 1; i >= 0; i--)
      {
         m_BigInt.push_back((int)s[i] - 48);
      }
      base = 10;
   }

   BigInt(std::string s, int base)
   {
      for (int i = s.length() - 1; i >= 0; i--)
      {
         // handle base above 10
         if (s[i] >= 'A' && s[i] <= 'Z')
         {
            m_BigInt.push_back(s[i] - 'A' + 10); //character A is 10
         }
         else
         {
            m_BigInt.push_back((int)s[i] - 48);
         }
      }
      this->base = base;
   }

   std::string to_string() // get string representation
   {
      m_String = ""; // clear before print
      for (auto it = m_BigInt.rbegin(); it != m_BigInt.rend(); it++)
      {
         // handle base above 10
         if (*it >= 10)
         {
            m_String += char(*it - 10 + 'A');
         }
         else
         {
            m_String += std::to_string(*it);
         }
      }
      return m_String;
   }

   void add(BigInt b) // add another BigInt to this one with carry function
   {
      for (int digit = 0; digit != b.getVect().size(); digit++)
      {
         int temp = m_BigInt[digit] + b.getVect()[digit];
         if (temp >= base)
         {
            if (digit + 1 != m_BigInt.size())
            {
               m_BigInt[digit] = temp % base;
               m_BigInt[digit + 1] = m_BigInt[digit + 1] + temp / base;
            }
            else
            {
               m_BigInt[digit] = temp % base;
               m_BigInt.push_back(temp / base);
            }
         }
         else
            m_BigInt[digit] = temp;
      }
   }

   std::vector<int> getVect() // get BigInt vector
   {
      return m_BigInt;
   }
};

// demo for changes
int main()
{
   BigInt a1("1101", 2);
   BigInt b1("1101", 2);
   a1.add(b1);
   std::cout <<"1101 (base 2) + 1111  (base 2) = "<< a1.to_string() << std::endl;

   BigInt a2("A20", 16);
   BigInt b2("13F", 16);
   a2.add(b2);
   std::cout <<"A20 (base 16) + 13F  (base 16) = "<< a2.to_string() << std::endl;
}

Related Solutions

I would like to have a response on this form or post for my psychology class....
I would like to have a response on this form or post for my psychology class. My paper will examine Autism throughout the lifespan. Specifically I am trying to determine if there is a time throughout one's lifetime where techniques to 'treat' an Autism disorder are most effective. While there are many symptoms of Autism including social, cognitive and even psychological 'deficiencies' compared to an individual without autism, I am only looking at treating the individuals cognitions, or at least...
I would like to have a response on this forms or post for my psychology class....
I would like to have a response on this forms or post for my psychology class. The focus of my paper is on the relationship between a father and daughter, and how that influences daughters’ selection of romantic partners in emerging adulthood. I wanted to look at research on the effects of the fathers’ own attachment, as well as maladaptive parenting behaviors/styles, and if this is connected and transferred to future partners. I looked at research on attachment styles of...
A manufacturer of chocolate chips would like to know whether its bag filling machine works correctly...
A manufacturer of chocolate chips would like to know whether its bag filling machine works correctly at the 440 gram setting. It is believed that the machine is underfilling or overfilling the bags. A 21 bag sample had a mean of 443 grams with a standard deviation of 11. Assume the population is normally distributed. A level of significance of 0.01 will be used. Specify the type of hypothesis test. answer can be left tailed test,right tailed test or two...
A manufacturer of banana chips would like to know whether its bag filling machine works correctly...
A manufacturer of banana chips would like to know whether its bag filling machine works correctly at the 400 gram setting. Is there sufficient evidence at the 0.02 level that the bags are overfilled? Assume the population is normally distributed. State the null and alternative hypotheses for the above scenario.
A manufacturer of chocolate chips would like to know whether its bag filling machine works correctly...
A manufacturer of chocolate chips would like to know whether its bag filling machine works correctly at the 413.0 gram setting. It is believed that the machine is underfilling the bags. A 41 bag sample had a mean of 403.0 grams. A level of significance of 0.02 will be used. Determine the decision rule. Assume the variance is known to be 676.00.
A manufacturer of chocolate chips would like to know whether its bag filling machine works correctly...
A manufacturer of chocolate chips would like to know whether its bag filling machine works correctly at the 440 gram setting. It is believed that the machine is underfilling the bags. A 21 bag sample had a mean of 430 grams with a standard deviation of 29 . Assume the population is normally distributed. A level of significance of 0.02 will be used. Find the P-value of the test statistic. You may write the P-value as a range using interval...
A manufacturer of potato chips would like to know whether its bag filling machine works correctly...
A manufacturer of potato chips would like to know whether its bag filling machine works correctly at the 409 gram setting. Based on a 21 bag sample where the mean is 417 grams and the standard deviation is 26, is there sufficient evidence at the 0.1 level that the bags are overfilled? Assume the population distribution is approximately normal. Step 1 of 5: State the null and alternative hypotheses. Step 2 of 5: Find the value of the test statistic....
A manufacturer of potato chips would like to know whether its bag filling machine works correctly...
A manufacturer of potato chips would like to know whether its bag filling machine works correctly at the 410.0 gram setting. Based on a 46 bag sample where the mean is 408.0 grams, is there sufficient evidence at the 0.1 level that the bags are underfilled? Assume the standard deviation is known to be 20.0. Step one: Enter the hypotheses: H0: Ha: Step two: Enter the value of the z test statistic. Round your answer to two decimal places. Step...
A manufacturer of banana chips would like to know whether its bag filling machine works correctly...
A manufacturer of banana chips would like to know whether its bag filling machine works correctly at the 431 gram setting. It is believed that the machine is underfilling or overfilling the bags. A 31 bag sample had a mean of 425 grams with a standard deviation of 18 . Assume the population is normally distributed. A level of significance of 0.05 will be used. Specify the type of hypothesis test.
A manufacturer of banana chips would like to know whether its bag filling machine works correctly...
A manufacturer of banana chips would like to know whether its bag filling machine works correctly at the 447 gram setting. It is believed that the machine is underfilling the bags. A 39 bag sample had a mean of 441 grams. Assume the population standard deviation is known to be 24. A level of significance of 0.05 will be used. Find the P-value of the test statistic. You may write the P-value as a range using interval notation, or as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT