将C中的代码段转换为伪代码

时间:2019-02-06 15:10:45

标签: c pseudocode

我只是在学习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学习任务列表中,但是我必须解决它,这非常如果有人愿意帮助我,我会很友善。

2 个答案:

答案 0 :(得分: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)。

答案 1 :(得分: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 );
 }
}