You can generate a (pseudo)random number (possibly using std::srand() and std::rand() from <cstdlib>) and map it to one of the operators (the principle is the same whether you mean merely characters or actual operations).
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
You can generate a (pseudo)random number (possibly using std::srand() and std::rand() from <cstdlib>) and map it to one of the operators (the principle is the same whether you mean merely characters or actual operations).
Ok so I would need to generate a random number (Was what I had initially thought), but then how would I map an operator to the number?
Sorry to sound like such a numptie. Would it be possible to give a quick example perhaps if it's not too much hassle?
Last edited by jordieg03; December 10th, 2008 at 03:40 AM.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009 In theory, there is no difference between theory and paractice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
TheCPUWizard has made a good point, the client should be able to clearly understand what the overloaded operators purpose is just by looking at the interface and not having to sift through the implementation to see how it really works.
If it seems unclear at what an operator does with a value, then it should be a function instead.
I believe the OP didn't ask about operator overloading, rather how to implement some random logic of selection which arithmetic operator to use in some computation.
Here's a simple function that uses the rand() function to generate a number between low and high, inclusive:
Code:
int randomNumber(int low, int high)
{
int range = high - low + 1;
int result = low + rand % range;
return result;
}
I believe the OP didn't ask about operator overloading, rather how to implement some random logic of selection which arithmetic operator to use in some computation.
Quite possible. But the word "operator" has a specific meaning in C++. Section 5 (Expressions) of the ISO specification provides the definition of what qualifiers as an "operator".
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009 In theory, there is no difference between theory and paractice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
I would have to agree with the Wiz. You are confusing the term "operator" with something else. Section 5 (Expressions) of the ISO specification strictly forbids operating on data types with "user-defined operators".
I believe the OP didn't ask about operator overloading, rather how to implement some random logic of selection which arithmetic operator to use in some computation.
Yes, that is how I interpreted the question (or if not an arithmetic operation, then one of the characters '+', '-', '/' or '*', chosen at random). In retrospect, _uj's question is worth repeating: what do you mean by random operator?
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Try something like this (if i understood what you wanted correctly that is)
Code:
double randOper(double a, double b)
{
switch(rand()%4)
{
case 0:
return a+b;
case 1:
return a-b;
case 2:
return a*b;
case 3:
return a/b; // Div by zero?
}
}
Bookmarks