473,471 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Symbolic manipulation


Can anyone improve this C++ program so that it can compete with the other
languages:

http://www.codecodex.com/wiki/index....tle=Derivative

--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/product...rp_journal/?u6
May 22 '07 #1
4 1663
Jon Harrop wrote:
Can anyone improve this C++ program so that it can compete with the other
languages:

http://www.codecodex.com/wiki/index....tle=Derivative
Troll. Please ignore. He's just trying to evangelize his F# crud.
May 22 '07 #2

red floyd <no*****@here.dudewrote in message...
Jon Harrop wrote:
Can anyone improve this C++ program so that it can compete with the
other
languages:
http://www.codecodex.com/wiki/index....tle=Derivative
Troll. Please ignore. He's just trying to evangelize his F# crud.
I wonder what website it bought it's 'Dr' from.

I searched my Debian packages for 'F#', found nothing. So, 'F#' must be
WORTHLESS JUNK!

"Troll. Please ignore.", NO. Report it to your(and it's) ISP!

Sometimes 'freedom of speech' needs 'freedom of plonk'!

Oh crap, now I'm the troll. Sorry. Maybe the jerk will 'plonk' me?

if( Harrop ) Harrop /dev/nul

<serious>
The way to handle this is for *nobody* to reply to his posts!
(others: note that I'm just jawin' with a friend == Red.)

<click>

May 22 '07 #3
BobR wrote:
I wonder what website it bought it's 'Dr' from.
http://www.cam.ac.uk
I searched my Debian packages for 'F#', found nothing. So, 'F#' must be
WORTHLESS JUNK!
Try:

apt-get install ocaml

--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/product...rp_journal/?u6
May 22 '07 #4
On May 22, 9:53 pm, Jon Harrop <j...@ffconsultancy.comwrote:
Can anyone improve this C++ program so that it can compete with the other
languages:

http://www.codecodex.com/wiki/index....tle=Derivative
Here is one. It does not leaks, is exception safe
and prints prettier result.

#include <string>
#include <iostream>
#include <ostream>
using namespace std;

template <class T>
class Ref{
public:
explicit Ref(T* ref):ref_(ref),count_(new int(1)){}
Ref(const Ref& in)
:ref_(in.ref_),count_(in.count_)
{
Add();
}
Ref&operator=(const Ref& in)
{
if(&in!=this)
{
Release();
count_ = in.count_;
ref_ = in.ref_;
Add();
}
return *this;
}
T* operator ->()const{ return ref_; }
T& operator *()const{ return *ref_; }
~Ref(){ Release(); }
void Release()
{
--*count_;

if(*count_==0)
{
delete ref_;
delete count_;
}
}
void Add(){ ++*count_; }
private:
T* ref_;
int* count_;
};

class Op{
public:
// could be any shared ptr
typedef const Ref<const OpPtr_t;
virtual string eval()const=0;
virtual Ptr_t d(const string&)const=0;
virtual ~Op(){}
};

class BinaryOp:public Op{
protected:
BinaryOp(Ptr_t lhs,Ptr_t rhs):left_(lhs),right_(rhs){}
static string smul(const string&lhs,const string&rhs)
{
if(lhs == "0" || rhs == "0")return "0";
else if(lhs == "1")return rhs;
else if(rhs == "1")return lhs;
else return string ("(") + lhs+"*"+rhs + ")";
}
static string sadd(const string& lhs,const string& rhs)
{
if(lhs == "0")return rhs;
else if(rhs == "0")return lhs;
else return string ("(")+lhs+"+"+rhs+")";
}
Ptr_t left_,right_;
};

class Add:public BinaryOp{
public:
Add(Ptr_t lhs,Ptr_t rhs):BinaryOp(lhs,rhs){}
string eval()const
{
return sadd(left_->eval(),right_->eval());
}
Ptr_t d(const string& x)const
{
return Ptr_t(new Add(left_->d(x),right_->d(x)));
}
};

class Mul:public BinaryOp{
public:
Mul(Ptr_t lhs,Ptr_t rhs):BinaryOp(lhs,rhs){}
string eval()const
{
return smul(left_->eval(),right_->eval());
}
Ptr_t d(const string& x)const
{
Ptr_t left(new Mul(left_,right_->d(x)));
Ptr_t right(new Mul(left_->d(x), right_));
return Ptr_t(new Add(left,right));
}
};

class Var:public Op,private string{
public:
Var(const string& x = ""):string(x){}
string eval()const{ return *this; }
Ptr_t d(const string& x)const
{
return Ptr_t(new Var(x==*this?"1":"0"));
}
};

typedef Op::Ptr_t Ptr_t;

Ptr_t operator + (Ptr_t lhs,Ptr_t rhs)
{
return Ptr_t(new Add(lhs,rhs));
}
Ptr_t operator * (Ptr_t lhs,Ptr_t rhs)
{
return Ptr_t(new Mul(lhs,rhs));
}

int main()
{
Ptr_t x(new Var("x")),
a(new Var("a")),
b(new Var("b")),
c(new Var("c"));
Ptr_t expr = a * x * x + b * x + c;
cout <<expr->eval()<<' '<<expr->d("x")->eval()<<'\n';
return 0;
}

Greetings, Branimir.
May 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: slurper | last post by:
i don't understand following snippet from "programming perl". $name = "bam"; $$name = 1; # Sets $bam ${$name} = 2; # Sets $bam ${$name x 2} = 3; # Sets...
35
by: Sandeep Sharma | last post by:
Right from the time the first edition of K&R was released, the advantages of using symbolic constants, as opposed to "magic numbers", has been emphasized ---- and for good reason. I don't dispute...
12
by: daniel.wolff | last post by:
I am looking for a quick C program that takes n+1 pairs of values (integers) (a_i, f(a_i)), i=0,...,n, generates the coefficients \alpha_i, i=0,...,n of the polynomial of degree n that fits these...
8
by: Henrik Dahl | last post by:
Hello! I would like to introduce a keyboard shortcut for refreshing the user interface in many programs. In english locales it's often F5 which is used, but how about other locales and/or...
22
by: mp | last post by:
i have a python program which attempts to call 'cls' but fails: sh: line 1: cls: command not found i tried creating an alias from cls to clear in .profile, .cshrc, and /etc/profile, but none...
1
by: solarin | last post by:
Hi, I've developed a program under VS 6.0. I can compile it and run it, but when I try to debbug , all my breakpoints are dissabled and I can see the following messages: Loaded...
4
by: TK | last post by:
Hi, I have to know whether a typical file (under Linux) a symbolic link is (in the sense of the stat()-Funktion). How can I check it excactly? Thanks for help. o-o THomas
0
by: humbleaptience | last post by:
Hey Ya'll, I basically have a bunch of unix servers with a dataset scrambled through about 10 of them. On unix, I maintain a symbolic link file tree so that for each server it appears to have...
1
by: amygrant1701 | last post by:
Hi, I've done this before so I don't see what I could doing wrong here. I'm running mysql 5x on freebsd. I'm using the default data directory of "/var/db/mysql" In there I have several dozen...
1
by: sora | last post by:
Hi, I've developed a MFC program under VS 6.0. My debugger *was* working fine and I've used it often for my project. Then, one day, the errors below appear and they prevent me from using the...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.