matlab进行学生成绩系统管理用结构体与类能够实现学生的增减修改成绩修改删除增加的代码在命令行窗口进行成绩和学生选择
以下是一个基本的学生成绩系统管理的matlab代码,使用结构体和类:
% 定义一个学生类 classdef Student properties name % 姓名 id % 学号 scores % 成绩,一个结构体,包括语文、数学、英语三个科目的成绩 end methods function obj = Student(name, id, scores) obj.name = name; obj.id = id; obj.scores = scores; end end end
% 定义一个学生管理类 classdef StudentManager properties students % 学生列表,一个结构体数组 end methods function obj = StudentManager() obj.students = struct([]); end
function addStudent(obj, name, id, scores)
% 增加学生
s = Student(name, id, scores);
obj.students(end+1) = struct('name', s.name, 'id', s.id, 'scores', s.scores);
end
function modifyStudent(obj, id, name, scores)
% 修改学生信息
for i = 1:length(obj.students)
if obj.students(i).id == id
if nargin > 2
obj.students(i).name = name;
end
if nargin > 3
obj.students(i).scores = scores;
end
end
end
end
function deleteStudent(obj, id)
% 删除学生
for i = 1:length(obj.students)
if obj.students(i).id == id
obj.students(i) = [];
break;
end
end
end
function displayStudents(obj)
% 显示学生列表
fprintf('学生列表:\n');
for i = 1:length(obj.students)
fprintf('姓名:%s,学号:%d\n', obj.students(i).name, obj.students(i).id);
end
end
function displayScores(obj, id)
% 显示学生成绩
for i = 1:length(obj.students)
if obj.students(i).id == id
fprintf('%s的成绩:\n', obj.students(i).name);
fprintf('语文:%d,数学:%d,英语:%d\n', obj.students(i).scores.chinese, obj.students(i).scores.math, obj.students(i).scores.english);
break;
end
end
end
function modifyScores(obj, id, subject, score)
% 修改学生成绩
for i = 1:length(obj.students)
if obj.students(i).id == id
switch subject
case '语文'
obj.students(i).scores.chinese = score;
case '数学'
obj.students(i).scores.math = score;
case '英语'
obj.students(i).scores.english = score;
end
break;
end
end
end
function addScores(obj, id, subject, score)
% 增加学生成绩
for i = 1:length(obj.students)
if obj.students(i).id == id
switch subject
case '语文'
obj.students(i).scores.chinese = obj.students(i).scores.chinese + score;
case '数学'
obj.students(i).scores.math = obj.students(i).scores.math + score;
case '英语'
obj.students(i).scores.english = obj.students(i).scores.english + score;
end
break;
end
end
end
end
end
% 测试代码 manager = StudentManager();
% 增加学生 manager.addStudent('张三', 1001, struct('chinese', 80, 'math', 90, 'english', 70)); manager.addStudent('李四', 1002, struct('chinese', 90, 'math', 85, 'english', 80));
% 显示学生列表 manager.displayStudents();
% 修改学生信息 manager.modifyStudent(1001, '王五', struct('chinese', 85, 'math', 92, 'english', 75));
% 显示学生成绩 manager.displayScores(1001);
% 修改学生成绩 manager.modifyScores(1001, '英语', 80);
% 增加学生成绩 manager.addScores(1001, '数学', 5);
% 删除学生 manager.deleteStudent(1002)
原文地址: https://www.cveoy.top/t/topic/eTWR 著作权归作者所有。请勿转载和采集!