How create a calculator function ?

Create a function calculator() that has as input: a: number; b: number; t: textfield containing the mathematical function to perform. The functions to calculate are: “+” “-“ “*” “/”. If another type of function is inputted (e.g. “^”) the output should be NaN. Tip: to verify a text you can use the function “strcmp”. You will also have to use an “if else” statement in your code. Create a proper header.
How do you do this?

3 件のコメント

Guillaume
Guillaume 2015 年 2 月 11 日
  • Start your editor
  • Write the code (read through your course notes)
  • Save the file
More seriously, nobody is going to do your homework for you. If you have a specific problem, we'll be happy to help you, but you need to come back with a much narrower question and with details of what you've tried.
James Tursa
James Tursa 2015 年 2 月 11 日
Contributors to this site are willing to help, but are generally not willing to do your homework for you. Please make an attempt and if you can't get it working, post your code and ask specific questions about your code and we will help.

サインインしてコメントする。

回答 (2 件)

Rick Rosson
Rick Rosson 2015 年 2 月 11 日

0 投票

Here is a start:
function y = calculator(a,b,t)
% Insert your header comments here
%
%
% insert your MATLAB code here:
end

1 件のコメント

John D'Errico
John D'Errico 2015 年 2 月 11 日
Oh, gosh. That gives the whole thing away. :)

サインインしてコメントする。

sths
sths 2015 年 2 月 12 日
編集済み: Guillaume 2015 年 2 月 12 日

0 投票

a=4
b=2
if strcmp(t,'+')
y(a,b)=6
elseif strcmp(t,'-')
y(a,b)=2
elseif strcmp(t,'*')
y(a,b)=8
elseif strcmp(t,'/')
y(a,b)=2
elseif strcmp(t,'^')
y(a,b)=NaN
save(calculator,a,b,t)
function y = calculator(a,b,t)
What am i doing wrong here?

1 件のコメント

Vennmukhil T
Vennmukhil T 2019 年 9 月 8 日
Function keyword use is actually invalid

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

質問済み:

2015 年 2 月 11 日

コメント済み:

2019 年 9 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by