作业社区
探索学习新天地,共享知识资源!
阿大月 的学生作业:
#include #include #include #include using namespace std; class String{ public: String(const char* str = NULL); void show(void); // 输出字符串中的每个字符和对应的ASCII码 private: char* str; }; String::String(const char* str){ if (str != NULL){ this->str = (char*)malloc((strlen(str) + 1)); int i = 0; for (i=0;istr[i] = str[i]; } this->str[i] = '\0'; } } void String::show(void){ cout str str[i], this->str[i]); } free(this->str); } int main(void){ String s("Hello World!"); s.show(); }
+12
浪潮君 的学生作业:
#include #include // 引入ctype.h头文件,里面有isupper()函数 // 设计一个函数,统计字符串中大写字母的个数 int count_uppercase_character(const char str[]) { int count = 0; // 用来记录大写字母的数量 for (int i = 0; str[i] != ‘\0’; i++) { // 遍历字符串,直到遇到结束符’\0’ if (isupper((unsigned char)str[i])) { // 如果当前字符是大写字母 count++; // 计数器加1 } } return count; // 返回大写字母的总数 } int main() { char buf[] = “I Love China”; // 定义一个字符串 int uppercase_count = count_uppercase_character(buf); // 调用函数统计大写字母数量 printf("Number of uppercase characters: %d\n", uppercase_count); // 输出结果 return 0; // 程序正常结束 }
+20
浪潮君 的学生作业:
#include int a[5]; // 全局数组 // 1. 输入数组 void input_array() { printf(“请输入5个整数:\n”); for (int i = 0; i < 5; i++) { scanf("%d", &a[i]); } } // 2. 输出数组 void output_array() { printf(“数组内容是:\n”); for (int i = 0; i < 5; i++) { printf("%d “, a[i]); } printf(”\n"); } // 3. 查找最大值 int find_max() { int max = a[0]; for (int i = 1; i < 5; i++) { if (a[i] > max) { max = a[i]; } } return max; } int main() { input_array(); // 输入数组 output_array(); // 输出数组 int max_value = find_max(); // 查找最大值 printf("数组中的最大值是:%d\n", max_value); return 0; }
+20
慕运维8597106 的学生作业:
没用互斥锁的情况 linux@linux:~/learn/chapter12/thread$ ./a.out 100000 global = 185480 linux@linux:~/learn/chapter12/thread$ ./a.out 100000 global = 191622 linux@linux:~/learn/chapter12/thread$ linux@linux:~/learn/chapter12/thread$ ./a.out 100000 global = 200000 linux@linux:~/learn/chapter12/thread$ ./a.out 100000 global = 200000 linux@linux:~/learn/chapter12/thread$ ./a.out 100000 global = 200000 linux@linux:~/learn/chapter12/thread$ ./a.out 100000 global = 200000 linux@linux:~/learn/chapter12/thread$ ./a.out 100000 global = 200000 linux@linux:~/learn/chapter12/thread$ ./a.out 100000 global = 198353 使用了互斥锁,再大也不会有问题 linux@linux:~/learn/chapter12/thread$ gcc job1-2--new.c -lpthread linux@linux:~/learn/chapter12/thread$ ./a.out 10000000 global = 20000000 linux@linux:~/learn/chapter12/thread$
+78
浪潮君 的学生作业:
#include // 函数:计算两个整数中的较大值,使用值传递 int calc_data(int a, int b) { return (a > b) ? a : b; // 使用三目运算符,简洁返回较大值 } int main() { int a, b; // 定义输入的两个整数 int max_value; // 保存最大值 int sum; // 保存两个数的和 printf("请输入两个整数(以空格分隔):"); scanf("%d %d", &a, &b); // 读取用户输入的两个整数 // 调用函数计算最大值 max_value = calc_data(a, b); // 直接计算两个数的和 sum = a + b; // 输出最大值 printf("两个数中较大的数是: %d\n", max_value); // 输出总和 printf("两个数的和是: %d\n", sum); return 0; // 程序正常结束 }
+21
慕运维8597106 的学生作业:
#include #include #include #include #include typedef struct { char name[20]; int age; } person_t; void *do_thread(void *arg) { static person_t stu; strcpy(stu.name, "张三"); stu.age = 18; pthread_exit(&stu); } int main(int argc, char const *argv[]) { pthread_t tid; int ret; person_t *stu; ret = pthread_create(&tid, NULL, do_thread, NULL); if (ret != 0) { fprintf(stderr, "[ERROR] pthread_create: %s\n", strerror(ret)); } pthread_join(tid, (void **)&stu); if (stu != NULL) { printf("name:%s age:%d\n", stu->name, stu->age); } return 0; } 运行结果: linux@linux:~/learn/chapter12/thread$ gcc job1-2-new.c -l pthread linux@linux:~/learn/chapter12/thread$ ./a.out name:张三 age:18 linux@linux:~/learn/chapter12/thread$
+91
浪潮君 的学生作业:
#include // 判断是否为闰年,返回1是闰年,0不是 int do_leap(int y) { return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0); } int main() { int year = 0; printf(“输入年份:”); // 读取用户输入并进行校验 if (scanf("%d", &year) != 1) { // 如果scanf返回值不是1,说明输入错误 printf("输入错误!\n"); return 1; // 程序异常结束 } // 根据do_leap函数的返回结果判断输出 if (do_leap(year)) { printf("%d年是闰年\n", year); } else { printf("%d年不是闰年\n", year); } return 0; // 正常结束 }
+22
慕运维8597106 的学生作业:
#include #include #include #include #include void *do_thread1(void *arg) { printf("thread1 start!\n"); pthread_exit(NULL); return NULL; } void *do_thread2(void *arg) { printf("thread2 start!\n"); pthread_exit(NULL); return NULL; } int main(int argc, char const *argv[]) { pthread_t tid1,tid2; int ret; ret = pthread_create(&tid1,NULL,do_thread1,NULL); if(ret != 0 ) { fprintf(stderr,"[ERROR] pthread_create : < %s >\n",strerror(ret)); } ret = pthread_create(&tid2,NULL,do_thread2,NULL); if(ret != 0 ) { fprintf(stderr,"[ERROR] pthread_create : < %s >\n",strerror(ret)); } printf("tid1 = %ld\n",tid1); printf("tid2 = %ld\n",tid2); pthread_detach(tid1); pthread_detach(tid2); while(1); return 0; }
+86
慕运维8597106 的学生作业:
#include #include #include #include #include void *do_thread1(void *arg) { printf("thread1 start!\n"); return NULL; } void *do_thread2(void *arg) { printf("thread2 start!\n"); return NULL; } int main(int argc, char const *argv[]) { pthread_t tid1,tid2; int ret; ret = pthread_create(&tid1,NULL,do_thread1,NULL); if(ret != 0 ) { fprintf(stderr,"[ERROR] pthread_create : < %s >\n",strerror(ret)); } ret = pthread_create(&tid2,NULL,do_thread2,NULL); if(ret != 0 ) { fprintf(stderr,"[ERROR] pthread_create : < %s >\n",strerror(ret)); } printf("tid1 = %ld\n",tid1); printf("tid2 = %ld\n",tid2); return 0; } linux@linux:~/learn/chapter12/thread$ ./a.out tid1 = 139657435653888 tid2 = 139657427261184
+89
胡汉三66 的学生作业:
seq(序列号) : 32位字段,表示数据字节流的起始编号。 ack (确认号) : 32位字段,表示接收方期望收到的下一个字节的序列号。具体数值。 A—ACK(确认标志位) :1位标志位,ACK=1时,表示前面的确认号字段有效。二进制位。TCP规定,连接建立后,ACK必须为1 S—SYN(同步标志位) :1位标志位,用于请求建立连接。二进制位。 前两次握手,SYN=1,表示请求建立连接或同意建立连接。 F—FIN(终止标志位) : 1位标志位,用于请求关闭连接。若FIN=1,表示数据已经发送完成,可以释放连接。 关键场景中的组合使用 三次握手: 第一次握手:客户端→服务器 SYN=1, seq=x 第二次握手:服务器→客户端 SYN=1, ACK=1, seq=y, ack=x+1 第三次握手:客户端→服务器 ACK=1, seq=x+1, ack=y+1 四次挥手 第一次挥手:主动方→被动方 FIN=1, seq=u 第二次挥手:被动方→主动方 ACK=1, ack=u+1 第三次挥手:被动方→主动方 FIN=1, ACK=1, seq=v, ack=u+1 第四次挥手:主动方→被动方 ACK=1, ack=v+1 总结: seq/ack:维护数据传输的顺序和完整性,确保可靠传输。 ACK:确认机制的基础,标记应答有效性。 SYN/FIN:分别控制连接的建立和释放,通过三次握手和四次挥手实现全双工通信的启停。 协同机制:这写字段通过组合使用(如SYN+ACK,FIN+ACK),实现TCP面向连接、可靠传输的核心特性。
+74