Instead of the 40 element HugeInteger array that Deitel mentioned, let’s start s
ID: 3607039 • Letter: I
Question
Instead of the 40 element HugeInteger array that Deitel mentioned, let’s start small with 5 digits (which is common int type integer), but we use one dimensional array of 5 elements.
Create a C# class HugeInteger of 5 element array. Here an int array of 5 elements like {1, 2, 3, 4, 5} can represent either the integer 12345 or the integer 54321. Please clearly state which way you want to do.
Let’s suppose your {1, 2, 3, 4, 5} means 54321 using an int array of 5 elements. This is very similar to the sum of two polynomials as in Q4, except if we add {1, 2, 5, 5, 4} and { 1, 2, 5, 5, 4} as two polynomials, we get {2, 4, 10, 10, 8} since the sum of polynomials 4x4 + 5x3 + 5x2 + 2x + 1 and 4x4 + 5x3 + 5x2 + 2x + 1 is 8x4 + 10x3 + 10x2 + 4x + 2, but if we add {1, 2, 5, 5, 4} and { 1, 2, 5, 5, 4} as two HugeIntegers of 5 digits, then we get {2, 4, 0, 1, 9} since 45521 + 45521 = 91042 as a 5 digits integer.
a) (4%) Define the HugeInteger class with 5 elements arrays. Add either Input method that Deitel suggested or a constructor from integer like HugeInteger (int a), that will convert integer 54321 to the array {1, 2, 3, 4, 5}.
b) (8%) Create the Sum method of two HugeIntegers of 5 elements (5 digits). Note {1, 2, 5, 5, 4} + {1, 2, 5, 5, 4} = {2, 4, 0, 1, 9}. We do not worry about the overflow case at this moment, i.e. {1, 2, 3, 4, 5} + {1, 2, 3, 4, 5} = {2, 4, 6, 8, 10} or {2, 4, 6, 8, 0, 1}, a HugeInteger array of 6 elements
Deitel did not seem to consider the case that HugeInteger can be negative such as -54321.
c) (3%) Enhance class HugeInteger to have an attribute sign, which is 1 for positive integer, -1 for negative integer 9an d1 for zero). You can also define sign to be Boolean.
d) (12%) Define Subtract method that calculates the difference of two HugeInteger of 5 digits. These two HugeIntegers can be positive or negative. The difference can be positive or negative. Also enhance your Sum method to handle the signs.
Test your subtract method and sum method (now with the sign) with two pairs of HugeIntegers: 54321 – 32176 (i.e. {1, 2, 3, 4, 5} sub {6, 7, 1, 2, 3}; and -12345 sum 23456, the first number is the negative 12345, or {5, 4, 3, 2, 1; -}, and the second number is the positive 23456, or {6, 5, 4, 3, 2: +}.
10.10 (HugeInteger Class) Create a class HugeInteger which uses a 40-element array of digits to store integers as large as 40 digits each. Provide methods Input, ToString, Add and Subtract. For comparing HugeInteger objects, provide the following methods: IsEqualTo, IsNotEqualTo, IsGrea- terThan, IsLessThan, IsGreaterThanorEqualTo and IsLessThanorEqualTo. Each of these is a meth- od that returns true if the relationship holds between the two HugeInteger objects and returns false if the relationship does not hold. Provide method IsZero. If you feel ambitious, also provide methods Multiply, Divide and Remainder. In the Input method, use the string method ToChar- Array to convert the input string into an array of characters, then iterate through these characters to create your HugeInteger. [Note: The .NET Framework Class Library includes type BigInteger for arbitrary sized integer values.]Explanation / Answer
HugeInteger.cs
using System;
using System.Numerics;
using System.Text;
public class HugeInteger
{
BigInteger num1 = 0; //For storing BigInteger values for use in the methods
BigInteger num2 = 0;
public void Input(string input1, string input2)
{
var chars1 = input1.ToCharArray(); //This could have been done without adding the strings to Char Array, but the book insisted I do so in this method so... yeah...
var chars2 = input2.ToCharArray(); //
string n1 = new String(chars1); //Creates new strings based on chars in arrays
string n2 = new String(chars2); //
num1 = BigInteger.Parse(n1); //Parses BigInteger numbers from new strings and stores in class variables
num2 = BigInteger.Parse(n2); //
}
public void Clear() //Clears values in Biginteger variables when called from Main
{
num1 = 0;
num2 = 0;
}
public string ToString(BigInteger value) //Converts any BigInteger value passed to it to string for use in Main for Console.Write
{
string output = value.ToString();
return output;
}
public string ToString(Decimal value) //Converts any Decimal value passed to it to string for use in Main for Console.Write
{
string output = value.ToString();
return output;
}
public BigInteger Add() //Adds numbers
{
BigInteger sum = num1 + num2;
return sum;
}
public BigInteger Subtract() //Subtracts numbers
{
BigInteger diff = num1 - num2;
return diff;
}
public bool IsEqualTo() //**Comparisons of numbers returning true or false
{
bool result = false;
if (num1 == num2)
result = true;
return result;
}
public bool IsNotEqualTo() //**
{
bool result = false;
if (num1 != num2)
result = true;
return result;
}
public bool IsGreaterThan() //**
{
bool result = false;
if (num1 > num2)
result = true;
return result;
}
public bool IsLessThan() //**
{
bool result = false;
if (num1 < num2)
result = true;
return result;
}
public bool IsGreaterThanOrEqualTo() //**
{
bool result = false;
if (num1 >= num2)
result = true;
return result;
}
public bool IsLessThanOrEqualTo() //**
{
bool result = false;
if (num1 <= num2)
result = true;
return result;
}
public bool IsZero(string choice) //Checks if numbers passed in are zero
{
bool result = false;
if (choice == "1")
{
if (num1 == 0)
result = true;
}
if (choice == "2")
{
if (num2 == 0)
result = true;
}
return result;
}
public BigInteger Multiply() //Returns product of numbers
{
BigInteger product = num1 * num2;
return product;
}
public Decimal Divide() //Returns quotient of numbers
{
Decimal decnum1 = Convert.ToDecimal(ToString(num1));
Decimal decnum2 = Convert.ToDecimal(ToString(num2));
Decimal quotient = Math.Round(decnum1 / decnum2, 3);
return quotient;
}
public Decimal Remainder() //Returns remainder of numbers
{
String strnum1 = ToString(num1);
String strnum2 = ToString(num2);
Decimal remainder = Math.Round(Convert.ToDecimal(strnum1) % Convert.ToDecimal(strnum2), 3);
return remainder;
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
namespace CSHARP_Chapter_10_Exercise_10._10
{
class Program
{
static void Main()
{
HugeInteger hugeInteger = new HugeInteger();
string number1 = "";
string number2 = "";
int n = 0;
int c = 0;
while (c == 0)
{
while (n == 0)
{
Console.Write("Enter the first number, no more than 40 digits in length: ");
number1 = Console.ReadLine();
if (number1.Length > 40) //Checks to ensure number is not larger than 40 digits
{
Console.WriteLine("Number entered has more than 40 digits, try again");
Console.WriteLine("");
}
else
{
n = 1; //Continues when number is within acceptable range
}
}
while (n == 1)
{
Console.Write("Enter the second number, no more than 40 digits in length: ");
number2 = Console.ReadLine();
if (number2.Length > 40) //Checks to ensure number is not larger than 40 digits
{
Console.WriteLine("Number entered has more than 40 digits, try again");
Console.WriteLine("");
}
else
{
Console.WriteLine(""); //Continues when number is within acceptable range
n = 2;
}
}
Console.WriteLine("You have entered the following numbers:");
Console.WriteLine("First Number: {0}", number1);
Console.WriteLine("Second Number: {0}", number2);
Console.WriteLine("Enter 1 if you accept these numbers, or 2 to enter new ones.");
Console.WriteLine("Keep in mind, all calculations will be done in order.");
Console.Write("Example (First Number - Second Number = ?) or (Is First Number > Second Number): ");
if (Console.ReadLine() == "1")
{
c = 1;
hugeInteger.Input(number1, number2); //From here down, uses HugeInteger Class heavily to calculate and compare numbers
Console.WriteLine("");
Console.WriteLine("The following information pertains to the two numbers you entered:");
Console.WriteLine("The sum of the numbers: {0}", hugeInteger.ToString(hugeInteger.Add()));
Console.WriteLine("The difference of the numbers: {0}", hugeInteger.ToString(hugeInteger.Subtract()));
Console.WriteLine("The numbers are equal: {0}", hugeInteger.IsEqualTo());
Console.WriteLine("The numbers are not equal: {0}", hugeInteger.IsNotEqualTo());
Console.WriteLine("The first number is greater than the second: {0}", hugeInteger.IsGreaterThan());
Console.WriteLine("The first number is less than the second: {0}", hugeInteger.IsLessThan());
Console.WriteLine("The first number is greater than or equal to the second: {0}", hugeInteger.IsGreaterThanOrEqualTo());
Console.WriteLine("The first number is less than or equal to the second: {0}", hugeInteger.IsLessThanOrEqualTo());
Console.WriteLine("The first number is zero: {0}", hugeInteger.IsZero("1"));
Console.WriteLine("The second number is zero: {0}", hugeInteger.IsZero("2"));
Console.WriteLine("The product of the numbers: {0}", hugeInteger.ToString(hugeInteger.Multiply()));
if (number2 == "0")
Console.WriteLine("The quotient of the numbers: Unable to process, you cannot divide by zero."); //Ensures dividing by 0 does not occur, as it is not possible
else
Console.WriteLine("The quotient of the numbers: {0}", hugeInteger.ToString(hugeInteger.Divide()));
if (number2 == "0")
Console.WriteLine("The remainder of the numbers: Unable to process, you cannot divide by zero."); //Ensures dividing by 0 does not occur, as it is not possible
else
Console.WriteLine("The remainder of the numbers: {0}", hugeInteger.ToString(hugeInteger.Remainder()));
}
else
{
hugeInteger.Clear(); //Clears values to enter new numbers
Console.WriteLine("");
n = 0;
}
}
}
}
}