class HugeInteger
{
public const int HUGE_INTEGER_LIMIT = 40;
public int[] hiDigits;
public bool[] comparison;
public int hiLength;
private string hugeInteger;
//constructor
public HugeInteger()
{
hiDigits = new int[HUGE_INTEGER_LIMIT];
}
public HugeInteger(string hi)
{
hiDigits = new int[HUGE_INTEGER_LIMIT];
hugeInteger = hi;
Input(hi);
}
public void Input(string input)
{
char[] hiDigitss = new char[HUGE_INTEGER_LIMIT];
hiDigitss = input.ToCharArray();
hiLength = hiDigits.Length;
for (int i = hiLength - 1; i > 0; i--)
{
hiDigits[i] = hiDigitss[i] - '0';
}
public override string ToString()
{
string num = string.Join("", hiDigits.Select(x => x.ToString()).ToArray());
return num;
}
public HugeInteger Add(HugeInteger val)
{
var result = new HugeInteger();
int carry = 0;
int sum = 0;
hiLength = Math.Max(val.hiDigits.Length, this.hiDigits.Length);
for (int i = 0; i < result.hiLength - 1; i++)
{
sum = this.hiDigits[i] + val.hiDigits[i] + carry;
result.hiDigits[i] = sum % 10;
carry = sum / 10;
}
//int[] result = new int[number1.length];
//for (int i = number1.length - 1; i >= 0; i--)
//{
// sum = number1[i] + number2[i] + carry;
// result[i] = sum % 10;
// carry = sum / 10;
//}`enter code here`
return result;
}
public bool IsEqualTo(HugeInteger hi)
{
comparison = new bool[hi.hiDigits.GetUpperBound(0)];
for (int i = 0; i < this.hiDigits.GetUpperBound(0); i++)
{
if (this.hiDigits[i] == hi.hiDigits[i])
{
comparison[i] = true;
}
else
{
comparison[i] = false;
}
}
if(comparison.All(c => c.Equals(true)))
{
return true;
}
else
{
return false;
}
}
In the code above, I am trying to add two objects in main by using
num1.Add(num2)
num1 and num2 both hold an array of ints (int[]) that represent digits in a string of numbers. I am trying to create a method that will create a new array called result from adding num1 array and num2 array. Going through debugging, it's giving me
Index out of range
and val(num2) isn't seen when adding but this(num1) is.
I am also trying to make another method to substract.
edit: pasted more code as requested. Currently trying to change/fix input method.