我还在试图找出C中的宏:
#define A(x) #x
int main()
{
int i = -i;
char *s = A(i);
i = -(s[0] == 'i');
printf("%d", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何人都喜欢启发我并评论代码,尤其是宏的作用和这一行: i = -(s[0] == 'i');
我目前正在开发一个通过数据库检查启用功能的系统.
if(feature_db(id)) {
//new logic
else {
//old logic
}
Run Code Online (Sandbox Code Playgroud)
我想用MACRO替换它.
FEATURE_ENABLE(feature_db, id)
//new logic
FEATURE_DISABLE
//old logic
END_FEATURE
Run Code Online (Sandbox Code Playgroud)
我想这样做是因为它更清楚地发生在何处以及它更明确地发生了什么.我想知道是否只有一般的更优雅的方法来做到这一点?
#include<stdio.h>
#define SQUARE(x) x*x
int main(){
float s=10,u=30,t=2,a;
a=2*(s-u*t)/SQUARE(t); // How is this evaluated ?
printf("Result %f\n",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器显示的输出为-100.000000.但据我说它应该是-25.000000.我该怎么做才能纠正它,我的错误是什么?
需要解释以下代码:
#include <stdio.h>
int foo(int x) {
printf("in foo\n");
return x;
}
int __foo(int x) {
printf("in ___foo\n");
int y = foo(x);
return y + 1;
}
#define foo(x) __foo(x)
int main()
{
printf("=> %d\n", foo(2));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出: 我从上面的实验中收到的输出是:
in ___foo
in foo
=> 3
Run Code Online (Sandbox Code Playgroud)
虽然我期待这会导致递归调用?
编辑:宏定义后应定义__foo()
以更好的方式构思问题:
有一个logger C++类,它有<<运算符重载以接受整数,字符串..
logR <<"Test"<<endl
Run Code Online (Sandbox Code Playgroud)
现在endl被定义为宏
#define endl "\n\r"
Run Code Online (Sandbox Code Playgroud)
现在在任何.cpp文件中,如果我包含此记录器类的头文件,我曾经得到使用endl的编译错误.
找到一个修复此问题而不是定义宏endl,我重载运算符以接收endl()本身.感谢它帮助解决问题的输入.
所以看了3个小时的youtube视频,并花了很长时间阅读Lisp,我还没有看到这些"魔术宏"允许人们编写DSL,甚至可以做一些简单的事情,比如4 + 5 没有把它嵌入到一些大括号中.
这里有一些讨论:常见的lisp:输入数学表达式有一种不太痛苦的方法吗?但语法看起来并不好看,仍需要一些封闭的绒毛来定义宏的开始和结束位置.
所以这是挑战,定义一个中缀宏,然后使用它而不必用某种附加语法将其括起来.即
Some macro definition here
1 + 2
Run Code Online (Sandbox Code Playgroud)
不
Some macro definition here
(my-macro 1 + 2)
Run Code Online (Sandbox Code Playgroud)
不
Some macro definition here
ugly-syntax 1 + 2 end-ugly-syntax
Run Code Online (Sandbox Code Playgroud)
如果在Lisp中这是不可能的,那么大概是关于什么的呢?这有点像说"你有很棒的强大的宏,允许很酷的DSL,只要这些DSL包含在Lisp语法中".
Sub Seperate_Item_Codes_and_Descriptions()
'Seperate the item codes and the descriptions and put them in respectively in columns D and E.
Dim s As Long, a As Long, aVALs As Variant
With Worksheets(1)
aVALs = .Range(.Cells(12, "B"), .Cells(.Rows.Count, "B").End(xlUp)).Value2
ReDim Preserve aVALs(LBound(aVALs, 1) To UBound(aVALs, 1), 1 To 2)
For a = LBound(aVALs, 1) To UBound(aVALs, 1)
s = InStr(1, aVALs(a, 1), Chr(32))
aVALs(a, 2) = Mid(aVALs(a, 1), s + 1)
aVALs(a, 1) = Left(aVALs(a, 1), s - 1)
Next a
.Cells(12, …Run Code Online (Sandbox Code Playgroud)