switch statements and trig function

3 次查看(过去 30 天)
val = str2double(input('enter number','s'));
chr = input('enter letter','s');
switch chr
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
I wrote this code to find the sin,cos,or tan of an entered individual number. I now need to modify it so that instead of reading a single character/number, the program continually reads character/number pairs and displays the trigonometry result. If a number outside the range [-100, 100] is entered, the program should not display the trigonometry result and should ask for another character/number pair to be entered. I can't figure out how to make the initial modifications of reading in pairs - any shortcuts I can use?
  2 个评论
Kevin Phung
Kevin Phung 2019-1-24
what do you mean by reading in pairs?
Marina Christakos
Marina Christakos 2019-1-24
编辑:Marina Christakos 2019-1-24
instead of the program asking for a letter and number separately, I as the user am able to write c90 and the output will respond with 0

请先登录,再进行评论。

回答(2 个)

Kevin Chng
Kevin Chng 2019-1-24
I think the most simple approach is to add another loop which is
val = str2double(input('enter number','s'));
chr = input('enter letter','s');
if val>=-100 && val<=100
switch chr
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
else
warning('enter new input, your input is out of range')
end
  2 个评论
Marina Christakos
Marina Christakos 2019-1-24
that still makes me input the number and letter at separate times
Kevin Chng
Kevin Chng 2019-1-24
I might miss understanding your question.
However, it is still input them in separate line, if wants to input in the same line, later, you might need to separate them since one is string/char and the other one is numeric
clc;clear all;
chr = input('enter letter & number :','s');
% Need some logic to separate them and ensure the user key in the right thing
C = {'1','2','3','4','5','6','7','8','9','0'};
for i = 1:10
Index = strfind(chr, C{i});
if isempty(Index)~=1
if exist('thefirstdigit')==0
thefirstdigit=Index;
else
if thefirstdigit>Index
thefirstdigit=Index;
end
end
end
end
%indexing to chr, then separate them into val and chr2
val = str2double(chr(thefirstdigit:end));
chr2 = (chr(1:thefirstdigit-1));
if val>=-100 && val<=100
switch chr2
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
else
Run the program and enter :s100

请先登录,再进行评论。


Kevin Phung
Kevin Phung 2019-1-24
编辑:Kevin Phung 2019-1-24
you can just run the input once, and evaluate the input as a whole.
word = input('enter letter followed by number','s');
if numel(word)>1
chr = word(1);
val =str2num(word(2:end))
if isempty(val) || val >100 || val < -100 %occurs when 2nd character is a letter and not a number
disp('Try again')
return
end
switch chr
case 's'
disp(sin(val))
case 'c'
disp(cos(val));
case 't'
disp(tan(val));
otherwise
disp('Oh no! Try a different character.');
end
else
disp('Try again')
end

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by