×
Reviews 4.9/5 Order Now

Mastering Self Rubric Creation in C++: Assignment Solution Explained

July 02, 2024
Len E. Villasenor
Len E.
🇺🇸 United States
C++
Len E. Villasenor, with a master’s in computer science from Northern Arizona University, boasts 5 years of expertise in C++ assignment assistance, providing exceptional guidance in this specialized area.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Always start SQL assignments by understanding the schema and relationships between tables. Use proper indentation and aliases for clarity, and test queries incrementally to catch errors early.
News
Owl Scientific Computing 1.2: Updated on December 24, 2024, Owl is a numerical programming library for the OCaml language, offering advanced features for scientific computing.

Instructions

Objective
Write a C++ assignment program to create a self rubric language.

Requirements and Specifications

Below are the sample outputs. Replace each screenshot below with your own. Note that each member function called during a specific test is printed. This way, you can trace when and which member functions are called.
  • Test 0 – 3: Constructors, long, const char*, const char* long
  • Test 4-8: Binary Operators, +, -, *, /, %
  • Test 9-10 : Unary Operator
  •  Test 11-12: Comparison and operator>>()
  • Test 13-15: Assignment operator, index operator, conversion operator, destructor

Source Code

#include <cctype.h> //isdigit()

#include <cstring> //strlen

#include <HugeInt.h>

using std::endl;

using std::cout;

HugeInt::HugeInt(long value)

{

    cout <<"HugeInt(long) called" << endl;

    number = new char[30];

  long temp = value;

    for(int i = 0; i < 30; i++) {

        number[i] = temp % 10;

    temp /= 10;

  }

    //Complete the code here to set number[i]

    computeSize();

}

//Set the size of the number of an HugeInt object

void HugeInt::computeSize()

{

 size = 1;

 for(int i = 29; i >=0; i--) {

  if (number[i] != 0) {

   size = i + 1;

   break;

  }

 }

}

HugeInt::HugeInt(const char* string)

{

    cout <<"HugeInt(const char*) called" << endl;

  int len = strlen(string);

  number = new char[30];

  for (int i = 0; i<30; i++) {

   if (i < len) {

    number[i] = string[len - 1 - i] - '0';

   }

   else {

    number[i] = 0;

   }

  }

  computeSize();

}

HugeInt::HugeInt(const HugeInt& copy)

{

    cout <<"HugeInt(const HugeInt&) called" << endl;

  number = new char[30];

  for (int i = 0; i<30; i++) {

   number[i] = copy.number[i];

  }

  size = copy.size;

}

HugeInt::~HugeInt()

{

    cout <<"~HugeInt() called" << endl;

    delete[] number;

}

HugeInt& HugeInt::operator=(const HugeInt& right)

{

    cout <<"operator=(const HugeInt&) called" << endl;

  for (int i = 0; i<30; i++) {

   number[i] = right.number[i];

  }

  size = right.size;

  return *this;

}

HugeInt HugeInt::operator+(const HugeInt& right)

{

    cout << "operator+(const HugeInt&)" << endl;

  char string[31];

  string[30] = 0;

  char carry = 0;

  for (int i = 0; i<30; i++) {

   char sum = carry + number[i] + right.number[i];

   string[30-i-1] = (sum % 10) + '0';

   carry = sum / 10;

  }

  return HugeInt(string);

}

HugeInt HugeInt::operator-(const HugeInt& right)

{

    cout << "operator-(const HugeInt&)" << endl;

  char string[31];

  string[30] = 0;

  char carry = 0;

  for (int i = 0; i<30; i++) {

   char sum = number[i] - right.number[i] + carry;

   if (sum < 0) {

    carry = -1;

    sum += 10;

   }

   else {

    carry = 0;

   }

   string[30-i-1] = sum + '0';

  }

  return HugeInt(string);

}

HugeInt HugeInt::operator*(const HugeInt& right)

{

    cout << "operator*(const HugeInt&)" << endl;

  HugeInt result(0L);

  for (int j = 0; j

   char string[31];

   string[30] = 0;

   for (int i = 0; i

    string[29-i] = '0';

   }

   char carry = 0;

   for(int i = 0; i<30-j; i++) {

    char sum = carry + number[i] * right.number[j];

    string[29-i-j] = (sum % 10) + '0';

    carry = sum / 10;

   }

   HugeInt add(string);

   result = result + add;

  }

  return result;

}

