[過去ログ] プログラミングのお題スレ Part13 (1002レス)
前次1-
抽出解除 レス栞

このスレッドは過去ログ倉庫に格納されています。
次スレ検索 歴削→次スレ 栞削→次スレ 過去ログメニュー
リロード規制です。10分ほどで解除するので、他のブラウザへ避難してください。
723
(5): 2019/03/17(日)06:18 ID:k04EM+xl(1/4)調 AAS
2桁までの足し算の筆算を表示せよ。

例:
29
+15
----
14
3
----
44

29
+ 5
----
14
2
----
34
730
(1): 2019/03/17(日)12:38 ID:TUY5K+QC(1/2)調 AAS
>>723 Perl5

for (<DATA>) {
 ($l, $o, $r) = split;
 @l = $l =~ /(\d?)(\d)/;
 @r = $r =~ /(\d?)(\d)/;
 $a1 = $l[0] + $r[0];
 $a0 = $l[1] + $r[1];
 @a1 = $a1 =~ /(\d?)(\d)/;
 @a0 = $a0 =~ /(\d?)(\d)/;
 $a = 10*$a1 + $a0;
 $" = '';
 print <<"EOF";
@l
$o@r
---
@a1
@a0
---
$a

EOF
}
__DATA__
29 + 15
29 + 5
733: 2019/03/17(日)13:19 ID:k04EM+xl(3/4)調 AAS
>>723
掛け算に対応したり、1桁どうしの計算用フォーマット作ったりしてたら規模がでかくなった(・ω・`)

1
* 2
-----
2

10
* 0
-----
0

Haskell

https://ideone.com/SCzGKq
735
(1): 2019/03/17(日)18:42 ID:7j/Zhhp5(1)調 AAS
>>723 ネタ回答: WolframAlpha API / node
// index.js
const {get} = require('http');
const appid = '<<YOUR WolframAlpha APP ID>>';
process.stdin.on('data', data => {
get(`http://api.wolframalpha.com/v2/query?appid=${appid}&input=${encodeURIComponent(data.toString().trim())}&podstate=Result__Step-by-step+solution&format=image&output=json`, res => {
res.setEncoding('utf8');
let body = '';
res.on('data', chunk => {body += chunk;});
res.on('end', () => {
get(JSON.parse(body).queryresult.pods.filter(({title}) => title === 'Results')[0].subpods.filter(({title}) => title === 'Possible intermediate steps')[0].img.src, res => {
res.on('data', data => {process.stdout.write(data)});
});
});
});
});
$ echo 29+15 | node . > ./test1.gif
https://i.imgur.com/QxolfNz.gif

$ echo 29+5 | node . > ./test2.gif
https://i.imgur.com/D0r3sSg.gif

$ echo 29*15 | node . > ./test3.gif
https://i.imgur.com/imvQO5g.gif


試しに一桁×一桁やってみたらなんだか微妙な計算過程に…
$ echo 5*5 | node . > ./test.gif
https://i.imgur.com/8z3kZPf.gif


あと割り算はAPIからでは計算過程のURL取れなかった
743: 2019/03/21(木)03:20 ID:ZuEvTwkR(1/2)調 AAS
>>723
Kotlin
https://paiza.io/projects/IQdiv40t3eXnM8CEXzF2Og
748: 2019/03/21(木)22:33 ID:4Lv8Ku6J(2/2)調 AAS
>>723 Squeak Smalltalk

| fn |
fn := [:int1 :int2 |
| strings width digits interms ans max line |
strings := {int1. int2} collect: #asString.
width := (strings detectMax: #size) size.
digits := strings collect: [:str |
(str forceTo: width paddingStartWith: $0) reversed asArray collect: #asString].
interms := (digits reduce: #+) collectWithIndex: [:interm :idx |
interm, (String new: idx-1 withAll: Character space)].
ans := interms polynomialEval: '10'.
strings atLast: 1 put: '+ ', strings last.
max := ((strings, {ans}) collect: #size) max.
line := String new: max withAll: $-.
(strings, {line}, interms, {line. ans} collect: [:str |
(str forceTo: max paddingStartWith: Character space) withoutTrailingBlanks
]) asStringWithCr
].

fn value: 987 value: 65. "=>
' 987
+ 65
----
12
14
9
----
1052' "
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.081s