您的位置:首页 > 其它

计算一个字符串中每个字符出现的次数

2010-01-21 16:42 651 查看
/************************************************************************
***By        : summon                                                 ***
***Date      : 2010.01.21                                             ***
***Function  : 求一个字符串中每个字符出现的次数                                ***
***Method1   : 把这个字符串用快速排序处理,处理完后相同的字符全部临接          ***
***Method2   : 用空间换时间,首先声明一个静态数组count[128],每个字符        ***
***            ch出现一次就把count[ch]++。                                   ***
/************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>

//////////////////////////Method1////////////////////////////////////////
#define MAXLEN 128
typedef int STATUS;
typedef struct _Result
{
char ch;
unsigned int times;
}Result;
typedef struct _Node
{
Result res;
_Node *next;
}Node;
typedef struct _Linker
{
Node *head;
int length;
}Linker;

STATUS InputString(char *&InputData);
STATUS QSortString(char *InputData);
STATUS Method1(char *InputData, Linker *&lin);
STATUS OutputResult(Linker *lin);
int cmpCHAR(const void *a, const void *b);
////////////////////////////////////////////////////////////////////////

//////////////////////////Method2///////////////////////////////////////
static unsigned int count[128];
STATUS InputString2(char *&InputData);
STATUS Method2(char *InputData);
STATUS OutputResult2();
////////////////////////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
int ret = -1;
char *InputData;
Linker *lin;

ret = InputString(InputData);
ret = QSortString(InputData);
ret = Method1(InputData, lin);
ret = OutputResult(lin);

ret = InputString2(InputData);
ret = Method2(InputData);
ret = OutputResult2();

return 0;
}

int cmpCHAR(const void *a, const void *b)
{
return *(char *)a - *(char *)b;
}

STATUS InputString(char *&InputData)
{
char temp[100];
memset(temp, 0, 100);
printf("Please input a string!/n");
fflush(stdin);
cin.getline(temp, 100);
int len = strlen(temp);
InputData = (char *)malloc(sizeof(char)*(len + 1));
memcpy(InputData, temp, len);
InputData[len] = '/0';
return 1;
}

STATUS QSortString(char *InputData)
{
if (InputData == NULL)
{
return 0;
}

int len = strlen(InputData);
qsort(InputData, len, sizeof(char), cmpCHAR);
return 1;
}

STATUS Method1(char *InputData, Linker *&lin)
{
if (InputData == NULL)
{
return 0;
}

lin = (Linker *)malloc(sizeof(Linker));
lin->head = NULL;
lin->length = 0;

Node *temp = (Node *)malloc(sizeof(Node));
lin->head = temp;
lin->head->res.ch = InputData[0];
lin->head->res.times = 1;
lin->head->next = NULL;
lin->length += 1;

int len = strlen(InputData);
for (int i=1; i<len; i++)
{
if (InputData[i-1] == InputData[i])
{
temp->res.times += 1;
}else
{
Node *temp2 = (Node *)malloc(sizeof(Node));
temp->next = temp2;
temp2->res.ch = InputData[i];
temp2->res.times = 1;
temp2->next = NULL;

temp = temp2;
}
}
return 1;
}

STATUS OutputResult(Linker *lin)
{
if (lin == NULL)
{
return 0;
}

Node *temp = lin->head;
while (temp->next != NULL)
{
printf("Charector :%c, Times: %d/n", temp->res.ch, temp->res.times);
temp = temp->next;
}
printf("Charector :%c, Times: %d/n", temp->res.ch, temp->res.times);
return 1;
}

STATUS InputString2(char *&InputData)
{
return InputString(InputData);
}

STATUS Method2(char *InputData)
{
if (InputData == NULL)
{
return 0;
}

for (int j=0; j<128; j++)
{
count[j] = 0;
}

int len = strlen(InputData);
for (int i=0; i<len; i++)
{
count[InputData[i]]++;
}
return 1;
}

STATUS OutputResult2()
{
for (int i=0; i<128; i++)
{
if (count[i] != 0)
{
printf("Charector: %c, Times: %d/n", i, count[i]);
}else
{
continue;
}
}
return 1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: