Matlab的if语句switch语句for循环while循环语句练习

来源:互联网 发布:防火墙软件推荐 编辑:程序博客网 时间:2024/06/10 03:04

1、 输入一组整数a,输出其中奇偶数、奇偶数的和、积以及个数。

m文件代码

clearclcn=input('输入数字个数');for i=1:n     x(i)=input('输入数字:');endj=1;k=1;%y向量存奇数,z向量存偶数for i=1:n     if mod(x(i),2)         y(j)=x(i);         j=j+1;     else         z(k)=x(i);         k=k+1;     end         end%输出奇数和偶数yz%输出所有奇数和、所有偶数和sum(y)sum(z)%输出所有奇数连乘积、所有偶数乘积prod(y)prod(z)%输出所有奇数个数、所有偶数个数length(y)length(z)


其他方法(下面的方法输入必须是矩阵形式,如:[2 3 4 5 6 7 8],前一个用除法,后一个用find函数)

cleara=input('input some numbers:')n=length(a);j=0;k=0;for i=1:n   if rem(a(i),2)==0      j=j+1;      b(j)=a(i);   else      k=k+1;      c(k)=a(i);   endendab,jc,k----------------------a=input('input some numbers:')b=a(find(rem(a,2)==0))j=length(b)c=a(find(rem(a,2)~=0))k=length(c)-------------



2、计算s=e-(1/1+1/1!+1/2!+...+1/N!)使得s<10^(-6),求最小的N

m文件代码

clearclcs=exp(1);i=0;while(1)    s=s-1.0/factorial(i);    if(s<1e-6)        break;    end    i=i+1;endi

结果为9

注:阶乘除了可以用factorial(i),还可以用prod(1:i),prod是连乘函数

3、 试计算以下循环语句将进行多少步操作

(1) for i= 32768:32767

(2) for j= 32768:32767

(3) for k=2:4:3

(4) for m=ones(5,5)

m文件代码

count=0;for i= -32768:32767    count=count+1;endcountcount=0;for j= 32768:32767    count=count+1;endcountcount=0;for k=2:4:3    count=count+1;endcountcount=0;for m=ones(5,5)    count=count+1;endcount

结果为65536、0、1、5

4、 观察以下循环语句,试计算每个循环次数和循环结束之后iresx的值

(1) ires=1;

while mod(ires,10)~=0

ires=ires+1;

end

(2) ires=2;

while ires<=200

ires=ires^2;

end

(3) ires=2;

while ires>200

ires=ires^2;

end

4x = 500;

while x > 0

    if isprime(x)

        break;

    end

    x = x -1;

end

m文件代码

ires=1;while mod(ires,10)~=0ires=ires+1;endiresires=2;while ires<=200ires=ires^2;endiresires=2;while ires>200ires=ires^2;endiresx = 500;while x > 0    if isprime(x)        break;    end    x = x -1;endx

结果为:10、256、2、449

注:isprime(x)为求素数函数,如果是素数返回1,否则返回0

ISPRIME True for prime numbers.
    ISPRIME(X) is 1 for the elements of X that are prime, 0 otherwise.

5、分别用ifswitch多分支语句计算税款,用户输入货价,输出相应的税款:

货价<2000,免税;

货价在20005000之间,超过2000部分抽税2%

5000以上,除2%以外,5000以上抽税5%,加收手续费60元。

if语句

m文件代码

clear  clc  price=input('请输入价格:'); taxes=0;fee=0;if price<2000    taxes=0;elseif price<5000    taxes=(price-2000)*0.02;else    taxes=(5000-2000)*0.02+(price-5000)*0.05;    fee=60;endtaxesfee

switch语句

m文件代码

clear  clc  price=input('请输入价格:'); taxes=0;fee=0;switch floor(price/1000)    case {0,1}        taxes=0;     case {2,3,4}        taxes=(price-2000)*0.02;     otherwise        taxes=(5000-2000)*0.02+(price-5000)*0.05;        fee=60;        end        taxesfee

6、分别用ifwhile做,m=1+2+2^2+...+2^n,直到1000为止,求最大的N

只用if的话,用递归可以实现

保存下面两个m文件

sum2n.m

%matlab递归计算1+2+2^2+...+2^nfunction num=sum2n(n)if n==0    num=1;else    num=2^n+sum2n(n-1);end
sum2nbigm.m

%判断1+2+2^2+...+2^n是否大于m,参数为m,n的初值(n的初值建议取0)%对于本题调用方法为sum2nbigm(1000,0)function num=sum2nbigm(m,n)if sum2n(n)>m    num=n;else    num=sum2nbigm(m,n+1);end

然后调用sum2nbigm(1000,0),结果为9

while循环方法

m文件代码

clearclcm=0;i=0;while m<1000    m=m+2^i;    i=i+1;endn=i-1;n

7、‘The quick brown fox jumps over the lazy dog’,26个字母至少出现过一次,统计每个字母出现的次数。

逐个遍历即可

方法一:sum函数遍历

m文件代码

%利用sum函数遍历clearclcstr='The quick brown fox jumps over the lazy dog';str=lower(str);       %将字符串中的大写转换为小写for i=1:26    x(i)=sum(str==char('a'-1+i));endx

方法二:逐个遍历累加

m文件代码

%逐个遍历累加clearclcstr='The quick brown fox jumps over the lazy dog';str=lower(str);z=zeros(1,26);for i=1:length(str)    if(str(i)<='z'&&str(i)>='a')         z(str(i)-'a'+1)=z(str(i)-'a'+1)+1;    endendz

8、输入一个字符,如果是大写字母,输出小写;如果是小写字母,输出大写;其他字符,原样输出。

m文件

clearclcn=input('请输入一个字符','s');n=n(1);%即使输入多个字符也只取第一个if(n>='a'&&n<='z')    n=char(n-32);elseif(n>='A'&&n<='Z')    n=char(n+32);else    n=n;endn

9、利用rand函数编制一个新的函数rand10,该函数能够产生在[-1010]之内的随机数。

rand10.m

function num=rand10()num=rand()*20-10;end


10、对上题的函数加以修改,使得产生的随机数在[low,high]之间,其中lowhigh为用户输入参数。

randLowToHigh.m

%调用示例:randLowToHigh(-50,100)function num=randLowToHigh(low,high)num=rand()*(high-low)+low;end









0 0
原创粉丝点击