Compare commits

..

No commits in common. "4e10a4d84d3d37fde126dda962aac4c3cb92f948" and "e855ec7605a98eebbe7399123e6bbb723930db78" have entirely different histories.

13 changed files with 3 additions and 642 deletions

View File

@ -1,55 +0,0 @@
// 编写一个程序要求用户输入一串字符然后动态创建一个字符数组并将用户输入的字符存储到数组中。然后使用realloc函数将数组的大小扩展为原来的两倍并继续接受用户输入的字符直到用户输入字符为换行符 '\n')。最后,打印数组中存储的所有字符。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *
str_cpy(char *dest, const char *src)
{
int cnt;
while (src[cnt++])
;
char *res = (char *)malloc(cnt * sizeof(char *));
if (NULL == res)
{
perror("malloc");
exit(0);
}
for (int i = 0; i < cnt; i++)
res[i] = src[i];
return res;
}
int main()
{
char s1[100]; // 相当于 char *s1 = (char *)malloc(100 * sizeof(char *));
printf("请输入一个字符串: ");
// scanf("%s", s1);
fgets(s1, sizeof(s1), stdin); // scanf() 不能接收换行,故改用 fgets()
char *s2 = str_cpy(s2, s1);
int tmp_size = strlen(s2);
// 扩展数组大小
s2 = (char *)realloc(s2, 2 * tmp_size);
if (NULL == s2)
{
perror("realloc");
exit(0);
}
printf("请继续输入字符, 输入换行符结束输入\n");
char *ch;
s2[tmp_size - 1] = ' ';
while ((ch = getchar()) != '\n')
{
s2[tmp_size++] = ch;
}
printf("结果为: %s\n", s2);
free(s2);
return 0;
}

View File

@ -1,78 +0,0 @@
// 设计函数 char (*tok(char *s, const char *delim))[100] 实现s按给定的分隔符分隔出所有的内容并返回分隔之后的二维数组指针。
// 示例:
// char s[] = "Hello,World,How,Are,You";
// const char delim[] = ",";
// char(*tokens)[100] = tok(s, delim);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char (*tok(char *s, const char *delim))[100]
{
// 计算分隔符的长度
size_t delim_len = strlen(delim);
// 计算字符串中分隔符的个数
int count = 1;
char *p = s;
while (*p != '\0')
{
if (strncmp(p, delim, delim_len) == 0)
{
count++;
p += delim_len; // 跳过分隔符
}
else
{
p++;
}
}
// 分配存储结果的二维数组
char(*tokens)[100] = malloc(count * sizeof(*tokens));
// 分隔字符串
int i = 0;
p = s;
char *token_start = p;
while (*p != '\0')
{
if (strncmp(p, delim, delim_len) == 0)
{
strncpy(tokens[i], token_start, p - token_start);
tokens[i][p - token_start] = '\0';
i++;
p += delim_len; // 跳过分隔符
token_start = p;
}
else
{
p++;
}
}
// 处理最后一个分隔符后的部分
strncpy(tokens[i], token_start, p - token_start);
tokens[i][p - token_start] = '\0';
return tokens;
}
int main()
{
char s[] = "Hello,World,How,Are,You";
const char delim[] = ",";
char(*tokens)[100] = tok(s, delim);
// 打印分隔后的结果
for (int i = 0; tokens[i][0] != '\0'; i++)
{
printf("%s\n", tokens[i]);
}
// 释放内存
free(tokens);
return 0;
}

View File

@ -1,14 +0,0 @@
// 给定一张图片的规格信息,请提取出图片的格式、宽和高。 char *s = "base64;image/png:320,480";
// 输出结果: image / png 320 480
#include <stdio.h>
#include <string.h>
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;
}

View File

