Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,68 +0,0 @@
|
|||
with Ada.Text_IO, Ada.Containers.Vectors;
|
||||
|
||||
procedure RPN_Calculator is
|
||||
|
||||
package IIO is new Ada.Text_IO.Float_IO(Float);
|
||||
|
||||
package Float_Vec is new Ada.Containers.Vectors
|
||||
(Index_Type => Positive, Element_Type => Float);
|
||||
Stack: Float_Vec.Vector;
|
||||
|
||||
Input: String := Ada.Text_IO.Get_Line;
|
||||
Cursor: Positive := Input'First;
|
||||
New_Cursor: Positive;
|
||||
|
||||
begin
|
||||
loop
|
||||
-- read spaces
|
||||
while Cursor <= Input'Last and then Input(Cursor)=' ' loop
|
||||
Cursor := Cursor + 1;
|
||||
end loop;
|
||||
|
||||
exit when Cursor > Input'Last;
|
||||
|
||||
New_Cursor := Cursor;
|
||||
while New_Cursor <= Input'Last and then Input(New_Cursor) /= ' ' loop
|
||||
New_Cursor := New_Cursor + 1;
|
||||
end loop;
|
||||
|
||||
-- try to read a number and push it to the stack
|
||||
declare
|
||||
Last: Positive;
|
||||
Value: Float;
|
||||
X, Y: Float;
|
||||
begin
|
||||
IIO.Get(From => Input(Cursor .. New_Cursor - 1),
|
||||
Item => Value,
|
||||
Last => Last);
|
||||
Stack.Append(Value);
|
||||
Cursor := New_Cursor;
|
||||
|
||||
exception -- if reading the number fails, try to read an operator token
|
||||
when others =>
|
||||
Y := Stack.Last_Element; Stack.Delete_Last; -- pick two elements
|
||||
X := Stack.Last_Element; Stack.Delete_Last; -- from the stack
|
||||
case Input(Cursor) is
|
||||
when '+' => Stack.Append(X+Y);
|
||||
when '-' => Stack.Append(X-Y);
|
||||
when '*' => Stack.Append(X*Y);
|
||||
when '/' => Stack.Append(X/Y);
|
||||
when '^' => Stack.Append(X ** Integer(Float'Rounding(Y)));
|
||||
when others => raise Program_Error with "unecpected token '"
|
||||
& Input(Cursor) & "' at column" & Integer'Image(Cursor);
|
||||
end case;
|
||||
Cursor := New_Cursor;
|
||||
end;
|
||||
|
||||
for I in Stack.First_Index .. Stack.Last_Index loop
|
||||
Ada.Text_IO.Put(" ");
|
||||
IIO.Put(Stack.Element(I), Aft => 5, Exp => 0);
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end loop;
|
||||
|
||||
Ada.Text_IO.Put("Result = ");
|
||||
IIO.Put(Item => Stack.Last_Element, Aft => 5, Exp => 0);
|
||||
|
||||
|
||||
end RPN_Calculator;
|
||||
|
|
@ -1,126 +0,0 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. RPN.
|
||||
AUTHOR. Bill Gunshannon.
|
||||
INSTALLATION.
|
||||
DATE-WRITTEN. 9 Feb 2020.
|
||||
************************************************************
|
||||
** Program Abstract:
|
||||
** Create a stack-based evaluator for an expression in
|
||||
** reverse Polish notation (RPN) that also shows the
|
||||
** changes in the stack as each individual token is
|
||||
** processed as a table.
|
||||
************************************************************
|
||||
|
||||
DATA DIVISION.
|
||||
|
||||
|
||||
WORKING-STORAGE SECTION.
|
||||
|
||||
01 LineIn PIC X(25).
|
||||
01 IP PIC 99
|
||||
VALUE 1.
|
||||
01 CInNum PIC XXXX.
|
||||
|
||||
01 Stack PIC S999999V9999999
|
||||
OCCURS 50 times.
|
||||
01 SP PIC 99
|
||||
VALUE 1.
|
||||
01 Operator PIC X.
|
||||
01 Value1 PIC S999999V9999999.
|
||||
01 Value2 PIC S999999V9999999.
|
||||
01 Result PIC S999999V9999999.
|
||||
01 Idx PIC 99.
|
||||
01 FormatNum PIC ZZZZZZ9.9999999.
|
||||
01 Zip PIC X.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
|
||||
Main-Program.
|
||||
DISPLAY "Enter the RPN Equation: "
|
||||
WITH NO ADVANCING.
|
||||
ACCEPT LineIn.
|
||||
|
||||
PERFORM UNTIL IP GREATER THAN
|
||||
FUNCTION STORED-CHAR-LENGTH(LineIn)
|
||||
|
||||
|
||||
UNSTRING LineIn DELIMITED BY SPACE INTO CInNum
|
||||
WITH POINTER IP
|
||||
|
||||
MOVE CInNum TO Operator
|
||||
|
||||
PERFORM Do-Operation
|
||||
|
||||
PERFORM Show-Stack
|
||||
|
||||
END-PERFORM.
|
||||
|
||||
DISPLAY "End Result: " FormatNum
|
||||
|
||||
STOP RUN.
|
||||
|
||||
Do-Operation.
|
||||
|
||||
EVALUATE Operator
|
||||
WHEN "+"
|
||||
PERFORM Pop
|
||||
Compute Result = Value2 + Value1
|
||||
PERFORM Push
|
||||
|
||||
WHEN "-"
|
||||
PERFORM Pop
|
||||
Compute Result = Value2 - Value1
|
||||
PERFORM Push
|
||||
|
||||
WHEN "*"
|
||||
PERFORM Pop
|
||||
Compute Result = Value2 * Value1
|
||||
PERFORM Push
|
||||
|
||||
WHEN "/"
|
||||
PERFORM Pop
|
||||
Compute Result = Value2 / Value1
|
||||
PERFORM Push
|
||||
|
||||
WHEN "^"
|
||||
PERFORM Pop
|
||||
Compute Result = Value2 ** Value1
|
||||
PERFORM Push
|
||||
|
||||
WHEN NUMERIC
|
||||
MOVE Operator TO Result
|
||||
PERFORM Push
|
||||
END-EVALUATE.
|
||||
|
||||
|
||||
Show-Stack.
|
||||
|
||||
DISPLAY "STACK: " WITH NO ADVANCING.
|
||||
MOVE 1 TO Idx.
|
||||
PERFORM UNTIL (Idx = SP)
|
||||
MOVE Stack(Idx) TO FormatNum
|
||||
IF Stack(Idx) IS NEGATIVE
|
||||
THEN
|
||||
DISPLAY " -" FUNCTION TRIM(FormatNum)
|
||||
WITH NO ADVANCING
|
||||
ELSE
|
||||
DISPLAY FormatNum WITH NO ADVANCING
|
||||
END-IF
|
||||
ADD 1 to Idx
|
||||
END-PERFORM.
|
||||
DISPLAY " ".
|
||||
|
||||
Push.
|
||||
|
||||
MOVE Result TO Stack(SP)
|
||||
ADD 1 TO SP.
|
||||
|
||||
Pop.
|
||||
|
||||
SUBTRACT 1 FROM SP
|
||||
MOVE Stack(SP) TO Value1
|
||||
SUBTRACT 1 FROM SP
|
||||
MOVE Stack(SP) TO Value2.
|
||||
|
||||
|
||||
END-PROGRAM.
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use feature 'say';
|
||||
|
||||
my $number = '[+-]?(?:\.\d+|\d+(?:\.\d*)?)';
|
||||
my $operator = '[-+*/^]';
|
||||
|
||||
my @tests = ('3 4 2 * 1 5 - 2 3 ^ ^ / +');
|
||||
|
||||
for (@tests) {
|
||||
while (
|
||||
s/ \s* ((?<left>$number)) # 1st operand
|
||||
\s+ ((?<right>$number)) # 2nd operand
|
||||
\s+ ((?<op>$operator)) # operator
|
||||
(?:\s+|$) # more to parse, or done?
|
||||
/
|
||||
' '.evaluate().' ' # substitute results of evaluation
|
||||
/ex
|
||||
) {}
|
||||
say;
|
||||
}
|
||||
|
||||
sub evaluate {
|
||||
(my $a = "($+{left})$+{op}($+{right})") =~ s/\^/**/;
|
||||
say $a;
|
||||
eval $a;
|
||||
}
|
||||
|
|
@ -1,132 +0,0 @@
|
|||
function Invoke-Rpn
|
||||
{
|
||||
<#
|
||||
.SYNOPSIS
|
||||
A stack-based evaluator for an expression in reverse Polish notation.
|
||||
.DESCRIPTION
|
||||
A stack-based evaluator for an expression in reverse Polish notation.
|
||||
|
||||
All methods in the Math and Decimal classes are available.
|
||||
.PARAMETER Expression
|
||||
A space separated, string of tokens.
|
||||
.PARAMETER DisplayState
|
||||
This switch shows the changes in the stack as each individual token is processed as a table.
|
||||
.EXAMPLE
|
||||
Invoke-Rpn -Expression "3 4 Max"
|
||||
.EXAMPLE
|
||||
Invoke-Rpn -Expression "3 4 Log2"
|
||||
.EXAMPLE
|
||||
Invoke-Rpn -Expression "3 4 2 * 1 5 - 2 3 ^ ^ / +"
|
||||
.EXAMPLE
|
||||
Invoke-Rpn -Expression "3 4 2 * 1 5 - 2 3 ^ ^ / +" -DisplayState
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[AllowEmptyString()]
|
||||
[string]
|
||||
$Expression,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[switch]
|
||||
$DisplayState
|
||||
)
|
||||
Begin
|
||||
{
|
||||
function Out-State ([System.Collections.Stack]$Stack)
|
||||
{
|
||||
$array = $Stack.ToArray()
|
||||
[Array]::Reverse($array)
|
||||
$array | ForEach-Object -Process { Write-Host ("{0,-8:F3}" -f $_) -NoNewline } -End { Write-Host }
|
||||
}
|
||||
|
||||
function New-RpnEvaluation
|
||||
{
|
||||
$stack = New-Object -Type System.Collections.Stack
|
||||
|
||||
$shortcuts = @{
|
||||
"+" = "Add"; "-" = "Subtract"; "/" = "Divide"; "*" = "Multiply"; "%" = "Remainder"; "^" = "Pow"
|
||||
}
|
||||
|
||||
:ARGUMENT_LOOP foreach ($argument in $args)
|
||||
{
|
||||
if ($DisplayState -and $stack.Count)
|
||||
{
|
||||
Out-State $stack
|
||||
}
|
||||
|
||||
if ($shortcuts[$argument])
|
||||
{
|
||||
$argument = $shortcuts[$argument]
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$stack.Push([decimal]$argument)
|
||||
continue
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
$argCountList = $argument -replace "(\D+)(\d*)",‘$2’
|
||||
$operation = $argument.Substring(0, $argument.Length – $argCountList.Length)
|
||||
|
||||
foreach($type in [Decimal],[Math])
|
||||
{
|
||||
if ($definition = $type::$operation)
|
||||
{
|
||||
if (-not $argCountList)
|
||||
{
|
||||
$argCountList = $definition.OverloadDefinitions |
|
||||
Foreach-Object { ($_ -split ", ").Count } |
|
||||
Sort-Object -Unique
|
||||
}
|
||||
|
||||
foreach ($argCount in $argCountList)
|
||||
{
|
||||
try
|
||||
{
|
||||
$methodArguments = $stack.ToArray()[($argCount–1)..0]
|
||||
$result = $type::$operation.Invoke($methodArguments)
|
||||
|
||||
$null = 1..$argCount | Foreach-Object { $stack.Pop() }
|
||||
|
||||
$stack.Push($result)
|
||||
|
||||
continue ARGUMENT_LOOP
|
||||
}
|
||||
catch
|
||||
{
|
||||
## If error, try with the next number of arguments
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($DisplayState -and $stack.Count)
|
||||
{
|
||||
Out-State $stack
|
||||
if ($stack.Count)
|
||||
{
|
||||
Write-Host "`nResult = $($stack.Peek())"
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$stack
|
||||
}
|
||||
}
|
||||
}
|
||||
Process
|
||||
{
|
||||
Invoke-Expression -Command "New-RpnEvaluation $Expression"
|
||||
}
|
||||
End
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
Invoke-Rpn -Expression "3 4 2 * 1 5 - 2 3 ^ ^ / +" -DisplayState
|
||||
|
|
@ -20,7 +20,7 @@ fn (mut stack Stack) push(value f32) {
|
|||
stack.depth++
|
||||
}
|
||||
|
||||
fn (mut stack Stack) pop() ?f32 {
|
||||
fn (mut stack Stack) pop() !f32 {
|
||||
if stack.depth > 0 {
|
||||
stack.depth--
|
||||
result := stack.data[stack.depth]
|
||||
|
|
@ -29,7 +29,7 @@ fn (mut stack Stack) pop() ?f32 {
|
|||
return error('Stack Underflow!!')
|
||||
}
|
||||
|
||||
fn (stack Stack) peek() ?f32 {
|
||||
fn (stack Stack) peek() !f32 {
|
||||
if stack.depth > 0 {
|
||||
result := stack.data[0]
|
||||
return result
|
||||
|
|
@ -37,7 +37,7 @@ fn (stack Stack) peek() ?f32 {
|
|||
return error('Out of Bounds...')
|
||||
}
|
||||
|
||||
fn (mut stack Stack) rpn(input string) ?f32 {
|
||||
fn (mut stack Stack) rpn(input string) !f32 {
|
||||
println('Input: $input')
|
||||
tokens := input.split(' ')
|
||||
mut a := f32(0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue