Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #6249

Designing a suite of algorithms in C++

Newsgroups comp.programming
Date 2016-02-01 03:38 -0800
Message-ID <2df6fc12-e6c9-4f5b-bdb7-21e35a57edbb@googlegroups.com> (permalink)
Subject Designing a suite of algorithms in C++
From Paul <pepstein5@gmail.com>

Show all headers | View raw


The post below was also sent to comp.lang.c++ but it's been more than half a day, and I haven't got any responses so I'd like to post it to this group.

I am trying to present in C++ a set of algorithms for solving a problem.  It is intended that the set of algorithms will expand.  So far, I have five basic algorithms for solving the same problem.  The end goal (so far) is to develop a simple user interface at the console so that the user can provide the parameters, together with a string indicating the name of the algorithm, and my code will provide the solution (and possibly the runtime). 

Four of these algorithms are simple conceptually (although the maths is complicated).  However, the fifth algorithm is much more involved because it has four or five features which can be absent or present so the fifth algorithm can really be thought of as 16 or 32 algorithms.  (The vagueness is because I haven't decided which features to be settable by the user). 

If I only had the first four to worry about, the design could be very simple. 
I would have a pure abstract class as a base of all the algorithms.  Finding the solution is done via a virtual function which depends on the algorithm selected by the user. 

class Algorithm{ 

public: 
Algorithm(some parameters); 
virtual void findSolution() = 0; 
virtual ~Algorithm()
{}
}; 

class Method1 : public Algorithm 
{ 
   public: 
   Method1(some parameters) : Algorithm(some parameters) 
   { 
    
   } 
  void findSolution() 
   { 

   } 

}; 

Methods 2, 3 and 4 could have the same design as Method 1. 

But the complex method, method 5, makes it harder to design elegantly. 

I could try 

class Method5 : public Algorithm 
{ 
   bool flag1; 
   bool flag2; 
... 
   public: 
   Method5 (some parameters, bool someFlag, bool someOtherFlag): Algorithm(some parameters), flag1(someFlag), flag2(someOtherFlag) 
   { 
  
   } 

  void findSolution() 
  { 
     if(flag1) doThis; 
     if(flag2) doThat; 
     //// 
    
  } 
} 

However, now for Method 5, the approach no longer looks elegant. 

Any ideas?  I'm deliberately being vague about the precise nature of the problem because I want to decouple the maths side from the design side and I definitely don't want help on the maths side. 

Many thanks, 

Paul

Back to comp.programming | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Designing a suite of algorithms in C++ Paul <pepstein5@gmail.com> - 2016-02-01 03:38 -0800
  Re: Designing a suite of algorithms in C++ Mark Carroll <mtbc@bcs.org> - 2016-02-01 12:04 +0000
    Re: Designing a suite of algorithms in C++ Paul <pepstein5@gmail.com> - 2016-02-01 05:52 -0800
      Re: Designing a suite of algorithms in C++ Mark Carroll <mtbc@bcs.org> - 2016-02-01 15:10 +0000
  Re: Designing a suite of algorithms in C++ Paul N <gw7rib@aol.com> - 2016-02-01 14:15 -0800
  Re: Designing a suite of algorithms in C++ BartC <bc@freeuk.com> - 2016-02-01 23:00 +0000
  Re: Designing a suite of algorithms in C++ Kaz Kylheku <330-706-9395@kylheku.com> - 2016-02-01 23:24 +0000

csiph-web