Recent

Author Topic: {SOLVED}Simple calculator  (Read 10010 times)

wareospike

  • Newbie
  • Posts: 6
{SOLVED}Simple calculator
« on: August 25, 2017, 07:59:08 am »
Hey guys!

I am starting with a course on school with lazarus. But I am kind of stuck at the moment.

I should create a very basic calculator. I need to insert a number, do an operation (+,-,*,/) insert another number, a new operation and so on. Until I press '='.

After '=' the result should be shown. The operations should be in sequence and not so much as in the normal rules of math! I hope someone can help me out with this. I already have a part of the code, but can't to figure it out well.

Code: Pascal  [Select][+][-]
  1. program NieuwRekenmachine;
  2.  
  3. var
  4.   { TODO: check and accomplish variable declarations }
  5.   uitkomst: Single;
  6.   keuze: string;
  7.   getalB: Longint;
  8.   getalA: Longint;
  9.  
  10. begin
  11.   uitkomst := 0;
  12.   writeln('Geef Een getal');
  13.   readln(getalA);
  14.   while (keuze<>'=') do
  15.   begin
  16.     writeln('+');
  17.     writeln('-');
  18.     writeln('*');
  19.     writeln('/');
  20.     writeln('=');
  21.     readln(keuze);
  22.     writeln('Geef een getal');
  23.     readln(getalB);
  24.     case (keuze) of
  25.       '+':
  26.         begin
  27.           uitkomst := getalA+getalB;
  28.         end;
  29.       '-':
  30.         begin
  31.           uitkomst := getalA-getalB;
  32.         end;
  33.       '*':
  34.         begin
  35.           uitkomst := getalA*getalB;
  36.         end;
  37.       '/':
  38.         begin
  39.           uitkomst := getalA/getalB;
  40.         end;
  41.       else
  42.         writeln('Niet geldig');
  43.     end;
  44.   end;
  45.   if (keuze<>'=') then
  46.   begin
  47.     writeln('de uitkomst bedraagt:', uitkomst:0:2);
  48.   end;
  49.   writeln;
  50.   writeln('Druk op <ENTER> om het programma te sluiten.');
  51.   readln();
  52. end.    
« Last Edit: August 29, 2017, 09:57:59 am by wareospike »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Simple calculator
« Reply #1 on: August 25, 2017, 09:44:00 am »
Try to understand the logical structure of the program
  • you enter a number (A)
  • you enter an operation symbol, e.g. '+'
  • you enter another number (B)
  • you calculate the result of the operation: A+B
  • you enter another operation symbol
  • you enter another number
  • you calculate the result of the operation
  • etc, until the operation symbol is '=': Exit the loop and writeln the final result.
At which item of this list does a loop start, and at which item does it end? What do you have to do that the loop re-uses the result of the previous iteration?

Lulu

  • Full Member
  • ***
  • Posts: 230
Re: Simple calculator
« Reply #2 on: August 25, 2017, 10:01:14 am »
Hello,
You use:
Code: Pascal  [Select][+][-]
  1. ...
  2. while (keuze<>'=') do
  3. ...
but may be at the beginning of the program, 'keuze' variable already contain '=' character. Yes... it is unlikely, but why not ? :)
You should use repeat - until structure like:
Code: Pascal  [Select][+][-]
  1. repeat
  2. ...
  3. readln(keuze);
  4. ...
  5. until keuze='=';
  6. ...
  7.  
Here, no test on variable 'keuze' without initializing it.
wishing you a nice life

wareospike

  • Newbie
  • Posts: 6
Re: Simple calculator
« Reply #3 on: August 25, 2017, 10:24:44 am »
Hey!

Thanks for the input! I found a way to make it work! I'm glad with this insight! Thanks again!

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018