Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are a programming intern at a bank and your boss has come to ask for your he

ID: 3844454 • Letter: Y

Question

You are a programming intern at a bank and your boss has come to ask for your help. When adding all the values in the accounts to compute a total, the result is often totally wrong.

Part 1:
It occurs to you that when dealing with really large values the computation overflows if you use a fixed size data type like int or long. You plan to design a class that can handle arbitrarily large values and support a few simple operations. Since all the values are nonnegative integers (all calculations are done in pennies and there is no “half pennies”) you decide to use a string data member to represent the numeric value.

Hints: – Number.h header file has been given to you. All you need to do is implement Number.cpp. – Comment out all functions except Number(), Number(string val), getValue() first and implement those. Once you have tested your simplified class with them, comment back in, one function at a time, pretty much in the order given. Implement the function, test it, move on to the next function, repeat. – operator<< relies on print function to do all the work (cout<<n1 does the same thing as n1.print(cout)), operator>> relies on read function (cin>>n1 does the same thing as n1.read(cin)) to do all the work. Once you have print and read functions all you have to do is use them in operator << and >>. – You may find the Number With Operators example in Unit 14 Demo Source pretty useful for this. – For operator + you will need to combine two number strings into a third string which represents the sum of the two, one digit at a time just like in grade school. Once you have that string create a Number with that value and return it. – To get the numeric value of a char you can subtract '0' from it, e.g.: string num = "142"; int four = num[1] - '0';

Part 2:
Your boss is very happy your Number class can handle really large values but she would also like another number class which has all the capabilities of Number but slightly different behaviors useful in modern accounting :) A FunnyNumber does strange math. For example, 2 + 2 = 22, 100 + 1 = 1001. For equality testing, two funny numbers are considered equal if they both use the same digits, regardless of numeric value. For example 100 == 101 (both use 0 and 1), 123321 == 231, 112 != 223 (one uses 1 and 2, the other uses 2 and 3). It also has a new member function which reverses a number. E.g. 1234 becomes 4321, 1200 becomes 21, 0 stays 0.

Hints: – Make a FunnyNumber.h header which extends Number.h, add two constructors to it and verify that it works just like a Number – Add a void reverse() function and implement it. – Override operators + and == in FunnyNumber so they have a different behavior.

Number.h:

#ifndef NUMBER_H
#define NUMBER_H

#include <iostream>
#include <string>

using namespace std;

class Number {
public:
// Make a number with value 0
Number();
// Make a number with value val
Number(string val);

// Get the number's value
virtual string getValue() const;

// Print this number to the stream
virtual void print(ostream& stream) const;

// Read this number from the stream
virtual void read(istream& stream);

// Overload the insertion operator
friend ostream& operator <<(ostream& outs, const Number& n);

// Overload the extraction operator
friend istream& operator >>(istream& ins, Number& n);

// Operator +
virtual Number operator+ (const Number& other) const;

// Operator ==
virtual bool operator== (const Number& other) const;


protected:
string value;
};

#endif // NUMBER_H

SampleMain.cpp:

#include <iostream>
#include <Number.h>
#include <FunnyNumber.h>

using namespace std;

