作业社区
探索学习新天地,共享知识资源!
慕运维8597106 的学生作业:
#include #include using namespace std; class String { public: String(const char *str); String(const String &other); String &operator++(); String operator++(int); char &operator[](int index); ~String(void); void show(); int size(); private: char *str; int len = 0; }; String::String(const char *str) { cout str = new char[len]; strcpy(this->str, str); } } String::String(const String &other) { cout str = new char[len]; strcpy(this->str, other.str); } } int String::size() { return this->len; } String &String::operator++() { if (this->str) { for (char *p = this->str; p && *p; p++) { *p = *p + 1; } } cout str; p && *p; p++) { *p = *p + 1; } } return oldstr; } char &String::operator[](int index) { if (index < 0 || index >= this->len) { cout
+65
weixin_慕哥3021856 的学生作业:
#include #include #include #include #include static int number = 0; static int total_of_produce = 0; static int total_of_consume = 0; static bool done = false; // 生产是否完成,默认否 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond_produce = PTHREAD_COND_INITIALIZER; static pthread_cond_t cond_consume = PTHREAD_COND_INITIALIZER; void *thread_produce(void *arg) { int cnt = atoi((char *)arg); for (int i = 0; i < cnt; i++) { pthread_mutex_lock(&mtx); printf("生产者线程 [%ld] 生产1个产品,产品数量是 %d\n", pthread_self(), ++number); pthread_mutex_unlock(&mtx); pthread_cond_broadcast(&cond_consume); usleep(1000); } pthread_exit((void *)0); } void *thread_consume(void *arg) { while (1) { pthread_mutex_lock(&mtx); // 当产品数量为0时,阻塞线程并释放锁,这里设置循环是为了防止没有重新获得锁 while (number == 0 && !done) { pthread_cond_wait(&cond_consume, &mtx); } // 推出循环,结束线程 if (number == 0 && done) { pthread_mutex_unlock(&mtx); break; } if (number > 0) { total_of_consume++; printf("消费者 [%ld] 消费1个产品,产品数量是 %d\n", pthread_self(), --number); } pthread_mutex_unlock(&mtx); usleep(1000); } pthread_exit((void *)0); } int main(int argc, const char *argv[]) { int i = 0; int consume = 0, produce = 0; pthread_t tid; if (argc < 2) { fprintf(stderr, "Usage: %s producer1_num producer2_num ...\n", argv[0]); exit(EXIT_FAILURE); } // 创建生产者线程 pthread_t producers[argc-1]; pthread_t consumers[3]; // 3个消费者,固定数量 for (i = 1; i < argc; i++) { total_of_produce += atoi(argv[i]); produce = pthread_create(&producers[i-1], NULL, thread_produce, (void *)argv[i]); if (produce != 0) { perror("[ERROR] pthread_create()"); exit(EXIT_FAILURE); } } for (i = 0; i < 3; i++) { consume = pthread_create(&consumers[i], NULL, thread_consume, NULL); if (consume != 0) { perror("[ERROR] pthread_create()"); exit(EXIT_FAILURE); } } // 等待所有生产者完成 for (i = 0; i < argc - 1; i++) { pthread_join(producers[i], NULL); } // 设置生产完成标志,唤醒所有消费者 pthread_mutex_lock(&mtx); done = true; pthread_cond_broadcast(&cond_consume); pthread_mutex_unlock(&mtx); for (i = 0; i < 3; i++) { pthread_join(consumers[i], NULL); } printf("总共生产:%d,总共消费:%d\n", total_of_produce, total_of_consume); return 0; } 【图片】
+78
慕运维8597106 的学生作业:
错误1:拷贝构造传入的参数需要是引用 错误2:拷贝构造是浅拷贝,并没有重新分配内存,复制数据 错误3:delete data应该是delete [] data; 构造函数调用了5次 第一次:Test obj1(3);//普通构造 第二次:Test function(Test obj)//参数传递调用拷贝构造 第三次: function里面的Test tmp = obj; 第四次:function的返回值,因为tmp在函数结束的时候要释放,又调用了一次拷贝构造用于返回 第五次:Test obj2 = function(obj1);//obj2 = 调用拷贝构造 析构函数也调用了5次 function里面参数和Test tmp = obj; 各调用一次 累计两次 return之后的临时的拷贝也会在程序结束的时候调用一次 另外两次是main函数里的obj1和obj2 #include using namespace std; class Test { public: Test(int size) { cout
+76
慕运维8597106 的学生作业:
#include #include using namespace std; class String { public: String(const char *str = NULL); void show(void); private: char *str; }; String::String(const char *str) { if (str == NULL) { this->str = NULL; } else { this->str = new char[strlen(str) + 1]; strcpy(this->str, str); } } void String::show(void){ for(char *p = this->str;p && *p;p++){ cout
+88