c 程序设计语言 第二版 练习题 6-4
来源:互联网 发布:mac怎么iphone铃声 编辑:程序博客网 时间:2024/05/18 04:46
#pragma warning(disable:4996)#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <string.h>typedef struct List {//链表struct List *next;}List;typedef struct WordList { //单词结构List *next;char *word;int count;}WordList;static WordList *createWordList(char *word);static WordList *addWordList(WordList *list, char *word);static void freeWordList(WordList *list);static void printWordList(WordList *list);static void sortWordList(WordList *list);static int getword(char *word) { //按单词 ,读取文件if (word == NULL)return -1;char c;int count = 0;//记录读取数量 while (isspace(c = getchar())) //跳过空字符;if (c == EOF)return EOF;if (!isalpha(c)) { //判断第一个字符,是否是字母字符,如果不是则表示不是一个单词,跳过while (!(isspace(c = getchar())) && c != EOF)//直到下个空字符结束;if (c == EOF)return EOF;return 0;}if (isalpha(c)) { //读取单词word[count++] = c;while (isalpha(c = getchar()) && c != EOF && (word[count++] = c))//读取连续的字母;word[count] = '\0';if (c == EOF)ungetc(c, stdin);}return count;}int main() {WordList *list = NULL;char buf[128];int ret;while ((ret=getword(buf)) != EOF) {if (ret>0){list = addWordList(list, buf);}}sortWordList(list);printWordList(list);freeWordList(list);system("pause");return 0;}static WordList *createWordList(char *word) {//创建一个节点if (word == NULL)return NULL;WordList *list = (WordList*)calloc(1, sizeof(WordList));list->word = strdup(word);list->count = 1;return list;}static WordList *addWordList(WordList *list, char *word) {//递归查找,并添加添加节点if (list == NULL) {//当前节点是空 则创建节点return createWordList(word);}else if (strcmp(list->word, word) == 0) {//如果有当前单词,++list->count;//计数器加1}else{//否则递归下一个节点if (list->next)addWordList(list->next, word);elselist->next = addWordList(list->next, word);}return list;}static void freeWordList(WordList *list) {//释放节点if (list == NULL)return;free(list->word);//释放当前单词空间freeWordList(list->next);//递归下一个节点free(list);//释放自己}static void printWordList(WordList *list) {if (list == NULL)return;printf("次数:%d 单词:%s\n", (WordList*)list->count, (WordList*)list->word);printWordList(list->next);}static void sortWordList(WordList *list) { //排序WordList *t1, *t2, *temp;temp = (WordList*)calloc(1, sizeof(WordList));for (t1 = list;t1->next != NULL;t1 = (WordList*)t1->next) {for (t2 = (WordList*)t1->next;t2->next != NULL;t2 = (WordList*)t2->next) {if (t1->count < t2->count) {temp->word = t1->word;temp->count = t1->count;t1->word = t2->word;t1->count = t2->count;t2->word = temp->word;t2->count = temp->count;}}free(temp);}
0 0
- c 程序设计语言 第二版 练习题 6-4
- c 程序设计语言 第二版 练习题 6-3
- c 程序设计语言 第二版 练习题 6-5
- c 程序设计语言 第二版 练习题 4-10
- c 程序设计语言 第二版 练习题 4-13
- c 程序设计语言 第二版 练习题 4-12
- c 程序设计语言 第二版 练习题 5-4
- c 程序设计语言 第二版 练习题 5-1
- c 程序设计语言 第二版 练习题 5-2
- c 程序设计语言 第二版 练习题 5-3
- c 程序设计语言 第二版 练习题 5-5
- c 程序设计语言 第二版 练习题 4-5 4-6 4-7 4-8
- c程序设计语言第二版1.18
- C程序设计语言第二版1.20
- C程序设计语言第二版1.21
- C程序设计语言练习题.第一章
- C程序设计语言练习题2-2
- C程序设计语言(第二版 ) 读书笔记
- javaSE学习10_大数值
- 回车键代替鼠标点击登录
- 谢希仁计算机网络1
- HDU 2544 最短路
- 暗影精灵2pro安装win10+ubuntu16.10双系统
- c 程序设计语言 第二版 练习题 6-4
- C++——NOIP例题——字符变换
- 11-TCP 协议(状态机)
- SpringBoot 项目属性的配置和使用
- Android源码-Android系统启动源码分析
- 用java编程时,一个语句行可以分行书写吗?
- Chapter 1 : MySQL体系结构和搜索引擎
- tomcat 配置
- 第1课