【 Grey Hack 】大数四则运算

版本:Grey Hack v0.7.3619 - Alpha


       在Gs中,位数大于15的整数将以科学计数法显示,故这里提供一种基于字符串加法的四则大数运算算法。由于位数大于10的字符串无法用to_int方法转化为整数,因此本示例中以长度9分割字符串以加速计算。

效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

乘方

在这里插入图片描述
对照:
在这里插入图片描述

源码

add = function(Str0, Str1)
	Str0 = Str0.trim
	Str1 = Str1.trim
	
	if Str0[0] == "+" or Str0[0] == "-" then
		StrNum0 = Str0[1:]
	else
		StrNum0 = Str0[0:]
	end if 
	while StrNum0[0] == "0" and len(StrNum0) > 1
		StrNum0 = StrNum0[1:]
	end while
	Len0 = len(StrNum0)
	
	if Str1[0] == "+" or Str1[0] == "-" then
		StrNum1 = Str1[1:]
	else
		StrNum1 = Str1[0:]
	end if
	while StrNum1[0] == "0" and len(StrNum1) > 1
		StrNum1 = StrNum1[1:]
	end while
	Len1 = len(StrNum1)
	
	if Str0[0] == "-" then 
		Sign0 = "-"
	else
		Sign0 = "+"
	end if
	if Str1[0] == "-" then 
		Sign1 = "-"
	else
		Sign1 = "+"
	end if
	
	Sign = ""
	if Len0 > Len1 then
		Sign = Sign0
		StrLonger = StrNum0[0:]
		StrSmaller = StrNum1[0:]
	else if Len0 < Len1 then
		Sign = Sign1
		StrLonger = StrNum1[0:]
		StrSmaller = StrNum0[0:]
	else
		for _ in range(0, Len0 - 1, 1)
			if StrNum0[_].to_int > StrNum1[_].to_int then
				Sign = Sign0
				StrLonger = StrNum0[0:]
				StrSmaller = StrNum1[0:]
			else if StrNum0[_].to_int < StrNum1[_].to_int then
				Sign = Sign1
				StrLonger = StrNum1[0:]
				StrSmaller = StrNum0[0:]
			end if
			if Sign != "" then break
		end for
	end if
	if Sign == "" then 
		Sign = Sign0
		StrLonger = StrNum0[0:]
		StrSmaller = StrNum1[0:]
	end if
	
	StrLongerList = [] 
	while len(StrLonger) > 9
		StrLongerList = StrLongerList + [StrLonger[-9:]]
		StrLonger = StrLonger[:-9]
	end while
	if StrLonger != "" then StrLongerList = StrLongerList + [StrLonger]
	
	StrSmallerList = [] 
	while len(StrSmaller) > 9
		StrSmallerList = StrSmallerList + [StrSmaller[-9:]]
		StrSmaller = StrSmaller[:-9]
	end while
	if StrSmaller != "" then StrSmallerList = StrSmallerList + [StrSmaller]

	ResultStr = ""
	if Sign0 == Sign1 then 
		Jinwei = 0
		
		for _ in range(0, len(StrSmallerList) - 1, 1)
			temp = Jinwei + StrLongerList[_].to_int + StrSmallerList[_].to_int
			if temp >= 1000000000 then
				temp = temp - 1000000000
				Jinwei = 1
			else
				Jinwei = 0
			end if
			temp = str(temp)
			
			while len(temp) < 9
				temp = "0" + temp
			end while
			
			ResultStr = temp + ResultStr
			
		end for
		if len(StrLongerList) != len(StrSmallerList) then
			for _ in range(len(StrSmallerList), len(StrLongerList) - 1, 1)
				temp = Jinwei + StrLongerList[_].to_int
				if temp >= 1000000000 then
					temp = temp - 1000000000
					Jinwei = 1
				else
					Jinwei = 0
				end if
				temp = str(temp)
				while len(temp) < 9
					temp = "0" + temp
				end while
				ResultStr = temp + ResultStr
			end for
		end if
		if Jinwei == 1 then
			ResultStr = "1" + ResultStr
			Jinwei = 0
		end if
	else
		Jiewei = 0
		for _ in range(0, len(StrSmallerList) - 1, 1)
			temp = Jiewei + StrLongerList[_].to_int - StrSmallerList[_].to_int
			if temp < 0 then
				temp = temp + 1000000000
				Jiewei = -1
			else
				Jiewei = 0
			end if
			temp = str(temp)
			while len(temp) < 9
				temp = "0" + temp
			end while
			ResultStr = temp + ResultStr
		end for
		if len(StrLongerList) != len(StrSmallerList) then
			for _ in range(len(StrSmallerList), len(StrLongerList) - 1, 1)
				temp = Jiewei + StrLongerList[_].to_int
				if temp < 0 then
					temp = temp + 1000000000
					Jiewei = -1
				else
					Jiewei = 0
				end if
				temp = str(temp)
				while len(temp) < 9
					temp = "0" + temp
				end while
				ResultStr = temp + ResultStr
			end for
		end if
	end if
	while ResultStr[0] == "0" and len(ResultStr) > 1
		ResultStr = ResultStr[1:]
	end while
	ResultStr = Sign + ResultStr
	if len(ResultStr) == 2 and ResultStr[1] == "0" then
		return "0"
	else
		return ResultStr
	end if
