I'm analysing this code for a calculator here and I noticed 2 bugs. Any insight would be heplful.

The first is that whenever you divide a number by zero it displays inf when it should be zero. The second is that when you want to enter a number, say 20, instead of it entering 2 and then 0 it doubles the first number so it enters 22 instead of 20 or if you want to enter say 56, it enters 55. Here is the code I want to figure out.
function calculator_v2 figure('Position',[220 200 220 200],'Name','Calculator','NumberTitle','off','Color','black','Menubar','None','Resize','off'); txt=uicontrol('Style','Text','Position',[10 135 200 50],'String','0'); global stack value top op tops opstack num1 num2 flag; flag=0; x=10; y=130; top=1; tops=1; name=['7','8','9','/','4','5','6','*','1','2','3','-','0','+','C','='];%k
for k=1:size(name,2)
uicontrol('Style','Pushbutton','Position',[x y 50 30],'String',name(k),'Callback',@arithmetic);
x=x+50;
if(mod(k,4)==0)
x=10;y=y-35;
end
end
function arithmetic(object,~)
num=str2double(get(object,'String'));
if((num>=0)&&(num<=9))
value=num;
evaluate();
else
op=get(object,'String');
if(op=='C')
top=1;
tops=1;
set(txt,'String','0');
else
operator();
end
end
end
function evaluate()
stack(top)=value;
if((stack(1)==0)&&(top==2))
stack(1)=stack(top);
top=top-1;
end
str=num2str(stack(1));
for i=2:top
str=strcat(str,num2str(stack(1)));
end
top=top+1;
set(txt,'String',' ');
set(txt,'String',str);
flag=0;
end
function operator()
if((top~=1)||(flag==1))
opstack(tops)=op;
tops=tops+1;
if((tops==2)&&(flag~=1))
str=num2str(stack(1));
for i=2:top-1
str=strcat(str,num2str(stack(1)));
end
num1=str2double(str);
flag=1;
top=1;
elseif(tops>=3)
if(flag==0)
str=num2str(stack(1));
for i=2:top-1
str=strcat(str,num2str(stack(1)));
end
num2=str2double(str);
top=1;
if(opstack(tops-1)=='=')
calculate();
set(txt,'String',' ');
set(txt,'String',num2str(num1));
flag=1;
tops=1;
else
calculate();
set(txt,'String',' ');
set(txt,'String',num2str(num1));
flag=1;
tmp=opstack(tops-1);
opstack(1)=tmp;
tops=tops-1;
end
else
tmp=opstack(tops-1);
opstack(1)=tmp;
tops=tops-1;
end
end
end
end
function calculate()
switch(opstack(1))
case'+'
num1=num1+num2;
case '-'
num1=num1-num2;
case '/'
num1=num1/num2;
case '*'
num1=num1*num2;
end
end
end

 Accepted Answer

C - look closely at your evaluate function and in particular the code that creating the string (of numbers) to display in the text window
str=num2str(stack(1));
for i=2:top
str=strcat(str,num2str(stack(1)));
end
Note how the first number is always "pulled" from the stack rather than the ith. Just replace the 1 with i (though you may wish to use a different indexing variable name since MATLAB uses i and j to represent the imaginary number).
As for displaying Inf when you divide by zero, I think that this is reasonable because you want to make it clear to the user that dividing by zero is not zero. If you really want this number to be zero, then use the MATLAB function isinf to indicate whether your number is infinite or not (and then replace with zero if it is).

More Answers (0)

Asked:

C
C
on 1 Dec 2015

Edited:

on 1 Dec 2015

Community Treasure Hunt

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

Start Hunting!