将该代码转换成C语言#include<stdio.h> #include<string.h> #include<stdbool.h> struct character { char* name; bool flag; }; void ready(char a[],struct character b[]); int main() { struct character Isready[8] = { {"Captain", false}, {"Doctor", false}, {"Cook", false}, {"Engineer", false}, {"Hunter", false}, {"Gunner", false}, {"Navigator", false}, {"Priest", false} }; char boss[10], str[201]; int n ; bool ans = true; scanf("%s", boss); ready(boss, Isready); scanf("%d", &n); getchar(); for (int i = 0; i < n; i++) { gets(str); ready(str, Isready); } for (int i = 0; i < 8; i++) { if (!Isready[i].flag) { printf("%s\n", Isready[i].name); ans = false; } } if (ans) { printf("Ready"); } return 0; } void ready(char a[], struct character b[]) { for (int i = 0; i < 8; i++) { if (strncmp(a,b[i].name,strlen(b[i].name)) == 0) { b[i].flag = true; break; } } }
时间: 2024-01-28 18:03:09 浏览: 64
SeqList.h #include <stdio.h> //包含标准输入输出流的头文件 #include <assert.h
以下是将该代码转换成C语言的过程:
```
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
struct character {
char* name;
bool flag;
};
void ready(char a[], struct character b[]);
int main() {
struct character Isready[8] = {
{"Captain", false},
{"Doctor", false},
{"Cook", false},
{"Engineer", false},
{"Hunter", false},
{"Gunner", false},
{"Navigator", false},
{"Priest", false}
};
char boss[10], str[201];
int n;
bool ans = true;
scanf("%s", boss);
ready(boss, Isready);
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++) {
fgets(str, 201, stdin);
ready(str, Isready);
}
for (int i = 0; i < 8; i++) {
if (!Isready[i].flag) {
printf("%s\n", Isready[i].name);
ans = false;
}
}
if (ans) {
printf("Ready");
}
return 0;
}
void ready(char a[], struct character b[]) {
for (int i = 0; i < 8; i++) {
if (strncmp(a, b[i].name, strlen(b[i].name)) == 0) {
b[i].flag = true;
break;
}
}
}
```
注意到以下几点变化:
1. 在C语言中,结构体的定义需要加上struct关键字;
2. 在C语言中,字符串需要使用字符数组来表示,因此需要将char* name改为char name[10](根据实际需要修改数组长度),并在结构体中初始化;
3. 在C语言中,输入字符串需要使用fgets函数,而不是gets函数(因为gets存在安全问题);
4. 在C语言中,字符串比较需要使用strncmp函数,而不是==运算符;
5. 在C语言中,函数需要先声明后使用,因此需要在main函数前面声明ready函数。
阅读全文