{"id":8092,"date":"2023-02-04T22:27:47","date_gmt":"2023-02-04T20:27:47","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=8092"},"modified":"2024-02-12T13:15:13","modified_gmt":"2024-02-12T11:15:13","slug":"lendava-objekti-tuvastamine-loendi-naide","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programmeerimine-ii\/koodinaited\/lendava-objekti-tuvastamine-loendi-naide\/","title":{"rendered":"Lendava objekti tuvastamine (loendi n\u00e4ide)"},"content":{"rendered":"<p>J\u00e4rgnevas n\u00e4ites on kaetud uue loendi t\u00fc\u00fcbi loomine, loendi t\u00fc\u00fcbi p\u00f5hjal muutuja deklareerimine, loendite kasutamine switch lauses, loendi edastamine funktsiooni ja loendi v\u00e4\u00e4rtuse tagastamine funktsioonist.<\/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>J\u00e4rgnevas n\u00e4ites on kaetud uue loendi t\u00fc\u00fcbi loomine, loendi t\u00fc\u00fcbi p\u00f5hjal muutuja deklareerimine, loendite kasutamine switch lauses, loendi edastamine funktsiooni ja loendi v\u00e4\u00e4rtuse tagastamine funktsioonist. #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;, \\ &#8220;Seagul&#8221;, &#8220;Pigeon&#8221;, &#8220;Passer&#8221;} #define LIST_PLANES {&#8220;Antonov AN-225&#8221;, &#8220;The &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programmeerimine-ii\/koodinaited\/lendava-objekti-tuvastamine-loendi-naide\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Lendava objekti tuvastamine (loendi n\u00e4ide)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":684,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-8092","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8092","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=8092"}],"version-history":[{"count":0,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8092\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/684"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=8092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}