int main()
{
cout<<"Math with regular ints:"<<endl;
uint num1 = 2800000000;
uint num2 = 3000000000;
cout<<num1<<" + "<<num2<<" = "<<num1+num2<<endl;
cout<<"ouch...!"<<endl;


cout<<endl;
cout<<"We can handle really large values."<<endl;
Number n1, n2;
cout<<"Enter a really big positive integer: ";
// n1.read(cin);
// n1.print(cout); cout<<endl;
cin>>n1;
cout<<"Enter another really big integer: ";
cin>>n2;
cout<<n1<<" + "<<n2<<" = "<<n1+n2<<endl;
n1 = Number("123456789012345678901234567890");
n2 = Number("213456789012345678901234567890");
if (n1 == n2) {
cout<<n1<<" == "<<n2<<endl;
} else {
cout<<n1<<" != "<<n2<<endl;
}
cout<<endl;

cout<<"We can do fancy math too."<<endl;
FunnyNumber f1, f2;
cout<<"Enter a positive integer: ";
cin>>f1;
cout<<"Enter another positive integer: ";
cin>>f2;
cout<<f1<<" + "<<f2<<" = "<<f1+f2<<endl;

f1 = FunnyNumber("123456789012345678901234567890");
f2 = FunnyNumber("999999999912345678901234567890000000000000000");
if (f1 == f2) {
cout<<f1<<" == "<<f2<<endl;
} else {
cout<<f1<<" != "<<f2<<endl;
}

f2.reverse();
cout<<"Reversed value "<<f2<<endl;

return 0;
}

Explanation / Answer

It is not the difficult to implement, here is the code for both the .cpp files. Have a look:

#include<iostream>
#include <string>
#include <algorithm> //added to compute reverse of the string
#include "Number.h"

using namespace std;
Number::Number(){
value="0"; //initiating with zero
}
Number::Number(string val){
value=val; //initiating with the given value
}
string Number:: getValue() const{
return value; // just return the value
}
void Number::print(ostream& stream) const{
stream<<value; //print ostream
}
void Number::read(istream& stream){
stream>>value; //read into the value
}
ostream& operator <<(ostream& outs, const Number& n){
n.print(outs); // prints to the screen
return outs;
}
istream& operator >>(istream& ins, Number& n){
n.read(ins);
return ins;
}
Number Number::operator+ (const Number& other) const{
string str1=other.getValue();
string str2=value;   
if (str1.length() > str2.length()) //str2 is the larger one
swap(str1, str2);
string str = "";
int n1 = str1.length(), n2 = str2.length(); //getting the length
int diff = n2 - n1;
int carry = 0; //in case of addition goes above 10
for (int i=n1-1; i>=0; i--)
{
int sum = ((str1[i]-'0') +
(str2[i+diff]-'0') +
carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}
for (int i=n2-n1-1; i>=0; i--)
{
int sum = ((str2[i]-'0')+carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}
if (carry) //if carry still left add it to the str
str.push_back(carry+'0');
reverse(str.begin(), str.end());
Number addition(str); //create an instance with parameterized constructor
return addition;
}
bool Number::operator== (const Number& other) const{
string second=other.getValue(); if(second.length()!=value.length())return false;

for(int i=0;i<second.length() && i<value.length();i++){ //iterating through all the length
if(second[i]!=value[i])return false;
}
return true;
}

This is the FunnyNumber.h, I have included the function definiton in the same file

#ifndef FUNNYNUMBER_H
#define FUNNYNUMBER_H
#include <iostream>
#include <string>
#include <algorithm>
#include "Number.h"
using namespace std;
class FunnyNumber: public Number{
public:
FunnyNumber(){
value="0";
}
FunnyNumber(string val){
value=val; //same as previous function
}

void reverse(){
std::reverse(value.begin(), value.end()); //just use the function, this ain't recursion
value.erase(0, value.find_first_not_of('0')); //removing leading zeroes
}
virtual FunnyNumber operator+ (const FunnyNumber& other) const{
string check=value;
check+=other.getValue(); //concatenate the strings
FunnyNumber addition(check);
return addition;
  
}

virtual bool operator== (const FunnyNumber& other) const{
string str1=other.getValue();
string str2=value;   
int flag=0; //setting a flag to check
if (str1.length() > str2.length())
swap(str1, str2);
for(int i=0;i<str1.length();i++){
flag=0;
for(int j=0;j<str2.length();j++){
if(str1[i]==str2[j])flag=1;
}
if(flag!=1) return false;
}
return true;
}

};
#endif

The Main.cpp is already given, go ahead and compile. Let me know if you encounter any sort of problem !