c语言单词排序

Python09

c语言单词排序,第1张

程序第一次运行时,会创建一个“word.txt”(不包括引号)的文本文件,然后要求输入单词。若要退出,请不要点DOS窗口的小叉叉,输入d即可。因为程序在结束之前,对数组中的单词重新排序,并存储到文件中。 #include "stdio.h"---

#include "stdlib.h" ---为exit()函数提供原型; #include "string.h"---字符串处理函数原型; #include "ctype.h"---字符处理函数原型; #define ROWS 256

#define COLS 32---定义“字典”的大小:可存放256个单词,每个单词的长度不超过31

static FILE *fp---定义文件指针:内部链接,文件作用域;

static char a[ROWS][COLS]---定义数组:内部链接,文件作用域;该数组的作用是将文件的内容复制进来,并加以处理。因为处理数组比处理文件方便。

char get_option(void)---接收用户的选项,防止误操作。若输入“a”(不包括引号),那么将视为选项a

int b(int count)---完成选项b的作用--接收新单词;

void c(char *pt[], int count)---完成选项c的作用--通过指针对数组排序,实际数组元素位置未改变;

int check(char arr[], int count)---对输入的单词进行分辨,若输入 ni hao ,将视为单词 ni ,并且提示并剔除重复的单词;

void storage(char *pt[], int count)---在程序结束之前重新排序存储数组中的单词到文件中。

#include "stdio.h" #include "stdlib.h" #include "string.h" #include "ctype.h" #define ROWS 256 #define COLS 32 static FILE *fp

static char a[ROWS][COLS] char get_option(void) int b(int count)

void c(char *pt[], int count) int check(char arr[], int count) void storage(char *pt[], int count) int main(void) {

int i,count int start

char *pt[ROWS] char ch, len char input

if((fp=fopen("words.txt","a+"))==NULL) {

fputs("不能打开或建立文件!\n",stderr) exit(1) }

fseek(fp,0L,SEEK_END) start=(int)ftell(fp)/32 count=start rewind(fp)

if(fread(a,32*sizeof(char),start,fp)==0) { i=0

puts("开始创建词库")

puts("请输入单词(每行一个)")

puts("在新行输入END结束输入:") while(i<ROWS&&scanf("%s", a[i])==1) {

fflush(stdin)

if(strncmp(a[i],"END",3)==0) {

count+=i break

}

if(check(a[i], i)) continue i++ } }

puts("\t\t*********************欢迎使用字典排版系统*******************\n\n")

puts(" MENU ")puts("您要做些什么?")

puts("a. 显示已有的单词 b. 添加新单词") puts("c. 对已有的单词进行排序 d. 退出")

puts("\n\n\t\t**********************************************************\n")while((input=get_option())!='d')

{

if(input=='a') { puts("已有的单词:") for(i=0i<counti++)

{

printf(" ") puts(a[i]) } }

if(input=='b')

{

puts("开始创建词库")

puts("请输入新的单词(每行一个)")puts("在新行输入END结束输入: ") count=b(count) }

if(input=='c') {

puts("对单词进行排序:") c(pt, count)

for(i=0i<counti++) {

printf(" ") puts(pt[i]) } }

puts("还要做些什么?") }

storage(pt,count) fclose(fp)

puts("谢谢使用,再见!")

return 0 }

char get_option(void) {

char ch

while((ch=getchar())<'a'||ch>'d') {

while((ch=getchar())!='\n')

puts("请输入a,b,c或者d.") }

fflush(stdin)

return ch }

int b(int count) { int i

i=count

while(i<ROWS&&scanf("%s", a[i])==1) {

fflush(stdin) if(check(a[i], i)) continue

if(strncmp(a[i],"END",3)==0) {

count=i break } i++ }

return count }

void c(char *pt[], int count) { int i,j

char *temp

for(i=0i<ROWSi++) pt[i]=a[i]

for(i=0i<counti++) for(j=i+1j<countj++) {

if(strcmp(pt[i],pt[j])>0) {

temp=pt[i] pt[i]=pt[j] pt[j]=temp } } }

int check(char arr[], int count) { int i

int flag=0

for(i=0i<strlen(arr)i++) if(isalpha(arr[i])==0) {

printf("%s不是一个单词.\n",arr) flag=1 break }

for(i=0i<counti++)

if(strncmp(a[i],a[count],strlen(a[count])+1)==0) {

puts("重复的单词!") flag=1 }

return flag }

void storage(char *pt[], int count) { int i,j

char ptr[ROWS][COLS]

c(pt, count)

for(i=0i<counti++)

for(j=0pt[i][j]!='\0'j++) ptr[i][j]=pt[i][j]

fp=fopen("words.txt","w+") rewind(fp)

fwrite(ptr,32*sizeof(char),count,fp) }

说明:原题目中的const要删除,否则过不了编译。因为const了就不能排序了…… #include #include "string.h"int GetWords(char *sentence, char *words[])void SortStrings(char *strs[], int count)//const int main(int argc,char *argv[]){ char str[200]int nWords = 0char *words[20]int iprintf("input a string: ")gets(str)nWords = GetWords(str,words)SortStrings(words, nWords)puts("output:")for(i=0i0) k=jif(k-i) p=strs[i],strs[i]=strs[k],strs[k]=p} /******end******/} 执行结果如下:

#include <iostream>

#include <fstream>

#include <vector>

#include <string>

#include <algorithm>

#include <iterator>

using namespace std

int main(int argc, char *argv[])

{

  if (argc != 3) {

      cout<<"need two param\nexample: sort file.in file.out"<<endl

  }

  ifstream in(argv[1])

  ofstream out(argv[2])

  //read in and sort

  istream_iterator<string>it_file(in)

  istream_iterator<string>end_of_stream

  vector<string>vec(it_file, end_of_stream)

  sort(vec.begin(), vec.end())

  //output

  ostream_iterator<string>output(out, " ")

  unique_copy(vec.begin(), vec.end(), output)

  in.close()

  out.close()                                                                                                                         

  return 0

}