2018.9.19.Matlab实验四:Matlab程序设计

版权声明:欢迎转载 https://blog.csdn.net/qq_40828914/article/details/82792078

一、实验任务和目的

  1. 熟悉程序设计思想。
  2. 掌握伪代码的编写方法。
  3. 掌握分支语句和循环结构的用法。

二、实验内容

  1. 输入一个百分制成绩,要求输出成绩等级A、B、C、D、E,其中90-100为A,80-89为B,70-79为C,60-69为D,60分以下为E。要求
    (1)分别用if语句和switch语句实现;
    (2)应对输入的成绩进行合理性判断,对不合理的成绩应输出错误信息。
  2. 编程实现一个九九乘法表,并屏幕显示出来,如下图所示:
    在这里插入图片描述
  3. 计算the day of year(年积日),the day of year 是指这一年已经逝去的天数(包括当天)。在平年中,它的取值范围为1到365,在闰年中,它的取值范围1 到366。编写一个MATLAB 程序,输入年、月、日,输出为对应的the of year。

三、实验过程和结果

  1. 输入一个百分制成绩,要求输出成绩等级A、B、C、D、E,其中90-100为A,80-89为B,70-79为C,60-69为D,60分以下为E。要求
    (1)分别用if语句和switch语句实现;
    If语句:
    x=input(‘成绩’);
    if x>=90&&x<=100
        disp('A');
    elseif x>=80&&x<=89
        disp('B');
    elseif x>=70&&x<=79
        disp('C');
    elseif x>=60&&x<=69
        disp('D');
    else disp('E');
    end

switch语句:

 x=input('成绩');
switch x
    case num2cell(90:100)
        disp('A');
    case num2cell(80:89)
        disp('B');
    case num2cell(70:79)
        disp('C');
    case num2cell(60:69)
        disp('D');
    otherwise
        disp('E');
end

(2)应对输入的成绩进行合理性判断,对不合理的成绩应输出错误信息。

x=input('成绩');
if(x>100||x<0)
    disp('wrong input');
else
    switch x
        case num2cell(90:100)
            disp('A');
        case num2cell(80:89)
            disp('B');
        case num2cell(70:79)
            disp('C');
        case num2cell(60:69)
            disp('D');
        otherwise
            disp('E');
    end
end
  1. 编程实现一个九九乘法表,并屏幕显示出来,如下图所示:
    在这里插入图片描述
        for i=1:9
        	for j=1:i
            	fprintf('%dx%d=%d',j,i,i*j);
            	if(j~=i) 
                fprintf(' ');
            	end
        	end
        	fprintf('\n');
        end
  1. 计算the day of year(年积日),the day of year 是指这一年已经逝去的天数(包括当天)。在平年中,它的取值范围为1到365,在闰年中,它的取值范围1 到366。编写一个MATLAB 程序,输入年、月、日,输出为对应的the of year。
y=input('年');
m=input('月');
d=input('日');
sum=0;
a=[31,28,31,30,31,30,31,31,30,31,30,31];
b=[31,29,31,30,31,30,31,31,30,31,30,31];
if(y<0||m<0||d<0||m>12||d>31)
    fprintf('wrong input');
else 
    if(mod(y,400)==0||mod(y,100)~=0 && mod(y,4)==0)
        for i=1:m-1
            sum=sum+b(i);
        end
        if(d>b(m)) fprintf('wrong input');
        else fprintf('%d',sum+d);
        end
    else
        for i=1:m-1
            sum=sum+a(i);
        end
        if(d>b(m)) fprintf('wrong input');
        else fprintf('%d',sum+d);
        end
    end
end

四、实验总结和心得

熟悉了程序设计思想。
掌握了伪代码的编写方法。
掌握了分支语句和循环结构的用法。

猜你喜欢

转载自blog.csdn.net/qq_40828914/article/details/82792078