具有C,Pascal,Java,JavaScript,LaTeX3,Prolog,Perl,Scheme,Lua,Python,TeX / PGF的Perl / Tk
以下Perl脚本显示一个窗口,其中列出了π值和计算出的面积。π的值取自不同的语言,如下所示。
一文件脚本:
#!/usr/bin/env perl
use strict;
$^W=1;
use Tk;
use Tk::Font;
use Tk::HList;
use Tk::ItemStyle;
use Tk::PNG;
# Function to calculate the area of the circle with radius 3
sub A ($) {
use bignum;
return 9*$_[0];
}
my $title = 'Pi Day';
# Configuration of external program names
my %prg = qw[
Pascal fpc
Perl perl
Prolog swipl
Scheme guile1
TeX tex
LaTeX latex
];
sub prg ($) {
my $prg = shift;
return $prg{$prg} // $prg;
}
# Column headers
my @header = (
'',
'Language',
"\N{U+03C0}",
"A(3) = A(r) = \N{U+03C0}\N{U+2009}r\N{U+00B2}",
);
my $mw = MainWindow->new(
-title => $title,
);
# Font setup (larger font)
my $font_size = '22';
my $font = $mw->Font();
$font->configure(-size => $font_size);
# ---------
# Utilities
# ---------
# Run program in backticks, quote arguments if needed and some error checking
sub backticks_pi (@) {
my @cmd = map{/[ ()$;<>|\x22]/ && length > 1 ? "'$_'" : $_} @_;
print "[@cmd]\n";
my $catch = `@cmd`;
if ($? == -1) {
warn "Failed to execute: $!\n";
}
elsif ($? & 127) {
warn sprintf "Child died with signal %d!\n", $? & 127;
}
elsif ($?) {
warn sprintf "Child exited with value %d!\n", $? >> 8;
}
else {
return $1 if $catch =~ /^\s*(\d+\.\d+)\s*$/
or $catch =~ /\bpi\s*=\s*(\d+\.\d+)/;
}
warn "Could not find pi in the output of \"@cmd\"!\n";
return 0;
}
# Run a program with error checking
sub run_cmd (@) {
print "[@_]\n";
system @_;
if ($? == -1) {
warn "Failed to execute: $!\n";
}
elsif ($? & 127) {
warn sprintf "Child died with signal %d!\n", $? & 127;
}
elsif ($?) {
warn sprintf "Child exited with value %d!\n", $? >> 8;
}
else {
return $1;
}
return undef;
}
# Create a bitmap logo
sub make_logo ($$$@) {
my $name = shift;
my $logo = shift;
my $contents = shift;
my $file = "piday-logo-$name.tmp";
if ($contents) {
open(OUT, '>', $file) or die "!!! Error: Cannot write `$file': $!";
print OUT $contents;
close(OUT);
}
foreach (@_) {
run_cmd @$_;
}
return $mw->Photo(
-file => $logo,
) if -f $logo;
return undef;
}
# Call foreign language to calculate pi
sub make_pi ($$@) {
my $file = shift;
my $source = shift;
if ($source) {
open(OUT, '>', $file) or die "!!! Error: Cannot write `$file': $!";
print OUT $source;
close(OUT);
}
my $cmd_last = pop;
foreach (@_) {
run_cmd @$_;
}
return backticks_pi @$cmd_last;
}
# Add result list table
my $h = $mw->HList(
-header => 1,
-columns => scalar @header,
-width => 100,
-height => 20,
-font => $font,
)->pack(
-expand => 1,
-fill => 'both',
);
# Add header for the result list table
for (0 .. @header-1) {
$h->header('create', $_,
-text => $header[$_],
);
}
# Exit button
my $quit = $mw->Button(
-text => 'Quit',
-command => sub {exit},
-font => $font,
)->pack;
my @list;
my @cmd;
my $pi;
my $source;
my $img;
# GNU C
# -----
$img = make_logo(
'C',
'piday-logo-c.png',
'',
[
prg('wget'),
'-O', 'piday-logo-c-gccegg.png',
'http://gcc.gnu.org/img/gccegg-65.png',
],
[
prg('convert'),
'-scale', '54x64',
'piday-logo-c-gccegg.png',
'piday-logo-c.png',
],
);
$source = <<'END_SOURCE';
#define _GNU_SOURCE
#include <math.h>
#include <stdio.h>
#define xstr(s) str(s)
#define str(s) #s
int main() {
long double pi = M_PI;
printf("pi=%s", xstr(M_PIl));
return 0;
}
END_SOURCE
$pi = make_pi(
'piday-c.c',
$source,
[
prg('gcc'),
'-o', 'piday-c',
'piday-c.c',
],
[
prg('./piday-c')
],
);
push @list, {
language => 'GNU C',
pi => $pi,
image => $img,
};
# Java
# ----
$img = make_logo(
'Java',
'piday-java.png',
'',
[
prg('wget'),
'-O', 'piday-java.svg',
'https://upload.wikimedia.org/wikipedia/commons/a/a4/Java_logo_and_wordmark.svg',
],
[
prg('convert'),
'-scale', '35x64',
'piday-java.svg',
'piday-java.png',
],
);
$source = <<'END_SOURCE';
public class PiDayJava {
public static void main(String args[]) {
System.out.println(Math.PI);
}
}
END_SOURCE
$pi = make_pi(
'PiDayJava.java',
$source,
[
prg('javac'),
'PiDayJava.java',
],
[
prg('java'),
'PiDayJava',
],
);
push @list, {
language => 'Java',
pi => $pi,
image => $img,
};
# Perl
# ----
# Math/Complex.pm: sub pi () { 4 * CORE::atan2(1, 1) }
@cmd = (prg('Perl'), '-e', 'use Math::Complex; print pi');
$pi = backticks_pi @cmd;
my $img = Tk->findINC('Camel.xpm');
$img = $mw->Photo(
-file => $img,
);
push @list, {
language => 'Perl',
pi => $pi,
image => $img,
};
# Python
# ------
@cmd = (
prg('echo'),
'import math;print math.pi',
'|',
prg('python'),
);
$pi = backticks_pi @cmd;
$img = make_logo(
'python',
'piday-logo-python.png',
'',
[
prg('wget'),
'-O',
'piday-logo-python-master.png',
'http://www.python.org/static/community_logos/python-logo-master-v3-TM.png',
],
[
prg('convert'),
'-crop', '111x111+79+33',
'piday-logo-python-master.png',
'piday-logo-python-crop.png'
],
[
prg('convert'),
'-scale', '64x64',
'piday-logo-python-crop.png',
'piday-logo-python.png',
],
);
push @list, {
language => 'Python',
pi => $pi,
image => $img,
};
# TeX
# ---
@cmd = (
prg('TeX'),
'\input pgf \pgfmathparse{pi}\message{pi=\pgfmathresult}\end',
);
$pi = backticks_pi @cmd;
my $img = make_logo(
'tex',
'piday-logo-tex.png',
'',
[
prg('pdftex'),
'\mag=4000 \nopagenumbers\font\sc=cmcsc10 \sc pgf\bye'
],
[
prg('pdfcrop'),
'texput.pdf',
'piday-logo-tex.pdf',
],
[
prg('convert'),
'piday-logo-tex.pdf',
'piday-logo-tex.png',
]
);
push @list, {
language => 'TeX/PGF',
pi => $pi,
image => $img,
};
# LaTeX3
# ------
my $logo_source = <<'END_LOGO';
\mag=4000
\documentclass{article}
\usepackage{hologo}
\pagestyle{empty}
\begin{document}
\hologo{LaTeX3}
\end{document}
END_LOGO
$img = make_logo(
'latex3',
'piday-logo-latex3.png',
$logo_source,
[
prg('pdflatex'),
'piday-logo-latex3.tmp'
],
[
prg('pdfcrop'),
'piday-logo-latex3.pdf',
'piday-logo-latex3-crop.pdf',
],
[
prg('convert'),
'piday-logo-latex3-crop.pdf',
'piday-logo-latex3.png',
]
);
$source = <<'END_LATEX3';
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\msg_term:n { pi=\fp_eval:n { pi } }
\ExplSyntaxOff
\stop
END_LATEX3
$pi = make_pi(
'piday-latex3.tex',
$source,
[
prg('LaTeX'),
'piday-latex3.tex',
],
);
push @list, {
language => 'LaTeX3',
pi => $pi,
image => $img,
};
print "****************\n";
# Lua
# ---
$img = make_logo(
'Lua',
'piday-logo-lua.png',
'',
[
prg('wget'),
'-O', 'piday-logo-lua.gif',
'http://www.lua.org/images/lua-logo.gif',
],
[
prg('convert'),
'-scale', '64x64', # '50x50',
'piday-logo-lua.gif',
'piday-logo-lua.png',
],
);
$source = 'print(math.pi)';
$pi = make_pi(
'piday-lua.lua',
$source,
[
prg('texlua'),
'piday-lua.lua',
]
);
push @list, {
language => 'Lua',
pi => $pi,
image => $img,
};
# JavaScript
# ----------
$img = make_logo(
'JavaScript',
'piday-logo-javascript.png',
'',
[
prg('wget'),
'-O', 'piday-logo-rhino.jpg',
'https://developer.mozilla.org/@api/deki/files/832/=Rhino.jpg',
],
[
prg('convert'),
'-scale', '127x64',
'piday-logo-rhino.jpg',
'piday-logo-javascript.png',
],
);
$source = 'print(Math.PI)';
$pi = backticks_pi(
prg('java'),
'-cp', prg('js.jar'),
'org.mozilla.javascript.tools.shell.Main',
'-e', $source,
);
push @list, {
language => 'JavaScript',
pi => $pi,
image => $img,
};
# Scheme
# ------
$img = make_logo(
'Scheme',
'piday-logo-scheme.png',
'',
[
prg('wget'),
'-O', 'piday-logo-lambda.svg',
'https://upload.wikimedia.org/wikipedia/commons/3/39/Lambda_lc.svg',
],
[
prg('convert'),
'-scale', '64x64',
'piday-logo-lambda.svg',
'piday-logo-scheme.png',
],
);
$source = '(display (* 2 (acos 0)))';
$pi = backticks_pi(
prg('Scheme'),
'-c', $source,
);
push @list, {
language => 'Scheme',
pi => $pi,
image => $img,
};
# Prolog
# ------
$img = make_logo(
'Prolog',
'piday-logo-prolog.png',
'',
[
prg('wget'),
'-O', 'piday-logo-swipl.png',
'http://www.swi-prolog.org/icons/swipl.png',
],
[
prg('convert'),
'-scale', '78x64',
'piday-logo-swipl.png',
'piday-logo-prolog.png',
],
);
$source = ":- format('~15f~n', [pi]).\n";
$pi = make_pi(
'piday-prolog.pro',
$source,
[
prg('Prolog'),
'-c', 'piday-prolog.pro',
]
);
push @list, {
language => 'Prolog',
pi => $pi,
image => $img,
};
# Pascal
# ------
$img = make_logo(
'Pascal',
'piday-logo-pascal.gif',
'',
[
prg('wget'),
'-O', 'piday-logo-pascal.gif',
'http://www.freepascal.org/pic/logo.gif',
]
);
$source = <<'END_PASCAL';
program piday_pascal;
uses sysutils, math;
begin
writeln(format('%.16f', [pi]));
end.
END_PASCAL
$pi = make_pi(
'piday-pascal.pas',
$source,
[
prg('Pascal'),
'piday-pascal.pas',
],
[
prg('./piday-pascal'),
]
);
push @list, {
language => 'Pascal',
pi => $pi,
image => $img,
};
# Sort and fill the table rows
@list = sort {
my $diff = (length $b->{'pi'} <=> length $a->{'pi'});
return $diff if $diff;
return "\L$a->{'language'}\E" cmp "\L$b->{'language'}\E";
} @list;
foreach my $x (@list) {
my $e = $h->addchild("");
my $col = 0;
if ($x->{'image'}) {
$h->itemCreate($e, $col++,
-itemtype => 'image',
-image => $x->{'image'},
);
}
else {
$col++;
}
$h->itemCreate($e, $col++,
-itemtype => 'text',
-text => $x->{'language'},
);
$h->itemCreate($e, $col++,
-itemtype => 'text',
-text => $x->{'pi'},
);
$h->itemCreate($e, $col++,
-itemtype => 'text',
-text => A $x->{'pi'},
);
}
MainLoop;
__END__
语言能力
以下列表显示了用于获取π的语言和代码。
GNU C: GNU扩展用于获得更高的π精度。
#define _GNU_SOURCE
#include <math.h>
#include <stdio.h>
#define xstr(s) str(s)
#define str(s) #s
int main() {
long double pi = M_PI;
printf("pi=%s", xstr(M_PIl));
return 0;
}
Pascal:与Free Pascal一起编译。
program piday_pascal;
uses sysutils, math;
begin
writeln(format('%.16f', [pi]));
end.
Java:
public class PiDayJava {
public static void main(String args[]) {
System.out.println(Math.PI);
}
}
JavaScript: Rhino用于执行JavaScript。
print(Math.PI)
LaTeX3:
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\msg_term:n { pi=\fp_eval:n { pi } }
\ExplSyntaxOff
\stop
Prolog: SWI Prolog用作Prolog编译器。
:- format('~15f~n', [pi]).
Perl:为了乐趣和完整性。
use Math::Complex;
print pi;
Scheme:使用Scheme实现是GNU Guile。
(display (* 2 (acos 0)))
Lua: texlua
用作Lua解释器。
print(math.pi)
蟒蛇:
import math
print math.pi
TeX / PGF: π来自其对pgf包的定义,纯TeX用作TeX格式:
\input pgf
\pgfmathparse{pi}
\message{pi=\pgfmathresult}
\end