Instant Calculators with Dropdowns (v3)
16 Jan 08, 11:30PM
Instant Calculators with Dropdowns (v3)
A minor but significant improvement to the instant calculators has now been made, which allows them to contain drop down menus. For example:
<iframe src="http://www.codecogs.com/ic-269&u=3" frameborder="0" scrolling="no" width="235" height="190"></iframe>
These are created by defining enumerated types (enum), which contains each of the options for the drop-down. The enum must occur in the same namespace and the same file as the function you're designing. As an example, consider the following function, which returns the number of corners on a shape.
Example 1
- First we define an enum containing the shapes:
enum shapes { shape_dot=1, shape_line, shape_triangle, shape_square, shape_polygon, shape_hexagon, shape_septagon, shape_octagon };
Then immediately after it, you define your function, i.e.double corners(shapes shape) { return (int)shape; // don't have to do much since we've already defined // the number of corners within our enum. }
If the enum name is plural, as in 'shapes', then if the enums start with the non-plural form, this is removed from each key when generating the drop-down for the calculator. Obviously its good C/C++ practice to add 'shape_' to each enumerated type to avoid potential clashes with other system variables [good programming practice btw :) ] Any its as easy as that. So here is the final calculator we've just created: <iframe src="http://www.codecogs.com/ic-446&u=1" frameborder="0" scrolling="no" width="235" height="144"></iframe>
Login