Project Euler Homework

032_times_minus.h

Go to the documentation of this file.
00001 
00004 #ifndef _INCL_032_TIMES_MINUS
00005 #define _INCL_032_TIMES_MINUS
00006 #include <iostream>
00007 #include "032_perm032.h"
00008 using namespace std;
00009 
00010 class times_minus
00012 {
00013   unsigned N;
00014   int *current_perm;
00015   int A, B, C;
00016   unsigned times_pos;           
00017   unsigned minus_pos;           
00018   void findABC ();
00019   int slice_perm (idx, idx);
00020 public:
00021     times_minus (perm032 &);
00022    ~times_minus ();
00023   bool next_opr_pos ();
00024   bool isZero ();
00025   void printExpr ();            
00026   int getProduct ();
00027 };
00028 
00029 
00030 int
00031 times_minus::getProduct ()
00032 {
00033   return C;
00034 }
00035 
00036 void
00037 times_minus::findABC ()
00038 {
00039   A = slice_perm (0, times_pos);
00040   B = slice_perm (times_pos, minus_pos);
00041   C = slice_perm (minus_pos, N);
00042 }
00043 
00044 bool
00045 times_minus::next_opr_pos ()
00046 {
00047   times_pos++;
00048   if (times_pos == minus_pos)
00049     {                           // two operators directly adjacent
00050       minus_pos++;
00051       times_pos = 1;
00052     }
00053 
00054   if (minus_pos == N)
00055     {
00056       //reset
00057       /*      times_pos = 1;
00058          minus_pos = times_pos + 2;
00059          findABC (); */
00060       return 0;
00061     }
00062 
00063   findABC ();
00064   return 1;
00065 }
00066 
00067 bool times_minus::isZero ()
00068 {
00069   return (A * B - C == 0);
00070 }
00071 
00072 times_minus::times_minus (perm032 & perm)
00073 {
00074   times_pos = 1;
00075   minus_pos = times_pos + 2;
00076 
00077   N = perm.length ();
00078 
00079   current_perm = new digit[N];
00080   for (unsigned i = 0; i < N; i++)
00081     current_perm[i] = perm[i];
00082 
00083   findABC ();
00084 }
00085 
00086 times_minus::~times_minus ()
00087 {
00088   delete[]current_perm;
00089 }
00090 
00091 void
00092 times_minus::printExpr ()
00093 {
00094   cout << A << " * " << B << " - " << C << " = " << (A * B - C) << endl;
00095 }
00096 
00097 int
00098 times_minus::slice_perm (idx begin, idx end)
00103 {
00104   int number = 0;
00105   for (idx i = begin; i < end; i++)
00106     number = number * 10 + current_perm[i];
00107   return number;
00108 }
00109 
00110 #endif
 All Classes Files Functions Variables