提问人:PALP King 提问时间:3/8/2023 最后编辑:PALP King 更新时间:3/8/2023 访问量:21
使用预处理器指令使给定程序成功编译,而无需修改代码
Using preprocessor directives to make a given program compile successfully without modifying the code
问:
我得到了以下不编译的程序,我的任务是使用预处理器指令来编译它(我没有这些指令的先验知识):
#include <iostream>
#include <vector>
using namespace std;
#if !defined toStr || !defined io || !defined FUNCTION || !defined INF
#error Missing preprocessor definitions
#endif
FUNCTION(minimum, <)
FUNCTION(maximum, >)
int main(){
int n; cin >> n;
vector<int> v(n);
foreach(v, i) {
io(v)[i];
}
int mn = INF;
int mx = -INF;
foreach(v, i) {
minimum(mn, v[i]);
maximum(mx, v[i]);
}
int ans = mx - mn;
cout << toStr(Result =) <<' '<< ans;
return 0;
}
在通读本教程以更好地理解指令后,以下是我在文件顶部对程序所做的添加:
/* Enter your macros here */
#define toStr(input) #input
#define foreach(v,i) for(int i=0; i<n; ++i)
#define io(v) (cin>>v)
#define FUNCTION(x,c) (toStr(x)(a,v);)
#define minimum(a, v) ((v[i])<(a)?(a=v[i]):(a=a);)
#define maximum(a, v) ((v[i])>(a)?(a=v[i]):(a=a);)
#define INF 2147483647
但是,它不起作用。我收到以下错误:
Solution.cpp:19:20: error: expected unqualified-id before string constant
FUNCTION(minimum, <)
^
Solution.cpp:2:23: note: in definition of macro ‘toStr’
#define toStr(input) #input
^~~~~
Solution.cpp:19:1: note: in expansion of macro ‘FUNCTION’
FUNCTION(minimum, <)
期望在包含宏后成功编译给定的程序。请帮帮我。
答: 暂无答案
评论