end function

sub = function(Str0, Str1)
	Str0 = Str0.trim
	Str1 = Str1.trim
	
	if Str0[0] == "+" or Str0[0] == "-" then
		StrNum0 = Str0[1:]
	else
		StrNum0 = Str0[0:]
	end if 
	while StrNum0[0] == "0" and len(StrNum0) > 1
		StrNum0 = StrNum0[1:]
	end while
	
	if Str1[0] == "+" or Str1[0] == "-" then
		StrNum1 = Str1[1:]
	else
		StrNum1 = Str1[0:]
	end if
	while StrNum1[0] == "0" and len(StrNum1) > 1
		StrNum1 = StrNum1[1:]
	end while
	
	if Str0[0] == "-" then 
		Sign0 = "-"
	else
		Sign0 = "+"
	end if
	if Str1[0] == "-" then 
		Sign1 = "+"
	else
		Sign1 = "-"
	end if
	return add(Sign0 + StrNum0, Sign1 + StrNum1)
end function

mul = function(Str0, Str1)
	Str0 = Str0.trim
	Str1 = Str1.trim
	
	if Str0[0] == "+" or Str0[0] == "-" then
		StrNum0 = Str0[1:]
	else
		StrNum0 = Str0[0:]
	end if 
	while StrNum0[0] == "0" and len(StrNum0) > 1
		StrNum0 = StrNum0[1:]
	end while
	Len0 = len(StrNum0)
	
	if Str1[0] == "+" or Str1[0] == "-" then
		StrNum1 = Str1[1:]
	else
		StrNum1 = Str1[0:]
	end if
	while StrNum1[0] == "0" and len(StrNum1) > 1
		StrNum1 = StrNum1[1:]
	end while
	Len1 = len(StrNum1)
	
	if StrNum0 == "0" or StrNum1 == "0" then return "0"
	
	if Str0[0] == "-" then 
		Sign0 = "-"
	else
		Sign0 = "+"
	end if
	if Str1[0] == "-" then 
		Sign1 = "-"
	else
		Sign1 = "+"
	end if
	
	Sign = ""
	if Len0 > Len1 then
		Sign = Sign0
		StrLonger = StrNum0[0:]
		StrSmaller = StrNum1[0:]
	else if Len0 < Len1 then
		Sign = Sign1
		StrLonger = StrNum1[0:]
		StrSmaller = StrNum0[0:]
	else
		for _ in range(0, Len0 - 1, 1)
			if StrNum0[_].to_int > StrNum1[_].to_int then
				Sign = Sign0
				StrLonger = StrNum0[0:]
				StrSmaller = StrNum1[0:]
			else if StrNum0[_].to_int < StrNum1[_].to_int then
				Sign = Sign1
				StrLonger = StrNum1[0:]
				StrSmaller = StrNum0[0:]
			end if
			if Sign != "" then break
		end for
	end if
	if Sign == "" then 
		Sign = Sign0
		StrLonger = StrNum0[0:]
		StrSmaller = StrNum1[0:]
	end if
	
	if Sign0 == Sign1 then
		Sign = "+"
	else
		Sign = "-"
	end if
	
	ResultStr = "0"
	while StrSmaller != "0"
		StrSmaller = add(StrSmaller, "-1")
		ResultStr = add(ResultStr, StrLonger)
	end while
	return (Sign + ResultStr[1:])
