1、编写一个脚本,查找给定字符串(' She found the hands of others full of sunshine and warmth')中指定字符h出现的次数和位置。
clc
letter = 'h';
string = 'She found the hands of others full of sunshine and warmth';
places = findstr(string,letter)
ntimes = length(places)
2、编写一个脚本,判断输入字符串中每个单词的首字母是否为大写,若不是则将其修改为大写,其他字母为小写。
clc;clear;
str = 'Hangzhou, capital of Zhejiang Province in East China, is one of the more modern and prosperous cities in China, about 100 kilometers southwest of Shanghai. It sits at the southern end of the Grand Canal and is one of Chinese seven ancient national capitals. When Marco Polo came to Hangzhou in the 13th century, he declared it to be 'the most beautiful and elegant city in the world '.'; %%创造字符串
nlength = length(str); %%通过函数length()得到字符串str的长度
for k=1:nlength %%将其设置为循环次数
%判断每一个单词首字母是不是大写(通过对应asc码大小进行比较),若不是则将其修改为大写,其他字母为小写。
if (k==1 || isspace(str(k-1))) && (str(k)<='z' && str(k)>='a')
str(k) = char(double(str(k)) - 32);
end
end
disp(str);
3、编写脚本文件。创建结构体,用于统计学生的情况,包括学生的序号,姓名,各科成绩等。1)用该结构体计算各门功课的平均分;2)计算每位学生的总分,平均分,并按照学生总分升序排列,得到成绩最好的同学名字;3)判断是男生成绩好还是女生成绩好(总分判断)。
No. | name | Gender | English | Math | Chemistry | Biophysics | |
103 | Jingwang | Female | 74 | 69 | 60 | 94 | |
104 | Yuedai | Male | 72 | 87 | 63 | 96 | |
105 | Cuiyang | Male | 72 | 97 | 81 | 96 | |
106 | Miuming | Female | 76 | 71 | 53 | 94 | |
108 | Wuchu | Male | 80 | 93 | 72 | 96 | |
109 | Caoyan | Female | 70 | 75 | 61 | 97 | |
110 | Qianjie | Male | 77 | 73 | 65 | 91 | |
111 | Mayao | Female | 77 | 64 | 65 | 92 |
clc;clear;
st_i(1)=struct('Number',103,'name','Jingwang','Gender',...
'Female','English',74,'Math',69,'Chemistry',60,'Biophysics',94)
st_i(2)=struct('Number',104,'name','Yuedai','Gender',...
'Male','English',72,'Math',87,'Chemistry',63,'Biophysics',96)
st_i(3)=struct('Number',105,'name','Cuiyang','Gender',...
'Male','English',72,'Math',97,'Chemistry',81,'Biophysics',96)
st_i(4)=struct('Number',106,'name','Miuming','Gender',...
'Female','English',76,'Math',71,'Chemistry',53,'Biophysics',94)
st_i(5)=struct('Number',108,'name','Wuchu','Gender',...
'Male','English',80,'Math',93,'Chemistry',72,'Biophysics',96)
st_i(6)=struct('Number',109,'name','Caoyan','Gender',...
'Female','English',70,'Math',75,'Chemistry',61,'Biophysics',97)
st_i(7)=struct('Number',110,'name','Qianjie','Gender',...
'Male','English',77,'Math',73,'Chemistry',65,'Biophysics',91)
st_i(8)=struct('Number',111,'name','Mayao','Gender',...
'Female','English',77,'Math',64,'Chemistry',65,'Biophysics',92)
%%计算各门平均分
aE_score=mean(cat(1,st_i.English));% 74.7500
aM_score=mean(cat(1,st_i.Math));% 78.6250
aC_score=mean(cat(1,st_i.Chemistry));%65
aB_score=mean(cat(1,st_i.Biophysics));%94.5
av=[aE_score,aM_score,aC_score,aB_score]
%%总分计算
st_i(1).zongfen=st_i(1).English+st_i(1).Math+st_i(1).Chemistry+st_i(1).Biophysics;
st_i(2).zongfen=st_i(2).English+st_i(2).Math+st_i(2).Chemistry+st_i(2).Biophysics;
st_i(3).zongfen=st_i(3).English+st_i(3).Math+st_i(3).Chemistry+st_i(3).Biophysics;
st_i(4).zongfen=st_i(4).English+st_i(4).Math+st_i(4).Chemistry+st_i(4).Biophysics;
st_i(5).zongfen=st_i(5).English+st_i(5).Math+st_i(5).Chemistry+st_i(5).Biophysics;
st_i(6).zongfen=st_i(6).English+st_i(6).Math+st_i(6).Chemistry+st_i(6).Biophysics;
st_i(7).zongfen=st_i(7).English+st_i(7).Math+st_i(7).Chemistry+st_i(7).Biophysics;
st_i(8).zongfen=st_i(8).English+st_i(8).Math+st_i(8).Chemistry+st_i(8).Biophysics;
%%计算每个学生的平均分
st_i(1).average=st_i(1).zongfen/4;st_i(2).average=st_i(2).zongfen/4;
st_i(3).average=st_i(3).zongfen/4;st_i(4).average=st_i(4).zongfen/4;
st_i(5).average=st_i(5).zongfen/4;st_i(6).average=st_i(6).zongfen/4;
st_i(7).average=st_i(7).zongfen/4;st_i(8).average=st_i(8).zongfen/4;
%%排序
[blath,order]=sort([st_i.zongfen],'descend');
%346 341 318 306 303 298 297 294
sortedStruct = st_i(order);
%最高分
max_score=sortedStruct(1).name;% 'Cuiyang'
%计算男生总分平均分
j=0
sc_F=0;sc_M=0;
for i=1:8
if st_i(i).Gender == "Female"
sc_F=st_i(i).zongfen+sc_F
j=j+1
elseif st_i(i).Gender == "Male"
sc_M=st_i(i).zongfen+sc_M
end
end
boy=sc_M/(8-j);
girl=sc_F/j;
if boy > girl
disp("Boys were better!")
elseif boy < girl
disp("girls were better!")
else
disp("Boys and girls did equally well")
end