Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Calculator Help
#1
What is the code to create a simple calculator that adds, subtracts, multiplies and divides using a nested IF Statement? I can not figure this out for anything. The program should print and continue until -1 is entered. Thank You for your help.
Reply
#2
Stop spamming or get banned.
Reply
#3
Ok, qbasichelp, I hope this is not for homework, because we're not supposed to do your homework for you.

Anyway, since some of the guys gave you a hard time, I thought I'd help you. There's no such thing a a simple calculator. There's too many factors like handling negative numbers, numbers with decimals, validating the numbers, divide by zero considerations, etc. But, I tried to keep as simple as possible, but functional.
Code:
defint a-z

dim ValA as double
dim ValB as double
dim Ans  as double

const DecimalPlaces = 3  'Maximum decimal places allowed

print "Value.A Operator Value.B"
print

geta:
print "Enter Value.A";
input z$
z$=ltrim$(rtrim$(z$))
if z$="-1" then system
'Note: If you really need -1, then enter -1. that is -1 and a decimal.
if left$(z$,1)="-" then
   signa=-1
   z$=mid$(z$,2)
else
   signa=1
end if
gosub Numdec
if NumValid=0 then
   print "Invalid Value.A"
   goto geta
end if
ValA=val(z$)
ValA=ValA*signa

getop:
print "Enter the Operator (+-*/)";
input op$
if instr("+-*/",op$)=0 or len(op$)<>1 then
   print "Invalid Operator"
   goto getop
end if

getb:
print "Enter Value.B";
input z$
z$=ltrim$(rtrim$(z$))
if left$(z$,1)="-" then
   signb=-1
   z$=mid$(z$,2)
else
   signb=1
end if
gosub Numdec
if NumValid=0 then
   print "Invalid Value.B"
   goto getb
end if
ValB=val(z$)
ValB=ValB*signb

'Ok, we have all the parameters, now compute.

divzero=0

SELECT CASE op$
    CASE "+"
         Ans = ValA + ValB
    CASE "-"
         Ans = ValA - ValB  
    CASE "*"
         Ans = ValA * ValB
    CASE "/"
         if ValB<>0 then Ans = ValA / ValB else divzero=1
    CASE ELSE
END SELECT
if divzero=1 then
   print "ERROR: Divide by zero"
else
   print "Answer = ";Ans
end if
print
goto geta

REM *
REM *** CHECK FOR STRICTLY NUMERIC, NO NULL, ALLOW A DECIMAL POINT.
REM *
Numdec:
   NumValid=0         'Init to False
   W$=Z$
   IF W$="" THEN RETURN
   ZZP=INSTR(W$,".")
   IF ZZP>0 THEN      
      MID$(W$,ZZP,1)="0"
      IF ZZP < LEN(W$)-DecimalPlaces OR INSTR(W$,".") THEN RETURN
   END IF                                                        
   FOR ZZ = 1 TO LEN(W$)
       A=ASC(MID$(W$,ZZ,1))
       IF A<48 OR A>57 THEN RETURN
   NEXT ZZ

   NumValid=-1          'True
RETURN
If it's not clear, just ask me.
*****
Reply
#4
Quote:What do you mean stop spamming I am in a class and can not figure out one of my labs and I have to get this done ASAP. So why can I not get help?
Reply
#5
i don't know, but it sounds like homework to me. Consider these points... And btw, i'm not trying to give you a hard time right now... but as Wildcard said in his qb problems forum, the user shouldn't post homework, and must have at least shown an attempt to solve the problem himself. qbasichelp has failed both.

1. It must end when the user enters -1... why not "q" like a good coder of esc? why not "e"? I've only heard of parameters like that in challenges or in homework topics.
2. he says he must use nested if statements. if it wasn't a project or homework, he wouldn't of asked for such a thing. If this was a personal project, he (or i for that matter) wouldn't care if it used nested IF's or not.
3. he says that he has to print it out... any coder worth his salt (or even half of it) would know that in order to see the output, the products/sums/whatevers must be printed out. (or poked, putted, etc. etc.)
4. Z!re's pm clearly shows that it's homework.
Jumping Jahoolipers!
Reply
#6
spamming is posting on multiple forums the same question. Nested IF was explained to you on The QBasic Forum. Now that you know what that means, write some code and post it.

You have an assignment and you appear to want someone else to do it for you.

Mac
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)