- 締切済み
16進数のままで加減乗除
2つの値を10進数でユーザーが入力して、それを16進数に変換し、16進数のままで加減乗除をするプログラムを考えています。ただ答えるだけではなく、筆算形式で答えを出さなければならないので、なかなかできません。教えてください。
- みんなの回答 (3)
- 専門家の回答
みんなの回答
- jacta
- ベストアンサー率26% (845/3158)
こんな感じでしょうか? #include <stdio.h> #include <limits.h> int table[][SCHAR_MAX+1] = { ['0'] = { ['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4, ['5'] = 5, ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9, ['a'] = 10, ['b'] = 11, ['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15, }, ['1'] = { ['0'] = 1, ['1'] = 2, ['2'] = 3, ['3'] = 4, ['4'] = 5, ['5'] = 6, ['6'] = 7, ['7'] = 8, ['8'] = 9, ['9'] = 10, ['a'] = 11, ['b'] = 12, ['c'] = 13, ['d'] = 14, ['e'] = 15, ['f'] = 16, }, ['2'] = { ['0'] = 2, ['1'] = 3, ['2'] = 4, ['3'] = 5, ['4'] = 6, ['5'] = 7, ['6'] = 8, ['7'] = 9, ['8'] = 10, ['9'] = 11, ['a'] = 12, ['b'] = 13, ['c'] = 14, ['d'] = 15, ['e'] = 16, ['f'] = 17, }, ['3'] = { ['0'] = 3, ['1'] = 4, ['2'] = 5, ['3'] = 6, ['4'] = 7, ['5'] = 8, ['6'] = 9, ['7'] = 10, ['8'] = 11, ['9'] = 12, ['a'] = 13, ['b'] = 14, ['c'] = 15, ['d'] = 16, ['e'] = 17, ['f'] = 18, }, ['4'] = { ['0'] = 4, ['1'] = 5, ['2'] = 6, ['3'] = 7, ['4'] = 8, ['5'] = 9, ['6'] = 10, ['7'] = 11, ['8'] = 12, ['9'] = 13, ['a'] = 14, ['b'] = 15, ['c'] = 16, ['d'] = 17, ['e'] = 18, ['f'] = 19, }, ['5'] = { ['0'] = 5, ['1'] = 6, ['2'] = 7, ['3'] = 8, ['4'] = 9, ['5'] = 10, ['6'] = 11, ['7'] = 12, ['8'] = 13, ['9'] = 14, ['a'] = 15, ['b'] = 16, ['c'] = 17, ['d'] = 18, ['e'] = 19, ['f'] = 20, }, ['6'] = { ['0'] = 6, ['1'] = 7, ['2'] = 8, ['3'] = 9, ['4'] = 10, ['5'] = 11, ['6'] = 12, ['7'] = 13, ['8'] = 14, ['9'] = 15, ['a'] = 16, ['b'] = 17, ['c'] = 18, ['d'] = 19, ['e'] = 20, ['f'] = 21, }, ['7'] = { ['0'] = 7, ['1'] = 8, ['2'] = 9, ['3'] = 10, ['4'] = 11, ['5'] = 12, ['6'] = 13, ['7'] = 14, ['8'] = 15, ['9'] = 16, ['a'] = 17, ['b'] = 18, ['c'] = 19, ['d'] = 20, ['e'] = 21, ['f'] = 22, }, ['8'] = { ['0'] = 8, ['1'] = 9, ['2'] = 10, ['3'] = 11, ['4'] = 12, ['5'] = 13, ['6'] = 14, ['7'] = 15, ['8'] = 16, ['9'] = 17, ['a'] = 18, ['b'] = 19, ['c'] = 20, ['d'] = 21, ['e'] = 22, ['f'] = 23, }, ['9'] = { ['0'] = 9, ['1'] = 10, ['2'] = 11, ['3'] = 12, ['4'] = 13, ['5'] = 14, ['6'] = 15, ['7'] = 16, ['8'] = 17, ['9'] = 18, ['a'] = 19, ['b'] = 20, ['c'] = 21, ['d'] = 22, ['e'] = 23, ['f'] = 24, }, ['a'] = { ['0'] = 10, ['1'] = 11, ['2'] = 12, ['3'] = 13, ['4'] = 14, ['5'] = 15, ['6'] = 16, ['7'] = 17, ['8'] = 18, ['9'] = 19, ['a'] = 20, ['b'] = 21, ['c'] = 22, ['d'] = 23, ['e'] = 24, ['f'] = 25, }, ['b'] = { ['0'] = 11, ['1'] = 12, ['2'] = 13, ['3'] = 14, ['4'] = 15, ['5'] = 16, ['6'] = 17, ['7'] = 18, ['8'] = 19, ['9'] = 20, ['a'] = 21, ['b'] = 22, ['c'] = 23, ['d'] = 24, ['e'] = 25, ['f'] = 26, }, ['c'] = { ['0'] = 12, ['1'] = 13, ['2'] = 14, ['3'] = 15, ['4'] = 16, ['5'] = 17, ['6'] = 18, ['7'] = 19, ['8'] = 20, ['9'] = 21, ['a'] = 22, ['b'] = 23, ['c'] = 24, ['d'] = 25, ['e'] = 26, ['f'] = 27, }, ['d'] = { ['0'] = 13, ['1'] = 14, ['2'] = 15, ['3'] = 16, ['4'] = 17, ['5'] = 18, ['6'] = 19, ['7'] = 20, ['8'] = 21, ['9'] = 22, ['a'] = 23, ['b'] = 24, ['c'] = 25, ['d'] = 26, ['e'] = 27, ['f'] = 28, }, ['e'] = { ['0'] = 14, ['1'] = 15, ['2'] = 16, ['3'] = 17, ['4'] = 18, ['5'] = 19, ['6'] = 20, ['7'] = 21, ['8'] = 22, ['9'] = 23, ['a'] = 24, ['b'] = 25, ['c'] = 26, ['d'] = 27, ['e'] = 28, ['f'] = 29, }, ['f'] = { ['0'] = 15, ['1'] = 16, ['2'] = 17, ['3'] = 18, ['4'] = 19, ['5'] = 20, ['6'] = 21, ['7'] = 22, ['8'] = 23, ['9'] = 24, ['a'] = 25, ['b'] = 26, ['c'] = 27, ['d'] = 28, ['e'] = 29, ['f'] = 30, }, }; int main() { unsigned int a, b; scanf("%d%d", &a, &b); int n = sizeof(unsigned int)*2; char lhs[n+1], rhs[n+1]; sprintf(lhs, "%.*x", n, a); sprintf(rhs, "%.*x", n, b); printf(" %s\n" "+) %s\n" "-----------\n", lhs, rhs); char result[1+n+1]; int carry = 0; for (int i = n-1; i >= 0; i--) { int t = table[lhs[i]][rhs[i]] + carry; // 16進数のまま計算 if (t < 16) { carry = 0; } else { t -= 16; carry = 1; } result[i] = "0123456789abcdef"[t]; } result[1+n] = '\0'; printf(" %s\n", result); } エラーチェックは手抜きです。
- Tacosan
- ベストアンサー率23% (3656/15482)
どこがわからないのか, 書いてもらえませんか? 「筆算ができない」というときと, 「筆算はできるけど表示ができない」というときでは全くアドバイスが変わってきます. 前者であれば #1 と同じ, 後者なら「どこまでできているのかわからんのでプログラムを見せてくれ」ってことになります.
- koko_u_u
- ベストアンサー率18% (216/1139)
>ただ答えるだけではなく、筆算形式で答えを出さなければならないので、なかなかできません。 16 進数の筆算ができない、という意味ですか? 10 進数と方法は同じですよ。
補足
出力結果を筆算形式で表したいのです。つまり人間が紙に書いて計算するプロセスまで表したいのです。
補足
足し算の方法ですね、大変参考になりました。引き算、掛け算はこのプログラムを参考にして何とかなりそうです。しかし、割り算の方法がすっかりお手上げです。ご面倒ですが、割り算の方法をご教授いただけましたら幸いです。