C# - Class 使用 ILists 来存储没有 BigInt 的大整数。无法弄清楚如何将 CompareTo 和 Int.TryParse 用于 +、- 和 * 两个列表
C# - Class that uses ILists to store huge integers without BigInt. Can't figure out how to use CompareTo and Int.TryParse to +, -, and * two Lists
我一直在做作业,我是 C# 的初学者。我必须实现一个类似于 BigInt 可以做的程序:用两个大得离谱的值执行加法、减法或乘法(实际上没有使用 BigInt 库)。有人告诉我使用 CompareTo,这将使创建加法、减法和乘法变得容易,但我不知道如何实现 CompareTo。我什至不知道我的 class 是否正确实施,或者我是否遗漏了一些重要的东西。
这是我的代码:
public class HugeInt
{
char sign;
public IList<int> theInt = new List<int>();
public string ToString(IList<int> theInt)
{
string bigInt = theInt.ToString();
return bigInt;
}
public HugeInt CompareTo(HugeInt num1)
{
int numParse;
string number = ToString(theInt); /// I did this to convert the List into a string
for(int i = 0; i < number.Length; i++)
{
bool temp = Int32.TryParse(number, out numParse); /// Supposed to change each index of the string to a separate integer (not sure how to properly do this)
/// These are *supposed to* perform operations on two HugeInts ///
num1.plus(numParse, num1);
num1.minus(numParse, num1);
num1.times(numParse, num1);
}
return num1;
}
我不是来这里询问这个作业的所有答案的,我已经为此工作了几个小时,我不知道我做错了什么 -- 我已经完成了很多 google 搜索。预先感谢所有建议和帮助!
要写出这样的class,需要您对如何手算有所了解。例如,将两个数字相加时,首先要将它们的最低有效数字相加。如果结果大于 9,则必须将 1 带入下一位(explanation)。然后你继续下一个数字。
现在,这是我的看法。我想将 "huge int" 保存为从最低有效数字开始的数字列表。然后我如上所述实现 Plus
方法。我可以通过查看位数来比较两个 "huge ints"。位数最多的数是最大的。在位数相同的情况下,我需要从最高位开始逐个比较每个数字。
以下只是帮助您入门的内容。它只处理正整数并且有 Plus
和 CompareTo
方法。请注意,还有很多我没有处理的极端情况。
可以这样使用:
var num1 = new HugeInt("11112222333399998888777123123");
var num2 = new HugeInt("00194257297549");
Console.WriteLine(num1.Plus(num2).ToString()); // Writes 11112222333399999083034420672
Console.WriteLine(num1.CompareTo(num2)); // Writes -1 since num1 > num2
这里是 class:
public class HugeInt
{
// The array that contains all the digits of the number. To create a new number, you do not change this array but instead you create a new instance of HugeInt.
// The first digit is the least significant digit.
private readonly int[] digits;
public HugeInt(string number)
{
// Trim off the leading zeros
number = number.TrimStart('0');
if (number == "")
number = "0";
// Convert to digit array with the least significant digit first
digits = number.ToCharArray().Select(c => int.Parse(c.ToString())).Reverse().ToArray();
}
public HugeInt(IList<int> digits)
{
// Trim off the leading zeros
var d = digits.ToList();
while (d.Count > 1 && d.Last() == 0)
d.RemoveAt(d.Count - 1);
// Convert to digit array with the least significant digit first
this.digits = d.ToArray();
}
public HugeInt Plus(HugeInt num)
{
// Add two positive integers by adding each digit together, starting with the least significant digit.
var result = new List<int>();
int carry = 0;
for (var i = 0; i < this.digits.Length || i < num.digits.Length; i++)
{
var digit1 = i < this.digits.Length ? this.digits[i] : 0;
var digit2 = i < num.digits.Length ? num.digits[i] : 0;
var digitResult = digit1 + digit2 + carry;
carry = 0;
if (digitResult >= 10)
{
digitResult -= 10;
carry = 1;
}
result.Add(digitResult);
}
if (carry > 0)
result.Add(carry);
return new HugeInt(result);
}
public int CompareTo(HugeInt num)
{
// First compare by length of number
if (this.digits.Length > num.digits.Length)
return -1;
else if (this.digits.Length < num.digits.Length)
return 1;
else
{
// If lengths are equal, then compare each digit - starting with the most significant digit.
for (var i = this.digits.Length - 1; i >= 0; i--)
{
var cmp = this.digits[i].CompareTo(num.digits[i]);
if (cmp != 0)
return cmp;
}
return 0;
}
}
public override string ToString()
{
return String.Join("", digits.Reverse());
}
}
我一直在做作业,我是 C# 的初学者。我必须实现一个类似于 BigInt 可以做的程序:用两个大得离谱的值执行加法、减法或乘法(实际上没有使用 BigInt 库)。有人告诉我使用 CompareTo,这将使创建加法、减法和乘法变得容易,但我不知道如何实现 CompareTo。我什至不知道我的 class 是否正确实施,或者我是否遗漏了一些重要的东西。 这是我的代码:
public class HugeInt
{
char sign;
public IList<int> theInt = new List<int>();
public string ToString(IList<int> theInt)
{
string bigInt = theInt.ToString();
return bigInt;
}
public HugeInt CompareTo(HugeInt num1)
{
int numParse;
string number = ToString(theInt); /// I did this to convert the List into a string
for(int i = 0; i < number.Length; i++)
{
bool temp = Int32.TryParse(number, out numParse); /// Supposed to change each index of the string to a separate integer (not sure how to properly do this)
/// These are *supposed to* perform operations on two HugeInts ///
num1.plus(numParse, num1);
num1.minus(numParse, num1);
num1.times(numParse, num1);
}
return num1;
}
我不是来这里询问这个作业的所有答案的,我已经为此工作了几个小时,我不知道我做错了什么 -- 我已经完成了很多 google 搜索。预先感谢所有建议和帮助!
要写出这样的class,需要您对如何手算有所了解。例如,将两个数字相加时,首先要将它们的最低有效数字相加。如果结果大于 9,则必须将 1 带入下一位(explanation)。然后你继续下一个数字。
现在,这是我的看法。我想将 "huge int" 保存为从最低有效数字开始的数字列表。然后我如上所述实现 Plus
方法。我可以通过查看位数来比较两个 "huge ints"。位数最多的数是最大的。在位数相同的情况下,我需要从最高位开始逐个比较每个数字。
以下只是帮助您入门的内容。它只处理正整数并且有 Plus
和 CompareTo
方法。请注意,还有很多我没有处理的极端情况。
可以这样使用:
var num1 = new HugeInt("11112222333399998888777123123");
var num2 = new HugeInt("00194257297549");
Console.WriteLine(num1.Plus(num2).ToString()); // Writes 11112222333399999083034420672
Console.WriteLine(num1.CompareTo(num2)); // Writes -1 since num1 > num2
这里是 class:
public class HugeInt
{
// The array that contains all the digits of the number. To create a new number, you do not change this array but instead you create a new instance of HugeInt.
// The first digit is the least significant digit.
private readonly int[] digits;
public HugeInt(string number)
{
// Trim off the leading zeros
number = number.TrimStart('0');
if (number == "")
number = "0";
// Convert to digit array with the least significant digit first
digits = number.ToCharArray().Select(c => int.Parse(c.ToString())).Reverse().ToArray();
}
public HugeInt(IList<int> digits)
{
// Trim off the leading zeros
var d = digits.ToList();
while (d.Count > 1 && d.Last() == 0)
d.RemoveAt(d.Count - 1);
// Convert to digit array with the least significant digit first
this.digits = d.ToArray();
}
public HugeInt Plus(HugeInt num)
{
// Add two positive integers by adding each digit together, starting with the least significant digit.
var result = new List<int>();
int carry = 0;
for (var i = 0; i < this.digits.Length || i < num.digits.Length; i++)
{
var digit1 = i < this.digits.Length ? this.digits[i] : 0;
var digit2 = i < num.digits.Length ? num.digits[i] : 0;
var digitResult = digit1 + digit2 + carry;
carry = 0;
if (digitResult >= 10)
{
digitResult -= 10;
carry = 1;
}
result.Add(digitResult);
}
if (carry > 0)
result.Add(carry);
return new HugeInt(result);
}
public int CompareTo(HugeInt num)
{
// First compare by length of number
if (this.digits.Length > num.digits.Length)
return -1;
else if (this.digits.Length < num.digits.Length)
return 1;
else
{
// If lengths are equal, then compare each digit - starting with the most significant digit.
for (var i = this.digits.Length - 1; i >= 0; i--)
{
var cmp = this.digits[i].CompareTo(num.digits[i]);
if (cmp != 0)
return cmp;
}
return 0;
}
}
public override string ToString()
{
return String.Join("", digits.Reverse());
}
}