您的位置:首页 > 编程语言 > MATLAB

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

2015-04-25 09:56 691 查看
1、 输入一组整数a,输出其中奇偶数、奇偶数的和、积以及个数。

m文件代码

clear
clc
n=input('输入数字个数');
for i=1:n
x(i)=input('输入数字:');
end
j=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
%输出奇数和偶数
y
z
%输出所有奇数和、所有偶数和
sum(y)
sum(z)
%输出所有奇数连乘积、所有偶数乘积
prod(y)
prod(z)
%输出所有奇数个数、所有偶数个数
length(y)
length(z)


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

clear
a=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);
end
end
a
b,j
c,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文件代码

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

结果为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;
end
count
count=0;
for j= 32768:32767
count=count+1;
end
count
count=0;
for k=2:4:3
count=count+1;
end
count
count=0;
for m=ones(5,5)
count=count+1;
end
count

结果为65536、0、1、5

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

(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

(4)x = 500;

while x > 0

if isprime(x)

break;

end

x = x -1;

end

m文件代码

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


结果为: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、分别用if和switch多分支语句计算税款,用户输入货价,输出相应的税款:

货价<2000,免税;

货价在2000到5000之间,超过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;
end
taxes
fee

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
taxes
fee


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

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

保存下面两个m文件

sum2n.m

%matlab递归计算1+2+2^2+...+2^n
function 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文件代码

clear
clc
m=0;
i=0;
while m<1000
m=m+2^i;
i=i+1;
end
n=i-1;
n


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

逐个遍历即可

方法一:sum函数遍历

m文件代码

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

方法二:逐个遍历累加

m文件代码

%逐个遍历累加
clear
clc
str='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;
end
end
z


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

m文件

clear
clc
n=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;
end
n


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

rand10.m

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


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

randLowToHigh.m

%调用示例:randLowToHigh(-50,100)
function num=randLowToHigh(low,high)
num=rand()*(high-low)+low;
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