Last active
February 8, 2022 05:41
-
-
Save ikasoba/085bfb7463a5c32fbab341ceaf806462 to your computer and use it in GitHub Desktop.
漢数字を整数に
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 漢字で表した整数を整数の数値として返す | |
| const kanToNum = (knum="三百二十一")=>{ | |
| const or=(a,b)=>a==null ? b : a; | |
| const numb = new Map() | |
| .set("零",0).set("一",1).set("二",2).set("三",3).set("四",4) | |
| .set("五",5).set("六",6).set("七",7).set("八",8).set("九",9); | |
| const places = new Map() | |
| .set("十",10).set("百",100).set("千",1000).set("万",10000) | |
| .set("億",10**8).set("兆",10**12).set("京",10**16).set("垓",10**20); | |
| const stack = []; | |
| for (let i=0;i<knum.length;i++){ | |
| if (numb.has(knum[i])){ | |
| stack.push(numb.get(knum[i])) | |
| }if (places.get(knum[i]) > or(numb.get(knum[i-1]), places.get(knum[i-1]))){ | |
| stack.push(stack.pop() * places.get(knum[i])) | |
| }else if (places.has(knum[i])){ | |
| stack.push(places.get(knum[i])) | |
| } | |
| } | |
| for (let i=0;i<knum.length;i++){ | |
| stack.push(stack.pop()+(stack.pop()||0)) | |
| } | |
| return stack.pop() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment