gpt4 book ai didi

c - 将 c 代码片段翻译为伪代码

转载 作者:行者123 更新时间:2023-11-30 16:22:08 25 4
gpt4 key购买 nike

我刚刚学习 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 学习任务列表中,但我必须解决它,这会非常困难。如果有人愿意帮助我,那就太好了。

最佳答案

我已尝试为您发表评论。阅读一下,看看是否有助于您理解:)。

/* 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 );
}
}

关于c - 将 c 代码片段翻译为伪代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54556745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com