Advertisement
pseudocreator

Procedural data type, exercise

Mar 17th, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MODULE proceduraltype;
  2. //Procedural data type, basic exercise with 2 operations
  3.  
  4. FROM InOut IMPORT WriteString, WriteLn, Read;
  5. FROM RealInOut IMPORT WriteReal, ReadReal;
  6. VAR
  7.    operation: PROCEDURE(REAL, REAL): REAL;
  8.    nm1, nm2, result : REAL;
  9.    smb : CHAR;
  10.    again : BOOLEAN;
  11.  
  12. PROCEDURE addition(nmb1, nmb2: REAL): REAL;
  13. VAR
  14.    result : REAL;
  15. BEGIN
  16.      result := nmb1 + nmb2;
  17.      RETURN result
  18. END addition;
  19.  
  20. PROCEDURE subtraction(nmb1, nmb2: REAL): REAL;
  21. VAR
  22.    result : REAL;
  23. BEGIN
  24.      result := nmb1 - nmb2;
  25.      RETURN result
  26. END subtraction;
  27.  
  28. BEGIN
  29.      WriteString('Enter first number: ');
  30.      ReadReal(nm1);
  31.      WriteLn;
  32.      WriteString('Enter second number: ');
  33.      ReadReal(nm2);
  34.      WriteLn;
  35.      WriteString('Enter the operation(+ OR -) :');
  36.      again := FALSE;
  37.      REPEAT
  38.            IF again THEN
  39.              WriteString('Enter again, the operation must  be + OR -! : ');
  40.              WriteLn;
  41.            END;
  42.            Read(smb);
  43.            WriteLn;
  44.            again := TRUE;
  45.      UNTIL (smb = '+') OR (smb = '-');
  46.      CASE smb OF
  47.          '+' : operation := addition|
  48.          '-' : operation := subtraction
  49.      END;
  50.      result := operation(nm1,nm2);
  51.      WriteLn;
  52.      WriteString('Result is: ');
  53.      WriteReal(result,0);
  54.      WriteLn;
  55. END proceduraltype.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement