排序的文本文件 [英] Sorting A text file

查看:96
本文介绍了排序的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/* Program that reads a sequence of words from keyboard
   and prints the list of words without duplicates and
   sorted in ascending lexicographic order.
   The input words are written one per line and the
   sequence is terminated by an empty line.
   The program works with at most MAX words, each at
   most MAXL characters long. Longer words are truncated
   and words in excess are ignored.
*/

#include <stdio.h>
#include <string.h>

#define MAXL           80  /* maximum word length */
#define MAX           100  /* maximum number of words */

/* word storage */
char storage[MAX][MAXL];
char *words[MAX];

void init(char *pnt[], char matr[][MAXL], int max);
int read_words (char *s[], int max);
void sort_strings (char *s[], int len);
void swap_char_pnt(char **xp, char **yp);
void print_words(char *s[], int n);
int find (char *s[], char w[], int n);

main()
{
  int nw; /* actual number of words */

  /* initialize array of pointers */
  init(words, storage, MAX);

  /* read and store words */
  printf("Enter words one per line\n");
  nw = read_words(words, MAX);

  printf("\nList of unsorted words:\n");
  print_words(words, nw);

  /* sort words */
  sort_strings(words, nw);

  /* print words */
  printf("\nList of sorted words:\n");
  print_words(words, nw);
}


/* initializes an array of pointers to the rows of
 a matrix of characters
 max is the number of pointers to initialize
*/
void init(char *pnt[], char matr[][MAXL], int max) {
  int i;

  for (i=0; i<max; i++)
    pnt[i] = matr[i];
}

/* Reads a sequence of words from stdin, one per line
 Reads at most max words and stores them in s
 Returns actual number of words read
*/
int read_words (char *s[], int max)
{
  int i;
  fgets(s[0], MAXL, stdin);
  for (i=1; i<max && fgets(s[i], MAXL, stdin)!=NULL; ) {
        if(!strcmp(s[i],"\n"))
    break;
    if (find(s, s[i], i)==-1)
    i++;
  }
  return i;
}

int find (char *s[], char w[], int n)
{
  int i;

  if (n<0)
    return -1;
  for (i=0; i<n; i++)
    if (!strcmp(s[i],w))
    break;
  if (i==n)
    return -1;
  else
    return i;
}

/* Sorts an aray of pointers to strings in ascending order
*/
void sort_strings (char *s[], int len)
{
  int i,k; /* vector index and counter */
  char swaps=1; /* boolean variable: true if any swap has occurred in last round */

  for (k=1; k<=len-1 && swaps; k++) {
  swaps=0;
  for (i=0; i<len-k; i++)
  if (strcmp(s[i],s[i+1])>0) {
      /* swap */
      swap_char_pnt(&s[i], &s[i+1]);
      swaps=1;
  }
  }
}

void swap_char_pnt(char **xp, char **yp) {
  char *temp;

  temp = *xp;
  *xp = *yp;
  *yp = temp;
}

void print_words(char *s[], int n)
{
  int i;

  for (i=0; i<n; i++)
    printf("%s", s[i]);

}

我能做些什么来排序字符串中的升序文本文件。

What Can i Do to Sort Text File in Ascending Order of Strings.

推荐答案


  • 阅读所有行的char *数组 S,

  • 从定义的qsort 字符串比较函数&LT;文件stdlib.h&GT; 头文件:

  • Read all lines to an array of char* s,
  • define a string comparator function for qsort from <stdlib.h> header file:

    int compare(const void* a, const void* b) {
        return strcmp(*(const char**)a, *(const char**)b); 
    }
    


  • 使用的qsort()的阵列上,使用这种比较功能。

  • Use qsort() on that array, using this compare function.

    这篇关于排序的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