#include <stdio.h> #include <stdlib.h> /* atof()用 */ #include <ctype.h> #include "char_def.h" #include "postfix_nortation_calc.h" #include "getline.h" #include "stack.h" #include "getop.h" int do_calc(char *, char *); int main(void) { char line[LINEMAX]; char op[OPMAX]; printf("**** Postfix Nortation ****\n"); while (1){ /* コンソールからの入力読み込み */ printf("input > "); getline(line, LINEMAX); /* 計算処理 */ if (do_calc(line, op) != CONTINUE) { break; } } printf("********* Quit **********\n"); return 0; } /* 入力文字列をもとに計算を行う */ int do_calc(char *line, char *op) { int rslt = CONTINUE; double op2; /* 演算子、被演算子を取得する */ line = getop(line, op); /* 改行なら結果を表示する */ if (*op == NEW_LINE) { printf("answer : %.8g\n", pop()); } /* EOFならプログラム終了の記号をセットする */ else if (*op == EOF) { rslt = !CONTINUE; } else { /* 数値をスタックにプッシュ */ if (isdigit(*op)) { push(atof(op)); } /* 加減乗除の計算 */ else if (*op == OP_PLUS) { push(pop() + pop()); } else if (*op == OP_ASTERISK) { push(pop() * pop()); } else if (*op == OP_MINUS) { op2 = pop(); push(pop() - op2); } else if (*op == OP_SLASH) { op2 = pop(); (op2 != 0.0) ? push(pop() / op2) : printf("error: zero divisor\n"); } /* 上記以外ならエラー */ else { printf("error: unknown command %s\n", op); return rslt; } /* do_calc()の再呼び出し */ rslt = do_calc(line, op); } return rslt; }