gpt4 book ai didi

C++ 比较两个数组并显示重复项 - 代码现在可以工作

转载 作者:行者123 更新时间:2023-11-30 02:06:10 33 4
gpt4 key购买 nike

最后,这是有效的代码:

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <Windows.h>

using namespace std;

bool checkBuffer( char buffArray[], int buffSize );
void parseBuffer( char alphaArray[], char buffArray[], int alphaSize, int buffSize );

int main()
{
char alphabetArray[]= "abcdefghijklmnopqrstuvwxyz";
char buffer[11];

cout << "Enter the string up to 10 letters." << endl;
cin.get(buffer, strlen(buffer) );

checkBuffer(buffer, sizeof(buffer) );
parseBuffer( alphabetArray, buffer, sizeof(alphabetArray), sizeof(buffer) );

system("Pause");
return 0;
}
bool checkBuffer( char buffArray[], int buffSize )
{
if(buffArray, strlen(buffArray) == 0)
{
cout << "The buffer is empty. The program will end in 3 seconds. " << endl;
Sleep(3000);
exit(1);
}
}
void parseBuffer( char alphaArray[], char buffArray[], int sizeOne, int sizeTwo )
{
int countFreq = 0;
for(int i = 0; i < strlen(alphaArray); i++ )
{
countFreq = 0;

for(int j = 0; j < strlen(buffArray); j++)
{
if( alphaArray[i] == buffArray[j] )
countFreq = countFreq + 1;
}
cout << "The letter " << alphaArray[i] << " matched " << countFreq
<< " times." << endl;
}
}

我正在研究一个练习教科书问题,要求用户将 10 个字符输入一个数组,然后将该数组与硬编码字母表数组进行比较。输出应显示每个字母的重复次数(如果有)。例如:“有2个a。”“有 0 个 b。”“有 3 个 c。” .....等等。

我的代码正确地计算了 2 个数组之间重复(或不重复)的数量。但是,问题是每次循环迭代时它都会显示计数。我只需要它来显示总计数。我尝试将“cout”语句移到循环下方,但该语句不起作用,因为它需要 [i] 和 [j] 从它循环遍历数组的位置开始。请指出我的错误所在,在此先感谢!

#include <iostream>   // using DevCPP editor
#include <iomanip>
#include <string>
#include <algorithm>
#include <Windows.h>

using namespace std;

void parseBuffer( char buffArray[], char alphaArray[], int sizeOne, int sizeTwo );

int main()
{
// precode alphabet into an array with null terminating character
char alphabetArray[]={'a','b','c','d','e','f','g','h','i','j','k','l','m',n',
'o','p','q','r','s','t','u','v','w','x','y','z','\0'};

char buffer[11];
cout << "Enter the string up to 10 letters." << endl;
cin.get(buffer, 11);
parseBuffer(buffer, alphabetArray, 11, 11);

system("Pause");
return 0;

}
void parseBuffer(char buffArray[], char alphaArray[], int sizeOne, int sizeTwo)
{
int countFreq = 0;
cout << "This is buffer array: " << buffArray << endl;
cout << "This is alphabet array: " << alphaArray << endl<< endl;

for(int i = 0; i < (sizeTwo - 1); i++)
{
alphaArray[i];

for(int j = 0; j < (sizeOne -1); j++)
{
buffArray[j];

if(alphaArray[i] == buffArray[j] )
{
countFreq = countFreq + 1;
}
else
{
countFreq = 0;
}
cout << "It's a match. " << alphaArray[i] << " shows up " << countFreq << " times." << endl << endl;
}
}
} // end "parseBuffer"

最佳答案

您的代码存在一些主要问题。

  1. 循环顺序不正确。如果你想重复使用 countFreq对于 alphaArray 中的每个字符,你需要切换内循环和外循环。

  2. 您必须初始化 countFreq = 0对于每个 alphaArray性格。

  3. 您必须递增 countFreq ,即 ++countFreq , countFreq++ , 或 countFreq += 1对于 buffArray 中的每个匹配字符而不是通过 countFreq = 1 分配 1 .你现在正在做的是重置countFreq = 1每当有比赛时。

  4. 您减少了 1 个循环,即您最多检查 10 个字符而不是 11 个,并且您似乎对 alphaArray 使用了错误的大小限制| .

请格式化您的代码并正确使用 for 循环。即 for(int j = 0; j < sizeOne; ++j) .

关于C++ 比较两个数组并显示重复项 - 代码现在可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8997820/

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