My assignment is about working with dynamic integers with instructions as follow: Programming Exercise 11 in Chapter 8 explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largeIntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects and functions to set, retrieve, and print the values of objects. Write a program to test your class.
I am currently getting some issues with my addition function, the main issue im experiencing is that when the first two numbers of the integers add up to 10+, I cant figure out how to properly get it to print the 1. for example 805+805 is return 610
largeIntegers.h
static const int SIZE = 25;
#ifndef H_largeIntegers
#include <string>
#define H_largeIntegers
class largeIntegers{
public:
int a = 0;
int b = 0;
void setNum1(std::string num1);
void setNum2(std::string num2);
void setResults(int a ,int indexValue);
int getNum1(int indexLocation);
int getNum2(int indexLocation);
int getResults(int indexLocation);
void subtract();
void multiply();
void compare();
void printLists();
void add();
largeIntegers(std::string num1 = "10", std::string num2 = "10");
private:
int *num1Ptr;
int *num2Ptr;
int *resultPtr;
int bigNumber1[SIZE]{};
int bigNumber2[SIZE]{};
int result[SIZE]{};
int x1 = 0;
int x2 = 0;
};
#endif
'
largeIntegers.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "largeIntegers.h"
using namespace std;
void largeIntegers::setNum1(string num1){
x1 = num1.size();
int x = num1.size(), a = 0;
num1Ptr = new int[SIZE];
while (0 <= x) {
num1Ptr[x-1] = num1[a] - '0';
x--;
a++;
}
}
void largeIntegers::setNum2(string num2){
x2 = num2.size();
int x = num2.size(), a = 0;
num2Ptr = new int[SIZE];
while (0 <= x) {
num2Ptr[x-1] = num2[a] - '0';
x--;
a++;
}
}
void largeIntegers::setResults(int a, int indexValue){
resultPtr[a] = indexValue;
}
int largeIntegers::getNum1(int indexLocation){
return num1Ptr[indexLocation];
}
int largeIntegers::getNum2(int indexLocation){
return num2Ptr[indexLocation];
}
int largeIntegers::getResults(int indexLocation){
return resultPtr[indexLocation];
}
void largeIntegers::add(){
int a = 0;
int result = 0;
int x = x1;
int y = x2;
resultPtr = new int[SIZE];
int indexSize = 0;
int longest = 0;
if (x > y) {
longest = x;
}else {
longest = y;
}
while (a < longest){
result = num1Ptr[a] + num2Ptr[a];
if(result>=10){
result = result % 10;
resultPtr[a-1] += 1;
resultPtr[a] = result;
cout << "This is result " << endl;
}else{
resultPtr[a] = result;
}
setResults(a, result);
a++;
indexSize++;
}
for (int a = indexSize - 1; a >= 0; a--) {
cout << resultPtr[a];
}
cout << " " << endl;
}
void largeIntegers::subtract(){
}
void largeIntegers::multiply(){
};
void largeIntegers::compare(){
};
void largeIntegers::printLists(){
for (int a = x1 - 1; a >= 0; a--) {
cout << num1Ptr[a];
}
cout << " " << endl;
for (int b = x2 - 1; b >= 0; b--) {
cout << num2Ptr[b];
}
cout << " " << endl;
}
largeIntegers::largeIntegers(string num1, string num2){
setNum1(num1);
setNum2(num2);
}
main.cpp
#include <iostream>
#include "largeIntegers.h"
using namespace std;
int main(){
int indexLocation =0;
string num1 = " ";
string num2 = " ";
cout<< "Enter the first integer" << endl;
cin >> num1;
cout<< "Enter the second integer" << endl;
cin >> num2;
largeIntegers customList(num1, num2);
cout << "The numbers entered are "<< endl;
customList.printLists();
cout << "When added together they equal " << endl;
customList.add();
cout << "The bigger integer minus the shorter integer is " << endl;
customList.subtract();
return 0;
}