只是一個基本的Casaer密碼。我已經測試了所有的子函數,只是encryptChar()不是特別有用。我得到一個無限循環。它應該是遞歸的。以下是所有代碼:一個真正基本的SML問題,我似乎無法弄清楚(小碼)
fun replace (str : string, index : int, newChar : char) : string = String.substring(str,0,index)^String.str(newChar)^String.substring(str,index+1,(size str) - index - 1;
fun encryptChar (msgStr : string, shiftAmnt : int, index : int) : string =
let val asciiCode = 0
in
if (not (String.sub(msgStr, index) = #" ")) then
(
asciiCode = ord(String.sub(msgStr, index)) + shiftAmnt;
if (asciiCode < ord(#"A")) then asciiCode = asciiCode + 26
else if (asciiCode > ord(#"Z")) then asciiCode = asciiCode - 26
else asciiCode = asciiCode;
msgStr = replace(msgStr, index, chr(asciiCode))
)
else asciiCode = asciiCode;
index = index + 1;
if (index < (size msgStr - 1)) then encryptChar(msgStr, shiftAmnt, index)
else msgStr
end
;
fun encrypt(msgStr : string, shiftAmnt : int) : string = encryptChar (String.map Char.toUpper msgStr, shiftAmnt mod 26, 0);