📄 counter.c
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct List {
char* content;
int count;
struct List* next;
};
struct List* head;
struct List* last;
int size = -1;
int cou = 0;
void parse(char* input);
void display();
void add(char* temp);
struct List* find(char* temp);
void deal(struct List* head);
void sort();
int getSize();
int main(int argc, char* argv[])
{
char p[255];
char s[130];
int c ;
last = NULL;
head = NULL;
if (2==argc){
parse(argv[1]);
}
while (head==NULL){
puts("Please input some word!\nSize<255\nFormat:n,W1,W2,...Wn");
fgets(p,254,stdin);
p[strlen(p)-1]=0;
parse(p);
}
size=getSize();
while (cou<1){
printf("%s%d%s","Please input the number of word which to display(1~",size,"):");
gets(s);
c=atoi(s);
if ((c>0)&&(c<=size)){
cou = c;
}
}
display();
return 0;
}
void parse(char* input){
char*temp = NULL;
int sc = 0;
unsigned int tPos = 0;
unsigned int tLeng = 10;
char c;
unsigned int leng = 0;
signed int i = 0;
if (NULL==input){
return;
}
size = -1;
leng = strlen(input);
for(;i<leng;i++){
c = input[i];
if (' '==c){
break;
}else if (','==c){
if (NULL!=temp){
if (size<1){
size = atoi(temp);
if (size<1){
break;
}
sc = size;
}else {
sc--;
add(temp);
}
temp=NULL;
}else {
break;
}
}else {
if (NULL ==temp){
tLeng = 10;
temp = (char*)malloc(sizeof(char)*tLeng);
tPos = 0;
if (NULL!=temp){
memset(temp,0,sizeof(char)*tLeng);
}
}
if (tPos>=(tLeng - 1)){
char* t = temp;
tLeng +=10;
temp = (char*)malloc(sizeof(char)*tLeng);
if (NULL!=temp){
memset(temp,0,sizeof(char)*tLeng);
if (NULL!=t){
strcat(temp,t);
free(t);
}
}
}
if (NULL!=temp){
temp[tPos++]=c;
}
}
}
if ((1==sc)&&(NULL!=temp)){
add(temp);
}else {
deal(head);
head = NULL;
}
if (NULL==head){
puts("Input error!\n");
}
}
void display(){
struct List* info=NULL;
sort();
for (info = head;cou>0;cou--,info=info->next){
if (NULL==info){
continue;
}
printf("%s%s%d\n",info->content,":",info->count);
}
}
void add(char* temp){
if (NULL==head){
head =(struct List*)malloc(sizeof(struct List));
if (NULL==head){
puts("out of memory!\n");
exit(1);
}
head->content = temp;
head->count = 1;
head->next = NULL;
last=head;
}else {
struct List* info=NULL;
info = find(temp);
if (NULL!=info){
info->count = (info->count+1);
}else if (NULL!=last) {
info = (struct List*)malloc(sizeof(struct List));
if (NULL==info){
puts("out of memory!\n");
exit(1);
}
info->content = temp;
info->count = 1;
info->next = NULL;
last->next = info;
last=info;
}else {
puts("interal error!\n");
exit(1);
}
}
}
void deal(struct List* head){
struct List* info ;
struct List* temp ;
info = head;
if (NULL==head){
return;
}
temp = info->next;
while (!info){
free(info);
info = temp;
temp=info->next;
}
head=NULL;
}
struct List* find(char* temp){
struct List* info ;
if (NULL==temp){
return NULL;
}
info = head;
while(info){
if (!strcmp(temp,info->content)){
return info;
}
info = info->next;
}
return NULL;
}
void sort(){
struct List*info=NULL;
struct List*proI = NULL;
info=head;
while (info){
struct List*MaxList=NULL;
struct List*MaxListPro=NULL;
struct List*proT=NULL;
struct List*temp=NULL;
int cT = 0,MaxCou = 0;
int cI = info->count;
proT = info;
temp = proT->next;
while(temp){
cT= temp->count;
if((cT>cI)&&(cT>MaxCou)){
MaxList = temp;
MaxListPro = proT;
MaxCou = cT;
}
proT = temp;
temp = proT->next;
}
if (NULL!=MaxList){
struct List*infoNext = info->next;
struct List*maxNext = MaxList->next;
MaxListPro->next=info;
info->next = maxNext;
if (NULL==proI) {
head=MaxList;
}else {
proI->next = MaxList;
}
MaxList->next= infoNext;
info = MaxList;
}
proI = info;
info = proI->next;
}
}
int getSize(){
struct List*info=head;
int t = 0;
while(info){
t += 1;
info=info->next;
}
return t;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -