diff --git a/day12/homework/h1.c b/day12/homework/h1.c new file mode 100644 index 0000000..6d89330 --- /dev/null +++ b/day12/homework/h1.c @@ -0,0 +1,14 @@ +// 给定一张图片的规格信息,请提取出图片的格式、宽和高。 char *s = "base64;image/png:320,480"; +// 输出结果: image / png 320 480 +#include +#include + +int main() +{ + char *s = "base64;image/png:320,480"; + char *format; + int width, height; + sscanf(s, "%*[^;];%[^:]:%d,%d", format, &width, &height); + printf("%s %d %d\n", format, width, height); + return 0; +} \ No newline at end of file diff --git a/day12/homework/h2.c b/day12/homework/h2.c new file mode 100644 index 0000000..f5cb4e6 --- /dev/null +++ b/day12/homework/h2.c @@ -0,0 +1,27 @@ +/* + 找出以下程序的bug, 并修正 int main() + { + const int *a = malloc(20); + for (int i = 0; i < 20; i++) + { + *(a + i) = i; + } + printf("%d %d\n", a[0], a + 2); + } +*/ +#include +#include +#include + +int main() +{ + int *a = malloc(20); // 修改1: 去掉 const 修饰 + for (int i = 0; i < 20; i++) + { + *(a + i) = i; + } + printf("%d %d\n", a[0], *(a + 2)); // 修改2: a+2 加上解引用 *, 以取得元素值 + free(a); // 修改3: 释放手动分配的内存空间 + + return 0; // 修改4: 加上 return 0 +} \ No newline at end of file diff --git a/day12/homework/h3.c b/day12/homework/h3.c new file mode 100644 index 0000000..0c1dc05 --- /dev/null +++ b/day12/homework/h3.c @@ -0,0 +1,21 @@ +/* + 请找出程序的错误行数( ) + int main() + { + int a='21'; + const char *p = (char *) &a; + *p = 48; + printf("%s\n", p); + } +*/ +#include +#include +#include + +int main() +{ + int a = '21'; // 错误1: '21' 是字符常量,不是整型常量,应该写成 21 + const char *p = (char *)&a; + *p = 48; // 错误2: *p 在上一行被 const 修饰为了只读变量,无法修改 + printf("%s\n", p); +} \ No newline at end of file diff --git a/day12/homework/h4.c b/day12/homework/h4.c new file mode 100644 index 0000000..bb368a1 --- /dev/null +++ b/day12/homework/h4.c @@ -0,0 +1,23 @@ +// 编写一个程序,定义一个结构体表示学生的信息,包括姓名、年龄和分数。通过键盘输入学生的信息,然后输出这些信息。 +#include +#include +#include + +typedef struct stu_s +{ + char *name; + int age; + float score; +} STU; + +int main() +{ + STU stu1; + stu1.name = malloc(20); // 因为 name 是指针,所以需要手动分配内存空间 + printf("请输入学生的姓名 年龄 分数:"); + scanf("%s %d %f", stu1.name, &stu1.age, &stu1.score); + printf("姓名: %s, 年龄: %d, 分数: %.2f", stu1.name, stu1.age, stu1.score); + free(stu1.name); // 释放手动分配的内存空间 + + return 0; +} \ No newline at end of file diff --git a/day12/homework/h5.c b/day12/homework/h5.c new file mode 100644 index 0000000..e74f5c4 --- /dev/null +++ b/day12/homework/h5.c @@ -0,0 +1,78 @@ +// 1. 2. 编写一个程序,定义一个结构体表示日期,包括年、月和日。编写一个函数,接受两个日期作为参数,并计算并返回它们之间的天数差。 +#include +#include +#include + +typedef struct date_s +{ + int year; + int month; + int day; +} DATE; + +int sumDays(int year, int month, int day) +{ + int sum = day; + switch (month) + { + case 12: + sum += 30; + case 11: + sum += 31; + case 10: + sum += 30; + case 9: + sum += 31; + case 8: + sum += 31; + case 7: + sum += 30; + case 6: + sum += 31; + case 5: + sum += 30; + case 4: + sum += 31; + case 3: + sum += 28; + case 2: + sum += 31; + } + + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) + sum += 1; + + return sum; +} + +int yearOFdays(int year1, int year2) +{ + int sum = (abs(year1 - year2) - 1) * 365; + int runYearCount = 0; // 闰年计数器 + int minYear = (year1 < year2) ? year1 : year2; // 找出较小的年份 + int maxYear = (year1 > year2) ? year1 : year2; // 找出较大的年份 + for (; minYear <= maxYear; minYear++) + if ((minYear % 4 == 0 && minYear % 100 != 0) || minYear % 400 == 0) + runYearCount++; + + sum += runYearCount; // 每多一个闰年多一天 + return sum; +} +int main() +{ + DATE day1, day2; + printf("请输入第一个日期,输入格式为 年,月,日 "); + scanf("%d,%d,%d", &day1.year, &day1.month, &day1.day); + printf("请输入第二个日期,输入格式为 年,月,日 "); + scanf("%d,%d,%d", &day2.year, &day2.month, &day2.day); + + int sumdays1 = sumDays(day1.year, day1.month, day1.day); + int sumdays2 = sumDays(day2.year, day2.month, day2.day); + + int yearMoreDays = yearOFdays(day1.year, day2.year); + + int days = abs(sumdays1 - sumdays2) + yearMoreDays; + printf("两个日期的天数差为: %d", days); + + return 0; +} \ No newline at end of file diff --git a/day12/homework/h5_2.c b/day12/homework/h5_2.c new file mode 100644 index 0000000..2938dd1 --- /dev/null +++ b/day12/homework/h5_2.c @@ -0,0 +1,73 @@ +#include +#include +#include + +typedef struct date_s +{ + int year; + int month; + int day; +} DATE; + +int daysOfMonth(int year, int month) +{ + switch (month) + { + case 2: + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) + return 29; + else + return 28; + case 4: + case 6: + case 9: + case 11: + return 30; + default: + return 31; + } +} + +int sumDays(int year, int month, int day) +{ + int sum = day; + for (int i = 1; i < month; i++) + { + sum += daysOfMonth(year, i); + } + return sum; +} + +int yearOFdays(int year1, int year2) +{ + int sum = 0; + int minYear = (year1 < year2) ? year1 : year2; + int maxYear = (year1 > year2) ? year1 : year2; + for (int i = minYear; i < maxYear; i++) + { + if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) + sum += 366; + else + sum += 365; + } + return sum; +} + +int main() +{ + DATE day1, day2; + printf("请输入第一个日期,输入格式为 年,月,日 "); + scanf("%d,%d,%d", &day1.year, &day1.month, &day1.day); + printf("请输入第二个日期,输入格式为 年,月,日 "); + scanf("%d,%d,%d", &day2.year, &day2.month, &day2.day); + + int sumdays1 = sumDays(day1.year, day1.month, day1.day); + int sumdays2 = sumDays(day2.year, day2.month, day2.day); + + int yearMoreDays = yearOFdays(day1.year, day2.year); + + int days = abs(sumdays1 - sumdays2) + yearMoreDays; + printf("两个日期的天数差为: %d", days); + + return 0; +} diff --git a/day12/homework/h6.c b/day12/homework/h6.c new file mode 100644 index 0000000..87f6b8e --- /dev/null +++ b/day12/homework/h6.c @@ -0,0 +1,37 @@ +// 编写一个程序,定义一个结构体表示员工的信息,包括姓名、工号和工资。编写一个函数,接受一个员工结构体数组和数组长度作为参数,并计算并返回所有员工的平均工资。 +#include +#include +#include + +typedef struct employee_s +{ + char name[32]; + int eid; + double salary; +} EMPLOYEE; + +double avgSalary(EMPLOYEE *emps, int n) +{ + double avgS; + for (int i = 0; i < n; i++) + { + avgS += emps[i].salary / n; + } + + return avgS; +} + +int main() +{ + int n; // 数组长度 + printf("请输入数组长度: "); + scanf("%d", &n); + EMPLOYEE *employees = malloc(n * sizeof(EMPLOYEE)); + employees[0].salary = 100; + employees[1].salary = 59; + + printf("平均工资为: %.2lf", avgSalary(employees, n)); + free(employees); + + return 0; +} \ No newline at end of file diff --git a/day12/homework/h7.c b/day12/homework/h7.c new file mode 100644 index 0000000..b10eb5e --- /dev/null +++ b/day12/homework/h7.c @@ -0,0 +1,59 @@ +// 编写一个程序,定义一个枚举(enum)表示一周的每一天,包括周一至周日。编写一个函数,接受一个枚举值作为参数,并根据枚举值打印出相应的星期几的名称。例如,传递枚举值为2时,函数应该打印出 "Tuesday"。 +#include +#include +#include + +enum +{ + Monday, + Tuesday, + Wednesday, + Thursday, + Friday, + Saturday, + Sunday +}; + +void print_weekday(int day) +{ + switch (day) + { + case Monday: + printf("Monday\n"); + break; + case Tuesday: + printf("Tuesday\n"); + break; + case Wednesday: + printf("Wednesday\n"); + break; + case Thursday: + printf("Thursday\n"); + break; + case Friday: + printf("Friday\n"); + break; + case Saturday: + printf("Saturday\n"); + break; + case Sunday: + printf("Sunday\n"); + break; + default: + printf("Invalid day\n"); + break; + } +} + +int main() +{ + print_weekday(Monday); + print_weekday(Tuesday); + print_weekday(Wednesday); + print_weekday(Thursday); + print_weekday(Friday); + print_weekday(Saturday); + print_weekday(Sunday); + + return 0; +} \ No newline at end of file diff --git a/day12/homework/h8.c b/day12/homework/h8.c new file mode 100644 index 0000000..3d36516 --- /dev/null +++ b/day12/homework/h8.c @@ -0,0 +1,56 @@ +// 练习结构体数组排序   从键盘输入5个学生的信息(姓名、学号、成绩), +// 存入一个结构体数组中,计算平均分,并按成绩高低排序并输出 +#include +#include +#include + +typedef struct stu_s +{ + char name[32]; + int sid; + float score; +} STU; + +void arrAndOutput(STU *stus) // 排序并输出 +{ + float avg = 0.0f; + for (int i = 0; i < 5; i++) + { + avg += stus[i].score / 5; + } + + printf("平均分为: %.2f\n", avg); + + // 冒泡排序 + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 5 - i - 1; j++) + { + if (stus[j].score < stus[j + 1].score) + { + STU tmp = stus[j]; + stus[j] = stus[j + 1]; + stus[j + 1] = tmp; + } + } + } + + // 输出 + for (int i = 0; i < 5; i++) + { + printf("姓名: %s, 学号: %d, 成绩: %.2f\n", stus[i].name, stus[i].sid, stus[i].score); + } +} + +int main() +{ + STU s[5]; + for (int i = 0; i < 5; i++) + { + printf("请输入第 %d 个学生的,格式为:(姓名 学号 成绩)", i + 1); + scanf("%s %d %f", s[i].name, &s[i].sid, &s[i].score); + } + arrAndOutput(s); + + return 0; +} \ No newline at end of file diff --git a/day12/homework/h9.c b/day12/homework/h9.c new file mode 100644 index 0000000..cac3778 --- /dev/null +++ b/day12/homework/h9.c @@ -0,0 +1,118 @@ +/* +了解GPS相关知识 +$GPRMC协议格式 +  例:$GPRMC, 024813.640, A, 3158.4608, N, 11848.3737, E, 10.05, 324.27, 150706, , , A * 50 +  字段0:$GPRMC,语句ID,表明该语句为Recommended Minimum Specific GPS / TRANSIT Data(RMC)推荐最小定位信息 +  字段1:UTC时间,hhmmss.sss格式(UTC时间是世界统一时间,中国的时间是UTC + 8,比如:上面的时间是02 : 48 : 13而中国的时间是10 : 48 : 13) +  字段2:状态,A = 定位,V = 未定位 +  字段3:纬度ddmm.mmmm,度分格式(前导位数不足则补0) +  字段4:纬度N(北纬)或S(南纬) +  字段5:经度dddmm.mmmm,度分格式(前导位数不足则补0) +  字段6:经度E(东经)或W(西经) +  字段7:速度,节,Knots +  字段8:方位角,度 +  字段9:UTC日期,DDMMYY格式 +  字段10:磁偏角,(000 - 180)度(前导位数不足则补0) +  字段11:磁偏角方向,E = 东W = 西 +  字段16:校验值 + +【提示】利用strtok切割、sscanf或者strncpy、atoi、atof +*/ +#include +#include + +typedef struct +{ + char time[10]; // UTC时间,格式为hhmmss.sss + char status; // 状态,A表示定位,V表示未定位 + double latitude; // 纬度,格式为ddmm.mmmm + char latitude_dir; // 纬度半球,N表示北半球,S表示南半球 + double longitude; // 经度,格式为dddmm.mmmm + char longitude_dir; // 经度半球,E表示东经,W表示西经 + double speed; // 速度,以节为单位 + double course; // 方位角,以度为单位 + char date[7]; // UTC日期,格式为DDMMYY + double declination; // 磁偏角,以度为单位 + char declination_dir; // 磁偏角方向,E表示东偏,W表示西偏 +} gprmc_t; + +int parse_gprmc(const char *str, gprmc_t *gprmc) +{ + char *token; + int i = 0; + token = strtok((char *)str, ","); + while (token != NULL) + { + switch (i) + { + case 0: + if (strcmp(token, "$GPRMC") != 0) + { + return -1; + } + break; + case 1: + sscanf(token, "%2s", gprmc->time); + sscanf(token + 2, "%2lf%lf", &gprmc->time[2], &gprmc->time[4]); + sscanf(token + 4, "%lf", &gprmc->time[6]); + break; + case 2: + gprmc->status = *token; + break; + case 3: + sscanf(token, "%lf", &gprmc->latitude); + sscanf(token + 2, "%lf", &gprmc->latitude); + break; + case 4: + gprmc->latitude_dir = *token; + break; + case 5: + sscanf(token, "%lf", &gprmc->longitude); + sscanf(token + 3, "%lf", &gprmc->longitude); + break; + case 6: + gprmc->longitude_dir = *token; + break; + case 7: + sscanf(token, "%lf", &gprmc->speed); + break; + case 8: + sscanf(token, "%lf", &gprmc->course); + break; + case 9: + sscanf(token, "%2s", gprmc->date); + sscanf(token + 2, "%2lf%lf", &gprmc->date[2], &gprmc->date[4]); + sscanf(token + 4, "%lf", &gprmc->date[6]); + break; + case 10: + sscanf(token, "%lf", &gprmc->declination); + break; + case 11: + gprmc->declination_dir = *token; + break; + default: + break; + } + token = strtok(NULL, ","); + i++; + } + return 0; +} + +int main() +{ + const char *str = "$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50"; + gprmc_t gprmc; + if (parse_gprmc(str, &gprmc) == 0) + { + printf("UTC时间:%s\n", gprmc.time); + printf("状态:%c\n", gprmc.status); + printf("纬度:%lf %c\n", gprmc.latitude, gprmc.latitude_dir); + printf("经度:%lf %c\n", gprmc.longitude, gprmc.longitude_dir); + printf("速度:%lf\n", gprmc.speed); + printf("方位角:%lf\n", gprmc.course); + printf("UTC日期:%s\n", gprmc.date); + printf("磁偏角:%lf %c\n", gprmc.declination, gprmc.declination_dir); + } + return 0; +}