matlab函数自定义怎么输入一个百分制成绩,要求输出成绩等级A、B、C、D、E

如题所述

至少几十行的程序,你知道还就5分,要不是我有现成的,真的没人会给你写。

function score2grade(varargin)
%作者:victoriajll 日期:2011.10.14
%特点:
%有较强的容错能力,功能比较完善。
%使用方法:
%1.没有输入参数:即直接在命令窗口输入函数名score2grade运行,会提示输入;
%2.有输入参数:支持标量和向量输入,例如score2grade(88)
%或者score2grade([-12 34 65 75 86 98 120])
if nargin==0
score=input('请输入学生成绩:');
while isempty(score)
score=input('请输入学生成绩:');
end
else
score=varargin{1};
end
grade=judge(score);
for i=1:length(score)
fprintf('%4d\t%s\n',score(i),grade{i})
end
end

function grade=judge(score)
n=length(score);
grade=cell(1,n);
for i=1:n
if score(i)>100||score(i)<0
grade{i}='错误';
continue
end
if score(i)>=90&&score(i)<=100
grade{i}='A';
continue
end
if score(i)>=80&&score(i)<90
grade{i}='B';
continue
end
if score(i)>=70&&score(i)<80
grade{i}='C';
continue
end
if score(i)>=60&&score(i)<70
grade{i}='D';
continue
end
if score(i)>=0&&score(i)<60
grade{i}='E';
end。
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2017-10-11
至少几十行的程序,你知道还就5分,要不是我有现成的,真的没人会给你写。

function score2grade(varargin)
%作者:victoriajll 日期:2011.10.14
%特点:
%有较强的容错能力,功能比较完善。
%使用方法:
%1.没有输入参数:即直接在命令窗口输入函数名score2grade运行,会提示输入;
%2.有输入参数:支持标量和向量输入,例如score2grade(88)
%或者score2grade([-12 34 65 75 86 98 120])
if nargin==0
score=input('请输入学生成绩:');
while isempty(score)
score=input('请输入学生成绩:');
end
else
score=varargin{1};
end
grade=judge(score);
for i=1:length(score)
fprintf('%4d\t%s\n',score(i),grade{i})
end
end

function grade=judge(score)
n=length(score);
grade=cell(1,n);
for i=1:n
if score(i)>100||score(i)<0
grade{i}='错误';
continue
end
if score(i)>=90&&score(i)<=100
grade{i}='A';
continue
end
if score(i)>=80&&score(i)<90
grade{i}='B';
continue
end
if score(i)>=70&&score(i)<80
grade{i}='C';
continue
end
if score(i)>=60&&score(i)<70
grade{i}='D';
continue
end
if score(i)>=0&&score(i)<60
grade{i}='E';
end
end
end本回答被提问者和网友采纳
第2个回答  2012-12-11
if a<60
disp('E')追问

至少要几十行程序

追答

有例子还不会 啊????

相似回答