帮我写一个查看学生申请并完成什么活动的C语言代码
时间: 2023-04-10 18:00:42 浏览: 80
好的,以下是一个简单的查看学生申请并完成活动的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_ACTIVITIES 10
typedef struct {
char name[50];
int age;
char gender[10];
char activity[MAX_ACTIVITIES][50];
int num_activities;
} Student;
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
int i, j;
// 添加学生
printf("请输入学生信息:\n");
while (num_students < MAX_STUDENTS) {
Student s;
printf("姓名:");
scanf("%s", s.name);
printf("年龄:");
scanf("%d", &s.age);
printf("性别:");
scanf("%s", s.gender);
printf("申请的活动(最多%d个,以空格分隔):", MAX_ACTIVITIES);
s.num_activities = 0;
while (s.num_activities < MAX_ACTIVITIES) {
scanf("%s", s.activity[s.num_activities]);
if (strcmp(s.activity[s.num_activities], "") == 0) {
break;
}
s.num_activities++;
}
students[num_students] = s;
num_students++;
printf("是否继续添加学生?(y/n)");
char c;
scanf(" %c", &c);
if (c == 'n' || c == 'N') {
break;
}
}
// 查看学生信息
printf("所有学生信息如下:\n");
for (i = 0; i < num_students; i++) {
printf("姓名:%s\n", students[i].name);
printf("年龄:%d\n", students[i].age);
printf("性别:%s\n", students[i].gender);
printf("申请的活动:");
for (j = 0; j < students[i].num_activities; j++) {
printf("%s ", students[i].activity[j]);
}
printf("\n");
}
// 查看某个活动的申请情况
printf("请输入要查看的活动名称:");
char activity_name[50];
scanf("%s", activity_name);
printf("申请了 %s 活动的学生有:\n", activity_name);
for (i = 0; i < num_students; i++) {
for (j = 0; j < students[i].num_activities; j++) {
if (strcmp(students[i].activity[j], activity_name) == 0) {
printf("%s\n", students[i].name);
break;
}
}
}
return 0;
}
```
希望这个代码能够帮到你!
阅读全文