我想通过电子邮件将一小段代码发送给我的数学老师,以便证明一点(演示我在作业中写的东西).代码需要是:
我可以用什么语言?
linux windows scripting programming-languages cross-platform
为了进行比较,我正在编写某个递归函数的几个相似版本.我的函数看起来像这样:
function rec1(n) {
/* some code */
rec1(n-1);
}
Run Code Online (Sandbox Code Playgroud)
然后,要创建另一个版本,我复制并粘贴并获取:
function rec2(n) {
/* some other code */
rec2(n-1);
}
Run Code Online (Sandbox Code Playgroud)
等等
我不知道是否有某种方法可以引用"当前函数"(就像在Unix脚本中一样,可以使用$ 0引用"当前脚本")而不必更改每个版本中函数的名称.变量),以便我可以写:
function rec1(n) {
/* some code */
$this_function$(n-1);
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个最小的C++程序:
int main() {
return 1234;
}
Run Code Online (Sandbox Code Playgroud)
并使用clang ++ 5.0编译它并禁用优化(默认值-O0).生成的汇编代码是:
pushq %rbp
movq %rsp, %rbp
movl $1234, %eax # imm = 0x4D2
movl $0, -4(%rbp)
popq %rbp
retq
Run Code Online (Sandbox Code Playgroud)
我理解大多数行,但我不明白"movl $ 0,-4(%rbp)".似乎程序将一些局部变量初始化为0.为什么?
什么编译器内部细节导致此存储不对应源中的任何内容?
我想在同意中解决这个微分方程:
f'(x) = f(x+1)
Run Code Online (Sandbox Code Playgroud)
我试试这个:
from sympy import *
x = symbols("x")
f = Function("f")
f_ = Derivative(f,x)
dsolve(f_(x) - f(x+1), f(x))
Run Code Online (Sandbox Code Playgroud)
但得到一个错误:"'衍生'对象不可调用".
当我用"f_"替换"f_(x)"时,我得到一个不同的错误:"TypeError:doit()缺少1个必需的位置参数:'self'".
这个的正确语法是什么?
这个 C++ 代码:
void f(int& i) {
cout << "by reference" << endl;
}
void f(const int& i) {
cout << "by const reference" << endl;
}
void f(int&& i) {
cout << "by rvalue reference" << endl;
}
int main() {
int x;
const int y = 5;
cout << "f(x): ";
f(x);
cout << "f(y): ";
f(y);
cout << "f(x+y): ";
f(x+y);
cout << "f(move(x)): ";
f(move(x));
cout << "f(move(y)): ";
f(move(y));
cout << "f(move(x+y)): ";
f(move(x+y));
} …Run Code Online (Sandbox Code Playgroud)