DJ_AM_Juicebox
asked on
unique identifiers
Hi,
I need a system of making exclusive number codes. Say i have a few products, like:
hammer
drill
wedge
and then I just enumerate them:
enum {
hammer = 0,
drill,
wedge
}
how can I make a number that uniquely identifies any combination of them? Like I get the #6 and that is the combination of hammer and wedge - a zero means only a hammer, etc. I don't have any requirements, just that when I get the final number, I can decompose it into the product types that made it up.
Thanks
I need a system of making exclusive number codes. Say i have a few products, like:
hammer
drill
wedge
and then I just enumerate them:
enum {
hammer = 0,
drill,
wedge
}
how can I make a number that uniquely identifies any combination of them? Like I get the #6 and that is the combination of hammer and wedge - a zero means only a hammer, etc. I don't have any requirements, just that when I get the final number, I can decompose it into the product types that made it up.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sample code:
=============
__________________________ __________ __________ __________ __________ __________ ___
#include <iostream>
#include <string>
using namespace std;
const int tools_num = 4;
enum Tools {
hammer = 0,
drill, // = 1
wedge, // = 2
screwdriver // = 3
};
string toolName (Tools t) {
switch (t) {
case 0: return "hammer"; break;
case 1: return "drill"; break;
case 2: return "wedge"; break;
case 3: return "screwdriver"; break;
}
}
int main() {
int k;
cout << "Enter unique number: ";
cin >> k;
int tool_used[tools_num];
for (int i = 0; i < tools_num; i++) {
tool_used[i] = k % 2;
k = k / 2;
}
cout << "Tools in group: ";
for (int i = 0; i < tools_num; i++)
if (tool_used[i])
cout << toolName(Tools(i)) << " ";
cout << endl;
cin.get();
}
__________________________ __________ __________ __________ __________ __________ ___
Sample input\output 1:
======================
__________________________ __________ ______
Enter unique number: 6
Tools in group: drill wedge
__________________________ __________ ______
Sample input\output 2:
======================
__________________________ __________ ______
Enter unique number: 15
Tools in group: hammer drill wedge screwdriver
__________________________ __________ ______
Note that probably using an enum in this case is not best idea, since you cannot automaticaly convert, Enum variable into it's string equivalent (drill (=1) into string "drill").
Maybe it is better to make an array of strings where i-th string is the name of i-th product/tool.
It is also more flexible since user can give product names and number that in this case are not limited like when using enum.
Hope this helps, Uros.
=============
__________________________
#include <iostream>
#include <string>
using namespace std;
const int tools_num = 4;
enum Tools {
hammer = 0,
drill, // = 1
wedge, // = 2
screwdriver // = 3
};
string toolName (Tools t) {
switch (t) {
case 0: return "hammer"; break;
case 1: return "drill"; break;
case 2: return "wedge"; break;
case 3: return "screwdriver"; break;
}
}
int main() {
int k;
cout << "Enter unique number: ";
cin >> k;
int tool_used[tools_num];
for (int i = 0; i < tools_num; i++) {
tool_used[i] = k % 2;
k = k / 2;
}
cout << "Tools in group: ";
for (int i = 0; i < tools_num; i++)
if (tool_used[i])
cout << toolName(Tools(i)) << " ";
cout << endl;
cin.get();
}
__________________________
Sample input\output 1:
======================
__________________________
Enter unique number: 6
Tools in group: drill wedge
__________________________
Sample input\output 2:
======================
__________________________
Enter unique number: 15
Tools in group: hammer drill wedge screwdriver
__________________________
Note that probably using an enum in this case is not best idea, since you cannot automaticaly convert, Enum variable into it's string equivalent (drill (=1) into string "drill").
Maybe it is better to make an array of strings where i-th string is the name of i-th product/tool.
It is also more flexible since user can give product names and number that in this case are not limited like when using enum.
Hope this helps, Uros.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
so your formula is actualy:
hamer_used * 2^hammer + drill_used * 2^drill + wedge_used * 2^wedge
================
On the other side, if you have an unique number k, and you want to know which tools are in group, the process is more complex but not very hard.
product_1_used = k % 2; k = k / 2;
product_2_used = k % 2; k = k / 2;
...
product_n_used = k % 2.
where % is mod operation.
In your example, let suppose we have number 6, so: k=6,
hammer_used = 6 % 2 = 0; k = k / 2 = 3;
drill_used = 3 % 2 = 1; k =3 / 2 = 1;
wedge_used = 1 % 2 = 1;
So number 6 means that drill and wedge are used.
I will post some sample code soon.