将 c 中的片段翻译成伪代码

Translating a snippet in c to pseudocode

我刚刚开始学习 Java,我有一项作业需要将 C 代码片段翻译成 Java。任何人都可以通过将片段翻译成伪代码来帮助我吗?我想自己编写 Java 编码,但我不懂 C,也无法从代码片段中获得太多意义。这是作业:


您正在寻找一种简单的模式匹配方法。类似于C中的strstr(...),它应该在一个字符串中搜索一个搜索字符串。搜索字符串应包含“*”(替换多个字符)和“?”。你有一个例子,但它在 C 中:

int match ( char *pat, char *str ) {
 switch ( *pat ) {
  case '[=10=]' : return !*str;
  case '*'  : return match( pat+1, str ) || *str && match( pat, str+1 );
  case '?'  : return *str && match( pat+1, str+1 );
  default   : return *pat == *str && match( pat+1, str+1 );
 }
}

将其翻译成 Java。


我知道尝试一项需要翻译一种您不懂的语言的作业是愚蠢的,我不明白为什么该作业包含在 Java 学习任务列表中,但我必须解决如果有人愿意帮助我,那将是非常友好的。

C 和 Java 是相似的语言,因此您可能会理解部分程序。

有一些重要的区别。 C 使用 char 数组作为字符串,并用尾随 '[=14=]' 标记字符串的结尾。类似于数组的是指针。

函数中

int match ( char *pat, char *str )

pat是指向模式字符串的指针,str是应该匹配的字符串。

在函数体中*pat是模式的第一个字符。 *str.

类似

pat+1 是指向字符串的下一个字符的指针,后面可能跟有其他字符。下一个字符也可能是结束标记 '[=14=]'.

在 C 中,您可以使用数字进行逻辑运算。

!*str

str指向的第一个字符值的逻辑非。 '[=14=]' 是值 0(字符串结尾),被视为 false。字符串中的任何字符都具有非零值,并被视为 true.

return 类型 int 用作布尔值。在 C 中没有特定的 booleanbool 类型。例如

case '[=12=]' : return !*str;

表示:当我们到达模式的末尾时(case '[=28=]':)我们returntrue(不是0)如果str指向字符串的末尾( '[=14=]') 否则我们 return false 因为某些东西 != 0 (true) 的逻辑否定是 0 (false)。

我试过为你评论它。读一读,看看它是否有助于您理解:)。

/* Returns an integer (nonzero if the strings match, zero if they don't).
 * - pat: A string (char *) which is your pattern.
 * - str: A string (char *) which is your source string.
 * Note: All strings end in the null-character ('[=10=]') which has integer value zero. 
*/
int match ( char *pat, char *str ) {

 /* The switch extracts the first character of the string "pat" (*pat).
  * Then, it will run the code in the case to which that character matches:
 */
 switch ( *pat ) {

  /* If you match null-character, then return nonzero only if the value 
   * of the leading character in str (*str) is zero (!*str) This means
   * that it returns a match if the leading character in str is also 
   * the null character
  */ 
  case '[=10=]' : return !*str;

  /* If you match an asterisk '*', then return nonzero in two cases:
   * (1) Calling your own function recursively having dropped the asterisk from
   *     the pattern returns nonzero (match(pat+1, str)).
   * ... OR ... 
   * (2) The leading character of str is nonzero (*str) AND calling
   *     this very function having dropped the leading character of str returns
   *     nonzero (match(pat, str + 1)).
  */
  case '*'  : return match( pat+1, str ) || *str && match( pat, str+1 );

  /* If you match '?', then return nonzero if both cases are true:
   * (1) *str is not the null-char (it is nonzero).
   * (2) Calling match recursively having skipped the current character
   * in both the pattern AND the string returns nonzero (match(pat+1, str+1)).
  */                             
  case '?'  : return *str && match( pat+1, str+1 );


  /* Otherwise, if you didn't match on any of the above patterns, return
   * nonzero only if both the following conditions are true:
   * (1) The current character at the head of pat is the same as that of str
   *      (*pat == *str)
   * (2) calling match recursively having skipped both the current character
   *     at the head of pattern AND the string also returns nonzero. 
   *     match(pat + 1, str + 1)
  */
  default   : return *pat == *str && match( pat+1, str+1 );
 }
}