{"id":354,"date":"2023-01-26T21:40:48","date_gmt":"2023-01-26T13:40:48","guid":{"rendered":"https:\/\/9iwd.top\/?p=354"},"modified":"2023-01-26T21:41:05","modified_gmt":"2023-01-26T13:41:05","slug":"09-7-c%e7%bb%93%e6%9e%84%e4%bd%93%e6%a1%88%e4%be%8b%e4%b8%8009-7-c","status":"publish","type":"post","link":"http:\/\/9iwd.top\/?p=354","title":{"rendered":"09.7 C++\u7ed3\u6784\u4f53\u6848\u4f8b\u4e00"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\/\/ \u8f93\u51fa\u6d41\u5e93\r\n#include &lt;string>\/\/ \u5b57\u7b26\u4e32\u5934\u6587\u4ef6\r\n#include &lt;ctime>\/\/ \u7cfb\u7edf\u65f6\u95f4\u5934\u6587\u4ef6\r\nusing namespace std;\/\/\u4f7f\u7528\u6807\u51c6\u547d\u540d\u7a7a\u95f4\r\n\r\n\/\/\u8bfe\u7a0b\uff1a9.7 \u7ed3\u6784\u4f53\u6848\u4f8b\u4e00\r\n\r\n\/\/\u672c\u8282\u5185\u5bb9\uff0c3\u4e2a\u8001\u5e08\uff0c\u4e00\u4e2a\u8001\u5e08\u5e265\u4e2a\u5b66\u751f\uff0c\u6bcf\u4e2a\u5b66\u751f\u6709\u59d3\u540d\uff0c\u5e74\u9f84\uff0c\u5206\u6570\r\n\r\n\/\/\u521b\u5efa\u5b66\u751f\u7684\u7ed3\u6784\u4f53\r\nstruct Student\r\n{\r\n    string sName;\/\/\u59d3\u540d\r\n    int age;\/\/\u5e74\u9f84\r\n    int score;\/\/\u5206\u6570\r\n};\r\n\r\n\/\/\u521b\u5efa\u8001\u5e08\u7ed3\u6784\u4f53\r\nstruct Teacher\r\n{\r\n    string tName;\/\/\u8001\u5e08\u540d\u5b57\r\n    struct Student sArray&#91;5];\r\n};\r\n\r\n\/\/\u521b\u5efa\u7ed9\u8001\u5e08\u548c\u5b66\u751f\u8d4b\u503c\u7684\u51fd\u6570\r\nvoid allocateSpace(struct Teacher tArray&#91;], int len)\r\n{\r\n    \/\/\u521d\u59cb\u521b\u5efa\u6807\u8bc6\u5b57\u7b26\r\n    string nameSeed = \"ABCDE\";\r\n\r\n    \/\/\u7ed9\u8001\u5e08\u5f00\u59cb\u8d4b\u503c\r\n    for (int i = 0; i &lt; len; i++)\r\n    {\r\n        tArray&#91;i].tName = \"Teacher_\";\r\n        tArray&#91;i].tName += nameSeed&#91;i];\/\/\u5728tArray&#91;i].tName\u539f\u57fa\u7840\u8ffd\u52a0nameSeed\u7b2ci\u4f4d\u7f6e\u7684\u5b57\u7b26\r\n\r\n         \/\/\u901a\u8fc7\u5faa\u73af\u7ed9\u6bcf\u540d\u8001\u5e08\u6240\u5e26\u7684\u5b66\u751f\u8d4b\u503c\r\n        for (int j = 0; j &lt; 5; j++)\r\n        {\r\n            \/\/\u8d4b\u503c\u5b66\u751f\u59d3\u540d\r\n            tArray&#91;i].sArray&#91;j].sName = \"Student_\";\r\n            tArray&#91;i].sArray&#91;j].sName += nameSeed&#91;j];\/\/\u5728tArray&#91;i].sArray&#91;j].sName\u539f\u57fa\u7840\u8ffd\u52a0nameSeed\u7b2ci\u4f4d\u7f6e\u7684\u5b57\u7b26\r\n\r\n            \/\/\u53d6\u968f\u673a\u6570(\u4e3a\u4e86\u5fc5\u987b\u662f\u968f\u673a\u7684\u6570\u503c\uff0c\u8fd8\u9700\u8981\u521b\u5efa\u968f\u673a\u6570\u79cd\u5b50)\r\n            int random1 = rand() % 6 + 10;\/\/\u53d6\u968f\u673a\u65700~5 + 10\r\n            int random2 = rand() % 61 + 40;\/\/\u53d6\u968f\u673a\u65700~60 + 40\r\n\r\n            tArray&#91;i].sArray&#91;j].age = random1;\/\/\u7ed9\u4e00\u4e2a\u968f\u673a\u6570\u5e74\u9f84\r\n            tArray&#91;i].sArray&#91;j].score = random2;\/\/\u7ed9\u4e00\u4e2a\u968f\u673a\u6570\u5206\u6570\r\n        }\r\n\r\n    }\r\n}\r\n\r\n\/\/\u521b\u5efa\u6253\u5370\u51fd\u6570\r\nvoid printInfo(struct Teacher tArray&#91;], int len)\r\n{\r\n    for (int i = 0; i &lt; len; i++)\r\n    {\r\n        \/\/\u6253\u5370\u8001\u5e08\u7684\u4fe1\u606f\r\n        cout &lt;&lt; \"\u8001\u5e08\u540d\u5b57\uff1a\" &lt;&lt; tArray&#91;i].tName &lt;&lt; endl;\r\n\r\n        for (int j = 0; j &lt; 5; j++)\r\n        {\r\n            \/\/\u6253\u5370\u5b66\u751f\u7684\u4fe1\u606f\r\n            cout &lt;&lt; \"\\t\u5b66\u751f\u540d\u5b57\uff1a\" &lt;&lt; tArray&#91;i].sArray&#91;j].sName \r\n                &lt;&lt; \" \u5b66\u751f\u5e74\u9f84\uff1a\" &lt;&lt; tArray&#91;i].sArray&#91;j].age \r\n                &lt;&lt; \" \u5b66\u751f\u5206\u6570\uff1a\" &lt;&lt; tArray&#91;i].sArray&#91;j].score &lt;&lt;endl;\r\n        }\r\n    }\r\n}\r\n\r\n\r\nint main() {\r\n    \/\/\u521b\u5efa\u968f\u673a\u6570\u79cd\u5b50\uff0c\u53d6\u7cfb\u7edf\u542f\u52a8\u65f6\u95f4\u968f\u673a\u6570\r\n    srand((unsigned int)time(NULL));\r\n\r\n    \/\/1\u3001\u521b\u5efa3\u540d\u8001\u5e08\u7684\u6570\u7ec4\r\n    struct Teacher tArray&#91;3];\r\n\r\n    \/\/2\u3001\u901a\u8fc7\u51fd\u6570\u7ed93\u540d\u8001\u5e08\u8d4b\u503c\uff0c\u5e76\u7ed9\u8001\u5e08\u7684\u5b66\u751f\u8d4b\u503c\r\n    int len = sizeof(tArray) \/ sizeof(tArray&#91;0]);\/\/\u8ba1\u7b97\u5f97\u51fa\u6570\u7ec4\u957f\u5ea6\r\n    allocateSpace(tArray, len);\r\n    \r\n    \/\/3\u3001\u6253\u5370\u6240\u6709\u8001\u5e08\u53ca\u6240\u5e26\u7684\u5b66\u751f\u4fe1\u606f\r\n    printInfo(tArray, len);\r\n\r\n    system(\"pause\");\/\/ \u63a7\u5236\u53f0\u6682\u505c\uff0c\u7b49\u5f85\u4e0b\u4e00\u6b65\u64cd\u4f5c\r\n    return 0;\/\/ \u7ed3\u675f\u8fd4\u56de\u503c\uff1a0\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"_links":{"self":[{"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/posts\/354"}],"collection":[{"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/9iwd.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=354"}],"version-history":[{"count":0,"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions"}],"wp:attachment":[{"href":"http:\/\/9iwd.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/9iwd.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/9iwd.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}