将c中的片段转换为伪代码

问题描述 投票:0回答:2

我只是学习Java而且我有一个任务,我需要将一段C片段翻译成Java。任何人都可以通过将代码段转换为伪代码来帮助我吗?我想自己做Java编码,但我不知道C,我不能从代码片段中理解。这是作业:


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

int match ( char *pat, char *str ) {
 switch ( *pat ) {
  case '\0' : 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 pseudocode
2个回答
0
投票

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

/* 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 ('\0') 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 '\0' : 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 );
 }
}

0
投票

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

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

在功能上

int match ( char *pat, char *str )

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

在函数体中,*pat是模式的第一个字符。类似于*str

pat+1是指向字符串的下一个字符的指针,其后可能跟随其他字符。下一个字符也可能是结束标记'\0'

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

!*str

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

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

case '\0' : return !*str;

意思是:当我们到达模式的末尾(case '\0':),如果true指向字符串的末尾(str),我们返回'\0'(不是0)否则我们返回false,因为!= 0true)的逻辑否定是0false) 。

© www.soinside.com 2019 - 2024. All rights reserved.