// Rascal, the Advanced Scientific CALculator
// Copyright (C) 2001, Sebastian Ritterbusch (Rascal@Ritterbusch.de)
//
// 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.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// This is an example of how you can use the heart of rascal, the value-class,
// in own applications.
// You can build this example using
// make valexample
#define STANDALONE_VALUE
#include "value.hpp"
int rascal_user_stop=0; // set this to 1 to interrupt libraries
// i.e. from a signal handler
struct init
{
char *fname;
value (*fcnt)(const value &);
};
struct init2
{
char *fname;
value (*fcnt)(const value &,const value &);
};
struct init3
{
char *fname;
value (*fcnt)(void);
};
struct init arith_fncts[] = // list of unary functions
{
#include "value.fn"
0,0
};
struct init2 arith_fncts2[] = // list of binary functions
{
#include "value.fn2"
0,0
};
struct init3 arith_proc[] = // list of procedures
{
#include "value.fn0"
0,0
};
int main(void)
{
// How to build values from strings:
string a,b;
cout << "Enter an integer: ";
cin >> a;
// So if your scanner identified an integer, then
// the function parseInteger will make a value out of
// this. Depending on the modules included in "value"
// This routine will either just invoke "atoi" and
// result in an integer within the value or
// a long number routine with a resulting big number.
value va=parseInteger(a);
cout << "You entered: ";
cout << va << endl;
// The same for floating point numbers:
cout << "Enter a floating point number:";
cin >> b;
value vb=parseDouble(b);
cout << vb << endl;
// Of course you can just use the value constructor
// if you would like to have a value represention p.e. a double
// value(0.2)
// Arithmetics: Note that ++ and -- are not defined and
// +=, -=, ... are not yet faster than a=a+b...
cout << "The sum of the two numbers: " << va+vb << endl;
cout << "The product: " << va*vb << endl;
// "value" really behaves like a usual datatype
// only that it can do more; try entering negative a's or
// squares like 16, 64, -25
cout << "The sqrt(a):" << sqrt(va) << endl;
cout << "The sin(b):" << sin(vb) << endl;
// There are default constructors from integers or doubles to
// value, thus you can use usual numbers nearly anywhere:
cout << "The complex(a,b)^3:" << pow(complex(va,vb),3) << endl;
// Building matrices is a bit more difficult;
// currently you have to build them bit by bit
// starting from a "valuematrix"
// (In fact this isn't that bad, because this is how you
// will probably want to parse the user input)
value X=valuematrix(va);
// First build the row-vectors:
X=appendcol(X.asMATRIX(),valuematrix(vb));
value X2=valuematrix(-vb);
X2=appendcol(X2.asMATRIX(),valuematrix(va));
// And join the rows to the matrix:
X=appendline(X.asMATRIX(),X2.asMATRIX());
cout << "And the matrix [a b;-b a]: " << X << endl;
cout << "And its inverse: " << 1/X << endl;
cout << "And the product of both: " << X/X << endl;
return 0;
}
// Okay value.cpp and this file should be compiled seperately, but just
// for brevity...
#include "value.cpp"
syntax highlighted by Code2HTML, v. 0.9.1