HugeInt HugeInt::operator/(const HugeInt& right)

{

    cout << "operator/(const HugeInt&)" << endl;

  HugeInt copy = right;

  if (!(copy <= (*this))) {

   return HugeInt(0L);

  }

  HugeInt temp(*this);

  HugeInt result(0L);

  HugeInt ten(10L);

  HugeInt one(1L);

  int diffLen = size - right.size;

  for (int i = diffLen; i>=0; i--) {

   HugeInt coeff = one;

   for (int j = 0; j

    coeff = coeff * ten;

   }

   HugeInt sub(0L);

   long currJ = 0;

   for (long j = 1; j<10; j++) {

    HugeInt J(j);

    HugeInt next = J * (coeff * right);

    if (!(next <= temp)) {

     break;

    }

    sub = next;

    currJ = j;

   }

   HugeInt JJ(currJ);

   result = (result * ten) + JJ;

   temp = temp - sub;

  }

    return result;

}

HugeInt HugeInt::operator%(const HugeInt& right)

{

    HugeInt copy = right;

  if (!(copy <= (*this))) {

   return HugeInt(0L);

  }

  HugeInt temp(*this);

  HugeInt result(0L);

  HugeInt ten(10L);

  HugeInt one(1L);

  int diffLen = size - right.size;

  for (int i = diffLen; i>=0; i--) {

   HugeInt coeff = one;

   for (int j = 0; j

    coeff = coeff * ten;

   }

   HugeInt sub(0L);

   long currJ = 0;

   for (long j = 1; j<10; j++) {

    HugeInt J(j);

    HugeInt next = J * (coeff * right);

    if (!(next <= temp)) {

     break;

    }

    sub = next;

    currJ = j;

   }

   HugeInt JJ(currJ);

   result = (result * ten) + JJ;

   temp = temp - sub;

  }

    return temp;

}

HugeInt& HugeInt::operator++()

{

    cout << "operator++()" << endl;

  char carry = 0;

  for (int i = 0; i<30; i++) {

   char sum = number[i] + carry + (i == 0 ? 1 : 0);

   number[i] = sum % 10;

   carry /= 10;

  }

  computeSize();

  return *this;

}

HugeInt HugeInt::operator++(int)

{

    cout << "operator++(int)" << endl;

  HugeInt copy(*this);

  char carry = 0;

  for (int i = 0; i<30; i++) {

   char sum = number[i] + carry + (i == 0 ? 1 : 0);

   number[i] = sum % 10;

   carry /= 10;

  }

  computeSize();

  return copy;

}

short& HugeInt::operator[](int pos)

{

    cout << "operator[](int)" << endl;

  return (short&)(number[29 - pos]);

}

HugeInt::operator long double()

{

    cout << "operator long double()" << endl;

  long curr = 0;

  for (int i = 29; i>=0; i--) {

   curr = curr * 10 + number[i];

  }

    return curr;

}

bool HugeInt::operator<=(const HugeInt& right) {

    cout << "operator<=(const HugeInt& )" << endl;

  if (size < right.size) {

   return true;

  }

  if (size > right.size) {

   return false;

  }

  for (int i = size-1; i>=0; i--) {

   if (number[i] < right.number[i]) {

    return true;

   }

   if (number[i] > right.number[i]) {

    return false;

   }

  }

    return true;

}

ostream& operator<<(ostream& out, const HugeInt& n)

{

    cout << "operator<<(ostream&, const HugeInt& )" << endl;

  out << "value : ";

  for (int i = n.size-1; i >=0; i--) {

   out << (char)(n.number[i] + '0');

  }

  out << " || size = " << n.size;

  return out;

}

istream& operator>>(istream& in, HugeInt& n)

{

    cout << "operator>>(istream&, HugeInt& )" << endl;

    //Converting a string to char* can be done with c_str() member function of string type

 string s;

 in >> s;

 n = HugeInt(s.c_str());

 return in;

}

Related Samples

Discover our C++ Assignment Samples for expertly crafted solutions to diverse programming challenges. Covering topics like classes, inheritance, and STL usage, these examples provide clear explanations and step-by-step implementations. Perfect for students aiming to strengthen their C++ skills and excel academically with practical, educational resources.