void fresh_all_light(void){ int i; static char all_slot_color_cmp[50] = {1,2,3,4,5,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0}; if(memcmp(all_slot_color_cmp,all_slot_color,50) != 0){ fresh_all_io(); memcpy(all_slot_color_cmp,all_slot_color,50); } }
时间: 2023-09-30 21:08:49 浏览: 38
这段代码是一个函数 `fresh_all_light`,它的作用是刷新所有的灯光。让我们逐步分析这段代码:
1. 定义了一个整型变量 `i`,用于循环计数。
2. 定义了一个静态字符数组 `all_slot_color_cmp`,长度为50。数组中的元素用于比较灯光的颜色。
3. 使用了 `memcpy` 函数来将 `all_slot_color` 数组的值复制到 `all_slot_color_cmp` 数组中。
4. 使用了 `memcmp` 函数来比较 `all_slot_color_cmp` 和 `all_slot_color` 两个数组的值是否相等。
5. 如果两个数组的值不相等,则调用 `fresh_all_io` 函数刷新所有的输入/输出。
6. 最后,将 `all_slot_color` 数组的值复制到 `all_slot_color_cmp` 数组中,以便下一次比较。
总体来说,这段代码的作用是检测灯光的颜色是否有变化,如果有变化则刷新输入/输出。
相关问题
函数指针数组指针如何作为参数写入一个函数内部,比如函数指针数组为int(*Cmp[5])(Con*,const char*,int) = {Cmp_name,Cmp_sex ,Cmp_age,Cmp_tel,Cmp_adress }; ,第一个问题将这个函数指针数组作为该参数如何存放在一个函数内,比如这个函数为void Pp_Delask(),第二个问题,在这个函数内部想调用函数指针数组中的一个函数如何表达这个函数
答:将函数指针数组作为参数存放在函数内部可以使用函数参数传递的方式,比如在函数Pp_Delask()中声明一个参数int(*Cmp[5])(Con*,const char*,int),在调用函数时将函数指针数组传入该参数即可。调用函数指针数组中的一个函数可以使用指针的方式,比如在函数Pp_Delask()中定义一个函数指针int(*p)(Con*,const char*,int),将它指向函数指针数组中的一个函数,比如(*p)=Cmp_name,然后直接调用(*p)(Con*,const char*,int)即可。
用c编写程序开始时,自动从文件读入信息到结构体数组,并提取每个公民的出生年月放入结构体中,同时进行身份证校验位是否正确,判断信息也放入结构体中。补全主函数,#include <stdio.h> #include <string.h> #define N 100 struct birth { int year; int month; int day; }; struct person { char ID[19]; char name[20]; struct birth birthday; int flag;//校验位是否正确, 1代表正确,0代表错误 }; int read(struct person p[]); int checkID(char *ID); struct birth get_birth(char *ID); void get_all_person(struct person p[], int n); void display_person(struct person p[], int n, int flag); int birth_cmp(struct birth birth1, struct birth birth2); void birth_sort(struct person p[], int n); int search(struct person p[], int n, char *name); void save(struct person p[], int n); int prompt(void); int main(void) { struct person p[N]; int cmd, n; while (1) { cmd = prompt(); } return 0; }
补全的代码如下:
```
#include <stdio.h>
#include <string.h>
#define N 100
struct birth {
int year;
int month;
int day;
};
struct person {
char ID[19];
char name[20];
struct birth birthday;
int flag; // 校验位是否正确, 1代表正确,0代表错误
};
int read(struct person p[]);
int checkID(char *ID);
struct birth get_birth(char *ID);
void get_all_person(struct person p[], int n);
void display_person(struct person p[], int n, int flag);
int birth_cmp(struct birth birth1, struct birth birth2);
void birth_sort(struct person p[], int n);
int search(struct person p[], int n, char *name);
void save(struct person p[], int n);
int prompt(void);
int main(void) {
struct person p[N];
int cmd, n;
while (1) {
cmd = prompt();
switch (cmd) {
case 1:
n = read(p);
break;
case 2:
get_all_person(p, n);
break;
case 3:
birth_sort(p, n);
break;
case 4:
display_person(p, n, 1);
break;
case 5:
display_person(p, n, 0);
break;
case 6:
save(p, n);
break;
case 7:
return 0;
default:
printf("Invalid command!\n");
break;
}
}
return 0;
}
int read(struct person p[]) {
FILE *fp;
char filename[20];
int i = 0;
printf("Enter the file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (!fp) {
printf("Failed to open file!\n");
return 0;
}
while (fscanf(fp, "%s %s", p[i].ID, p[i].name) != EOF) {
p[i].birthday = get_birth(p[i].ID);
p[i].flag = checkID(p[i].ID);
i++;
}
fclose(fp);
printf("Read file successfully!\n");
return i;
}
int checkID(char *ID) {
int i, sum = 0, weight;
int weight_list[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
char check_list[] = "10X98765432";
for (i = 0; i < 17; i++) {
sum += (ID[i] - '0') * weight_list[i];
}
weight = sum % 11;
if (ID[17] == check_list[weight]) {
return 1;
} else {
return 0;
}
}
struct birth get_birth(char *ID) {
struct birth birthday;
birthday.year = (ID[6] - '0') * 1000 + (ID[7] - '0') * 100 + (ID[8] - '0') * 10 + (ID[9] - '0');
birthday.month = (ID[10] - '0') * 10 + (ID[11] - '0');
birthday.day = (ID[12] - '0') * 10 + (ID[13] - '0');
return birthday;
}
void get_all_person(struct person p[], int n) {
int i;
printf("All persons:\n");
for (i = 0; i < n; i++) {
printf("%s %s %d-%02d-%02d %s\n", p[i].ID, p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day, p[i].flag ? "correct" : "incorrect");
}
}
void display_person(struct person p[], int n, int flag) {
int i;
printf("Persons with %s ID:\n", flag ? "correct" : "incorrect");
for (i = 0; i < n; i++) {
if (p[i].flag == flag) {
printf("%s %s %d-%02d-%02d\n", p[i].ID, p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day);
}
}
}
int birth_cmp(struct birth birth1, struct birth birth2) {
if (birth1.year != birth2.year) {
return birth1.year - birth2.year;
} else if (birth1.month != birth2.month) {
return birth1.month - birth2.month;
} else {
return birth1.day - birth2.day;
}
}
void birth_sort(struct person p[], int n) {
int i, j;
struct person temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (birth_cmp(p[i].birthday, p[j].birthday) > 0) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
printf("Sort by birthday successfully!\n");
}
int search(struct person p[], int n, char *name) {
int i;
for (i = 0; i < n; i++) {
if (strcmp(p[i].name, name) == 0) {
return i;
}
}
return -1;
}
void save(struct person p[], int n) {
FILE *fp;
char filename[20];
int i;
printf("Enter the file name: ");
scanf("%s", filename);
fp = fopen(filename, "w");
if (!fp) {
printf("Failed to create file!\n");
return;
}
for (i = 0; i < n; i++) {
fprintf(fp, "%s %s %d-%02d-%02d %s\n", p[i].ID, p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day, p[i].flag ? "correct" : "incorrect");
}
fclose(fp);
printf("Save file successfully!\n");
}
int prompt(void) {
int cmd;
printf("\nMenu:\n");
printf("1. Read from file\n");
printf("2. Display all persons\n");
printf("3. Sort by birthday\n");
printf("4. Display persons with correct ID\n");
printf("5. Display persons with incorrect ID\n");
printf("6. Save to file\n");
printf("7. Quit\n");
printf("Enter a command: ");
scanf("%d", &cmd);
return cmd;
}
```
阅读全文