SSL: 1
Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893
{"id":445,"date":"2016-07-31T18:27:18","date_gmt":"2016-07-31T10:27:18","guid":{"rendered":"https:\/\/blog.weskiller.com\/?p=445"},"modified":"2016-08-11T17:32:21","modified_gmt":"2016-08-11T09:32:21","slug":"c-class%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/blog.gamein.vip\/445.html","title":{"rendered":"C++ Class\u7ec3\u4e60"},"content":{"rendered":"

\u300aC++ Primer Plus\u300b<\/span>
\n\u7b2c12\u7ae0 \u8bfe\u540e\u7ec3\u4e602<\/span><\/p>\n

\r\n#ifndef STRING_H_\r\n#define STRING_H_\r\n#include <iostream>\r\nusing std::ostream;\r\nusing std::istream;\r\n\r\nclass String\r\n{\r\nprivate:\r\n        char * str;\r\n        int len;\r\n        static int num_strings;\r\n        static const int CINLIM = 80;\r\npublic:\r\n        String(const char * s);\r\n        String();\r\n        String(const String &);\r\n        ~String();\r\n        int length () const { return len; }\r\n\r\n        String & operator=(const String &);\r\n        String & operator=(const char *);\r\n\r\n        char & operator[](int i);\r\n        const char & operator[](int i) const;\r\n        void Stringlow();\r\n        void Stringup();\r\n        int has(const char c);\r\n        String operator+(const String & st1);\r\n\r\n        operator char *() const;\r\n        friend bool operator<(const String & st1, const String & st2);\r\n        friend bool operator>(const String & st1, const String & st2);\r\n        friend bool operator==(const String & st1, const String & st2);\r\n        friend String operator+(const char *, const String & st1);\r\n        friend ostream & operator<<(ostream & os, const String & st);\r\n        friend istream & operator>>(istream & is, String & st);\r\n\r\n        static int HowMany();\r\n};\r\n#endif\r\n<\/pre>\n
\r\n#include <cstring>\r\n#include <cctype>\r\n#include "String.h"\r\n#include <iostream>\r\nint String::num_strings = 0;\r\n\r\nint String::HowMany()\r\n{\r\n\treturn num_strings;\r\n}\r\n\r\nString::String(const char * s)\r\n{\r\n\tlen = std::strlen(s);\r\n\tstr = new char[len+1];\r\n\tstd::strcpy(str,s);\r\n\tnum_strings++;\r\n}\r\n\r\nString::String()\r\n{\r\n\tlen = 4;\r\n\tstr = new char[1];\r\n\tstr[0] = '\0';\r\n\tnum_strings++;\r\n}\r\n\r\nString::String(const String & st)\r\n{\r\n\tnum_strings++;\r\n\tlen = st.len;\r\n\tstr = new char[len+1];\r\n\tstd::strcpy(str,st.str);\r\n}\r\n\r\nString::~String()\r\n{\r\n\t--num_strings;\r\n\tdelete [] str;\r\n}\r\n\r\nString & String::operator=(const String & st)\r\n{\r\n\tdelete [] str;\r\n\t\r\n\tlen = strlen(st.str);\r\n\tstr = new char[len+1];\r\n\tstd::strcpy(str,st.str);\r\n\t\r\n\treturn *this;\r\n}\r\n\r\nString & String::operator=(const char * s)\r\n{\r\n\tdelete [] str;\r\n\tlen = std::strlen(s);\r\n\tstr = new char[len+1];\r\n\tstd::strcpy(str, s);\r\n\treturn *this;\r\n}\r\n\r\nchar & String::operator[](int i)\r\n{\r\n\treturn str[i];\r\n}\r\n\r\nbool operator<(const String & st1, const String & st2)\r\n{\r\n\treturn (std::strcmp(st1.str, st2.str) < 0);\r\n}\r\n\r\nbool operator>(const String & st1, const String & st2)\r\n{\r\n\treturn st2 < st1;\r\n}\r\n\r\nbool operator==(const String & st1, const String & st2)\r\n{\r\n\treturn (std::strcmp(st1.str, st2.str) == 0);\r\n}\r\n\r\nostream & operator<<(ostream & os, const String & st)\r\n{\r\n\tos << st.str;\r\n\treturn os;\r\n}\r\n\r\nistream & operator>>(istream & is, String & st)\r\n{\r\n\tchar temp[String::CINLIM];\r\n\tis.get(temp,String::CINLIM);\r\n\tif(is)\r\n\t\tst = temp;\r\n\twhile (is && is.get() != '\\n')\r\n\t\tcontinue;\r\n\treturn is;\r\n}\r\n\r\nvoid String::Stringlow()\r\n{\r\n\tfor (int i = 0;i < len; i++)\r\n\t\tstr[i] = tolower(str[i]);\r\n}\r\n\r\nvoid String::Stringup()\r\n{\r\n\tfor (int i = 0;i < len; i++)\r\n\t\tstr[i] = toupper(str[i]);\r\n}\r\n\/* \u9519\u8bef\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u95ee\u9898\u662f\u6784\u9020temp\u7684\u65f6\u5019\uff0c\u5df2\u7ecf\u4f7f\u7528new \u5206\u914d\u4e86\u56fa\u5b9a\u957f\u5ea6\u7684\u5185\u5b58\u3002\u4f7f\u7528strcat\u7684\u65f6\u5019\u5b9e\u9645\u4e0a\u5df2\u7ecf\u5185\u5b58\u6ea2\u51fa\u4e86.\r\nString String::operator+(const String & st1)\r\n{\r\n\tString temp(str);\r\n\tstrcat(temp.str,st1.str);\r\n\ttemp.len = strlen(str) + strlen(st1.str) + 1;\r\n\treturn temp;\r\n}\r\n*\/\r\nString String::operator+(const String & st1)\r\n{\r\n\tint nlen = strlen(st1.str) + strlen(str) + 1;\r\n\tchar * nstr = new char[nlen];\r\n\tstrcpy(nstr,str);\r\n\tstrcat(nstr,st1.str);\r\n\tString temp;\r\n\tdelete [] temp.str;\r\n\ttemp.str = nstr;\r\n\treturn temp;\r\n}\r\n\r\nString::operator char *() const\r\n{\r\n\treturn str;\r\n}\r\n\r\nString operator+(const char * st1,const String & st2)\r\n{\r\n\tString temp;\r\n\tstrcat(temp.str,st1);\r\n\tstrcat(temp.str,st2.str);\r\n\ttemp.len = strlen(st1) + st2.len;\r\n\treturn temp;\r\n}\r\n\r\nint String::has(const char c)\r\n{\r\n\tint count = 0;\r\n\tfor(int i = 0; i < len; i++)\r\n\t\t\tif (str[i] == c )\r\n\t\t\t\tcount++;\r\n\treturn count;\r\n}\r\n<\/pre>\n
\r\n#include <iostream>\r\nusing namespace std;\r\n#include "String2.h"\r\nint main()\r\n{\r\n        String s1(" and I am C++ stdudent.");\r\n        String s2 = "Pleas enter your name: ";\r\n        String s3;\r\n        cout << s2;\r\n        cin >> s3;\r\n        s2 = "My name is " + s3;\r\n        cout << s2 << ".\\n";\r\n        s2 = s2 + s1;\r\n        s2.Stringup();\r\n        cout << "The string\\n" << s2 << "\\ncontains " << s2.has('A')\r\n                << " 'A' characters in it.\\n";\r\n        s1 = "red";\r\n\r\n        String rgb[3] = { String(s1), String("green"), String("blue") };\r\n\r\n        cout << "Enter the name of a primary color for mixing light: ";\r\n        String ans;\r\n        bool success = false;\r\n        while (cin >> ans)\r\n        {\r\n                ans.Stringlow();\r\n                for(int i = 0; i < 3; i++)\r\n                {\r\n                        if(ans == rgb[i])\r\n                        {\r\n                                cout << "That's right!\\n";\r\n                                success = true;\r\n                                break;\r\n                        }\r\n                }\r\n                if (success)\r\n                        break;\r\n                else\r\n                        cout << "Try again!\\n";\r\n        }\r\n        cout << "Bye\\n";\r\n        return 0;\r\n}\r\n<\/pre>\n

\u7b2c12\u7ae0 \u8bfe\u540e\u7ec3\u4e606<\/span><\/p>\n

\r\n#ifndef QUEUE_H_\r\n#define QUEUE_H_\r\nclass Customer\r\n{\r\nprivate:\r\n        long arrive;\r\n        int processtime;\r\npublic:\r\n        Customer() { arrive = processtime = 0; }\r\n\r\n        void set(long when);\r\n        long when() const { return arrive; }\r\n        int ptime() const { return processtime; }\r\n};\r\n\r\ntypedef Customer Item;\r\n\r\nclass Queue\r\n{\r\nprivate:\r\n        struct Node { Item item; struct Node * next ; };\r\n        enum { Q_SIZE = 10 };\r\n\r\n        Node * front;\r\n        Node * rear;\r\n        int items;\r\n        const int qsize;\r\n        Queue(const Queue & q) : qsize(0) { }\r\n        Queue & operator=(const Queue & q) { return *this; }\r\npublic:\r\n        Queue( int qs = Q_SIZE );\r\n        ~Queue();\r\n        bool isempty() const;\r\n        bool isfull() const;\r\n        int queuecount() const;\r\n        bool enqueue(const Item & item);\r\n        bool dequeue(Item & item);\r\n};\r\n#endif\r\n<\/pre>\n
\r\n#include "queue.h"\r\n#include <cstdlib>\r\n#include <stdio.h>\r\n\r\nQueue::Queue(int qs) : qsize(qs)\r\n{\r\n\tfront = rear = NULL;\r\n\titems = 0;\r\n}\r\n\r\nQueue::~Queue()\r\n{\r\n\tNode * temp;\r\n\twhile(front != NULL)\r\n\t{\r\n\t\ttemp = front;\r\n\t\tfront = front->next;\r\n\t\tdelete temp;\r\n\t}\r\n}\r\n\r\nbool Queue::isempty() const\r\n{\r\n\treturn items == 0;\r\n}\r\n\r\nbool Queue::isfull() const\r\n{\r\n\treturn items == qsize;\r\n}\r\n\r\nint Queue::queuecount() const\r\n{\r\n\treturn items;\r\n}\r\n\r\nbool Queue::enqueue(const Item & item)\r\n{\r\n\tif (isfull())\r\n\t\treturn false;\r\n\tNode * add = new Node;\r\n\tadd->item = item;\r\n\tadd->next = NULL;\r\n\titems++;\r\n\tif(front == NULL)\r\n\t\tfront = add;\r\n\telse\r\n\t\trear->next = add;\r\n\trear = add;\r\n\treturn true;\r\n}\r\n\r\nbool Queue::dequeue(Item & item)\r\n{\r\n\tif(front == NULL)\r\n\t\treturn false;\r\n\titem = front->item;\r\n\titems--;\r\n\tNode * temp = front;\r\n\tfront = front->next;\r\n\tdelete temp;\r\n\tif(items == 0)\r\n\t\trear = NULL;\r\n\treturn true;\r\n}\r\n\r\nvoid Customer::set(long when)\r\n{\r\n\tprocesstime = std::rand() % 3 + 1;\r\n\tarrive = when;\r\n}\r\n\r\n[code lang="C" title="queue.cpp"]\r\n\r\n<\/pre>\n
\r\n#include <iostream>\r\n#include <cstdlib>\r\n#include <ctime>\r\n#include "queue.h"\r\n\r\nconst int MIN_PER_HR = 60;\r\n\r\nbool newcustomer(double x);\r\n\r\nint main()\r\n{\r\n\tusing std::cin;\r\n\tusing std::cout;\r\n\tusing std::endl;\r\n\tusing std::ios_base;\r\n\tstd::srand(std::time(0));\r\n\r\n\tcout << "Case Study: Bank of Heather Automatic Teller\\n";\r\n\tcout << "Enter maximum size of queue: ";\r\n\tint qs;\r\n\tcin >> qs;\r\n\tQueue line1(qs);\r\n\tQueue line2(qs);\r\n\r\n\tcout << "Enter the number of simulation hours: ";\r\n\tint hours;\r\n\tcin >> hours;\r\n\r\n\tlong cyclelimit = MIN_PER_HR * hours;\r\n\r\n\tcout << "Enter the average number of customers per hours: ";\r\n\tdouble perhour;\r\n\tcin >> perhour;\r\n\tdouble min_per_cust;\r\n\tmin_per_cust = MIN_PER_HR \/ perhour ;\r\n\r\n\tItem temp1,temp2;\r\n\tlong turnaways = 0;\r\n\tlong customers = 0;\r\n\tlong served = 0;\r\n\tlong sum_line = 0;\r\n\tint wait_time1,wait_time2;\r\n\twait_time1 = wait_time2 = 0;\r\n\tlong line_wait = 0;\r\n\tfor(int cycle = 0;cycle < cyclelimit; cycle++)\r\n\t{\r\n\t\tif (newcustomer(min_per_cust))\r\n\t\t{\r\n\t\t\tif ( line1.isfull() && line2.isfull() )\r\n\t\t\t\tturnaways++;\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tcustomers++;\r\n\t\t\t\tif ( line1.isfull() )\r\n\t\t\t\t{\r\n\t\t\t\t\ttemp2.set(cycle);\r\n\t\t\t\t\tline2.enqueue(temp2);\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\ttemp1.set(cycle);\r\n\t\t\t\t\tline1.enqueue(temp1);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (wait_time1 <= 0 && !line1.isempty())\r\n                {\r\n                        line1.dequeue(temp1);\r\n                        wait_time1 = temp1.ptime();\r\n                        line_wait += cycle - temp1.when();\r\n                        served++;\r\n                }\r\n\t\tif (wait_time2 <= 0 && !line2.isempty())\r\n                {\r\n                        line2.dequeue(temp2);\r\n                        wait_time2 = temp2.ptime();\r\n                        line_wait += cycle - temp2.when();\r\n                        served++;\r\n                }\r\n\t\tif(wait_time1 > 0)\r\n\t\t\twait_time1--;\r\n\t\tif(wait_time2 > 0)\r\n\t\t\twait_time2--;\r\n\t\tsum_line += ( line1.queuecount() + line2.queuecount() );\r\n\t}\r\n\r\n\tif (customers > 0)\r\n\t{\r\n\t\tcout << "customers accepted: " << customers << endl;\r\n\t\tcout << "   customers served: " << served << endl;\r\n\t\tcout << "      turnaways: " << turnaways << endl;\r\n\t\tcout << "average queue size: ";\r\n\t\tcout.precision(2);\r\n\t\tcout.setf(ios_base::fixed, ios_base::floatfield);\r\n\t\tcout << (double) sum_line \/ cyclelimit << endl;\r\n\t\tcout << " average wait time: "\r\n\t\t\t<< (double) line_wait \/ served << " minutes\\n";\r\n\t}\r\n\telse\r\n\t\tcout << "No customers!\\n";\r\n\tcout << "Done!\\n";\r\n\r\n\treturn 0;\r\n}\r\n\r\nbool newcustomer(double x)\r\n{\r\n\treturn (std::rand() * x \/ RAND_MAX < 1);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u300aC++ Primer Plus\u300b \u7b2c12\u7ae0 \u8bfe\u540e\u7ec3\u4e602 #ifndef STRING_H_ #define […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[23],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-c","tag-c"],"_links":{"self":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":14,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":475,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts\/445\/revisions\/475"}],"wp:attachment":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}