File: test.cc

package info (click to toggle)
scalc 0.2.4-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,724 kB
  • ctags: 826
  • sloc: sh: 10,131; cpp: 1,580; makefile: 34
file content (82 lines) | stat: -rw-r--r-- 2,531 bytes parent folder | download | duplicates (4)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
  test.cc, copyright (c) 2006 by Vincent Fourmond: 
  A small test program for SCalc.
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details (in the COPYING file).
  
*/

#include <scalc.hh>
using namespace SCalc;
using namespace std;

extern int yydebug;

void dump(ParserResult * r)
{
  if(r->is_expression() && r->to_expression()->is_valid())
    {
      Expression * e = r->to_expression();
      set<int> vars = e->used_variables();
      cerr << "Pretty: " << e->pretty_print() << endl;
      cerr << e->used_variables().size() << " variables used" << endl;
//       for(set<int>::iterator i = vars.begin(); i != vars.end(); i++)
// 	cerr << "Used var : " << *i << endl;
      if(e->evaluable())
	cerr << "  Evaluable -> " << e->evaluate() << endl;
      else
	cerr << "  Cannot be evaluated yet" << endl;
      Expression * s= e->simplify();
      cerr << "  Simplified : " << s->pretty_print() << endl;
      // now, we try derivation with regards to the first variable:
      Expression * der = e->derive(0);
      cerr << "Derivative with regards to var 0 : ";
      cerr << der->pretty_print() << endl;
      delete s;
      s = der->simplify();
      cerr << "  Simplified derivative : " << s->pretty_print() << endl;
      if(s->evaluable())
	cerr << "  Evaluable -> " << s->evaluate() << endl;
      else
	cerr << "  Cannot be evaluated yet" << endl;
      delete s;
      delete der;
      delete e;
    }
  else
    {
      cerr << r->pretty_print() << endl;
      if(! r->is_func_def() || r->to_func_def()->name().empty())
	delete r;
    }
}

int
main (void)
{
  Session s;
  string str;
  while(!cin.eof())
    {
      cerr << "Function table :" << endl;
      vector<string> funcnames = s.func_names();
      for(size_t i = 0; i < funcnames.size(); i++)
	cerr << "Function #" << i << " : " << funcnames[i] << endl;
      getline(cin,str);
      if(!str.empty())
	{
	  dump(s.eval(str.c_str()));
// 	  vector<string> varnames = s.varnames();
// 	  for(size_t i = 0; i < varnames.size(); i++)
// 	    cerr << "Variable #" << i << " : " << varnames[i] << endl;
	}
    }
}