Calculation in one field 计算在一个领域中

huangapple go评论66阅读模式
英文:

Calculation in one field

问题

我是新手SQL和PL/SQL。为了练习,我被分配了一个制作计算器的任务。那部分已经可以工作了。但他们还希望在文本字段中输入计算,然后它需要起作用。例如,4+4(然后=按钮或键盘上的Enter键),或4+6-3=。

我的带有按钮的计算器可以工作,但如果我在文本字段中输入计算,则无法工作。有人可以帮助我吗?

这是我总计代码中的内容:

declare
    l_operator varchar2(1) := :P3_OPERATOR;
    l_value1 number := :P3_VALUE1;
    l_value2 number := :P3_VALUE2;
    l_result number := nvl(:P3_VALUE1,0);
begin
    case l_operator
        when '+' then
            l_result := l_value1 + l_value2;
        when '-' then
             l_result := l_value1 - l_value2;
        when '*' then
             l_result := l_value1 * l_value2;
        when '/' then
             l_result := l_value1 / l_value2;
        else
            null;
    end case;
    :P3_OPERATOR := null;
    :P3_VALUE2 := null;
    :P3_VALUE1 := l_result;    
    :P3_NUMBERFIELD := l_result;
end;

以及针对+、-、*和\的附加部分代码:

:P12_OPERATOR := '*';
:P12_NUMBERFIELD := :P12_OPERATOR;

这是所有我的数字按钮的代码:

begin
    if :P12_OPERATOR is null then
        :P12_VALUE1 := :P12_VALUE1 || 4;
        :P12_NUMBERFIELD := :P12_VALUE1;
    elsif :P12_OPERATOR is not null then
        :P12_VALUE2 := :P12_VALUE2 || 4;
        :P12_NUMBERFIELD := :P12_VALUE2;
    end if;
end;

希望这些代码对你有所帮助。

英文:

i'm new to sql and pl/sql. To practice I was giving an assignment to make a calculator. That part works. But they also want the possibility to type the calculation in the text field and then it needs to work. For example 4+4 (then the = button or enter on your keyboard) or 4+6-3=.

My calculator with buttons works, but not if I type a calculation in the text field. Can anyone help me with this?

This is the code I have in my total:

declare
    l_operator varchar2(1) := :P3_OPERATOR;
    l_value1 number := :P3_VALUE1;
    l_value2 number := :P3_VALUE2;
    l_result number := nvl(:P3_VALUE1,0);
    begin
    case l_operator
    when '+' then
        l_result := l_value1 + l_value2;
    when '-' then
         l_result := l_value1 - l_value2;
    when '*' then
         l_result := l_value1 * l_value2;
    when '/' then
         l_result := l_value1 / l_value2;
    else
        null;
    end case;
    :P3_OPERATOR := null;
    :P3_VALUE2 := null;
    :P3_VALUE1 := l_result;    
    :P3_NUMBERFIELD := l_result;
    end;

with this for als extra for the +, -, * and \ .

   :P12_OPERATOR := '*';
    :P12_NUMBERFIELD := :P12_OPERATOR;

and this is the code for all my number buttons:

 begin
    if :P12_OPERATOR is null then
    :P12_VALUE1 := :P12_VALUE1 || 4;
    :P12_NUMBERFIELD := :P12_VALUE1;
    elsif :P12_OPERATOR is not null then
    :P12_VALUE2 := :P12_VALUE2 || 4;
    :P12_NUMBERFIELD := :P12_VALUE2;
    end if;
    end; 

答案1

得分: 2

这不是使用SQL、PL/SQL或APEX的典型方式(看起来您也在使用APEX)!

您可以使用以下代码来评估输入的任何表达式:

begin
   execute immediate 'select ' || :P3_NUMBERFIELD || ' from dual' into l_result;
exception
   when others then
      l_result := '无效输入';
end;

异常部分是为了防止用户输入无意义的内容,如"hello world",而不是算术表达式。用户需要输入类似于 4+4 这样的表达式,不要输入等号,然后按按钮来触发计算结果的过程。

英文:

This is not a typical way to use SQL or PL/SQL (or APEX which it looks like you are also using)!

You could evaluate any expression typed in with code like this:

begin
   execute immediate 'select ' || :P3_NUMBERFIELD || ' from dual' into l_result;
exception
   when others then
      l_result := 'Invalid input';
end;

The exception part is to stop the calculator going wrong if the user types in nonsense like "hello world" instead of an arithmetic expression. The user would need to type in an expression like 4+4 without typing the equals sign, and then press a button to invoke the process to calculate the result.

huangapple
  • 本文由 发表于 2020年1月4日 00:05:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定