@ -1,27 +0,0 @@
/*
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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
}

View File

@ -1,21 +0,0 @@
/*
int main()
{
int a='21';
const char *p = (char *) &a;
*p = 48;
printf("%s\n", p);
}
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a = '21'; // 错误1: '21' 是字符常量,不是整型常量,应该写成 21
const char *p = (char *)&a;
*p = 48; // 错误2: *p 在上一行被 const 修饰为了只读变量,无法修改
printf("%s\n", p);
}

View File

@ -1,23 +0,0 @@
// 编写一个程序,定义一个结构体表示学生的信息,包括姓名、年龄和分数。通过键盘输入学生的信息,然后输出这些信息。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

View File

@ -1,78 +0,0 @@
// 1. 2. 编写一个程序,定义一个结构体表示日期,包括年、月和日。编写一个函数,接受两个日期作为参数,并计算并返回它们之间的天数差。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

View File

@ -1,73 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

View File

@ -1,37 +0,0 @@
// 编写一个程序,定义一个结构体表示员工的信息,包括姓名、工号和工资。编写一个函数,接受一个员工结构体数组和数组长度作为参数,并计算并返回所有员工的平均工资。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

View File

@ -1,59 +0,0 @@
// 编写一个程序定义一个枚举enum表示一周的每一天包括周一至周日。编写一个函数接受一个枚举值作为参数并根据枚举值打印出相应的星期几的名称。例如传递枚举值为2时函数应该打印出 "Tuesday"。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

View File

@ -1,56 +0,0 @@
// 练习结构体数组排序   从键盘输入5个学生的信息姓名、学号、成绩,
// 存入一个结构体数组中,计算平均分,并按成绩高低排序并输出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

View File

@ -1,118 +0,0 @@
/*
GPS相关知识
$GPRMC协议格式
  $GPRMC, 024813.640, A, 3158.4608, N, 11848.3737, E, 10.05, 324.27, 150706, , , A * 50
  0$GPRMCIDRecommended Minimum Specific GPS / TRANSIT DataRMC
  1UTC时间hhmmss.sss格式(UTC时间是世界统一时间UTC + 802 : 48 : 1310 : 48 : 13)
  2A = V =
  3ddmm.mmmm0
  4NS
  5dddmm.mmmm0
  6EW西
  7Knots
  8
  9UTC日期DDMMYY格式
  10000 - 1800
  11E = W = 西
  16
strtok切割sscanf或者strncpyatoiatof
*/
#include <stdio.h>
#include <string.h>
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;
}

View File

@ -10,18 +10,18 @@ OBJ_DIR := ./objs
# CC 是编译器 # CC 是编译器
# CC = gcc # CC = gcc
# TARGET 是目标文件 # TARGET 是目标文件
TARGET = $(OBJ_DIR)/main TARGET = main
# OBJ 是目标文件依赖的文件 # OBJ 是目标文件依赖的文件
# OBJ = link.o main.o # OBJ = link.o main.o
OBJ = $(OBJ_DIR)/link.o $(OBJ_DIR)/main.o OBJ = $(OBJ_DIR)/link.o $(OBJ_DIR)/main.o
# 所有的 .o 文件都依赖于 .c 文件 # 所有的 .o 文件都依赖于 .c 文件
$(OBJ_DIR)/%.o: %.c $(OBJ_DIR)/%.o: %.c
@$(CC) -c $< -o $@ $(CC) -c $< -o $@
# 意思是: 目标文件依赖于 OBJ, 执行命令是 $(CC) $(OBJ) -o $(TARGET) # 意思是: 目标文件依赖于 OBJ, 执行命令是 $(CC) $(OBJ) -o $(TARGET)
$(TARGET): $(OBJ) $(TARGET): $(OBJ)
@$(CC) $(OBJ) -o $(TARGET) $(CC) $(OBJ) -o $(TARGET)
# # link.o 依赖于 link.c, 执行命令是 $(CC) -c link.c -o link.o # # link.o 依赖于 link.c, 执行命令是 $(CC) -c link.c -o link.o
# # 用于生成目标文件 link.o # # 用于生成目标文件 link.o