将 truth table 打印到 odt 文件
print truth table in to odt file
我想将真相 table 打印到 adt 文件中的 table,我有一个程序,但我不知道如何获取打印到 odt 文件的值或值,这个程序只需在屏幕上打印结果!
sub truth_table {
my $s = shift;
#print "$s\n";
my @vars;
for ($s =~ /([a-zA-Z_]\w*)/g) {
push @vars, $_ ;
}
#print "$s\n";
#print "$_\n";
#print Dumper \@vars;
#print "\n", join("\t", @vars, $s), "\n", '-' x 40, "\n";
#print Dumper \@vars;
@vars = map("$$_", @vars);
$s =~ s/([a-zA-Z_]\w*)/$/g;
$s = "print(".join(',"\t",', map("($_?'1':'0')", @vars, $s)).",\"\n\")";
$s = "for my $_ (0, 1) { $s }" for (reverse @vars);
eval $s;
}
truth_table 'A ^ A_1';
用Capture::Tiny, then split the string into a two-dimensional array based on 得到eval
的结果。
use Capture::Tiny 'capture_stdout';
sub truth_table {
#...the rest of your code here...
my $stdout = capture_stdout {
eval $s;
};
return $stdout;
}
$truth_string = truth_table 'A ^ A_1';
my @truth_array;
foreach my $line (split "\n", $truth_string) {
push @truth_array, [split ' ', $line];
}
foreach my $line (@truth_array) {
foreach my $val (@$line) {
print $val;
}
print "\n";
}
为此,我根据 What's the easiest way to install a missing Perl module?
执行了以下命令
cpan
install Capture::Tiny
但是,我会在 LibreOffice 中使用 Python 宏来解决这个问题。 APSO 方便输入和 运行 此代码。
import uno
from itertools import product
def truth_table():
NUM_VARS = 2 # A and B
columns = NUM_VARS + 1
rows = pow(2, NUM_VARS) + 1
oDoc = XSCRIPTCONTEXT.getDocument()
oText = oDoc.getText()
oCursor = oText.createTextCursorByRange(oText.getStart())
oTable = oDoc.createInstance("com.sun.star.text.TextTable")
oTable.initialize(rows, columns)
oText.insertTextContent(oCursor, oTable, False)
for column, heading in enumerate(("A", "B", "A ^ B")):
oTable.getCellByPosition(column, 0).setString(heading)
row = 1 # the second row
for p in product((0, 1), repeat=NUM_VARS):
result = truth_function(*p)
for column in range(NUM_VARS):
oTable.getCellByPosition(column, row).setString(p[column])
oTable.getCellByPosition(column + 1, row).setString(result)
row += 1
def truth_function(x, y):
return pow(x, y);
g_exportedScripts = truth_table,
这样使用product
是基于Creating a truth table for any expression in Python.
有关 Python-UNO 的更多文档可在 https://wiki.openoffice.org/wiki/Python 找到。
我想将真相 table 打印到 adt 文件中的 table,我有一个程序,但我不知道如何获取打印到 odt 文件的值或值,这个程序只需在屏幕上打印结果!
sub truth_table {
my $s = shift;
#print "$s\n";
my @vars;
for ($s =~ /([a-zA-Z_]\w*)/g) {
push @vars, $_ ;
}
#print "$s\n";
#print "$_\n";
#print Dumper \@vars;
#print "\n", join("\t", @vars, $s), "\n", '-' x 40, "\n";
#print Dumper \@vars;
@vars = map("$$_", @vars);
$s =~ s/([a-zA-Z_]\w*)/$/g;
$s = "print(".join(',"\t",', map("($_?'1':'0')", @vars, $s)).",\"\n\")";
$s = "for my $_ (0, 1) { $s }" for (reverse @vars);
eval $s;
}
truth_table 'A ^ A_1';
用Capture::Tiny, then split the string into a two-dimensional array based on 得到eval
的结果。
use Capture::Tiny 'capture_stdout';
sub truth_table {
#...the rest of your code here...
my $stdout = capture_stdout {
eval $s;
};
return $stdout;
}
$truth_string = truth_table 'A ^ A_1';
my @truth_array;
foreach my $line (split "\n", $truth_string) {
push @truth_array, [split ' ', $line];
}
foreach my $line (@truth_array) {
foreach my $val (@$line) {
print $val;
}
print "\n";
}
为此,我根据 What's the easiest way to install a missing Perl module?
执行了以下命令cpan
install Capture::Tiny
但是,我会在 LibreOffice 中使用 Python 宏来解决这个问题。 APSO 方便输入和 运行 此代码。
import uno
from itertools import product
def truth_table():
NUM_VARS = 2 # A and B
columns = NUM_VARS + 1
rows = pow(2, NUM_VARS) + 1
oDoc = XSCRIPTCONTEXT.getDocument()
oText = oDoc.getText()
oCursor = oText.createTextCursorByRange(oText.getStart())
oTable = oDoc.createInstance("com.sun.star.text.TextTable")
oTable.initialize(rows, columns)
oText.insertTextContent(oCursor, oTable, False)
for column, heading in enumerate(("A", "B", "A ^ B")):
oTable.getCellByPosition(column, 0).setString(heading)
row = 1 # the second row
for p in product((0, 1), repeat=NUM_VARS):
result = truth_function(*p)
for column in range(NUM_VARS):
oTable.getCellByPosition(column, row).setString(p[column])
oTable.getCellByPosition(column + 1, row).setString(result)
row += 1
def truth_function(x, y):
return pow(x, y);
g_exportedScripts = truth_table,
这样使用product
是基于Creating a truth table for any expression in Python.
有关 Python-UNO 的更多文档可在 https://wiki.openoffice.org/wiki/Python 找到。