Using C# InfInt.cs represents an integer that would normally overflow your stand
ID: 3748168 • Letter: U
Question
Using C#
InfInt.cs represents an integer that would normally overflow your standard int. See the code for implementation details they should be self explanatory. You are provided completed methods for Add and the associated helper methods and Subtract without the helper methods. You will need to complete the helper methods for subtract, for the constructor that takes a string, multiply, divide, toString, compareTo and any other non implemented methods in the class file. You can add private methods to the class as needed to complete this assignment.
Provided code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InfInt
{
public class InfInt : IComparable
{
private const int DIGITS = 40; //max size is 40
private int[] Integer { get; set; } //array containing our infint
private bool Positive { get; set; } //is it positive
//default value constructor
public InfInt()
{
Integer = new int[DIGITS];
Positive = true;
}
public InfInt(string input)
{
//accepts a string value of your infint and initialize the fields
//remember you are allowed a max length of 40 and it can be a negative number
//if you have a negative number the length could be 41 to account for the '-' in front
throw new NotImplementedException();
}
//freebie add courtesy of professor amack
public InfInt Add(InfInt addValue)
{
InfInt temp = new InfInt();
if (Positive == addValue.Positive)
{
temp = AddPositives(addValue);
}
//addvalue is negative
else if (Positive && (!addValue.Positive))
{
addValue.Positive = true;
if (IsGreaterThan(addValue))
{
temp = SubtractPositives(addValue);
}
else
{
temp = addValue.SubtractPositives(this);
temp.Positive = false;
}
addValue.Positive = false;
}
else if (!Positive && addValue.Positive)
{
addValue.Positive = false;
if (IsGreaterThan(addValue))
{
temp = addValue.SubtractPositives(this);
}
else
{
temp = SubtractPositives(addValue);
temp.Positive = false;
}
addValue.Positive = true;
}
return temp;
}
private InfInt SubtractPositives(InfInt addValue)
{
throw new NotImplementedException();
}
private bool IsGreaterThan(InfInt addValue)
{
throw new NotImplementedException();
}
private InfInt AddPositives(InfInt addValue)
{
InfInt temp = new InfInt();
int carry = 0;
//iterate the infint
for (int i = DIGITS - 1; i >= 0; i--)
{
temp.Integer[i] = Integer[i] + addValue.Integer[i] + carry;
//determine if we need to carry the 1
if (temp.Integer[i] > 9)
{
temp.Integer[i] %= 10; //reduce to 0-9
carry = 1;
}
else
{
carry = 0;
}
}
if (!Positive)
{
temp.Positive = false;
}
return temp;
}
public InfInt Subtract(InfInt subtractValue)
{
InfInt temp = new InfInt(); // temporary result
// subtractValue is negative
if (Positive && (!subtractValue.Positive))
{
temp = AddPositives(subtractValue);
}
// this InfInt is negative
else if (!Positive && subtractValue.Positive)
{
temp = AddPositives(subtractValue);
}
// at this point, both InfInts have the same sign
else
{
bool isPositive = Positive; // original sign
bool resultPositive = Positive; // sign of the result
// set both to positive so we can compare absolute values
Positive = true;
subtractValue.Positive = true;
if (this.IsGreaterThan(subtractValue))
{
temp = this.SubtractPositives(subtractValue);
}
else
{
temp = subtractValue.SubtractPositives(this);
resultPositive = !isPositive; // flip the sign
}
Positive = isPositive;
subtractValue.Positive = isPositive;
temp.Positive = resultPositive;
}
return temp;
}
public InfInt Multiply(InfInt multValue)
{
throw new NotImplementedException();
}
public InfInt Divide(InfInt divValue)
{
throw new NotImplementedException();
}
public override string ToString()
{
throw new NotImplementedException();
}
public int CompareTo(object obj)
{
throw new NotImplementedException();
}
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InfInt
{
public class InfInt : IComparable
{
private const int DIGITS = 40; //max size is 40
private int[] Integer { get; set; } //array containing our infint
private bool Positive { get; set; } //is it positive
//default value constructor
public InfInt()
{
Integer = new int[DIGITS];
Positive = true;
}
public InfInt(string input)
{
//accepts a string value of your infint and initialize the fields
//remember you are allowed a max length of 40 and it can be a negative number
//if you have a negative number the length could be 41 to account for the '-' in front
if( input[0] != '-' && input.Length > 40)
Console.WriteLine("integer size is too big");
else if (input[0] == '-' && input.Length > 41)
Console.WriteLine("integer size is too big");
else
{
// taking size as DIGITS and not actual input.length as in rest of the program DIGITS is used
this.Integer = new int[DIGITS];
for (int i = input.Length -1; i >= 0; i--)
{
if (input[i] >= '0' && input[i] <= '9')
Integer[i] = (int)Char.GetNumericValue(input[i]);
else
{
Console.WriteLine("Invalid Integer value");
return;
}
}
//filling rest of the array with zero to maintain DiGiTs size
for (int i = DIGITS - input.Length - 1; i > 0; i--)
Integer[i] = 0;
if (input[0] != '-')
this.Positive = true;
else
this.Positive = false;
}
}
//freebie add courtesy of professor amack
public InfInt Add(InfInt addValue)
{
InfInt temp = new InfInt();
if (Positive == addValue.Positive)
{
temp = AddPositives(addValue);
}
//addvalue is negative
else if (Positive && (!addValue.Positive))
{
addValue.Positive = true;
if (IsGreaterThan(addValue))
{
temp = SubtractPositives(addValue);
}
else
{
temp = addValue.SubtractPositives(this);
temp.Positive = false;
}
addValue.Positive = false;
}
else if (!Positive && addValue.Positive)
{
addValue.Positive = false;
if (IsGreaterThan(addValue))
{
temp = addValue.SubtractPositives(this);
}
else
{
temp = SubtractPositives(addValue);
temp.Positive = false;
}
addValue.Positive = true;
}
return temp;
}
private InfInt SubtractPositives(InfInt subValue)
{
InfInt temp = new InfInt();
int borrow = 0;
//iterate the infint
for (int i = DIGITS - 1; i >= 0; i--)
{
//check for borrow
if (Integer[i] < subValue.Integer[i])
{
for (int j = i - 1; j >= 0; j--)
{
if (Integer[j] == 0)
Integer[j] += 10;
if (Integer[j - 1] == 0)
continue;
else
{
Integer[j - 1] -= 1;
break;
}
}
Integer[i - 1] = Integer[i - 1] - 1;
Integer[i] = Integer[i] + 10;
}
temp.Integer[i] = Integer[i] - subValue.Integer[i] ;
}
return temp;
}
private bool IsGreaterThan(InfInt addValue)
{
for(int i=0; i<DIGITS; i++)
{
if (this.Integer[i] > addValue.Integer[i])
return true;
else if (this.Integer[i] < addValue.Integer[i])
return false;
}
return false;
}
private InfInt AddPositives(InfInt addValue)
{
InfInt temp = new InfInt();
int carry = 0;
//iterate the infint
for (int i = DIGITS - 1; i >= 0; i--)
{
temp.Integer[i] = Integer[i] + addValue.Integer[i] + carry;
//determine if we need to carry the 1
if (temp.Integer[i] > 9)
{
temp.Integer[i] %= 10; //reduce to 0-9
carry = 1;
}
else
{
carry = 0;
}
}
if (!Positive)
{
temp.Positive = false;
}
return temp;
}
public InfInt Subtract(InfInt subtractValue)
{
InfInt temp = new InfInt(); // temporary result
// subtractValue is negative
if (Positive && (!subtractValue.Positive))
{
temp = AddPositives(subtractValue);
}
// this InfInt is negative
else if (!Positive && subtractValue.Positive)
{
temp = AddPositives(subtractValue);
}
// at this point, both InfInts have the same sign
else
{
bool isPositive = Positive; // original sign
bool resultPositive = Positive; // sign of the result
// set both to positive so we can compare absolute values
Positive = true;
subtractValue.Positive = true;
if (this.IsGreaterThan(subtractValue))
{
temp = this.SubtractPositives(subtractValue);
}
else
{
temp = subtractValue.SubtractPositives(this);
resultPositive = !isPositive; // flip the sign
}
Positive = isPositive;
subtractValue.Positive = isPositive;
temp.Positive = resultPositive;
}
return temp;
}
/*this can be done like this
* of array values [1,2,3] & [5,6] i.e. 123 and 56
*
*
[3,2,1] * [6,5]
---------------
[18 12 6]
[15 10 5]
---------------
[18 27 16 5] // now propagate carries
[8 28 16 5]
[8 8 18 5]
[8 8 8 6]
============
result will be => [6,8,8,8]
but this considers no in little-endian
*
*/
public InfInt Multiply(InfInt multValue)
{
InfInt temp = new InfInt("0");
int carry = 0;
for (int j = DIGITS - 1; j >= 0; j--)
{
int offset = DIGITS - j - 1;
carry = 0;
for (int i = DIGITS - 1; i >= 0; i--)
{
temp.Integer[i - offset] = temp.Integer[i - offset] + (this.Integer[j] * multValue.Integer[i]) + carry;
if (temp.Integer[i] > 9)
{
carry = temp.Integer[i] / 10; ;
temp.Integer[i] %= 10; //reduce to 0-9
}
else
{
carry = 0;
}
}
}
return temp;
}
public InfInt Divide(InfInt divValue)
{
throw new NotImplementedException();
}
public override string ToString()
{
return string.Join("", Integer);
}
public int CompareTo(object obj)
{
InfInt temp = (InfInt)obj;
for (int i = 0; i < DIGITS; i++)
{
if (this.Integer[i] > temp.Integer[i])
return -1;
else if (this.Integer[i] < temp.Integer[i])
return 1;
}
return 0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.