- 229 名前:デフォルトの名無しさん mailto:sage [2006/05/20(土) 04:50:38 ]
- #include <vector>
#include <functional> #include <stdio.h> #include <ctype.h> bool expression(); char buf[1000], *pc = buf; std::vector<int> data; std::vector<bool(*)()> ops; bool end(int c) { return c == 0 || c == '\n' || c == '\r'; } bool get(char& c) { while (!end(*pc++)) if (!isspace(c = pc[-1])) return true; 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; data.push_back(0); do { data.back() = 10 * data.back() + c - '0'; } while (get_digit(c)); return true; } template<typename T> bool binary(T t) { int i = data.back(); data.pop_back(); data.back() = t(data.back(), i); return true; } bool add() { return binary(std::plus<int>()); } bool subtract() { return binary(std::minus<int>()); } bool multiply() { return binary(std::multiplies<int>()); } bool divide() { return binary(std::divides<int>()); }
|

|