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

时间:2012-01-25 04:29:35

标签: c++ arrays

最后,这是有效的代码:

#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个人。” “有0个。” “有3个c。” .....等等。

我的代码正确计算了2个数组之间(或不重复)的重复次数。然而,问题是它每次都显示计数循环ITERATES。我只需要它来显示THE TOTAL COUNT。 我尝试在循环下面移动“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"  

3 个答案:

答案 0 :(得分:2)

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

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

  2. 您必须为每个countFreq = 0字符初始化alphaArray

  3. 您必须为countFreq INSTEAD中的每个匹配字符增加++countFreq,即countFreq++countFreq += 1buffArray countFreq = 1。您现在正在做的是在匹配时重置countFreq = 1

  4. 您正在少循环1次,即您最多检查10个字符而不是11个字符,而您似乎使用了alphaArray的错误大小限制。

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

答案 1 :(得分:1)

看看那个。

#include <string>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main()
{
string s;
char c;
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'}; 
int count = 0;
cout << "Enter a string : ";
getline(cin, s);
for(int j = 0 ; j < strlen(alphabetArray); j++) {

for(int i = 0 ; i < s.size(); i++) {

if(s[i]==alphabetArray[j]) {
    count++;
    cout<<alphabetArray[j]<<" shows up "<<count<<" times"<<endl;
}
count=0;
}

}
system("pause");
return 0;
}

答案 2 :(得分:1)

我有一个更简单的解决方案,虽然这可能不是你想要的,它更快更小。 首先,你不需要一个字符数组,字符有int值('A'= 65'F'= 70,等等)。所以有了这些信息,您应该清楚,您可以在O(n)时间内执行此任务

这是伪代码:

for(int i=0 to size of your string i++)
{
    char ch is string.at(i);
    ch equal to upper case of ch
    short j = (convert ch to int);
    if( j is between A and Z) then
    {
        count at j ++; 
    }
}
for(A to Z)
{
    display frequency
}

这是实际的代码:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string data;
    int count[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    const int BASE=65;
    cout<<"string: "; 
    getline(cin,data);
    int dataSize=data.length();
    for(int i=0; i<dataSize; i++)
    {
        char ch=data.at(i);
        ch=toupper(ch);
        short j=(int)ch;

        if(j>=BASE && j<=BASE+25)
        {
            count[j-BASE]++;
        }
    }
    for(int i=0; i<26; i++)
    {
        char ch=i+BASE;
        cout<<"There are "<<count[i]<<" "<<ch<<"'s."<<endl;
    }
}