當前位置:編程學習大全網 - 編程語言 - 單詞排序 將壹篇英語文章出現的單詞去掉重復的,並按字母順序排列,用C語言,謝謝!!!

單詞排序 將壹篇英語文章出現的單詞去掉重復的,並按字母順序排列,用C語言,謝謝!!!

#include <stdio.h>

//鏈表節點

typedef struct Node

{

char* word;

struct Node* pNext;

}Node;

//鏈表頭

typedef struct List

{

Node* pFirst;

}List;

//比較值枚舉

enum {equal,less,bigger};

//單詞比較

int CmpWord(char* chNew, char*chExist)

{

int i;

for(i = 0; (chNew[i] != '\0') && (chExist[i] != '\0'); i++)

{

if(chNew[i] > chExist[i])

{

return bigger;

}

else if(chNew[i] < chExist[i])

{

return less;

}

}

//等長

if((chNew[i] == '\0') && (chExist[i] == '\0'))

{

return equal;

}

//新詞較短

else if (chNew[i] == '\0')

{

return less;

}

//新詞較長

else

{

return bigger;

}

}

//添加節點

void InsertNode(char* chNewWord, Node** ppNodeIndex)

{

Node* pNode = malloc(sizeof(Node));

pNode->word = chNewWord;

pNode->pNext = *ppNodeIndex;

(*ppNodeIndex) = pNode;

}

//添加單詞

void InsertWord(char* chNewWord, List* pList)

{

Node* pCurrentNode;

for(pCurrentNode = pList->pFirst;

pCurrentNode = pCurrentNode->pNext)

{

//鏈表末尾

if (!pCurrentNode)

{

InsertNode(chNewWord,&pCurrentNode);

return;

}

if (!pCurrentNode->pNext)

{

InsertNode(chNewWord,&(pCurrentNode->pNext));

return;

}

//和下壹個比較

switch(CmpWord(chNewWord, pCurrentNode->pNext->word))

{

case equal:

return;

case bigger:

continue;

case less:

InsertNode(chNewWord,&(pCurrentNode->pNext));

break;

default: break;

}

return;

}

}

//打印

void PrintWordsBySort(List* pList)

{

//鏈表末尾

if (!pList->pFirst)

{

return;

}

Node* pCurrentNode;

for(pCurrentNode = pList->pFirst;

pCurrentNode != NULL;

pCurrentNode = pCurrentNode->pNext)

{

printf("%s ",pCurrentNode->word);

}

return;

}

//銷毀節點

void DestroyNode(Node* pNode)

{

if (pNode->pNext)

{

DestroyNode(pNode->pNext);

}

free(pNode);

return;

}

//銷毀鏈表

void DestroyList(List* pList)

{

if (pList->pFirst)

{

DestroyNode(pList->pFirst);

}

free(pList);

}

int main()

{

List* pWordList = malloc(sizeof(List));

pWordList->pFirst = NULL;

//源字符串

char* strArticle;

for (;(*strArticle) != EOF;)

{

if (((*strArticle) < 'a' || (*strArticle) > 'z') &&

((*strArticle) < 'A' || (*strArticle) > 'Z') &&

((*(strArticle+1)) >= 'a' || (*(strArticle+1)) <= 'z') &&

((*(strArticle+1)) >= 'A' || (*(strArticle+1)) <= 'Z'))

{

InsertWord(strArticle+1,pWordList);

for(strArticle++;

((*strArticle) < 'a' || (*strArticle) > 'z') &&

((*strArticle) < 'A' || (*strArticle) > 'Z');

strArticle++);

}

else

{

strArticle++;

}

}

}

  • 上一篇:汽車鈑金檢具測量?
  • 下一篇:操作系統題目,好的追加高分,感謝大蝦
  • copyright 2024編程學習大全網