「コンパイラ・スクリプトエンジン」相談室10
at TECH
238:デフォルトの名無しさん
06/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"); }
次ページ続きを表示1を表示最新レス表示スレッドの検索類似スレ一覧話題のニュースおまかせリスト▼オプションを表示暇つぶし2ch
5368日前に更新/217 KB
担当:undef