-- Funciones para proximamente llamarlas para las operaciones
CREATE OR REPLACE FUNCTION sumar (n1 IN NUMBER, n2 IN NUMBER)
RETURN FLOAT IS
BEGIN
  RETURN n1 + n2;
END;
/

CREATE OR REPLACE FUNCTION restar (n1 IN NUMBER, n2 IN NUMBER)
RETURN FLOAT IS
BEGIN
  RETURN n1 - n2;
END;
/

CREATE OR REPLACE FUNCTION multiplicar (n1 IN NUMBER, n2 IN NUMBER)
RETURN FLOAT IS
BEGIN
  RETURN n1 * n2;
END;
/

CREATE OR REPLACE FUNCTION dividir (n1 IN NUMBER, n2 IN NUMBER)
RETURN FLOAT IS
BEGIN
  RETURN n1 / n2;
END;
/

-- Código de la calculadora en sí
DECLARE
  n1 DECIMAL := 4;
  n2 DECIMAL := 6;
  operador VARCHAR2(10) := '*';
  continuar VARCHAR2(10) := 'N';

BEGIN
  LOOP
    -- Solicitar los numeros (Simulado con asignaciones directas)
    -- En un entorno real, usar ACCEPT o un método adecuado para recoger la entrada del usuario

    -- Simulación de entrada del usuario
    n1 := 4;
    n2 := 0;
    operador := '/';

    -- Ejecutar la operación correspondiente
    CASE
      WHEN operador = '+' THEN DBMS_OUTPUT.PUT_LINE('El resultado es: ' || sumar(n1, n2));
      WHEN operador = '-' THEN DBMS_OUTPUT.PUT_LINE('El resultado es: ' || restar(n1, n2));
      WHEN operador = '*' THEN DBMS_OUTPUT.PUT_LINE('El resultado es: ' || multiplicar(n1, n2));
      WHEN operador = '/' THEN DBMS_OUTPUT.PUT_LINE('El resultado es: ' || dividir(n1, n2));
      ELSE DBMS_OUTPUT.PUT_LINE('Operación no válida. Inténtalo de nuevo.');
    END CASE;

    -- Preguntar si desea continuar (Simulado con una asignación directa)
    -- En un entorno real, usar ACCEPT o un método adecuado para recoger la entrada del usuario
    continuar := 'N';

    -- Salir del bucle si la respuesta es 'N'
    EXIT WHEN continuar = 'N';
  END LOOP;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/
 

PL/SQL Online Compiler

Write, Run & Share PL/SQL code online using OneCompiler's Oracle PL/SQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for Oracle PL/SQL running on latest version 23c (23.3.0.0). Getting started with the OneCompiler's Oracle PL/SQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'PL/SQL' and start writing code to learn and test online without worrying about tedious process of installation.

About PL/SQL

PL/SQL is procedural extension for SQL created by Oracle. It is by default embedded into the Oracle Database. PL/SQL program units are compiled and stored inside the Oracle Database which results in optimal execution times as the PL/SQL and SQL run within the same server process.

Syntax help

Following is the syntax structure for the PL/SQL code blocks

DECLARE 
   <declarations section> 
BEGIN 
   <executable command(s)>
EXCEPTION 
   <exception handling> 
END;

Example

DECLARE 
   message  varchar2(100):= 'Hello, World!'; 
BEGIN 
   dbms_output.put_line(message); 
END; 
/

Named procedures

CREATE OR REPLACE FUNCTION 
hello_user
   (user_name IN VARCHAR2) 
    RETURN VARCHAR2
IS
BEGIN
   RETURN 'Hello ' || user_name;
END hello_user;
/

BEGIN
   dbms_output.put_line(hello_user('Peter'));
END;
/

Exception handling

BEGIN
  DBMS_OUTPUT.put_line (1/0);
EXCEPTION
  WHEN OTHERS
  THEN
    DBMS_OUTPUT.put_line ('error is: ' || SQLERRM);
END;