Parsing/RPN calculator algorithm: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(fixed V to point to correct lang (title vlang in rosetta code) and re-ordered it)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 147:
Val= 3
</pre>
 
 
=={{header|Ada}}==
Line 399 ⟶ 398:
Result is: +3.00012207
</pre>
 
=={{header|ANSI Standard BASIC}}==
<lang ANSI Standard BASIC>1000 DECLARE EXTERNAL SUB rpn
Line 847:
return 0;
}</lang>
 
=={{header|C++}}==
<lang cpp>#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <cstdlib>
 
double rpn(const std::string &expr){
std::istringstream iss(expr);
std::vector<double> stack;
std::cout << "Input\tOperation\tStack after" << std::endl;
std::string token;
while (iss >> token) {
std::cout << token << "\t";
double tokenNum;
if (std::istringstream(token) >> tokenNum) {
std::cout << "Push\t\t";
stack.push_back(tokenNum);
} else {
std::cout << "Operate\t\t";
double secondOperand = stack.back();
stack.pop_back();
double firstOperand = stack.back();
stack.pop_back();
if (token == "*")
stack.push_back(firstOperand * secondOperand);
else if (token == "/")
stack.push_back(firstOperand / secondOperand);
else if (token == "-")
stack.push_back(firstOperand - secondOperand);
else if (token == "+")
stack.push_back(firstOperand + secondOperand);
else if (token == "^")
stack.push_back(std::pow(firstOperand, secondOperand));
else { //just in case
std::cerr << "Error" << std::endl;
std::exit(1);
}
}
std::copy(stack.begin(), stack.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;
}
return stack.back();
}
 
int main() {
std::string s = " 3 4 2 * 1 5 - 2 3 ^ ^ / + ";
std::cout << "Final answer: " << rpn(s) << std::endl;
return 0;
}</lang>
{{out}}
<pre>
Input Operation Stack after
3 Push 3
4 Push 3 4
2 Push 3 4 2
* Operate 3 8
1 Push 3 8 1
5 Push 3 8 1 5
- Operate 3 8 -4
2 Push 3 8 -4 2
3 Push 3 8 -4 2 3
^ Operate 3 8 -4 8
^ Operate 3 8 65536
/ Operate 3 0.00012207
+ Operate 3.00012
Final answer: 3.00012
</pre>
 
=={{header|C sharp|C#}}==
Line 1,041 ⟶ 968:
 
Result is 3.0001220703125
</pre>
 
=={{header|C++}}==
<lang cpp>#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <cstdlib>
 
double rpn(const std::string &expr){
std::istringstream iss(expr);
std::vector<double> stack;
std::cout << "Input\tOperation\tStack after" << std::endl;
std::string token;
while (iss >> token) {
std::cout << token << "\t";
double tokenNum;
if (std::istringstream(token) >> tokenNum) {
std::cout << "Push\t\t";
stack.push_back(tokenNum);
} else {
std::cout << "Operate\t\t";
double secondOperand = stack.back();
stack.pop_back();
double firstOperand = stack.back();
stack.pop_back();
if (token == "*")
stack.push_back(firstOperand * secondOperand);
else if (token == "/")
stack.push_back(firstOperand / secondOperand);
else if (token == "-")
stack.push_back(firstOperand - secondOperand);
else if (token == "+")
stack.push_back(firstOperand + secondOperand);
else if (token == "^")
stack.push_back(std::pow(firstOperand, secondOperand));
else { //just in case
std::cerr << "Error" << std::endl;
std::exit(1);
}
}
std::copy(stack.begin(), stack.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;
}
return stack.back();
}
 
int main() {
std::string s = " 3 4 2 * 1 5 - 2 3 ^ ^ / + ";
std::cout << "Final answer: " << rpn(s) << std::endl;
return 0;
}</lang>
{{out}}
<pre>
Input Operation Stack after
3 Push 3
4 Push 3 4
2 Push 3 4 2
* Operate 3 8
1 Push 3 8 1
5 Push 3 8 1 5
- Operate 3 8 -4
2 Push 3 8 -4 2
3 Push 3 8 -4 2 3
^ Operate 3 8 -4 8
^ Operate 3 8 65536
/ Operate 3 0.00012207
+ Operate 3.00012
Final answer: 3.00012
</pre>
 
Line 1,350:
+: 24577/8192
24577/8192</pre>
 
=={{header|D}}==
{{trans|Go}}
<lang d>import std.stdio, std.string, std.conv, std.typetuple;
 
void main() {
auto input = "3 4 2 * 1 5 - 2 3 ^ ^ / +";
writeln("For postfix expression: ", input);
writeln("\nToken Action Stack");
real[] stack;
foreach (tok; input.split()) {
auto action = "Apply op to top of stack";
switch (tok) {
foreach (o; TypeTuple!("+", "-", "*", "/", "^")) {
case o:
mixin("stack[$ - 2]" ~
(o == "^" ? "^^" : o) ~ "=stack[$ - 1];");
stack.length--;
break;
}
break;
default:
action = "Push num onto top of stack";
stack ~= to!real(tok);
}
writefln("%3s %-26s %s", tok, action, stack);
}
writeln("\nThe final value is ", stack[0]);
}</lang>
{{out}}
<pre>For postfix expression: 3 4 2 * 1 5 - 2 3 ^ ^ / +
 
Token Action Stack
3 Push num onto top of stack [3]
4 Push num onto top of stack [3, 4]
2 Push num onto top of stack [3, 4, 2]
* Apply op to top of stack [3, 8]
1 Push num onto top of stack [3, 8, 1]
5 Push num onto top of stack [3, 8, 1, 5]
- Apply op to top of stack [3, 8, -4]
2 Push num onto top of stack [3, 8, -4, 2]
3 Push num onto top of stack [3, 8, -4, 2, 3]
^ Apply op to top of stack [3, 8, -4, 8]
^ Apply op to top of stack [3, 8, 65536]
/ Apply op to top of stack [3, 0.00012207]
+ Apply op to top of stack [3.00012]
 
The final value is 3.00012</pre>
 
=={{header|EchoLisp}}==
Line 1,408 ⟶ 1,456:
→ 24577/8192
</pre>
 
 
=={{header|Ela}}==
Line 1,470 ⟶ 1,517:
+ Operate [3.000122f]
Result: 3.000122f</pre>
 
=={{header|D}}==
{{trans|Go}}
<lang d>import std.stdio, std.string, std.conv, std.typetuple;
 
void main() {
auto input = "3 4 2 * 1 5 - 2 3 ^ ^ / +";
writeln("For postfix expression: ", input);
writeln("\nToken Action Stack");
real[] stack;
foreach (tok; input.split()) {
auto action = "Apply op to top of stack";
switch (tok) {
foreach (o; TypeTuple!("+", "-", "*", "/", "^")) {
case o:
mixin("stack[$ - 2]" ~
(o == "^" ? "^^" : o) ~ "=stack[$ - 1];");
stack.length--;
break;
}
break;
default:
action = "Push num onto top of stack";
stack ~= to!real(tok);
}
writefln("%3s %-26s %s", tok, action, stack);
}
writeln("\nThe final value is ", stack[0]);
}</lang>
{{out}}
<pre>For postfix expression: 3 4 2 * 1 5 - 2 3 ^ ^ / +
 
Token Action Stack
3 Push num onto top of stack [3]
4 Push num onto top of stack [3, 4]
2 Push num onto top of stack [3, 4, 2]
* Apply op to top of stack [3, 8]
1 Push num onto top of stack [3, 8, 1]
5 Push num onto top of stack [3, 8, 1, 5]
- Apply op to top of stack [3, 8, -4]
2 Push num onto top of stack [3, 8, -4, 2]
3 Push num onto top of stack [3, 8, -4, 2, 3]
^ Apply op to top of stack [3, 8, -4, 8]
^ Apply op to top of stack [3, 8, 65536]
/ Apply op to top of stack [3, 0.00012207]
+ Apply op to top of stack [3.00012]
 
The final value is 3.00012</pre>
 
=={{header|Erlang}}==
Line 2,521 ⟶ 2,520:
Result:3.00012207
</pre>
 
=={{header|Lua}}==
<lang lua>
Line 2,702:
 
</pre >
 
 
=={{header|Mathematica}}==
Line 3,505 ⟶ 3,504:
The reverse polish expression = 3 4 2 * 1 5 - 2 3 ^ ^ / +
The evaluated expression = 3.0001220703125
</pre>
 
=={{header|PARI/GP}}==
Line 3,603 ⟶ 3,602:
(3)+(0.0001220703125)
3.0001220703125</pre>
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-09-25}}
<lang perl6>my $proggie = '3 4 2 * 1 5 - 2 3 ^ ^ / +';
 
class RPN is Array {
 
method binop(&op) { self.push: self.pop R[&op] self.pop }
 
method run($p) {
for $p.words {
say "$_ ({self})";
when /\d/ { self.push: $_ }
when '+' { self.binop: &[+] }
when '-' { self.binop: &[-] }
when '*' { self.binop: &[*] }
when '/' { self.binop: &[/] }
when '^' { self.binop: &[**] }
default { die "$_ is bogus" }
}
say self;
}
}
 
RPN.new.run($proggie);</lang>
{{out}}
<pre>3 ()
4 (3)
2 (3 4)
* (3 4 2)
1 (3 8)
5 (3 8 1)
- (3 8 1 5)
2 (3 8 -4)
3 (3 8 -4 2)
^ (3 8 -4 2 3)
^ (3 8 -4 8)
/ (3 8 65536)
+ (3 0.0001220703125)
3.0001220703125</pre>
 
=={{header|Phix}}==
Line 4,212 ⟶ 4,171:
(calculate-RPN (in-port read (open-input-string "3.0 4 2 * 1 5 - 2 3 ^ ^ / +")))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-25}}
<lang perl6>my $proggie = '3 4 2 * 1 5 - 2 3 ^ ^ / +';
 
