想象一下
exp(49/200)+(x-49/200)
我想将除加法或减法之外的任何运算作为函数“roundn”的参数传递,所以我的表达式变成了
roundn(exp(roundn(49/200,n)),n)+(x - roundn(49/200,n)
我想要操纵的表达式是这样的:
exp(49/200)+exp(49/200)*(x-49/200)+1/2*exp(49/200)*(x-49/200)^2+1/6*exp(49/200)*(x- 49/200)^3+1/24*exp(49/200)*(x-49/200)^4+1/120*exp(49/200)*(x-49/200)^5+1/720*exp(49/200)*(x-49/200)^6+1/5040*exp(49/200)*(x-49/200)^7+1/40320*exp(49/200)*(x-49/200)^8+1/362880*exp(49/200)*(x-49/200)^9+1/3628800*exp(49/200)*(x-49/200)^10+1/39916800*exp(49/200)*(x-49/200)^11
我正在寻找一种不基于语言编程的方法(包括任何程序),尽可能多地使用批处理或类似的东西......
答案1
尝试这个:
ro='roundn(' # roundn open
rc=',n)' # roundn close
fun='exp\('
expression='exp(49/200)+(x-49/200)'
echo "$expression" |
perl -pe "s/$fun[^)]*\K\)/)$rc/g; s/(?<!\^)[0-9\/*]+[0-9]/$ro\$&$rc/g; s/$fun[^)]*/$ro\$&/g"
这应该给你:
roundn(exp(roundn(49/200,n)),n)+(x-roundn(49/200,n))
您的较长表达应导致:
roundn(exp(roundn(49/200,n)),n)+roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))+roundn(1/2,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))^2+roundn(1/6,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))^3+roundn(1/24,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))^4+roundn(1/120,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))^5+roundn(1/720,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))^6+roundn(1/5040,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))^7+roundn(1/40320,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))^8+roundn(1/362880,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))^9+roundn(1/3628800,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n))^10+roundn(1/39916800,n)*roundn(exp(roundn(49/200,n)),n)*
(x-roundn(49/200,n) ^11
解释
/exp\([^)]*\K\)/)$rc/g
- 关闭 exp(),添加 roundn close- 对于以“exp(”开头并以“)”结尾的字符串
\K
使“exp(”成为零宽度匹配,因此只有右括号被替换
s/(?<!\^)[0-9\/*]+[0-9]/$ro\$&$rc/g
- 带有乘数和除数的数字串,用 roundn open 和 roundn close 包围- 不以“^”开头但可能包含“/”或“*”的数字字符串
- 总字符数必须为两个或更多 - 可能有更好的方法来实现这一点
- carat 是负向后视(零宽度),因此在进行替换时不包括它
s/exp\([^)]*/$ro\$&/g
- 打开 exp(),添加 roundn 打开- 在“exp(”后跟零个或多个非“)”字符之前,添加 roundn open
$ro
和$rc
是$fun
shell 变量- 将 Perl 脚本括在双引号中可以扩展这些变量
$&
包含除零宽度部分之外的整个匹配项- 转义可能没有必要,但我还是这么做了,以防万一——为了避免混淆 shell
如果有多个函数,实现起来并不难。但是,如果函数嵌套,它可能会完全崩溃。
编辑:
以下是 Perl 脚本版本:
$ro = "roundn(";
$rc = ",n)";
$fun = "exp\\(";
while (<>) {
s/$fun[^)]*\K\)/)$rc/g;
s/(?<!\^)[0-9\/*]+[0-9]/$ro$&$rc/g;
s/$fun[^)]*/$ro$&/g;
print
}
像这样运行:
perl script.pl < data.txt
答案2
好吧,我正在尝试执行你的代码...我用你写的第一段代码制作了一个小程序(practicalcase1.pl):
ro='roundn(' # roundn open
rc=',n)' # roundn close
fun='exp\('
expression='exp(49/200)+(x-49/200)'
echo "$expression" |
perl -pe "s/$fun[^)]*\K\)/)$rc/g; s/(?<!\^)[0-9\/*]+[0-9]/$ro\$&$rc/g; s/$fun[^)]*/$ro\$&/g"
但是我在 Windows 的 cmd 上收到了一些消息:
Semicolon seems to be missing at practicalcase1.pl line 1
Semicolon seems to be missing at practicalcase1.pl line 2
Semicolon seems to be missing at practicalcase1.pl line 3
Semicolon seems to be missing at practicalcase1.pl line 4
String found where operator expected at practicalcase1.pl line 5, near "echo "$expression""
(Do you need to predeclare echo?)
String found where operator expected at practicalcase1.pl line 6, near "pe "s/$fun[^)]*\K\)/)$rc/g; s/(?<!\^)[0-9\/*]+[0-9]/$ro\$&$rc/g""
(Do you need to predeclare pe?)
Can´t modify constant item in scalar assigment at practicalcase1.pl line 2, near "rc"
syntax error at practicalcase1.pl line 2, near "rc"
Execution of practicalcase1.pl aborted due to compilation errors.
我该如何处理“分号似乎...”类型的错误?我应该如何处理“在运算符...处找到字符串”错误?“无法修改标量中的常量项...”怎么办?“practicalcase1.pl 第 2 行语法错误...”怎么办?
太感谢了!