忍者ブログ
ゲーム関連の話題に絞っていましたが、全然書かないのでいろいろ解禁してみたり。
[10]  [9]  [8]  [7]  [6]  [5]  [4]  [3]  [2]  [1
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

というわけで2です。
早速コードから。

今回は電卓です。整数の四則演算(括弧付き)が出来ます。
とりあえずとあるサイトから拝借したspiritのバージョン1のコードを修正してみました。
今回は前回のコードとそんなに変わっていないので全部一気に。
 

#pragma warning(disable:4512) //代入演算子を生成できません。
#pragma warning(disable:4100) //引数は関数の本体部で 1 度も参照されません。

#include <iostream>
#include <string>
#include <stack>

#include <boost/bind.hpp>
#include <boost/spirit/home/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

template<typename String>
struct Calc : boost::spirit::qi::grammar<typename String::const_iterator, boost::spirit::ascii::space_type>{
 typedef typename String::const_iterator Iterator;

 Calc() : Calc::base_type(expression){
  using namespace boost;

  using namespace boost::spirit;
  namespace sp_arg = boost::spirit::arg_names;

  // 構文定義
  group  = '(' >> expression >> ')';
  factor  = int_[bind(&Calc::PUSH, this, _1)] | group;
  term  = factor >> *( ('*' >> factor)[bind(&Calc::MUL, this)] | ('/' >> factor)[bind(&Calc::DIV, this)] );
  expression = term >> *( ('+' >> term)[bind(&Calc::ADD, this)] | ('-' >> term)[bind(&Calc::SUB, this)] );
 };
 
 boost::spirit::qi::rule<Iterator, boost::spirit::ascii::space_type> expression;
 boost::spirit::qi::rule<Iterator, boost::spirit::ascii::space_type> term;
 boost::spirit::qi::rule<Iterator, boost::spirit::ascii::space_type> factor;
 boost::spirit::qi::rule<Iterator, boost::spirit::ascii::space_type> group;

 std::stack<int> stack;
 
 int GetResult(){
  int i = stack.top();
  Reset();
  return i;
 }

 void Reset(){
  std::stack<int> empty;
  std::swap(empty, stack);
 }

 void PUSH(int i){
  stack.push(i);
 }

 void ADD(){
  int a = stack.top();
  stack.pop();
  int b = stack.top();
  stack.pop();

  stack.push(b + a); 
 }

 void SUB(){
  int a = stack.top();
  stack.pop();
  int b = stack.top();
  stack.pop();

  stack.push(b - a);
 }

 void MUL(){
  int a = stack.top();
  stack.pop();
  int b = stack.top();
  stack.pop();

  stack.push(b * a);
 }
 
 void DIV(){
  int a = stack.top();
  stack.pop();
  int b = stack.top();
  stack.pop();

  stack.push(b / a);
 }

};
int main(void)
{
 Calc<std::string> calc;

 std::string str;
 while (getline(std::cin, str))
 {
  if (str.empty() || str[0] == 'q' || str[0] == 'Q')
   break;

  std::string::const_iterator it = str.begin();
  std::string::const_iterator end = str.end();

  bool result = phrase_parse(it, end, calc, boost::spirit::ascii::space);

  if (result && it == end)
  {
   std::cout << "-------------------------\n";
   std::cout << "Parsing succeeded\n";
   std::cout << str << " Parses OK: " << std::endl;
   std::cout << calc.GetResult() << std::endl;
  }
  else if(result)
  {
   std::cout << "-------------------------\n";
   std::cout << "Parsing succeeded\n";
   std::cout << std::string(str.begin(), it) << " Parses OK: " << std::endl;
   std::cout << calc.GetResult() << std::endl;
   std::cout << std::string(it, end) << " amari" << std::endl;
  }
  else
  {
   std::cout << "-------------------------\n";
   std::cout << "Parsing failed\n";
   std::cout << "-------------------------\n";
   calc.Reset();
  }
 }

 return 0;
}

気になるのはこの部分。

  group  = '(' >> expression >> ')';
  factor  = int_[bind(&Calc::PUSH, this, _1)] | group;
  term  = factor >> *( ('*' >> factor)[bind(&Calc::MUL, this)] | ('/' >> factor)[bind(&Calc::DIV, this)] );
  expression = term >> *( ('+' >> term)[bind(&Calc::ADD, this)] | ('-' >> term)[bind(&Calc::SUB, this)] );

何とかphoenixだけでうまく書く方法はないのでしょうか……
色々考えてみたのですが、思いつきませんでした。

//2009-10-25追記
コードの修正(mutableとconstの勘違い)

PR
この記事にコメントする
お名前
タイトル
文字色
メールアドレス
URL
コメント
パスワード   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
この記事へのトラックバック
この記事にトラックバックする:
Twitter
Twitter Update
ブログ内検索
カレンダー
05 2025/06 07
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
最新コメント
最新トラックバック
プロフィール
HN:
100poisha
性別:
非公開
アクセス解析
Copyright © 100poishaのブログ All Rights Reserved.
Designed by north sound
Powered by Ninja Blog

忍者ブログ [PR]