- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我刚刚参加了考试,被问到以下问题:
Write the function body of each of the methods GenStrLen, InsertChar and StrReverse for the given code below. You must take into consideration the following;
- How strings are constructed in C++
- The string must not overflow
- Insertion of character increases its length by 1
- An empty string is indicated by StrLen = 0
class Strings {
private:
char str[80];
int StrLen;
public:
// Constructor
Strings() {
StrLen=0;
};
// A function for returning the length of the string 'str'
int GetStrLen(void) {
};
// A function to inser a character 'ch' at the end of the string 'str'
void InsertChar(char ch) {
};
// A function to reverse the content of the string 'str'
void StrReverse(void) {
};
};
我给出的答案是这样的(见下文)。我的一个问题是使用了许多额外的变量,这让我相信我没有以最好的方式做到这一点,另一件事是它不起作用....
class Strings {
private:
char str[80];
int StrLen;
int index; // *** Had to add this ***
public:
Strings(){
StrLen=0;
}
int GetStrLen(void){
for (int i=0 ; str[i]!='\0' ; i++)
index++;
return index; // *** Here am getting a weird value, something like 1829584505306 ***
}
void InsertChar(char ch){
str[index] = ch; // *** Not sure if this is correct cuz I was not given int index ***
}
void StrRevrse(void){
GetStrLen();
char revStr[index+1];
for (int i=0 ; str[i]!='\0' ; i++){
for (int r=index ; r>0 ; r--)
revStr[r] = str[i];
}
}
};
如果有人能大致向我解释什么是回答问题的最佳方式以及原因,我将不胜感激。另外,我的教授怎么会像“};”那样关闭每个类函数,我认为这仅用于结束类和构造函数。
非常感谢您的帮助。
最佳答案
首先,琐碎的};
问题只是风格问题。当我将函数体放在类声明中时,我也会这样做。在这种情况下 ;
只是一个空语句,不会改变程序的含义。它可以放在函数的末尾(但不是类的末尾)。
以下是您所写内容的一些主要问题:
str
的内容.不能保证以 \0
开始字节。index
,你只能在 GetStrLen
内设置它.程序启动时它的值可能为 -19281281。如果有人调用 InsertChar
怎么办?在他们打电话之前 GetStrLen
?index
在 InsertChar
.如果有人调用 InsertChar
怎么办?连续两次?StrReverse
,您创建一个名为 revStr
的反转字符串,但是你永远不会用它做任何事情。 str
中的字符串后记保持不变。令我困惑的是为什么你创建了一个名为 index
的新变量,大概是为了跟踪字符串中最后一个字符的索引,当已经有一个名为 StrLen
的变量时为此,您完全忽略了这一点。最后一个字符的索引 是 字符串的长度,因此您应该只更新字符串的长度,并使用它,例如
int GetStrLen(void){
return StrLen;
}
void InsertChar(char ch){
if (StrLen < 80) {
str[StrLen] = ch;
StrLen = StrLen + 1; // Update the length of the string
} else {
// Do not allow the string to overflow. Normally, you would throw an exception here
// but if you don't know what that is, you instructor was probably just expecting
// you to return without trying to insert the character.
throw std::overflow_error();
}
}
但是,您的字符串反转算法完全错误。仔细考虑该代码说 的内容(假设index
已在别处正确初始化和更新)。它说“对于 str
中的每个字符,用这个字符向后覆盖整个 revStr
”。如果str
开始为 "Hello World"
, revStr
最终会是 "ddddddddddd"
, 自 d
是 str
中的最后一个字符.
你应该做的是这样的:
void StrReverse() {
char revStr[80];
for (int i = 0; i < StrLen; ++i) {
revStr[(StrLen - 1) - i] = str[i];
}
}
注意它是如何工作的。说StrLen = 10
.然后我们复制 str
的位置 0|进入 revStr
的位置 9 , 然后是 str
的位置 1到位9
的 revStr
等等,直到我们复制位置 StrLen - 1
的 str
到 revStr
的位置 0 .
但是你在 revStr
中得到了一个反转的字符串而且您仍然缺少将其放回 str
的部分, 所以完整的方法看起来像
void StrReverse() {
char revStr[80];
for (int i = 0; i < StrLen; ++i) {
revStr[(StrLen - 1) - i] = str[i];
}
for (int i = 0; i < StrLen; ++i) {
str[i] = revStr[i];
}
}
还有更聪明的方法可以做到这一点,您不必使用临时字符串 revStr
,但上面的功能是完美的,将是问题的正确答案。
顺便说一句,你真的不需要担心这段代码中的 NULL 字节( \0
s)。您正在(或者至少您应该)使用 StrLen
跟踪字符串的长度这一事实自从使用 StrLen
以来,变量使得结束哨兵变得不必要你已经知道 str
的内容超出了那个点应该被忽略。
关于关于字符串类实现的 C++ 考试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2791852/
我正在准备 SQL Server 考试 (70-431)。我有 Sybex 的书 "SQL Server 2005 - Implementation and Maintenance" .我对估计一张
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the proper declaration of main? 我刚刚参加了第一次 C++
我刚刚参加了考试,被问到以下问题: Write the function body of each of the methods GenStrLen, InsertChar and StrRevers
如何通过 {exams} 包创建一个 moodle 问题,该问题至少为多项选择题打分? 我已经试过了: exams2moodle(..., mchoice = list(eval = list(neg
我正在为我们的项目进行 PAX 集成测试,我在类加载方面遇到了一些问题。 我在 PAX(使用的 karaf 容器)中部署了几个包。 karaf 启动后,我可以看到我的包和服务已启动并处于事件状态。但是
我正在尝试使用 PAX Exam 设置测试,如下所示: @ExamReactorStrategy(PerMethod.class) public class AbstractTest { @C
我正在参加过去的 Java 考试,但有一个问题一直困扰着我。 问题是:“任何构造函数显式或自动调用其父类的构造函数,父类调用其父类,依此类推类层次结构。这个过程的名称是什么? 感谢您的回答! 最佳答案
我正在准备 Java 程序员认证 (SCJP) 考试。关于异常的问题,当处理异常时,最好是处理特定的异常,如 NumberFormatException ,还是使用父级 Exception 类捕获所有
我一直在准备 SCJP,现在是 Oracle 认证专业 Java SE 程序员考试。 我很难理解所有不同的集合以及何时使用它们。我也喜欢闪存卡。因此,我尝试创建一组本质上相同的类,除了它们使用的集合之
在 MS Exam 70-536 .Net Foundation ,案例场景 1 中的第 3 章“搜索、修改和编码文本” Your organization, Northwind Traders, i
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
在MS Exam 70-536 .Net Foundation ,第1课Creating Threads中的Chapter 7“Threading”有一段文字: Be aware that becau
我正在与 exams2nops 合作R/exams 包的功能。由于我所在大学的学生注册号只有六个数字,我尝试调整了reglength exams2nops 中的选项(通常最少 7 个)功能以及nops
我的目标是使用 R/exams 和 Moodle 创建一个问题,包括在 Rmd 练习文件中生成的一些图。学生应口头描述情节,然后手动评分练习。 是否可以使用 exams2moodle为 Moodle
大家好,最近一次 MCQ 复习了我做错的答案。 函数 getSum 被定义为计算大小为 a[] 的 double 组中值的总和大小,大于给定的输入值阈值。下面显示了四个原型(prototype)定义,
我从官方 MCTS 考试 70-562 书中得到的小测试程序没有触发事件。我在 Debug模式下跟踪了计算机逻辑,它甚至没有进入我的事件,即使我已经设置好一切来处理它们。所以最大的问题是我做错了什么?
我已经开始使用 PAX-EXAM 和 Karaf 容器来测试我们的应用程序。有时,测试只是在测试方法开始之前挂起,并且始终卡在 cxf 注册 mbean 上: 正在注册 MBean org.apach
在第一版C# 70-483 Exam Ref , 示例 1-12 给出了将子任务附加到父任务的示例。我认为这是错误的,并希望有人在继续这个假设之前检查我的理解。示例中的代码如下: using Syst
我目前正在准备 Microsoft 考试 70-480。在实践测试中,我发现了以下问题,我无法解释其答案。 谁能解释为什么这是正确答案? You are developing a web page b
我正在阅读 MCTS 自定进度培训工具包(考试 70-536):Microsoft .NET Framework — 应用程序开发基础,第二版电子书。 现在我正在完成线程章节(第 7 期)。在第 2
我是一名优秀的程序员,十分优秀!