When I tried to execute the attached file mycalc.c with a command line
argument, it could not be executed normally only in the case of '*'.
'+', '-' and '/' can be executed normally, but the compiler cannot
execute '*' normally. Isn't this a gcc bug?
~/Clearning$ ./mycalc 20 * 30
用法:mycalc 数値1 演算子 数値2
#include <stdio.h>
#include <stdlib.h> // for exit()
#include <string.h> // for strcpy() strlen()
int main(int argc, char *argv[])
{
double d1, d2,ans;
char ope[80];
if (argc != 4) {
printf("用法:mycalc 数値1 演算子 数値2\n");
exit(1);
}
d1 = atof(argv[1]);
d2 = atof(argv[3]);
strcpy(ope,argv[2]);
if (strlen(ope) != 1) {
printf("演算子が1文字でない\n");
exit(1);
}
switch (ope[0]) {
case '+':
ans = d1 + d2;
break;
case '-':
ans = d1 - d2;
break;
case '*':
ans = d1 * d2;
break;
case '/':
if (d2 == 0.0) ans = 0.0; // 0除算対応
else ans = d1 / d2;
break;
default:
printf("演算子が違っている\n");
exit(1);
}
printf("%f\n",ans);
}