% 主函数 function main() % 输入成绩 scores = [70 89 95 72 58 91 85 92 65 77]; % 统计各分段人数 [excellent, good, medium, pass, fail] = countNum(scores); % 显示各分段人数 fprintf('优秀:%d人\n', excellent); fprintf('良好:%d人\n', good); fprintf('中等:%d人\n', medium); fprintf('及格:%d人\n', pass); fprintf('不及格:%d人\n', fail); % 将成绩转换为优、良、中、及格和不及格表示 grades = convertGrade(scores); % 显示转换后的成绩 fprintf('转换后的成绩为:%s\n', mat2str(grades)); end

% 统计各分段人数的子函数 function [excellent, good, medium, pass, fail] = countNum(scores) % 初始化各分段人数 excellent = 0; good = 0; medium = 0; pass = 0; fail = 0; % 统计各分段人数 for i = 1:length(scores) switch fix((scores(i)-1)/10) case 9 excellent = excellent + 1; case 8 good = good + 1; case 7 medium = medium + 1; case 6 pass = pass + 1; otherwise fail = fail + 1; end end end

% 将成绩转换为优、良、中、及格和不及格表示的子函数 function grades = convertGrade(scores) % 初始化成绩 grades = cell(1,length(scores)); % 将每个成绩转换为相应的等级 for i = 1:length(scores) switch fix((scores(i)-1)/10) case 9 grades{i} = '优'; case 8 grades{i} = '良'; case 7 grades{i} = '中'; case 6 grades{i} = '及格'; otherwise grades{i} = '不及格'; end end end

编写函数文件将《工程matlab》的成绩:70 89 95 72 58 91 85 92 65 77用switch结构统计各分段的人数并将各人的成绩变为优、良、中、及格和不及格表示。统计人数和成绩都用子函数实现。

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

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