本节开始使用函数与脚本文件,命名规则为testx_x_x,自定义命名后命令行窗口执行时也须对应
一
function test4_1(x) if x<0&&x~=-3 y=x.*x+x-6; elseif (x>=0)&&(x<5)&&x~=2&&x~=3 y=x.*x-5.*x+6; else y=x.*x-x-1; end sprintf('%0.1f',y)
例
二
score=input('score='); if score<=100&&score>=90 sprintf('grade=A') elseif score<90&&score>=80 sprintf('grade=B') elseif score<80&&score>=70 sprintf('grade=C') elseif score<70&&score>=60 sprintf('grade=D') elseif score<60 sprintf('grade=E') else sprintf('错误,不合理,输入成绩无效!') end
score=input('score='); if score<=100&&score>=0 switch fix(score/10) case {9,10} grade='A'; case {8} grade='B'; case {7} grade='C'; case {6} grade='D'; otherwise grade='E'; end grade else disp('错误!输出成绩无效!'); end
三
time=input('time='); switch time case {time>120} money=(time-120)*(1+15/100)*84+120*84 case {time<60} money=time*84-700 otherwise money=time*84 end
四
A=floor(rand(1)*90+10) B=floor(rand(1)*90+10) x=input('输入一个运算符:','s'); switch x case {'+'} c=A+B; case {'-'} c=A-B; case {'*'} c=A*B; case {'/'} c=A/B; otherwise c='false'; end c
五
A=input('输入一个5*6矩阵A='); n=input('输入一个正整数n='); if n<5&&n>0 disp(A(n,:)); else disp(A(5,:)); disp('error:n>5') end