{"id":8088,"date":"2023-02-04T22:24:29","date_gmt":"2023-02-04T20:24:29","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=8088"},"modified":"2023-02-04T22:28:04","modified_gmt":"2023-02-04T20:28:04","slug":"enumeration-example","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/enumeration-example\/","title":{"rendered":"Identifying a flying object (enumeration example)"},"content":{"rendered":"<p>The example contains declaring a new enumeration type, declaring an enum type variable, using enums in a switch statement, passing an enum to a function as well as a function returning an enum value.<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;stdbool.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define STR_MAX 128\r\n#define FMT_LEN 16\r\n\r\n#define LIST_BIRDS {\"Eagle\", \"Peacock\", \"Flamingo\", \"Cockatoo\", \"Albatross\", \\\r\n                    \"Seagul\", \"Pigeon\", \"Passer\"}\r\n#define LIST_PLANES {\"Antonov AN-225\", \"The Wright Flyer\", \"Airbus A380\",\\\r\n                     \"Boeing 747-400\", \"Concorde\"}\r\n#define SUPERMAN \"Clark Kent\"\r\n\r\nenum FlyingObject {OBJ_BIRD, OBJ_PLANE, OBJ_SUPERMAN, OBJ_UFO};\r\n\r\nvoid PromptString(char *prompt, char *str, int max);\r\nenum FlyingObject IdentifyObjectType(char *str);\r\nvoid PrintObjectType(enum FlyingObject obj);\r\nbool IsTypeOfObject(const char *listOfObjects[], int n, char *object);\r\n\r\nint main(void)\r\n{\r\n    char sNameOfObject[STR_MAX];\r\n    PromptString(\"What object did you see in the sky?\", sNameOfObject, STR_MAX);\r\n    \r\n    enum FlyingObject eTypeOfObject = IdentifyObjectType(sNameOfObject);\r\n\r\n    PrintObjectType(eTypeOfObject);\r\n    \r\n    return EXIT_SUCCESS;\r\n}\r\n\r\n\/**\r\n * Description: Prints the prompt and stores reads a string from stdin in str,\r\n *              maximum length of max. Input longer than max is left in stdin.\r\n * \r\n * Parameters:  prompt - question printed before reading input\r\n *              str - user answer is stored in here\r\n *              max - maximum length of user answer.\r\n * \r\n * Return:      none\r\n *\/\r\nvoid PromptString(char *prompt, char *str, int max)\r\n{\r\n    puts(prompt);\r\n    \r\n    \/\/ Prepare input format\r\n    char fmt[FMT_LEN];\r\n    sprintf(fmt, \"%%%d[^\\n]s\", max);\r\n    \r\n    scanf(fmt, str);\r\n}\r\n\r\n\r\n\/**\r\n * Description: Identifies the type of flying object based on preset lists.\r\n * \r\n * Parameters:  str - name of the flying object to identify\r\n * \r\n * Return:      type of flying object\r\n *\/\r\nenum FlyingObject IdentifyObjectType(char *str)\r\n{\r\n    const char *birds[] = LIST_BIRDS;\r\n    const char *planes[] = LIST_PLANES;\r\n    \r\n    int nBirds = sizeof(birds) \/ sizeof(char *);\r\n    if (IsTypeOfObject(birds, nBirds, str))\r\n        return OBJ_BIRD;\r\n    \r\n    int nPlanes = sizeof(planes) \/ sizeof(char *);\r\n    if (IsTypeOfObject(planes, nPlanes, str))\r\n        return OBJ_PLANE;\r\n        \r\n    if (!strcmp(SUPERMAN, str))\r\n        return OBJ_SUPERMAN;\r\n        \r\n    return OBJ_UFO;\r\n}\r\n\r\n\/**\r\n * Description: Checks if a string is part of a list\r\n * \r\n * Parameters:  listOfObjects - list of strings to compare against\r\n *              n - number of strings in listOfObjects\r\n *              object - string to check if it is a part of the list\r\n * \r\n * Return:      true if it's part of the list, false if not\r\n *\/\r\nbool IsTypeOfObject(const char *listOfObjects[], int n, char *object)\r\n{\r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        if (!strcmp(listOfObjects[i], object))\r\n            return true;\r\n    }\r\n    return false;\r\n}\r\n\r\n\/**\r\n * Description: Prints the type of object from the enumerated list\r\n * \r\n * Parameters:  obj - numerated identifier for the object to print\r\n * \r\n * Return:      none\r\n *\/\r\nvoid PrintObjectType(enum FlyingObject obj)\r\n{\r\n    \/\/ Cheap puns ahead..\r\n    \r\n    switch (obj)\r\n    {\r\n        case OBJ_BIRD:\r\n            printf(\"It's a bird... \");\r\n            break;\r\n        case OBJ_PLANE:\r\n            printf(\"It's a plane... \");\r\n            \/\/ break;\r\n        case OBJ_SUPERMAN:\r\n            printf(\"It's superman!\");\r\n            \/\/ break;\r\n        case OBJ_UFO:\r\n            printf(\"Unknown flying object or invalid input!\");\r\n            break;\r\n    }\r\n    putchar('\\n');\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The example contains declaring a new enumeration type, declaring an enum type variable, using enums in a switch statement, passing an enum to a function as well as a function returning an enum value. #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdbool.h&gt; #include &lt;string.h&gt; #define STR_MAX 128 #define FMT_LEN 16 #define LIST_BIRDS {&#8220;Eagle&#8221;, &#8220;Peacock&#8221;, &#8220;Flamingo&#8221;, &#8220;Cockatoo&#8221;, &#8220;Albatross&#8221;, &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/enumeration-example\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Identifying a flying object (enumeration example)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2361,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-8088","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=8088"}],"version-history":[{"count":0,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8088\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2361"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=8088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}