So, I coded an implementation and the differences should be fairly obvious when you see the code.
Hm, I was a bit rusty with C++ skills so there could be some bugs that I missed, but I think I cleaned most of the **** up.
You should be able to tell how to use the class from the examples in main.
(The Ret_Val() is there for the sake of convenience only)
If you notice any crap lines or bugs just say something.
// Fuzzy Boolean Implemenation
#include <iostream>
#include <time.h>
using namespace std;
// In comparisons, an F_Bool type must come first in order for a fuzzy boolean comparison to occur
// Also, we use 0 to 100 as opposed to 0.0 to 1.0
// All types are infered as a decimal (1 to 100) except.. bool
// Seperate handler functions are required for f_bool and bool, everything else is handled by a generic
class F_Bool
{
public:
template <typename Type> F_Bool(Type data);
inline int Ret_Val() {return this->percent;}
int operator&&(F_Bool val);
int operator||(F_Bool val);
int operator&&(bool val);
int operator||(bool val);
template <typename V_Type> int operator&&(V_Type val);
template <typename V_Type> int operator||(V_Type val);
int operator!(void) {return (100 - this->percent);}
bool To_Bool();
private:
unsigned int percent;
string out_range;
int max_rand;
inline int Get_Rand() {return (rand() % this->max_rand);}
};
F_Bool f_true(100);
F_Bool f_false(0);
int main(int argc, char * argv[])
{
double number = 51;
F_Bool f_b((float)56);
int number2 = 12;
F_Bool f_b2((unsigned char)number2);
cout << f_b.To_Bool() << endl;
cout << (f_b && f_b2) << "\t" << endl;
cout << !(f_b2);
return 0;
}
template <typename Type> F_Bool::F_Bool(Type data)
{
this->max_rand = 101; // We want from 0 to 100
this->out_range = "Percent chance is out of range!\n";
srand(time(NULL));
try {
if (data > 100 || data < 0) throw this->out_range;
this->percent = static_cast<unsigned int>(data);
}
catch (string err) {
cout << err << endl;
}
}
// Next two are for F_Bools
int F_Bool::operator&&(F_Bool val)
{
if (this->percent > val.percent) return val.percent;
else return this->percent;
}
int F_Bool::operator||(F_Bool val)
{
if (this->percent > val.percent) return this->percent;
else return val.percent;
}
// Next two are for bools
int F_Bool::operator&&(bool val)
{
if (val) return this->percent;
else val;
}
int F_Bool::operator||(bool val)
{
if (val) return 100;
else return this->percent;
}
// Next two are for everything else
template <typename V_Type> int F_Bool::operator&&(V_Type val)
{
try {
if (val > 100 || val < 0) throw this->out_range;
if (this->percent > val) return static_cast<int>(val);
else return this->percent;
}
catch (string err) {
cout << err << endl;
return 0;
}
}
template <typename V_Type> int F_Bool::operator||(V_Type val)
{
try {
if (val > 100 || val < 0) throw this->out_range;
if (this->percent > val) return this->percent;
else return static_cast<int>(val);
}
catch (string err) {
cout << err << endl;
return 0;
}
}
// Convert the F_Bool to a bool
bool F_Bool::To_Bool() {
if (Get_Rand() <= this->percent) return true;
else return false;
}


Sign In
Create Account


Back to top









