Better way to do this?
Aug 22, 2013 at 5:28am UTC
I've been trying to make this code look nicer and run quicker but so far......it looks so bad to me right now. The code basically ask for the end result and pops out an expanded version to get to that solution.
ex:
End Result: 100
Function should pop out: 50 + 50
For larger end results I want each case to run equal amounts which caused the code to look like throw up.
Would there be a better, cleaner, faster way of doing this? As well as incorporate the max number of operations.
Ex: The example above would be the max number of operations would be 1.
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#include <Windows.h>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
template <typename T>
void WINAPI doMath(T nEndResult, T nMaxNumberofIterations, bool bNegatives)
{
T nRandomBlocks = 0;
T nCompletedBuilding = 0;
bool addcase = false , subcase = false , multcase = false , xorcase = false , andcase = false ;
while (nCompletedBuilding != nEndResult)
{
if (addcase && subcase && /*multcase &&*/ xorcase && andcase)
{
addcase = false ;
subcase = false ;
multcase = false ;
xorcase = false ;
andcase = false ;
}
switch (rand() % 6)
{
case 0: // Add
{
if (!addcase)
{
nRandomBlocks = rand() % ((nEndResult - nCompletedBuilding) + 1);
nCompletedBuilding += nRandomBlocks;
cout << "Add Result: " << nCompletedBuilding << endl;
addcase = true ;
}
break ;
}
case 1: // Sub
{
if (!subcase)
{
nRandomBlocks = rand() % ((nEndResult - nCompletedBuilding) + 1);
nCompletedBuilding -= nRandomBlocks;
cout << "Sub Result: " << nCompletedBuilding << endl;
subcase = true ;
}
break ;
}
case 2: // Mult
{
//Mult causes over flow because of the drastic increase. The only way to prevent this would to be use lower mod numbers
//nRandomBlocks = rand() % 5);
//nPrettyTower *= nRandomBlocks;
//cout << "MUL Result: " << nPrettyTower << endl;
break ;
}
case 4: // XOR
{
if (!xorcase)
{
nRandomBlocks = rand() % ((nEndResult - nCompletedBuilding) + 1);
nCompletedBuilding ^= nRandomBlocks;
cout << "XOR Result: " << nCompletedBuilding << endl;
xorcase = true ;
}
break ;
}
case 5: // AND
{
if (!andcase)
{
nRandomBlocks = rand() % ((nEndResult - nCompletedBuilding) + 1);
nCompletedBuilding &= nRandomBlocks;
cout << "AND Result: " << nCompletedBuilding << endl;
andcase = true ;
}
break ;
}
}
}
}
/*
//Ignore this...I was thinking of using vectors to allow the user to pick the amount of combinations to reach the end result, but this would cause a lot of delay time
template<typename T>
void WINAPI doSomeMoreMath(T nEndResult, T nNumberofCombinations, BOOL bNegatives)
{
std::vector<int>Branches;
}
*/
int main()
{
srand(time_t(NULL));
int nCombinationLevel = 0;
int nResult = 0;
cout << "Number of Combinations: " ; cin >> nCombinationLevel;
cout << "Number: " ; cin >> nResult;
DWORD dwTickEnd;
DWORD dwTickStart = GetTickCount();
doMath<int >(nResult, nCombinationLevel, false );
dwTickEnd = GetTickCount();
cout << "Total Duration: " << dwTickEnd - dwTickStart << endl;
cin.ignore();
cin.get();
return 0;
}
Last edited on Aug 22, 2013 at 4:11pm UTC
Topic archived. No new replies allowed.