上一篇
在实际开发中,我们经常会遇到超出 int 或 long 范围的大整数运算需求。例如密码学、金融系统或科学计算等领域,就需要使用 C#大数运算 技术。虽然 .NET 提供了 BigInteger 类,但理解其底层原理对提升编程能力至关重要。本文将手把手教你如何自定义大数类,实现基本的加减乘除功能。

尽管 C# 的 System.Numerics.BigInteger 已经非常强大,但在学习阶段,自己动手实现一个简化版的大数类有助于:
我们将用字符串表示大整数(如 "12345678901234567890"),内部用 List<int> 存储每一位数字(低位在前,便于进位处理)。例如:
"1234" → [4, 3, 2, 1] // 个位在索引0,十位在索引1...下面是一个支持加法和乘法的简化版 BigNumber 类:
using System;using System.Collections.Generic;using System.Linq;public class BigNumber{ private List<int> digits; // 低位在前 private bool isNegative; public BigNumber(string number) { if (string.IsNullOrEmpty(number)) throw new ArgumentException("数字字符串不能为空"); isNegative = number[0] == '-'; int startIndex = isNegative ? 1 : 0; digits = new List<int>(); for (int i = number.Length - 1; i >= startIndex; i--) { if (!char.IsDigit(number[i])) throw new ArgumentException("包含非法字符"); digits.Add(number[i] - '0'); } // 移除前导零(但保留至少一位) while (digits.Count > 1 && digits[digits.Count - 1] == 0) digits.RemoveAt(digits.Count - 1); } // 加法 public static BigNumber operator +(BigNumber a, BigNumber b) { // 简化:仅处理同号情况(实际应考虑异号即减法) if (a.isNegative != b.isNegative) throw new NotImplementedException("暂不支持异号加法"); var result = new List<int>(); int carry = 0; int maxLength = Math.Max(a.digits.Count, b.digits.Count); for (int i = 0; i < maxLength || carry != 0; i++) { int sum = carry; if (i < a.digits.Count) sum += a.digits[i]; if (i < b.digits.Count) sum += b.digits[i]; result.Add(sum % 10); carry = sum / 10; } string resultStr = (a.isNegative ? "-" : "") + string.Concat(result.AsEnumerable().Reverse()); return new BigNumber(resultStr); } // 乘法(模拟竖式) public static BigNumber operator *(BigNumber a, BigNumber b) { var result = new int[a.digits.Count + b.digits.Count]; for (int i = 0; i < a.digits.Count; i++) { for (int j = 0; j < b.digits.Count; j++) { result[i + j] += a.digits[i] * b.digits[j]; if (result[i + j] >= 10) { result[i + j + 1] += result[i + j] / 10; result[i + j] %= 10; } } } // 处理进位 for (int i = 0; i < result.Length - 1; i++) { if (result[i] >= 10) { result[i + 1] += result[i] / 10; result[i] %= 10; } } // 转为字符串 int lastIndex = result.Length - 1; while (lastIndex > 0 && result[lastIndex] == 0) lastIndex--; var sb = new System.Text.StringBuilder(); if (a.isNegative != b.isNegative) sb.Append('-'); for (int i = lastIndex; i >= 0; i--) sb.Append(result[i]); return new BigNumber(sb.ToString()); } public override string ToString() { var str = string.Concat(digits.AsEnumerable().Reverse()); return isNegative ? "-" + str : str; }}class Program{ static void Main() { var num1 = new BigNumber("12345678901234567890"); var num2 = new BigNumber("98765432109876543210"); var sum = num1 + num2; var product = num1 * num2; Console.WriteLine($"和: {sum}"); Console.WriteLine($"积: {product}"); // 输出: // 和: 111111111011111111100 // 积: 121932631137021795223727419430199820 }}当前实现仅为基础版本,你可以进一步完善:
通过本教程,你已经掌握了如何在 C# 中实现一个简易的 大整数加减乘除 类。这不仅加深了你对 C#大数运算 机制的理解,也为你日后处理高精度计算任务打下了坚实基础。记住,编程能力的提升源于不断实践——试着扩展这个类,让它支持更多功能吧!
本文由主机测评网于2025-12-13发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025126953.html