Recent

Author Topic: {SOLVED}Simple calculator  (Read 11273 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: 12476
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

  • Sr. Member
  • ****
  • Posts: 265
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!
GitHub repositories https://github.com/Lulu04

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: 2532
    • Additional info
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

germona

  • Newbie
  • Posts: 2
Re: {SOLVED}Simple calculator
« Reply #5 on: November 13, 2024, 04:43:49 am »
Hoe heb je het opgelost? Ik zit met hetzelfde probleem.
Om bijvoorbeeld een percentage te kunnen berekenen geef je het eerste getal, dan bv. het minteken, dan het percentage, en dan pas het = teken.
Maar hij voert (bij een case of) de bewerking al uit bij het min teken, in plaats van na het percentage.
Dank je wel.
Groetjes
Jean

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: {SOLVED}Simple calculator
« Reply #6 on: November 13, 2024, 06:17:09 am »
Dutch language speakers are usually better in English than Google translate. This is an Engllish language forum, please ask your questions in English.
About the minus sign: it does not only mean substraction but also negation... so you should handle both cases and that can be a little tricky for beginners.. ;D ;)
 1 - 1 = 0
-5 +1 = -4
5 - -1 = ?... //zie je dat? (- - = +)
You have to handle the minus sign in two ways. It is not difficult and probably what your teacher wants you to figure out for yourself...
Tip: look ahead...( the other answers left that out...but the OP seems to have figured that out, otherwise it would not be marked as solved...)
« Last Edit: November 14, 2024, 08:44:53 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11947
  • FPC developer.
Re: {SOLVED}Simple calculator
« Reply #7 on: November 13, 2024, 09:52:14 am »
There is also no support for a cascade of operations. The code in the first post is for one calculation only, though maybe a getalA:=uitkomst at the end of the while loop might fix that.

 

TinyPortal © 2005-2018