end function

div = function(Str0, Str1)
	Str0 = Str0.trim
	Str1 = Str1.trim
	
	if Str0[0] == "+" or Str0[0] == "-" then
		StrNum0 = Str0[1:]
	else
		StrNum0 = Str0[0:]
	end if 
	while StrNum0[0] == "0" and len(StrNum0) > 1
		StrNum0 = StrNum0[1:]
	end while
	
	if Str1[0] == "+" or Str1[0] == "-" then
		StrNum1 = Str1[1:]
	else
		StrNum1 = Str1[0:]
	end if
	while StrNum1[0] == "0" and len(StrNum1) > 1
		StrNum1 = StrNum1[1:]
	end while
	
	if StrNum1 == "0" then exit("Runtime error: divide by zero")
	if StrNum0 == "0" then return ["0", "0"]
	
	if Str0[0] == "-" then 
		Sign0 = "-"
	else
		Sign0 = "+"
	end if
	if Str1[0] == "-" then 
		Sign1 = "-"
	else
		Sign1 = "+"
	end if
	
	if Sign0 == Sign1 then
		Sign = "+"
	else
		Sign = "-"
	end if
	
	ResultStr = "0"
	TempStr = sub(StrNum0, StrNum1)
	while TempStr[0] != "-" 
		StrNum0 = TempStr
		TempStr = sub(StrNum0, StrNum1)
		ResultStr = add(ResultStr, "1")
	end while
	if ResultStr[0] == "+" then 
		ResultStr = Sign + ResultStr[1:]
	end if
	if StrNum0[0] == "+" then 
		StrNum0 = Sign0 + StrNum0[1:]
	end if
	return [ResultStr, StrNum0]
end function

pow = function(Str0, Str1)
	Str0 = Str0.trim
	Str1 = Str1.trim
	
	if Str0[0] == "+" or Str0[0] == "-" then
		StrNum0 = Str0[1:]
	else
		StrNum0 = Str0[0:]
	end if 
	while StrNum0[0] == "0" and len(StrNum0) > 1
		StrNum0 = StrNum0[1:]
	end while
	
	if Str1[0] == "+" or Str1[0] == "-" then
		StrNum1 = Str1[1:]
	else
		StrNum1 = Str1[0:]
	end if
	while StrNum1[0] == "0" and len(StrNum1) > 1
		StrNum1 = StrNum1[1:]
	end while
	
	if StrNum1 == "0" then return "+1"
	if StrNum0 == "0" then return "0"
	
	if Str0[0] == "-" then 
		Sign0 = "-"
	else
		Sign0 = "+"
	end if
	if Str1[0] == "-" then 
		Sign1 = "-"
	else
		Sign1 = "+"
	end if
	
	ResultStr = "1"
	while StrNum1 != "0"
		ResultStr = mul(ResultStr, Sign0 + StrNum0)
		StrNum1 = add(StrNum1, "-1")
	end while
	return ResultStr
end function

// print(add(params[0],params[1]))
// print(sub(params[0],params[1]))
// print(mul(params[0],params[1]))
// print(div(params[0],params[1]))
// print(pow(params[0],params[1]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乙酸氧铍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值