ページ 11

電卓プログラム・十六進電卓(hex calculator)

Posted: 2013年11月07日(木) 13:37
by ascii
初心者です。
十六進電卓のプログラミングをつくりたいです。
足し算・引き算をして、十進法と十六進法の両方の答えを下記のように導き出したいです。

> hexcalc 0xff87 + 0xaabb
0x1aa42 (109122)
> hexcalc 0xff87 - 0xaabb
0x54cc (21708)
> hexcalc 0x1000 - 0x10
0xff0 (4080)
> hexcalc 1000 - 10
0x3de (990)
> hexcalc 1000 - 0x10
0x3d8 (984)

下記ではsegment faultになってしまい、何が間違っているのか分からず困っています。
どなたか詳しい方がいらっしゃったらご教授いただけませんか。

コード:

# include <stdio.h>
# include <stdlib.h>

int main (int argc, char** argv)
{
  int a;
  a = strtol(argv[1], NULL, 0); //convert the input                             
  printf("0x%x\n", a); // print as a hexadecimal value                          
  printf("%u\n",(unsigned)strtol("0xAB8",NULL,0));
  return 0;
}

Re: 電卓プログラム・十六進電卓(hex calculator)

Posted: 2013年11月07日(木) 15:59
by へろりくしょん
argv[1] をチェックしてみてください。 NULL になってませんか?

Re: 電卓プログラム・十六進電卓(hex calculator)

Posted: 2013年11月08日(金) 08:57
by non
segment faultですか。コンパイラは何を使ってますか?
実行時に、コマンドラインでどう入力しましたか?