【问题标题】:#define OUTPUT(j) count++ what does this do?#define OUTPUT(j) count++ 这是做什么的?
【发布时间】:2019-08-13 10:17:40
【问题描述】:

我想把它翻译成 c#。但我被困在输出端。

“#define OUTPUT(j)”有什么作用?

我见过这个。 C++ Define function

它看起来像 func 定义,但输入参数 j 与计数无关。

代码来自: https://github.com/smart-tool/smart/blob/master/source/algos/raita.c

#define OUTPUT(j) count++  


void preBmBc(unsigned char *x, int m, int bmBc[]) {
   int i;
   for (i = 0; i < SIGMA; ++i)
      bmBc[i] = m;
   for (i = 0; i < m - 1; ++i)
      bmBc[x[i]] = m - i - 1;
}

int search(unsigned char *x, int m, unsigned char *y, int n) {
   int j, bmBc[SIGMA], count;
   unsigned char c, firstCh, *secondCh, middleCh, lastCh;
    if(m<2) return -1;

   /* Preprocessing */
   BEGIN_PREPROCESSING
   preBmBc(x, m, bmBc);
   firstCh = x[0];
   secondCh = x + 1;
   middleCh = x[m/2];
   lastCh = x[m - 1];
   END_PREPROCESSING

   /* Searching */
   BEGIN_SEARCHING
   count = 0;
   j = 0;
   while (j <= n - m) {
      c = y[j + m - 1];
      if (lastCh == c && middleCh == y[j + m/2] &&
          firstCh == y[j] &&
          memcmp(secondCh, y + j + 1, m - 2) == 0)
         OUTPUT(j);
      j += bmBc[c];
   }
   END_SEARCHING
   return count;
}

【问题讨论】:

  • 它一通过同行评审就被解雇了
  • 这是一个可怕的代码。
  • OUTPUT(j) 将被预处理器替换为 count++。不知道,为什么需要它,但无论如何。
  • 爱开源,这个 github 项目是纯金。为了伟大,每个文件都包含放置 main() 的 main.h。我特别喜欢带有#define UNDEFINED -1#define HALFDEFINED -2 的define.h。

标签: c


【解决方案1】:

预处理指令

#define OUTPUT(j) count++

使预处理器将每次出现的OUTPUT(j) 替换为count++,其中j 是可变的,可以在指令的模式部分中使用。

代码

#define OUTPUT(j) count++

int count(4);
OUTPUT(1234);
std::cout << count << '\n';

被翻译成

int count(4);
count++;
std::cout << count << '\n';

由预处理器,然后由编译器编译。

这段代码的输出是5。由于未使用j,因此忽略该参数。

【讨论】:

    猜你喜欢
    • 2020-12-25
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2016-04-09
    • 1970-01-01
    相关资源
    最近更新 更多