class RPN is Array {
 
method binop(&op) { self.push: self.pop R[&op] self.pop }
 
method run($p) {
for $p.words {
say "$_ ({self})";
when /\d/ { self.push: $_ }
when '+' { self.binop: &[+] }
when '-' { self.binop: &[-] }
when '*' { self.binop: &[*] }
when '/' { self.binop: &[/] }
when '^' { self.binop: &[**] }
default { die "$_ is bogus" }
}
say self;
}
}
 
RPN.new.run($proggie);</lang>
{{out}}
<pre>3 ()
4 (3)
2 (3 4)
* (3 4 2)
1 (3 8)
5 (3 8 1)
- (3 8 1 5)
2 (3 8 -4)
3 (3 8 -4 2)
^ (3 8 -4 2 3)
^ (3 8 -4 8)
/ (3 8 65536)
+ (3 0.0001220703125)
3.0001220703125</pre>
 
=={{header|REXX}}==
Line 4,691:
{{out}}
<pre>"postfix: 3 4 2 * 1 5 - 2 3 ^ ^ / +"</pre>
 
 
=={{header|Tcl}}==
Line 5,076 ⟶ 5,075:
 
Result: 3.000122</pre>
 
 
=={{header|Xojo}}==
Line 5,164 ⟶ 5,162:
 
Result: 3.000122</pre>
 
 
 
 
=={{header|zkl}}==
10,333

edits