问题描述:编制一个学生籍贯信息记录簿,每个学生信息包括:学号、姓名、籍贯。实现功能: 1、创建信息链表并以磁盘文件保存 2、读取磁盘文件并显示输出所有学生的籍贯信息 3、按学号或姓名查询其籍贯 4、按籍贯查询并输出该籍贯的所有学生 5、能添加、删除和修改学生的籍贯信息 6、显示输出湖北籍和非湖北籍学生的信息并可分别存盘 知识点:文件打开,保存,结构体,搜索,链表说明:其中1-5功能是必须要实现的功能。
时间: 2023-06-03 11:05:52 浏览: 136
这个问题属于计算机科学的范畴,我可以帮助你回答这个问题。对于这个问题,你可以使用C语言来实现一个基于链表的学生籍贯信息记录簿。首先,你需要定义一个包含学号、姓名和籍贯的结构体。然后,你可以使用链表来存储所有的学生信息并将链表保存到磁盘文件中。接下来,你可以实现按学号或姓名查询学生的籍贯,按籍贯查询所有学生,并能添加、删除和修改学生信息的功能。最后,你可以编写代码来显示并分别保存湖北籍和非湖北籍学生的信息。
相关问题
题目:学生籍贯管理系统 内容:编制一个学生籍贯信息记录簿,每个学生信息包括:学号、姓名、籍贯、通信地址,以顺序存储结构设计程序实现以下功能。 1、输入学生信息并以磁盘文件保存; 2、读取磁盘文件并显示输出所有学生的籍贯信息; 3、按学号或姓名查询其籍贯; 4、按籍贯查询并输出该籍贯的所有学生; 5、能添加、删除(按学号)和修改(按学号)学生的籍贯信息; 6、显示输出上海籍和非上海籍学生的信息并可分别存盘; 7、浏览所有学生的籍贯信息; 8、按学号进行排序。
以下是一个基于C语言的学生籍贯管理系统的简单实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 学生信息结构体
typedef struct {
char id[10]; // 学号
char name[20]; // 姓名
char hometown[20]; // 籍贯
char address[50]; // 通信地址
} Student;
// 存储所有学生信息的数组
Student students[100];
// 学生数量
int studentCount = 0;
// 从文件中读取学生信息
void readStudentsFromFile(char *fileName) {
FILE *fp = fopen(fileName, "r");
if (fp == NULL) {
printf("File %s not found.\n", fileName);
return;
}
// 逐行读取学生信息
while (feof(fp) == 0) {
Student student;
fscanf(fp, "%s %s %s %s", student.id, student.name, student.hometown, student.address);
students[studentCount] = student;
studentCount++;
}
fclose(fp);
}
// 将所有学生信息保存到文件中
void saveStudentsToFile(char *fileName) {
FILE *fp = fopen(fileName, "w");
for (int i = 0; i < studentCount; i++) {
fprintf(fp, "%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
fclose(fp);
}
// 添加学生
void addStudent() {
Student student;
printf("Enter student's id: ");
scanf("%s", student.id);
printf("Enter student's name: ");
scanf("%s", student.name);
printf("Enter student's hometown: ");
scanf("%s", student.hometown);
printf("Enter student's address: ");
scanf("%s", student.address);
students[studentCount] = student;
studentCount++;
}
// 删除学生
void deleteStudent() {
char id[10];
printf("Enter student's id to delete: ");
scanf("%s", id);
int index = -1;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].id, id) == 0) {
index = i;
break;
}
}
if (index != -1) {
for (int i = index; i < studentCount; i++) {
students[i] = students[i + 1];
}
studentCount--;
printf("Student with id %s deleted.\n", id);
} else {
printf("Student with id %s not found.\n", id);
}
}
// 修改学生
void modifyStudent() {
char id[10];
printf("Enter student's id to modify: ");
scanf("%s", id);
int index = -1;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].id, id) == 0) {
index = i;
break;
}
}
if (index != -1) {
printf("Enter new hometown for student with id %s: ", id);
scanf("%s", students[index].hometown);
printf("Enter new address for student with id %s: ", id);
scanf("%s", students[index].address);
printf("Student with id %s modified.\n", id);
} else {
printf("Student with id %s not found.\n", id);
}
}
// 按学号或姓名查询学生
void searchStudent() {
int option;
printf("Search by (1) id or (2) name? ");
scanf("%d", &option);
char keyword[20];
printf("Enter keyword: ");
scanf("%s", keyword);
for (int i = 0; i < studentCount; i++) {
if (option == 1 && strcmp(students[i].id, keyword) == 0) {
printf("%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
} else if (option == 2 && strcmp(students[i].name, keyword) == 0) {
printf("%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
}
}
// 按籍贯查询学生
void searchByHometown() {
char hometown[20];
printf("Enter hometown: ");
scanf("%s", hometown);
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].hometown, hometown) == 0) {
printf("%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
}
}
// 显示所有学生信息
void displayAllStudents() {
for (int i = 0; i < studentCount; i++) {
printf("%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
}
// 显示上海籍和非上海籍学生信息
void displayShanghaiStudents() {
FILE *shanghaiFile = fopen("shanghai.txt", "w");
FILE *nonShanghaiFile = fopen("non_shanghai.txt", "w");
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].hometown, "Shanghai") == 0) {
fprintf(shanghaiFile, "%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
} else {
fprintf(nonShanghaiFile, "%s %s %s %s\n", students[i].id, students[i].name, students[i].hometown, students[i].address);
}
}
fclose(shanghaiFile);
fclose(nonShanghaiFile);
}
// 按学号排序
void sortStudentsById() {
for (int i = 0; i < studentCount - 1; i++) {
for (int j = 0; j < studentCount - i - 1; j++) {
if (strcmp(students[j].id, students[j + 1].id) > 0) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
int main() {
char fileName[50] = "students.txt";
int option = 0;
// 读取学生信息文件
readStudentsFromFile(fileName);
while (option != 9) {
printf("\nPlease choose an option:\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student\n");
printf("4. Search student\n");
printf("5. Search by hometown\n");
printf("6. Display all students\n");
printf("7. Display Shanghai and non-Shanghai students\n");
printf("8. Sort students by id\n");
printf("9. Exit\n");
scanf("%d", &option);
switch (option) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
searchStudent();
break;
case 5:
searchByHometown();
break;
case 6:
displayAllStudents();
break;
case 7:
displayShanghaiStudents();
break;
case 8:
sortStudentsById();
printf("Students sorted by id.\n");
break;
case 9:
printf("Saving students to file %s...\n", fileName);
saveStudentsToFile(fileName);
printf("Students saved to file.\n");
break;
default:
printf("Invalid option.\n");
break;
}
}
return 0;
}
```
该程序使用了一个结构体来存储学生信息,并通过一个数组来存储所有学生信息。程序实现了输入学生信息、读取磁盘文件、查询学生籍贯、添加、删除和修改学生信息、分别显示上海籍和非上海籍学生信息、浏览所有学生信息、按学号排序等功能。
用代码实现学生籍贯信息记录簿(使用数据库) 编制一个学生籍贯(到省一级就行,不用到市)信息记录簿,每个学生信息包括:学号、姓名、籍贯。功能要求: (1)按学号或姓名查询其籍贯; (2)按籍贯查询并输出该籍贯的所有学生; (3)能添加、删除和修改学生的籍贯信息; (4)籍贯从JComboBox中选择
好的,下面是使用Java语言和MySQL数据库实现学生籍贯信息记录簿的代码示例:
首先,我们需要创建一个名为“student”的MySQL数据库,其中包括一个名为“student_info”的表,该表包含三个字段:id、name、hometown。
创建代码如下:
```sql
CREATE DATABASE student;
USE student;
CREATE TABLE student_info (
id INT PRIMARY KEY,
name VARCHAR(20),
hometown VARCHAR(20)
);
```
接下来,我们编写Java程序来连接MySQL数据库,并实现查询、添加、删除和修改学生籍贯信息的功能。
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentInfo extends JFrame implements ActionListener {
private JLabel label1, label2, label3;
private JTextField tf1, tf2;
private JComboBox<String> hometownBox;
private JButton btn1, btn2, btn3, btn4;
private JPanel panel1, panel2;
private static Connection conn;
private static Statement stmt;
public StudentInfo() {
super("学生籍贯信息记录簿");
label1 = new JLabel("学号:");
label2 = new JLabel("姓名:");
label3 = new JLabel("籍贯:");
tf1 = new JTextField(10);
tf2 = new JTextField(10);
String[] hometowns = {"北京", "上海", "广东", "江苏", "浙江", "山东", "河南", "湖北", "湖南", "四川", "重庆", "陕西", "其他"};
hometownBox = new JComboBox<>(hometowns);
btn1 = new JButton("查询");
btn2 = new JButton("添加");
btn3 = new JButton("删除");
btn4 = new JButton("修改");
panel1 = new JPanel(new GridLayout(3, 2));
panel1.add(label1);
panel1.add(tf1);
panel1.add(label2);
panel1.add(tf2);
panel1.add(label3);
panel1.add(hometownBox);
panel2 = new JPanel(new GridLayout(1, 4));
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
panel2.add(btn4);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
setSize(400, 150);
setVisible(true);
connectToDatabase();
}
private void connectToDatabase() {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
String user = "root";
String password = "123456";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
String id = tf1.getText();
String name = tf2.getText();
String hometown = (String) hometownBox.getSelectedItem();
String sql = "";
if (id.equals("")) {
sql = "SELECT * FROM student_info WHERE name='" + name + "'";
} else if (name.equals("")) {
sql = "SELECT * FROM student_info WHERE id=" + id;
} else {
JOptionPane.showMessageDialog(null, "请输入学号或姓名!");
return;
}
try {
ResultSet rs = stmt.executeQuery(sql);
String result = "";
while (rs.next()) {
int studentId = rs.getInt("id");
String studentName = rs.getString("name");
String studentHometown = rs.getString("hometown");
result += "学号:" + studentId + " 姓名:" + studentName + " 籍贯:" + studentHometown + "\n";
}
if (!result.equals("")) {
JOptionPane.showMessageDialog(null, result);
} else {
JOptionPane.showMessageDialog(null, "没有找到相关信息!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == btn2) {
String id = tf1.getText();
String name = tf2.getText();
String hometown = (String) hometownBox.getSelectedItem();
if (id.equals("") || name.equals("")) {
JOptionPane.showMessageDialog(null, "请输入学号和姓名!");
return;
}
String sql = "INSERT INTO student_info VALUES (" + id + ",'" + name + "','" + hometown + "')";
try {
int result = stmt.executeUpdate(sql);
if (result > 0) {
JOptionPane.showMessageDialog(null, "添加成功!");
} else {
JOptionPane.showMessageDialog(null, "添加失败!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == btn3) {
String id = tf1.getText();
String name = tf2.getText();
if (id.equals("")) {
JOptionPane.showMessageDialog(null, "请输入学号!");
return;
}
String sql = "DELETE FROM student_info WHERE id=" + id;
try {
int result = stmt.executeUpdate(sql);
if (result > 0) {
JOptionPane.showMessageDialog(null, "删除成功!");
} else {
JOptionPane.showMessageDialog(null, "删除失败!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == btn4) {
String id = tf1.getText();
String name = tf2.getText();
String hometown = (String) hometownBox.getSelectedItem();
if (id.equals("")) {
JOptionPane.showMessageDialog(null, "请输入学号!");
return;
}
String sql = "UPDATE student_info SET hometown='" + hometown + "' WHERE id=" + id;
try {
int result = stmt.executeUpdate(sql);
if (result > 0) {
JOptionPane.showMessageDialog(null, "修改成功!");
} else {
JOptionPane.showMessageDialog(null, "修改失败!");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new StudentInfo();
}
}
```
上述示例代码是一个基本的学生籍贯信息记录簿程序,其中包括连接MySQL数据库、查询、添加、删除和修改学生籍贯信息的功能。用户可通过输入学号或姓名查询学生的籍贯信息,也可通过选择籍贯来添加、删除和修改学生的籍贯信息。
阅读全文