45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:大数运算的详细介绍

大数运算的详细介绍

2016-08-30 10:52:40 来源:www.45fan.com 【

大数运算的详细介绍

Author:水如烟

一时兴起做做看看,发现真的不简单,花时间,头大。不过体会也相应深些。

运算单元改了又改。在基本运算单元中,变量能简则简,条件检测尽可能的放在外围。

下面的代码是关于大数值的存储和分割。其中把握好地址是关键。因为数值是分块运算的,如果地址转换不正确,最后合并结果时肯定也不正确的了。这个稍经修改,可以用来写文件切合器。区别是,处理对象不同,一是字符,一是字节。

整体的我还没做好。也是一个单元一个单元的做。如往后需要修改补充,以最后供下载的代码为准。

FriendClassInformation
PrivateSubNew()
EndSub

PrivateSharedgUnitMaxSizeAsInteger
PrivateSharedgUnitDataTypeAsTypeCode

'''<summary>
'''运算单元最大位数
'''</summary>
PublicSharedReadOnlyPropertyUnitMaxSize()AsInteger
Get
Return1'测试,不用太大。gUnitMaxSize
EndGet
EndProperty

'''<summary>
'''运算单元数据类型
'''</summary>
'''<value></value>
PublicSharedReadOnlyPropertyUnitDataType()AsTypeCode
Get
ReturngUnitDataType
EndGet
EndProperty

'''<summary>
'''转换为运算单元数据类型
'''</summary>
'''<paramname="Value">值</param>
PublicSharedFunctionConverToUnitDataType(ByValValueAsObject)AsObject
ReturnSystem.Convert.ChangeType(Value,UnitDataType)
EndFunction

PublicSharedFunctionConverToUnitDataTypeFullString(ByValValueAsObject)AsString
ReturnValue.ToString.PadLeft(UnitMaxSize,"0"c)
EndFunction

PublicSharedFunctionConverToUnitDataTypeFullString(ByValValueAsObject,ByValunitNumAsInteger)AsString
ReturnValue.ToString.PadLeft(CInt(UnitMaxSize*Math.Pow(2,unitNum)),"0"c)
EndFunction

PublicSharedFunctionSplitValueToArray(ByValValueAsString)AsString()
ReturnSplitValueToArray(Value,0)
EndFunction

PublicSharedFunctionSplitValueToArray(ByValValueAsString,ByValunitNumAsInteger)AsString()
ReturnCommonFunction.SplitStringToArray(Value,CInt(UnitMaxSize*Math.Pow(2,unitNum)))
EndFunction


PublicSharedFunctionAddressConvertFromAToB(ByValaAddressAsInteger,ByValaUnitNumAsInteger,ByValbUnitNumAsInteger)AsInteger
If(aAddress*Math.Pow(2,aUnitNum))ModMath.Pow(2,bUnitNum)>0ThenThrowNewException("转换无效")
ReturnCInt(aAddress*Math.Pow(2,aUnitNum)/(Math.Pow(2,bUnitNum)))
EndFunction

PublicSharedFunctionIsZero(ByValvalueAsString)AsBoolean
ReturnSystem.Text.RegularExpressions.Regex.IsMatch(value,"^[0]+$")
EndFunction

PublicSharedFunctionGetStringValue(ByValValueAsString)AsString
DimmResultAsString=System.Text.RegularExpressions.Regex.Match(Value,"^[0]{0,}(?<Value>[1-9].*)$").Groups("Value").Value
IfString.IsNullOrEmpty(mResult)ThenmResult="0"
ReturnmResult
EndFunction

PublicSharedFunctionGetStringsValue(ByValvalueCollectionAsCollections.ObjectModel.Collection(OfString),ByValunitNumAsInteger)AsString
ReturnGetStringsValue(valueCollection,0,unitNum)
EndFunction

PublicSharedFunctionGetStringsValue(ByValvalueCollectionAsCollections.ObjectModel.Collection(OfString),ByValappendEmptyValueCountAsInteger,ByValunitNumAsInteger)AsString
ReturnGetStringsValue(valueCollection,0,valueCollection.Count-1,appendEmptyValueCount,unitNum)
EndFunction

PublicSharedFunctionGetStringsValue(ByValvalueCollectionAsCollections.ObjectModel.Collection(OfString),ByValfirstIndexAsInteger,ByVallastIndexAsInteger,ByValunitNumAsInteger)AsString
ReturnGetStringsValue(valueCollection,firstIndex,lastIndex,0,unitNum)
EndFunction

PublicSharedFunctionGetStringsValue(ByValvalueCollectionAsCollections.ObjectModel.Collection(OfString),ByValfirstIndexAsInteger,ByVallastIndexAsInteger,ByValappendEmptyValueCountAsInteger,ByValunitNumAsInteger)AsString
DimbAsNewSystem.Text.StringBuilder
DimtmpAsString

ForiAsInteger=firstIndexTolastIndex

Ifi=firstIndexThen
tmp=valueCollection(i)
Else
tmp=Information.ConverToUnitDataTypeFullString(valueCollection(i),unitNum)
EndIf

b.Append(tmp)
Next

ForiAsInteger=0ToappendEmptyValueCount-1
tmp=Information.ConverToUnitDataTypeFullString("0",unitNum)
b.Append(tmp)

Next

Returnb.ToString
EndFunction

SharedSubNew()

'寻找可表示为整数的且表示范围最大的数据类型
DimmTypeNameAsString
DimmTypeAsType
DimmFieldInfoAsReflection.FieldInfo


DimmMaxValueAsString="0"
DimmDataTypeCodeAsTypeCode

DimmCurrentTypeMaxValueAsString="0"
ForEachcAsTypeCodeIn[Enum].GetValues(GetType(TypeCode))

mTypeName
=c.ToString
mType
=Type.GetType(String.Format("System.{0}",mTypeName))

mFieldInfo
=mType.GetField("MaxValue")
IfmFieldInfoIsNothingThenContinueFor

mCurrentTypeMaxValue=mFieldInfo.GetValue(Nothing).ToString

IfIsNumeric(mCurrentTypeMaxValue)Then
IfmCurrentTypeMaxValue.Length>mMaxValue.LengthThen
mMaxValue=mCurrentTypeMaxValue
mDataTypeCode
=c
EndIf
EndIf

Next

gUnitDataType=mDataTypeCode

'确定该数据类型下,可表示的最大位数。保证两数相乘结果仍可表示为整数。
DimxAsObject
x=ConverToUnitDataType(mMaxValue)

DimmSqrtResultAsString=Math.Sqrt(CDbl(x)).ToString'最大数开方

DimmPointIndexAsInteger=mSqrtResult.IndexOf("."c)'只取整数部分
IfmPointIndex>0Then
mSqrtResult=mSqrtResult.Substring(0,mPointIndex)
EndIf

DimmDigitsAsInteger=mSqrtResult.ToString.Length-1'可表示的最大位数是最大数开方后整数部分位数减一位

gUnitMaxSize=mDigits
EndSub

EndClass
FriendClassCommonFunction
PrivateSubNew()
EndSub

PublicSharedFunctionClone(OfT)(ByValobjAsT)AsT
DimtmpTAsT

DimmFormatterAsNewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter
DimmMemoryStreamAsNewSystem.IO.MemoryStream

UsingmMemoryStream
mFormatter.Serialize(mMemoryStream,obj)
mMemoryStream.Position
=0
tmpT=CType(mFormatter.Deserialize(mMemoryStream),T)
mMemoryStream.Close()

EndUsing

ReturntmpT
EndFunction

PublicSharedFunctionSplitStringToArray(ByVallineAsString,ByValdigitsAsInteger)AsString()
DimmLineLengthAsInteger=line.Length
DimmCountAsInteger=mLineLength/ digits
If(mLineLengthModdigits)>0ThenmCount+=1

DimmResult(mCount-1)AsString

DimmCurrentIndexAsInteger
ForiAsInteger=mCount-1To0Step-1
mCurrentIndex=mLineLength-(mCount-i)*digits

IfmCurrentIndex<0Then
mResult(0)=line.Substring(0,mLineLength-(mCount-1)*digits)
Else
mResult(i)=line.Substring(mCurrentIndex,digits)

EndIf

Next

ReturnmResult
EndFunction

EndClass
<Serializable()>_
PublicClassAddressInformation
PrivategAddressAsInteger=0
PrivategUnitNumAsInteger=0

PublicPropertyAddress()AsInteger
Get
ReturngAddress
EndGet
FriendSet(ByValvalueAsInteger)
gAddress
=value
EndSet
EndProperty


PublicReadOnlyPropertyUnitNum()AsInteger
Get
ReturngUnitNum
EndGet
EndProperty

FriendSubAdd(ByValvalueAsInteger)
gAddress
+=value
EndSub

FriendSubSetZero()
gAddress
=0
EndSub

FriendSubIncrease()
gAddress
+=1
EndSub

FriendSubResetAddressFrom(ByValaddressAsInteger,ByValunitNumAsInteger)
gAddress
=AddressConvertFromAToB(address,unitNum,gUnitNum)
EndSub

FriendSubCopyAddressTo(ByRefaddressInfoAsAddressInformation)
addressInfo.gAddress
=AddressConvertFromAToB(gAddress,gUnitNum,addressInfo.gUnitNum)
EndSub

FriendSubNew()
EndSub

FriendSubNew(ByValunitNumAsInteger)
gUnitNum
=unitNum
EndSub

PublicFunctionClone()AsAddressInformation
ReturnCommonFunction.Clone(OfAddressInformation)(Me)
EndFunction

PublicFunctionConvertTo(ByValunitNumAsInteger)AsAddressInformation
DimmAddressInfoAsNewAddressInformation(unitNum)
CopyAddressTo(mAddressInfo

本文地址:http://www.45fan.com/dnjc/69692.html
Tags: 运算 大数 Author
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部