从Char数组创建频率字典

时间:2015-10-09 09:10:49

标签: c++ algorithm

我有一个关于C的问题,任务是从Char数组创建一个频率字典,我差不多这样做了,但问题是最后,当我尝试将这些字应用到第二个字符数组中时。请提出如何管理此问题的建议。非常感谢。

#include<iostream>
#include<cstring>
#define _CRT_SECURE_NO_WARNINGS

template<typename T>
int Count(T* M);
template<typename T>
T** CreationOfMatrix(T **M, T* Text);
template<typename T>
void SortUp(T**Array, int count);
template<typename T>
void Reflect(T**Array, int count);
template<typename T>
int Computing(T**Array, int count, T**NewArray,int*NewIntArray );
using namespace std;
int main()
{
    char *Text=new char[100];
    strcpy_s(Text,strlen("she has a cancer and ... did anything ... she certainly did. ")+1,"she has a cancer and ... did anything ... she certainly did. ");
    //char Text[100]="she has a cancer and ... did anything ... she certainly did. ";

    char*pText=Text;
    cout<<Text;
    int count=Count(Text);
    cout<<endl<<count<<endl<<Text<<endl;
    char **Matrix= new char*[count];
    Matrix=CreationOfMatrix(Matrix, Text);
    SortUp(Matrix, count);
    Reflect(Matrix, count);
    char**NewArray=new char*[count];

    int* NewIntArray=new int[count];
    int size=Computing(Matrix, count, NewArray, NewIntArray);
    cout<<size<<"\n\n";
    for(int i=0; i<size; i++)
    {
        cout<<NewArray[i]<<"\t"<<NewIntArray[i]<<"\n";
    }
}

template<typename T>
int Count(T* M)
{
    int i=0;
    T *temp=NULL;
    char Copy[100];
    strcpy_s(Copy,strlen(M)+1, M);
    T* S=strtok_s(Copy, ".,?:! ", &temp);
    while(S)
    {
        i++;
        S=strtok_s(NULL,", .?!",&temp);
    }
    return i;
}
template<typename T>
T** CreationOfMatrix(T **M, T* Text)
{ 
    int i=0, j=0;
    T *temp=NULL;
    T* S=strtok_s(Text, ".,?:! ", &temp);

    while(S)
    {
        cout<<S<<"\n"; 
        i=strlen(S);
        M[j]=new char[i+1];
        strcpy_s(M[j],i+1,S);
        j++;
        S=strtok_s(NULL, ".,?:! ", &temp);
    }
    return M;
}
template<typename T>
void SortUp(T**Array, int count)
{
    T temp[50]=" ";
    for(int i=0; i<count; i++)
    {
        for(int j=0; j<count-1; j++)
        {
            strcpy_s(temp, strlen(Array[j])+1, Array[j]);
            if(strcmp(Array[j],Array[j+1])>0)
            {
                strcpy_s(Array[j], strlen(Array[j+1])+1, Array[j+1]);
                strcpy_s(Array[j+1], strlen(temp)+1, temp);
            }
        }
    }
}
template<typename T>
void Reflect(T**Array, int count)
{
    for(int i=0; i<count; i++)
    {
        cout<<Array[i]<<endl;
    }
}

template<typename T>
int Computing(T**Array, int count, T**NewArray,int* NewIntArray )
{
    int size=0;
    int repeat=1;
    bool Flag=true;
    for(int i=0; i<count; i++)
    {Flag=true;
    repeat=1;
    for(int k=i-1; k>=0&&(Flag); k--)
    {
        if(!strcmp(Array[i], Array[k]))
            Flag=false;
    }
    if(Flag)
    {
        for(int j=i+1; j<count; j++)
        {
            if(!strcmp(Array[i], Array[j]))
                repeat++;
        }
        NewIntArray[size]=repeat;
        NewArray[size]=new T[strlen(Array[size])+1];
        strcpy_s(NewArray[size],strlen(Array[size])+1,Array[size]);
        cout<<NewArray[size]<<"\t"<<NewIntArray[size]<<"\n";
        size++;
    }

    }
    return size;
}

1 个答案:

答案 0 :(得分:0)

您的问题出现在以下两行int Computing(...)

    NewArray[size]=new T[strlen(Array[size])+1];
    strcpy_s(NewArray[size],strlen(Array[size])+1,Array[size]);

您的Array索引应该是i,而不是size

    NewArray[size]=new T[strlen(Array[i])+1];
    strcpy_s(NewArray[size],strlen(Array[i])+1,Array[i]);

否则,当i不再等于size时,一旦您开始跳过重复的字词,事情就会开始出错。

正如其他人所说,使用C ++数据结构而不是普通数组会更容易。