<4>编写子函数,查找班级为“软件工程一班”的所有学生联系人的信息。在主函数中定义一个顺序表LB,并将查找出的班级为“软件工程一班”的所有学生联系人的信息存入该顺序表LB中,在主函数中调用输出函数输出查找结果。 拓展项目: <5>编写程序:要求定义一个子函数,功能为将顺序表中的学生联系人信息存入数据文件中,数据文件名可以从键盘输入。在主函数中调用该函数完成将顺序表LB中的信息存储成文件的功能。 <6>编写程序:在上一程序的基础上,编写子函数,将已经读入的学生信息以班级为单位分别写入不同的文件中(文件名可按班级名的方式命名)。
时间: 2023-05-11 17:01:54 浏览: 99
好的,以下是我的回答:
<4>编写子函数,查找班级为“软件工程一班”的所有学生联系人的信息。在主函数中定义一个顺序表LB,并将查找出的班级为“软件工程一班”的所有学生联系人的信息存入该顺序表LB中,在主函数中调用输出函数输出查找结果。
代码如下:
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 20
#define MAX_EMAIL_LEN 50
#define MAX_STUDENT_NUM 100
typedef struct {
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
char email[MAX_EMAIL_LEN];
char class[MAX_NAME_LEN];
} Student;
void search_students(Student students[], int n, char class_name[], Student result[], int *result_num) {
int i;
*result_num = 0;
for (i = 0; i < n; i++) {
if (strcmp(students[i].class, class_name) == 0) {
result[*result_num] = students[i];
(*result_num)++;
}
}
}
void output_students(Student students[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("Name: %s\nPhone: %s\nEmail: %s\nClass: %s\n\n", students[i].name, students[i].phone, students[i].email, students[i].class);
}
}
int main() {
Student students[MAX_STUDENT_NUM] = {
{"Tom", "123456", "tom@example.com", "Software Engineering 1"},
{"Jerry", "654321", "jerry@example.com", "Software Engineering 2"},
{"Alice", "111111", "alice@example.com", "Software Engineering 1"},
{"Bob", "222222", "bob@example.com", "Computer Science 1"},
{"Charlie", "333333", "charlie@example.com", "Computer Science 2"}
};
int n = 5;
Student result[MAX_STUDENT_NUM];
int result_num;
char class_name[MAX_NAME_LEN] = "Software Engineering 1";
search_students(students, n, class_name, result, &result_num);
output_students(result, result_num);
return 0;
}
<5>编写程序:要求定义一个子函数,功能为将顺序表中的学生联系人信息存入数据文件中,数据文件名可以从键盘输入。在主函数中调用该函数完成将顺序表LB中的信息存储成文件的功能。
代码如下:
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 20
#define MAX_EMAIL_LEN 50
#define MAX_STUDENT_NUM 100
typedef struct {
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
char email[MAX_EMAIL_LEN];
char class[MAX_NAME_LEN];
} Student;
void save_students(Student students[], int n, char filename[]) {
FILE *fp = fopen(filename, "w");
int i;
for (i = 0; i < n; i++) {
fprintf(fp, "%s,%s,%s,%s\n", students[i].name, students[i].phone, students[i].email, students[i].class);
}
fclose(fp);
}
int main() {
Student students[MAX_STUDENT_NUM] = {
{"Tom", "123456", "tom@example.com", "Software Engineering 1"},
{"Jerry", "654321", "jerry@example.com", "Software Engineering 2"},
{"Alice", "111111", "alice@example.com", "Software Engineering 1"},
{"Bob", "222222", "bob@example.com", "Computer Science 1"},
{"Charlie", "333333", "charlie@example.com", "Computer Science 2"}
};
int n = 5;
char filename[MAX_NAME_LEN];
printf("Please enter the filename to save the students' information: ");
scanf("%s", filename);
save_students(students, n, filename);
return 0;
}
<6>编写程序:在上一程序的基础上,编写子函数,将已经读入的学生信息以班级为单位分别写入不同的文件中(文件名可按班级名的方式命名)。
代码如下:
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 20
#define MAX_EMAIL_LEN 50
#define MAX_STUDENT_NUM 100
typedef struct {
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
char email[MAX_EMAIL_LEN];
char class[MAX_NAME_LEN];
} Student;
void save_students(Student students[], int n, char filename[]) {
FILE *fp = fopen(filename, "w");
int i;
for (i = 0; i < n; i++) {
fprintf(fp, "%s,%s,%s,%s\n", students[i].name, students[i].phone, students[i].email, students[i].class);
}
fclose(fp);
}
void save_students_by_class(Student students[], int n, char class_name[]) {
int i;
char filename[MAX_NAME_LEN];
sprintf(filename, "%s.txt", class_name);
Student result[MAX_STUDENT_NUM];
int result_num;
search_students(students, n, class_name, result, &result_num);
save_students(result, result_num, filename);
}
int main() {
Student students[MAX_STUDENT_NUM] = {
{"Tom", "123456", "tom@example.com", "Software Engineering 1"},
{"Jerry", "654321", "jerry@example.com", "Software Engineering 2"},
{"Alice", "111111", "alice@example.com", "Software Engineering 1"},
{"Bob", "222222", "bob@example.com", "Computer Science 1"},
{"Charlie", "333333", "charlie@example.com", "Computer Science 2"}
};
int n = 5;
char filename[MAX_NAME_LEN];
printf("Please enter the filename to save the students' information: ");
scanf("%s", filename);
save_students(students, n, filename);
int i;
for (i = 0; i < n; i++) {
save_students_by_class(students, n, students[i].class);
}
return 0;
}
阅读全文