回复 10楼 ybjkl
怎么按你的思路改着改着有递归的味道了。还是希望哪位大神帮我用指针数组解决,一是程序写到用指针数组是很自然的事,再就是对指针数组不是很了解,想看下眼!多谢啦!
#include <locale.h> #include <stdio.h> #include <string.h> #include <new.h> #include <ctype.h> #include <conio.h> struct WORDS { size_t Count; wchar_t* pWord[4096]; } Words; size_t Get_Word(FILE* File, wchar_t* Word); void my_strcpy(wchar_t* Destination, size_t Count, const wchar_t* Source); void main(int argc, char* argv[]) { FILE* File; errno_t Error; wchar_t Word[1024] = {'\0'}; size_t Count; setlocale(LC_ALL, "chs"); // 设中文输出 if (argc < 1) { wprintf_s(L"格式: test <FileName>\n"); goto end; } Error = fopen_s(&File, argv[1], "rt"); Words.Count = 0; while (!feof(File)) { Count = Get_Word(File, Word); if (Count > 0) { Words.pWord[Words.Count] = new wchar_t[Count+1]; my_strcpy(Words.pWord[Words.Count], Count, Word); _putws(Words.pWord[Words.Count]); Words.Count++; } } fclose(File); end: wprintf_s(L"\n=== 按任意键继续 ==="); _getwch(); while (Words.Count > 0) { delete[] Words.pWord[Words.Count-1]; Words.Count--; } } size_t Get_Word(FILE* File, wchar_t* Word) { wchar_t Character; size_t Count = 0; while (!feof(File)) { Character = fgetwc(File); if (!iswspace(Character) && !iswpunct(Character)) // 过滤空白符号和标点符号 { Word[Count] = Character; Count++; } else { break; } } Word[Count] = '\0'; return Count; } void my_strcpy(wchar_t* Destination, size_t Count, const wchar_t* Source) { size_t Index; for (Index = 0; Index < Count; Index++) { Destination[Index] = Source[Index]; } Destination[Index] = '\0'; }
#include <locale.h> #include <stdio.h> #include <string.h> #include <new.h> #include <ctype.h> #include <conio.h> #define MAX_COUNT 4096 // 单词表 struct WORDS { size_t Count; // 实际单词数 wchar_t* pWord[MAX_COUNT]; // 单词 size_t Len[MAX_COUNT]; // 单词长度表 size_t Index[MAX_COUNT]; // 序号 } Words; bool Read_Data(char* FileName); size_t Get_Word(FILE* File, wchar_t* Word); void Sort(); void main(int argc, char* argv[]) { setlocale(LC_ALL, "chs"); // 设定语言为中文输出 if (argc < 2) { printf_s("格式: %s <FileName>\n", argv[0]); goto end; } if (Read_Data(argv[1])) { Sort(); for (size_t Count = 0; Count < Words.Count; Count++) { wprintf_s(L"%s\n", Words.pWord[Words.Index[Words.Count - Count - 1]]); } while (Words.Count > 0) { delete[] Words.pWord[Words.Count-1]; Words.pWord[Words.Count-1] = nullptr; Words.Count--; } } else { printf_s("文件%s打开错误,请检查再运行!\n", argv[1]); } end: wprintf_s(L"\n=== 按任意键继续 ==="); _getwch(); } // 读入文本文件中的单词,存入单词表 bool Read_Data(char* FileName) { FILE* File; errno_t Error; size_t Count; wchar_t Word[1024] = {'\0'}; Error = fopen_s(&File, FileName, "rt"); if (Error == 0) { Words.Count = 0; while (!feof(File)) { Count = Get_Word(File, Word); if (Count > 0) { Words.Len[Words.Count] = Count; Words.pWord[Words.Count] = new wchar_t[Count+1]; wcscpy(Words.pWord[Words.Count], Word); Words.Index[Words.Count] = Words.Count; Words.Count++; } } fclose(File); return true; } else { return false; } } // 从文件中读入一个单词 size_t Get_Word(FILE* File, wchar_t* Word) { wchar_t Character; size_t Count = 0; while (true) { Character = fgetwc(File); if (Character == WEOF) { break; } if (!iswspace(Character) && !iswpunct(Character)) // 过滤空白符号和标点符号 { Word[Count] = Character; Count++; } else { break; } } Word[Count] = '\0'; return Count; } // 对数组排序 void Sort(void) { unsigned int i, j; unsigned int Temp1, Temp2; for (i = 0; i < Words.Count - 1; i++) { for (j = i + 1; j < Words.Count; j++) { if (Words.Len[j] < Words.Len[i]) { Temp1 = Words.Len[j]; Words.Len[j] = Words.Len[i]; Words.Len[i] = Temp1; Temp2 = Words.Index[j]; Words.Index[j] = Words.Index[i]; Words.Index[i] = Temp2; } } } }
#include <locale.h> #include <stdio.h> #include <string.h> #include <new.h> #include <ctype.h> #include <conio.h> size_t Get_Word(FILE* File, wchar_t* Word); void Sort(wchar_t* Words[], const size_t Count); void main(int argc, char* argv[]) { FILE* File; errno_t Error; wchar_t* Words[4096]; // 单词数组 size_t Word_Count = 0; // 单词数目 wchar_t Buffer[1024]; // 缓冲字符串 size_t Characters_Number; // 字符数 setlocale(LC_ALL, "chs"); // 设定语言为中文输出 if (argc < 2) { printf_s("Usage: %s <FileName>\n", argv[0]); goto end; } if ((Error = fopen_s(&File, argv[1], "rt")) != 0) { printf_s("文件%s打开错误,请检查再运行!\n", argv[1]); goto end; } while (!feof(File)) { Characters_Number = Get_Word(File, Buffer); Words[Word_Count] = new wchar_t[Characters_Number+1]; Words[Word_Count] = wcscpy(Words[Word_Count], Buffer); Word_Count++; } fclose(File); wprintf_s(L"共%d个单词, 按长度由大到小排序如下:\n", Word_Count); Sort(Words, Word_Count); while (Word_Count > 0) { wprintf_s(L"%s\n", Words[Word_Count-1]); delete[] Words[Word_Count-1]; Words[Word_Count-1] = nullptr; Word_Count--; } end: wprintf_s(L"\n=== 按任意键继续 ==="); _getwch(); } // 从文件中读入一个单词 size_t Get_Word(FILE* File, wchar_t* Word) { wchar_t Character; // 读入字符 size_t Count = 0; // 字符计数器 while ((Character = fgetwc(File)) != WEOF) { if (!iswspace(Character) && !iswpunct(Character)) // 过滤空白符号和标点符号 { Word[Count++] = Character; } else break; } Word[Count] = '\0'; return Count; // 返回字符串的字符数 } // 对数组排序 void Sort(wchar_t* Words[], const size_t Count) { unsigned int i, j; wchar_t* Temp; for (i = 0; i < Count - 1; i++) { for (j = i + 1; j < Count; j++) { if (wcslen(Words[j]) < wcslen(Words[i])) { Temp = Words[j]; Words[j] = Words[i]; Words[i] = Temp; } } } }