日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【 Grey Hack 】大数四则运算

發布時間:2023/12/9 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【 Grey Hack 】大数四则运算 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 效果
    • 乘方
  • 源碼

版本:Grey Hack v0.7.3619 - Alpha


???????在Gs中,位數大于15的整數將以科學計數法顯示,故這里提供一種基于字符串加法的四則大數運算算法。由于位數大于10的字符串無法用to_int方法轉化為整數,因此本示例中以長度9分割字符串以加速計算。

效果

乘方


對照:

源碼

add = function(Str0, Str1)Str0 = Str0.trimStr1 = Str1.trimif Str0[0] == "+" or Str0[0] == "-" thenStrNum0 = Str0[1:]elseStrNum0 = Str0[0:]end if while StrNum0[0] == "0" and len(StrNum0) > 1StrNum0 = StrNum0[1:]end whileLen0 = len(StrNum0)if Str1[0] == "+" or Str1[0] == "-" thenStrNum1 = Str1[1:]elseStrNum1 = Str1[0:]end ifwhile StrNum1[0] == "0" and len(StrNum1) > 1StrNum1 = StrNum1[1:]end whileLen1 = len(StrNum1)if Str0[0] == "-" then Sign0 = "-"elseSign0 = "+"end ifif Str1[0] == "-" then Sign1 = "-"elseSign1 = "+"end ifSign = ""if Len0 > Len1 thenSign = Sign0StrLonger = StrNum0[0:]StrSmaller = StrNum1[0:]else if Len0 < Len1 thenSign = Sign1StrLonger = StrNum1[0:]StrSmaller = StrNum0[0:]elsefor _ in range(0, Len0 - 1, 1)if StrNum0[_].to_int > StrNum1[_].to_int thenSign = Sign0StrLonger = StrNum0[0:]StrSmaller = StrNum1[0:]else if StrNum0[_].to_int < StrNum1[_].to_int thenSign = Sign1StrLonger = StrNum1[0:]StrSmaller = StrNum0[0:]end ifif Sign != "" then breakend forend ifif Sign == "" then Sign = Sign0StrLonger = StrNum0[0:]StrSmaller = StrNum1[0:]end ifStrLongerList = [] while len(StrLonger) > 9StrLongerList = StrLongerList + [StrLonger[-9:]]StrLonger = StrLonger[:-9]end whileif StrLonger != "" then StrLongerList = StrLongerList + [StrLonger]StrSmallerList = [] while len(StrSmaller) > 9StrSmallerList = StrSmallerList + [StrSmaller[-9:]]StrSmaller = StrSmaller[:-9]end whileif StrSmaller != "" then StrSmallerList = StrSmallerList + [StrSmaller]ResultStr = ""if Sign0 == Sign1 then Jinwei = 0for _ in range(0, len(StrSmallerList) - 1, 1)temp = Jinwei + StrLongerList[_].to_int + StrSmallerList[_].to_intif temp >= 1000000000 thentemp = temp - 1000000000Jinwei = 1elseJinwei = 0end iftemp = str(temp)while len(temp) < 9temp = "0" + tempend whileResultStr = temp + ResultStrend forif len(StrLongerList) != len(StrSmallerList) thenfor _ in range(len(StrSmallerList), len(StrLongerList) - 1, 1)temp = Jinwei + StrLongerList[_].to_intif temp >= 1000000000 thentemp = temp - 1000000000Jinwei = 1elseJinwei = 0end iftemp = str(temp)while len(temp) < 9temp = "0" + tempend whileResultStr = temp + ResultStrend forend ifif Jinwei == 1 thenResultStr = "1" + ResultStrJinwei = 0end ifelseJiewei = 0for _ in range(0, len(StrSmallerList) - 1, 1)temp = Jiewei + StrLongerList[_].to_int - StrSmallerList[_].to_intif temp < 0 thentemp = temp + 1000000000Jiewei = -1elseJiewei = 0end iftemp = str(temp)while len(temp) < 9temp = "0" + tempend whileResultStr = temp + ResultStrend forif len(StrLongerList) != len(StrSmallerList) thenfor _ in range(len(StrSmallerList), len(StrLongerList) - 1, 1)temp = Jiewei + StrLongerList[_].to_intif temp < 0 thentemp = temp + 1000000000Jiewei = -1elseJiewei = 0end iftemp = str(temp)while len(temp) < 9temp = "0" + tempend whileResultStr = temp + ResultStrend forend ifend ifwhile ResultStr[0] == "0" and len(ResultStr) > 1ResultStr = ResultStr[1:]end whileResultStr = Sign + ResultStrif len(ResultStr) == 2 and ResultStr[1] == "0" thenreturn "0"elsereturn ResultStrend if end functionsub = function(Str0, Str1)Str0 = Str0.trimStr1 = Str1.trimif Str0[0] == "+" or Str0[0] == "-" thenStrNum0 = Str0[1:]elseStrNum0 = Str0[0:]end if while StrNum0[0] == "0" and len(StrNum0) > 1StrNum0 = StrNum0[1:]end whileif Str1[0] == "+" or Str1[0] == "-" thenStrNum1 = Str1[1:]elseStrNum1 = Str1[0:]end ifwhile StrNum1[0] == "0" and len(StrNum1) > 1StrNum1 = StrNum1[1:]end whileif Str0[0] == "-" then Sign0 = "-"elseSign0 = "+"end ifif Str1[0] == "-" then Sign1 = "+"elseSign1 = "-"end ifreturn add(Sign0 + StrNum0, Sign1 + StrNum1) end functionmul = function(Str0, Str1)Str0 = Str0.trimStr1 = Str1.trimif Str0[0] == "+" or Str0[0] == "-" thenStrNum0 = Str0[1:]elseStrNum0 = Str0[0:]end if while StrNum0[0] == "0" and len(StrNum0) > 1StrNum0 = StrNum0[1:]end whileLen0 = len(StrNum0)if Str1[0] == "+" or Str1[0] == "-" thenStrNum1 = Str1[1:]elseStrNum1 = Str1[0:]end ifwhile StrNum1[0] == "0" and len(StrNum1) > 1StrNum1 = StrNum1[1:]end whileLen1 = len(StrNum1)if StrNum0 == "0" or StrNum1 == "0" then return "0"if Str0[0] == "-" then Sign0 = "-"elseSign0 = "+"end ifif Str1[0] == "-" then Sign1 = "-"elseSign1 = "+"end ifSign = ""if Len0 > Len1 thenSign = Sign0StrLonger = StrNum0[0:]StrSmaller = StrNum1[0:]else if Len0 < Len1 thenSign = Sign1StrLonger = StrNum1[0:]StrSmaller = StrNum0[0:]elsefor _ in range(0, Len0 - 1, 1)if StrNum0[_].to_int > StrNum1[_].to_int thenSign = Sign0StrLonger = StrNum0[0:]StrSmaller = StrNum1[0:]else if StrNum0[_].to_int < StrNum1[_].to_int thenSign = Sign1StrLonger = StrNum1[0:]StrSmaller = StrNum0[0:]end ifif Sign != "" then breakend forend ifif Sign == "" then Sign = Sign0StrLonger = StrNum0[0:]StrSmaller = StrNum1[0:]end ifif Sign0 == Sign1 thenSign = "+"elseSign = "-"end ifResultStr = "0"while StrSmaller != "0"StrSmaller = add(StrSmaller, "-1")ResultStr = add(ResultStr, StrLonger)end whilereturn (Sign + ResultStr[1:]) end functiondiv = function(Str0, Str1)Str0 = Str0.trimStr1 = Str1.trimif Str0[0] == "+" or Str0[0] == "-" thenStrNum0 = Str0[1:]elseStrNum0 = Str0[0:]end if while StrNum0[0] == "0" and len(StrNum0) > 1StrNum0 = StrNum0[1:]end whileif Str1[0] == "+" or Str1[0] == "-" thenStrNum1 = Str1[1:]elseStrNum1 = Str1[0:]end ifwhile StrNum1[0] == "0" and len(StrNum1) > 1StrNum1 = StrNum1[1:]end whileif StrNum1 == "0" then exit("Runtime error: divide by zero")if StrNum0 == "0" then return ["0", "0"]if Str0[0] == "-" then Sign0 = "-"elseSign0 = "+"end ifif Str1[0] == "-" then Sign1 = "-"elseSign1 = "+"end ifif Sign0 == Sign1 thenSign = "+"elseSign = "-"end ifResultStr = "0"TempStr = sub(StrNum0, StrNum1)while TempStr[0] != "-" StrNum0 = TempStrTempStr = sub(StrNum0, StrNum1)ResultStr = add(ResultStr, "1")end whileif ResultStr[0] == "+" then ResultStr = Sign + ResultStr[1:]end ifif StrNum0[0] == "+" then StrNum0 = Sign0 + StrNum0[1:]end ifreturn [ResultStr, StrNum0] end functionpow = function(Str0, Str1)Str0 = Str0.trimStr1 = Str1.trimif Str0[0] == "+" or Str0[0] == "-" thenStrNum0 = Str0[1:]elseStrNum0 = Str0[0:]end if while StrNum0[0] == "0" and len(StrNum0) > 1StrNum0 = StrNum0[1:]end whileif Str1[0] == "+" or Str1[0] == "-" thenStrNum1 = Str1[1:]elseStrNum1 = Str1[0:]end ifwhile StrNum1[0] == "0" and len(StrNum1) > 1StrNum1 = StrNum1[1:]end whileif StrNum1 == "0" then return "+1"if StrNum0 == "0" then return "0"if Str0[0] == "-" then Sign0 = "-"elseSign0 = "+"end ifif Str1[0] == "-" then Sign1 = "-"elseSign1 = "+"end ifResultStr = "1"while StrNum1 != "0"ResultStr = mul(ResultStr, Sign0 + StrNum0)StrNum1 = add(StrNum1, "-1")end whilereturn 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]))

總結

以上是生活随笔為你收集整理的【 Grey Hack 】大数四则运算的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。