当前位置:网站首页>Addition and subtraction of C super integer
Addition and subtraction of C super integer
2022-07-22 03:18:00 【Dongfang kuaidi】
A simple example , The theory supports infinite addition and subtraction
if (GUILayout.Button(" string Add"))
{
string v= AddOrReduceStrings("3091345589131",
"3091345589131");
string j = AddOrReduceStrings("5678931",
"43229",2);
Debug.Log(v);
Debug.Log(j);
}
Concrete realization
/// <summary>
/// String addition and subtraction
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <param name="type"> 1 add 2reduce </param>
/// <returns></returns>
public static string AddOrReduceStrings(string num1, string num2, int type = 1)
{
num1 = num1.TrimStart('0');
num2 = num2.TrimStart('0');
if (type == 2)
{
// Return directly with small decrement 0
if (num2.Length > num1.Length)
{
return "0";
}
else if (num2.Length == num1.Length)
{
for (int i = 0; i < num1.Length; i++)
{
int p = int.Parse(num1.Substring(i, 1));
int p2 = int.Parse(num2.Substring(i, 1));
if (p < p2)
{
return "0";
//break;
}
}
}
}
// Define a big 1 Bit array
var max = Math.Max(num1.Length, num2.Length) + 1;
var total = new int[max];
// To the left 0 To the same number , Otherwise, additional boundary treatment is required
num1 = num1.PadLeft(max, '0');
num2 = num2.PadLeft(max, '0');
// Carry sign
var carry = false;
var sum = 0;
// Borrow sign
var reduce = false;
if (type == 1)
{
for (int i = max - 1; i >= 0; i--)
{
sum = int.Parse(num1[i].ToString()) + int.Parse(num2[i].ToString());
if (carry) sum++;
total[i] = int.Parse(sum.ToString()[sum.ToString().Length - 1].ToString());
carry = sum > 9;
}
}
else
{
for (int i = max - 1; i >= 0; i--)
{
var n1 = int.Parse(num1[i].ToString());
var n2 = int.Parse(num2[i].ToString());
if (reduce)
{
n1--;
}
if (n1.CompareTo(n2) < 0)
{
sum = n1 + 10 - n2;
}
else
{
sum = n1 - n2;
}
//if (reduce)
//{
// sum--;
//}
total[i] = int.Parse(sum.ToString()[sum.ToString().Length - 1].ToString());
reduce = n1.CompareTo(n2) < 0;
}
}
// Define results
var res = string.Empty;
// Traverse the output to res
total.ToList().ForEach(r => { res += r.ToString(); });
Debug.Log("{0}={1}={2}={3}".Format(type, num1, num2, res));
// Cancel the highest position 0
res = res.TrimStart('0');
// If it is empty , return 0
if (res.Length == 0) res = "0";
// Return results
return res;
}
A gift of benefits
public static string NumFormat(this string _num, params object[] _values)
{
var outStr = "";
var len = _num.Length;
if (len > 4)
{
outStr = string.Format("{0}K",_num.ToString().Substring(0, len - 3));
}
else if (len > 5)
{
outStr = string.Format("{0} ten thousand ", _num.ToString().Substring(0, len - 4));
}
//x.ToString("###,###") or 1234567.ToString("N0")
// C# Convert a number into a thousandth character with two decimal places :
// Such as 1234567.891 become 1,234,567.89
// Method :String.Format("{0:N}", 1234567.891); // The default is two decimal places , If there are no decimal places , Then fill two decimal places 0
// or :String.Format("{0:N2}", 1234567.891);
return outStr;
}
边栏推荐
猜你喜欢
Write to yourself: 2021 version of the transfer reference book
word文档中动态生成excel表格(基金公告系列讲解)
Application of workflow engine in vivo marketing automation | engine 03
Stream stream filters the list set for duplicate fields
day 3
Self defined multiple code of station B
Packet format of network packets
Dynamically generate excel forms in word documents (explanation of Fund announcement Series)
游戏中的数学之各种插值
ECCV 2022 | multi domain long tailed distributed learning to solve the problem of unbalanced domain generalization
随机推荐
Jenkins: when building, enter the subdirectory and then perform MVN operation
Stream stream filters the list set for duplicate fields
ELF 格式详解(一)
Brief analysis of Redux technology stack (I): Redux Middleware
matplotlib(六)三维作图
Lamda表达式使用
CMake系列教程2-HelloWorld
[machine learning] dimension reduction technology PCA
百度地图、高德地图、腾讯地图三位一体地图定位开发
Harbor warehouse construction and simple use
正则化的基本思路
ECCV 2022 | multi domain long tailed distributed learning to solve the problem of unbalanced domain generalization
【机器学习】Kmeans聚类
MySQL creates new users and authorizes them
剑指 Offer 53 - I. 在排序数组中查找数字 I
如何使用界面控件Telerik UI for WinForms开发步骤进度条?
Write to yourself: 2021 version of the transfer reference book
【机器学习】层次聚类
我的创作纪念日
高效开发工具使用技巧