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