以下是一个简单的学生成绩管理系统的Matlab代码示例:

%% 定义学生类 classdef Student properties name id score end end

%% 定义学生管理类 classdef StudentManager properties students end

methods
    function obj = StudentManager()
        obj.students = struct('name',{}, 'id',{}, 'score',{});
    end
    
    function addStudent(obj, name, id, score)
        student = Student();
        student.name = name;
        student.id = id;
        student.score = score;
        obj.students(end+1) = struct('name',student.name, 'id',student.id, 'score',student.score);
    end
    
    function removeStudent(obj, id)
        for i=1:length(obj.students)
            if obj.students(i).id == id
                obj.students(i) = [];
                break;
            end
        end
    end
    
    function modifyStudent(obj, id, name, score)
        for i=1:length(obj.students)
            if obj.students(i).id == id
                obj.students(i).name = name;
                obj.students(i).score = score;
                break;
            end
        end
    end
    
    function printStudents(obj)
        fprintf('学生列表:\n');
        for i=1:length(obj.students)
            fprintf('%d %s %d\n', obj.students(i).id, obj.students(i).name, obj.students(i).score);
        end
    end
end

end

%% 测试代码 manager = StudentManager(); while true fprintf('\n'); fprintf('请选择要进行的操作:\n'); fprintf('1. 添加学生\n'); fprintf('2. 删除学生\n'); fprintf('3. 修改学生信息\n'); fprintf('4. 查看学生列表\n'); fprintf('5. 退出程序\n'); choice = input('请输入数字选择操作:');

switch choice
    case 1
        name = input('请输入学生姓名:', 's');
        id = input('请输入学生学号:');
        score = input('请输入学生成绩:');
        manager.addStudent(name, id, score);
        fprintf('添加学生成功!\n');
    case 2
        id = input('请输入要删除的学生学号:');
        manager.removeStudent(id);
        fprintf('删除学生成功!\n');
    case 3
        id = input('请输入要修改的学生学号:');
        name = input('请输入学生姓名:', 's');
        score = input('请输入学生成绩:');
        manager.modifyStudent(id, name, score);
        fprintf('修改学生信息成功!\n');
    case 4
        manager.printStudents();
    case 5
        break;
    otherwise
        fprintf('输入错误,请重新输入!\n');
end

end

fprintf('程序已退出。\n')

matlab进行学生成绩系统管理用结构体与类能够实现学生的增减修改成绩修改删除增加的代码在命令行窗口进行选择修改

原文地址: https://www.cveoy.top/t/topic/eTTG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录