系统还包括两个角色-管理员和学生。管理员登录有以下功能:1,活动信息录入功能-输入。2,增加活动信息。3,活动信息浏览-输出。4,按照活动名称查询。5,活动信息删除。6.随机选择学生参加。6,活动信息内容修改。7数据保存和读取。学生登录:1,浏览活动信息。2,报名参加活动内容。3查询参加活动记录
https://blog.csdn.net/weixin_64397656/article/details/128254415
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20
struct activity {
char name[20];
char date[20];
char place[20];
char description[100];
int quota;
int num;
};
struct student {
char name[20];
char id[20];
char password[20];
char activity[20];
};
void admin_menu(struct activity act[], int *n);
void student_menu(struct student stu[], int *m, struct activity act[], int n);
void add_activity(struct activity act[], int *n);
void view_activity(struct activity act[], int n);
void search_activity(struct activity act[], int n);
void delete_activity(struct activity act[], int *n);
void modify_activity(struct activity act[], int n);
void save_data(struct activity act[], int n, struct student stu[], int m);
void read_data(struct activity act[], int *n, struct student stu[], int *m);
void select_student(struct activity act[], int n);
int main(void) {
struct activity act[MAX];
struct student stu[MAX];
int n = 0, m = 0, choice;
read_data(act, &n, stu, &m);
while (1) {
printf("Welcome to Activity Management Platform System!\n");
printf("1. Admin Login\n");
printf("2. Student Login\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
admin_menu(act, &n);
break;
case 2:
student_menu(stu, &m, act, n);
break;
case 3:
save_data(act, n, stu, m);
printf("Thank you for using the system!\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
void admin_menu(struct activity act[], int *n) {
int choice;
while (1) {
printf("Admin Menu:\n");
printf("1. Add Activity\n");
printf("2. View Activity\n");
printf("3. Search Activity\n");
printf("4. Delete Activity\n");
printf("5. Modify Activity\n");
printf("6. Select Student\n");
printf("7. Return to Main Menu\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_activity(act, n);
break;
case 2:
view_activity(act, *n);
break;
case 3:
search_activity(act, *n);
break;
case 4:
delete_activity(act, n);
break;
case 5:
modify_activity(act, *n);
break;
case 6:
select_student(act, *n);
break;
case 7:
return;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
void student_menu(struct student stu[], int *m, struct activity act[], int n) {
int choice, i;
while (1) {
printf("Student Menu:\n");
printf("1. Browse Activity\n");
printf("2. Sign up for Activity\n");
printf("3. Check Activity Record\n");
printf("4. Return to Main Menu\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
view_activity(act, n);
break;
case 2:
printf("Enter your student ID: ");
char id[20];
scanf("%s", id);
int index = -1;
for (i = 0; i < *m; i++) {
if (strcmp(id, stu[i].id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("No such student!\n");
break;
}
printf("Enter the activity name you want to sign up: ");
char name[20];
scanf("%s", name);
for (i = 0; i < n; i++) {
if (strcmp(name, act[i].name) == 0) {
if (act[i].num >= act[i].quota) {
printf("Sorry, the activity has reached the quota.\n");
return;
}
act[i].num++;
strcpy(stu[index].activity, act[i].name);
printf("Sign up for activity successfully!\n");
return;
}
}
printf("No such activity!\n");
break;
case 3:
printf("Enter your student ID: ");
scanf("%s", id);
for (i = 0; i < *m; i++) {
if (strcmp(id, stu[i].id) == 0) {
printf("Name: %s\n", stu[i].name);
printf("Activity: %s\n", stu[i].activity);
return;
}
}
printf("No such student!\n");
break;
case 4:
return;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
void add_activity(struct activity act[], int *n) {
printf("Enter the activity name: ");
scanf("%s", act[*n].name);
printf("Enter the activity date (DD/MM/YYYY): ");
scanf("%s", act[*n].date);
printf("Enter the activity place: ");
scanf("%s", act[*n].place);
printf("Enter the activity description: ");
scanf("%s", act[*n].description);
printf("Enter the activity quota: ");
scanf("%d", &act[*n].quota);
act[*n].num = 0;
(*n)++;
printf("Activity added successfully!\n");
}
void view_activity(struct activity act[], int n) {
int i;
printf("Activity List:\n");
for (i = 0; i < n; i++) {
printf("Name: %s\n", act[i].name);
printf("Date: %s\n", act[i].date);
printf("Place: %s\n", act[i].place);
printf("Description: %s\n", act[i].description);
printf("Quota: %d\n", act[i].quota);
printf("Number of participants: %d\n\n", act[i].num);
}
}
void search_activity(struct activity act[], int n) {
char name[20];
int i;
printf("Enter the activity name you want to search: ");
scanf("%s", name);
for (i = 0; i < n; i++) {
if (strcmp(name, act[i].name) == 0) {
printf("Name: %s\n", act[i].name);
printf("Date: %s\n", act[i].date);
printf("Place: %s\n", act[i].place);
printf("Description: %s\n", act[i].description);
printf("Quota: %d\n", act[i].quota);
printf("Number of participants: %d\n\n", act[i].num);
return;
}
}
printf("No such activity!\n");
}
void delete_activity(struct activity act[], int *n) {
char name[20];
int i, j;
printf("Enter the activity name you want to delete: ");
scanf("%s", name);
for (i = 0; i < *n; i++) {
if (strcmp(name, act[i].name) == 0) {
for (j = i; j < *n - 1; j++) {
act[j] = act[j + 1];
}
(*n)--;
printf("Activity deleted successfully!\n");
return;
}
}
printf("No such activity!\n");
}
void modify_activity(struct activity act[], int n) {
char name[20];
int i;
printf("Enter the activity name you want to modify: ");
scanf("%s", name);
for (i = 0; i < n; i++) {
if (strcmp(name, act[i].name) == 0) {
printf("Enter the new activity name: ");
scanf("%s", act[i].name);
printf("Enter the new activity date (DD/MM/YYYY): ");
scanf("%s", act[i].date);
printf("Enter the new activity place: ");
scanf("%s", act[i].place);
printf("Enter the new activity description: ");
scanf("%s", act[i].description);
printf("Enter the new activity quota: ");
scanf("%d", &act[i].quota);
printf("Activity modified successfully!\n");
return;
}
}
printf("No such activity!\n");
}
void save_data(struct activity act[], int n, struct student stu[], int m) {
FILE *fp;
int i;
fp = fopen("data.txt", "w");
fprintf(fp, "%d\n", n);
for (i = 0; i < n; i++) {
fprintf(fp, "%s %s %s %s %d %d\n", act[i].name, act[i].date, act[i].place, act[i].description, act[i].quota, act[i].num);
}
fprintf(fp, "%d\n", m);
for (i = 0; i < m; i++) {
fprintf(fp, "%s %s %s %s\n", stu[i].name, stu[i].id, stu[i].password, stu[i].activity);
}
fclose(fp);
}
void read_data(struct activity act[], int *n, struct student stu[], int *m) {
FILE *fp;
int i;
fp = fopen("data.txt", "r");
if (fp == NULL){
printf("No data file found!\n");
return;
}
fscanf(fp, "%d", n);
for (i = 0; i < *n; i++) {
fscanf(fp, "%s %s %s %s %d %d", act[i].name, act[i].date, act[i].place, act[i].description, &act[i].quota, &act[i].num);
}
fscanf(fp, "%d", m);
for (i = 0; i < *m; i++) {
fscanf(fp, "%s %s %s %s", stu[i].name, stu[i].id, stu[i].password, stu[i].activity);
}
fclose(fp);
}
void select_student(struct activity act[], int n) {
srand(time(NULL));
int idx = rand() % n;
printf("%s\n", act[idx].name);
}
不知道你这个问题是否已经解决, 如果还没有解决的话: