gpt4 book ai didi

关于字符串类实现的 C++ 考试

转载 作者:可可西里 更新时间:2023-11-01 16:52:54 25 4
gpt4 key购买 nike

我刚刚参加了考试,被问到以下问题:

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];
}
}
};

如果有人能大致向我解释什么是回答问题的最佳方式以及原因,我将不胜感激。另外,我的教授怎么会像“};”那样关闭每个类函数,我认为这仅用于结束类和构造函数。

非常感谢您的帮助。

最佳答案

首先,琐碎的};问题只是风格问题。当我将函数体放在类声明中时,我也会这样做。在这种情况下 ;只是一个空语句,不会改变程序的含义。它可以放在函数的末尾(但不是类的末尾)。

以下是您所写内容的一些主要问题:

  1. 您从未初始化 str 的内容.不能保证以 \0 开始字节。
  2. 你从不初始化index ,你只能在 GetStrLen 内设置它.程序启动时它的值可能为 -19281281。如果有人调用 InsertChar 怎么办?在他们打电话之前 GetStrLen
  3. 你从不更新indexInsertChar .如果有人调用 InsertChar 怎么办?连续两次?
  4. 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" , 自 dstr 中的最后一个字符.

你应该做的是这样的:

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到位9revStr等等,直到我们复制位置 StrLen - 1strrevStr 的位置 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/

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