- 238 名前:デフォルトの名無しさん mailto:sage [2006/05/20(土) 19:16:56 ]
- >>235
ありがと 式の正当性のチェックだけならもっときれいになるかなあ・・・やってみた #include <ctype.h> #include <stdio.h> bool expression(); char buf[1000], *pc; bool get(char& c) { while (c = *pc++) if (!isspace(c)) return true; --pc; return false; } bool get_digit(char& c) { return get(c) && (isdigit(c) || --pc && false); } bool is(char c) { char d; return get(d) && (d == c || --pc && false); } bool number() { char c; if (!get_digit(c)) return false; while (get_digit(c)); return true; } bool factor() { return number() || is('(') && expression() && is(')'); } bool term() { return is('-') || is('+'), factor() && (!is('*') && !is('/') || term()); } bool expression() { return term() && (!is('+') && !is('-') || expression()); } int main() { while (fgets(pc = buf, sizeof buf, stdin) && expression() && !*pc) printf("OK\n"); }
|

|