版本2和3间的区别
于2006-06-18 16:59:33修订的的版本2
大小: 2310
编辑: czk
备注:
于2006-06-19 00:59:50修订的的版本3
大小: 7004
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 1: 行号 1:
输入行排序
行号 86: 行号 88:

命令行参数指定查找字符串
{{{#!cplusplus
   #include <stdio.h>
   #include <string.h>
   #define MAXLINE 1000

   int getline(char *line, int max);

   /* find: print lines that match pattern from 1st arg */
   main(int argc, char *argv[])
   {
       char line[MAXLINE];
       int found = 0;

       if (argc != 2)
           printf("Usage: find pattern\n");
       else
           while (getline(line, MAXLINE) > 0)
               if (strstr(line, argv[1]) != NULL) {
                   printf("%s", line);
                   found++;
               }
       return found;
   }
}}}

函数指针

{{{#!cplusplus
   #include <stdio.h>
   #include <string.h>

   #define MAXLINES 5000 /* max #lines to be sorted */
   char *lineptr[MAXLINES]; /* pointers to text lines */

   int readlines(char *lineptr[], int nlines);
   void writelines(char *lineptr[], int nlines);

   void qsort(void *lineptr[], int left, int right,
              int (*comp)(void *, void *));
   int numcmp(char *, char *);

   /* sort input lines */
   main(int argc, char *argv[])
   {
       int nlines; /* number of input lines read */
       int numeric = 0; /* 1 if numeric sort */

       if (argc > 1 && strcmp(argv[1], "-n") == 0)
           numeric = 1;
       if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
           qsort((void**) lineptr, 0, nlines-1,
             (int (*)(void*,void*))(numeric ? numcmp : strcmp));
           writelines(lineptr, nlines);
           return 0;
       } else {
           printf("input too big to sort\n");
           return 1;
       }
   }

   /* qsort: sort v[left]...v[right] into increasing order */
   void qsort(void *v[], int left, int right,
              int (*comp)(void *, void *))
   {
       int i, last;

       void swap(void *v[], int, int);

       if (left >= right) /* do nothing if array contains */
           return; /* fewer than two elements */
       swap(v, left, (left + right)/2);
       last = left;
       for (i = left+1; i <= right; i++)
           if ((*comp)(v[i], v[left]) < 0)
               swap(v, ++last, i);
       swap(v, left, last);
       qsort(v, left, last-1, comp);
       qsort(v, last+1, right, comp);
   }

   #include <stdlib.h>

   /* numcmp: compare s1 and s2 numerically */
   int numcmp(char *s1, char *s2)
   {
       double v1, v2;

       v1 = atof(s1);
       v2 = atof(s2);
       if (v1 < v2)
           return -1;
       else if (v1 > v2)
           return 1;
       else
           return 0;
   }

}}}


{{{#!cplusplus
   #include <stdio.h>
   #include <ctype.h>
   #include <string.h>

   #define MAXWORD 100

   struct key {
       char *word;
       int count;
   } keytab[] = {
       "auto", 0,
       "break", 0,
       "case", 0,
       "char", 0,
       "const", 0,
       "continue", 0,
       "default", 0,
       /* ... */
       "unsigned", 0,
       "void", 0,
       "volatile", 0,
       "while", 0
   };


   int getword(char *, int);
   int binsearch(char *, struct key *, int);

   /* count C keywords */
   main()
   {
       int n;
       char word[MAXWORD];

       while (getword(word, MAXWORD) != EOF)
           if (isalpha(word[0]))
               if ((n = binsearch(word, keytab, NKEYS)) >= 0)
                   keytab[n].count++;
       for (n = 0; n < NKEYS; n++)
           if (keytab[n].count > 0)
               printf("%4d %s\n",
                   keytab[n].count, keytab[n].word);
       return 0;
   }

   /* binsearch: find word in tab[0]...tab[n-1] */
   int binsearch(char *word, struct key tab[], int n)
   {
       int cond;
       int low, high, mid;

       low = 0;
       high = n - 1;
       while (low <= high) {
           mid = (low+high) / 2;
           if ((cond = strcmp(word, tab[mid].word)) < 0)
               high = mid - 1;
           else if (cond > 0)
               low = mid + 1;
           else
               return mid;
       }
       return -1;
   }
   /* getword: get next word or character from input */
   int getword(char *word, int lim)
   {
       int c, getch(void);
       void ungetch(int);
       char *w = word;

       while (isspace(c = getch()))
           ;
       if (c != EOF)
           *w++ = c;
       if (!isalpha(c)) {
           *w = '\0';
           return c;
       }
       for ( ; --lim > 0; w++)
           if (!isalnum(*w = getch())) {
               ungetch(*w);
               break;
           }
       *w = '\0';
       return word[0];
   }

}}}

输入行排序

   1    #include <stdio.h>
   2    #include <string.h>
   3 
   4    #define MAXLINES 5000     /* max #lines to be sorted */
   5 
   6    char *lineptr[MAXLINES];  /* pointers to text lines */
   7 
   8    int readlines(char *lineptr[], int nlines);
   9    void writelines(char *lineptr[], int nlines);
  10 
  11    void qsort(char *lineptr[], int left, int right);
  12 
  13    /* sort input lines */
  14    main()
  15    {
  16        int nlines;     /* number of input lines read */
  17 
  18        if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
  19            qsort(lineptr, 0, nlines-1);
  20            writelines(lineptr, nlines);
  21            return 0;
  22        } else {
  23            printf("error: input too big to sort\n");
  24            return 1;
  25        }
  26    }
  27 
  28    #define MAXLEN 1000  /* max length of any input line */
  29    int getline(char *, int);
  30    char *alloc(int);
  31 
  32    /* readlines:  read input lines */
  33    int readlines(char *lineptr[], int maxlines)
  34    {
  35        int len, nlines;
  36        char *p, line[MAXLEN];
  37 
  38        nlines = 0;
  39        while ((len = getline(line, MAXLEN)) > 0)
  40            if (nlines >= maxlines || p = alloc(len) == NULL)
  41                return -1;
  42            else {
  43                line[len-1] = '\0';  /* delete newline */
  44                strcpy(p, line);
  45                lineptr[nlines++] = p;
  46            }
  47        return nlines;
  48    }
  49 
  50    /* writelines:  write output lines */
  51    void writelines(char *lineptr[], int nlines)
  52    {
  53        int i;
  54 
  55        for (i = 0; i < nlines; i++)
  56            printf("%s\n", lineptr[i]);
  57    }
  58    /* qsort:  sort v[left]...v[right] into increasing order */
  59    void qsort(char *v[], int left, int right)
  60    {
  61        int i, last;
  62        void swap(char *v[], int i, int j);
  63 
  64        if (left >= right)  /* do nothing if array contains */
  65            return;         /* fewer than two elements */
  66        swap(v, left, (left + right)/2);
  67        last = left;
  68        for (i = left+1; i <= right; i++)
  69            if (strcmp(v[i], v[left]) < 0)
  70                swap(v, ++last, i);
  71        swap(v, left, last);
  72        qsort(v, left, last-1);
  73        qsort(v, last+1, right);
  74    }
  75    /* swap:  interchange v[i] and v[j] */
  76    void swap(char *v[], int i, int j)
  77    {
  78        char *temp;
  79 
  80        temp = v[i];
  81        v[i] = v[j];
  82        v[j] = temp;
  83    }

命令行参数指定查找字符串

   1    #include <stdio.h>
   2    #include <string.h>
   3    #define MAXLINE 1000
   4 
   5    int getline(char *line, int max);
   6 
   7    /* find:  print lines that match pattern from 1st arg */
   8    main(int argc, char *argv[])
   9    {
  10        char line[MAXLINE];
  11        int found = 0;
  12 
  13        if (argc != 2)
  14            printf("Usage: find pattern\n");
  15        else
  16            while (getline(line, MAXLINE) > 0)
  17                if (strstr(line, argv[1]) != NULL) {
  18                    printf("%s", line);
  19                    found++;
  20                }
  21        return found;
  22    }

函数指针

   1    #include <stdio.h>
   2    #include <string.h>
   3 
   4    #define MAXLINES 5000     /* max #lines to be sorted */
   5    char *lineptr[MAXLINES];  /* pointers to text lines */
   6 
   7    int readlines(char *lineptr[], int nlines);
   8    void writelines(char *lineptr[], int nlines);
   9 
  10    void qsort(void *lineptr[], int left, int right,
  11               int (*comp)(void *, void *));
  12    int numcmp(char *, char *);
  13 
  14    /* sort input lines */
  15    main(int argc, char *argv[])
  16    {
  17        int nlines;        /* number of input lines read */
  18        int numeric = 0;   /* 1 if numeric sort */
  19 
  20        if (argc > 1 && strcmp(argv[1], "-n") == 0)
  21            numeric = 1;
  22        if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
  23            qsort((void**) lineptr, 0, nlines-1,
  24              (int (*)(void*,void*))(numeric ? numcmp : strcmp));
  25            writelines(lineptr, nlines);
  26            return 0;
  27        } else {
  28            printf("input too big to sort\n");
  29            return 1;
  30        }
  31    }
  32 
  33    /* qsort:  sort v[left]...v[right] into increasing order */
  34    void qsort(void *v[], int left, int right,
  35               int (*comp)(void *, void *))
  36    {
  37        int i, last;
  38 
  39        void swap(void *v[], int, int);
  40 
  41        if (left >= right)    /* do  nothing if array contains */
  42            return;           /* fewer than two elements */
  43        swap(v, left, (left + right)/2);
  44        last = left;
  45        for (i = left+1; i <= right;  i++)
  46            if ((*comp)(v[i], v[left]) < 0)
  47                swap(v, ++last, i);
  48        swap(v, left, last);
  49        qsort(v, left, last-1, comp);
  50        qsort(v, last+1, right, comp);
  51    }
  52 
  53    #include <stdlib.h>
  54 
  55    /* numcmp:  compare s1 and s2 numerically */
  56    int numcmp(char *s1, char *s2)
  57    {
  58        double v1, v2;
  59 
  60        v1 = atof(s1);
  61        v2 = atof(s2);
  62        if (v1 < v2)
  63            return -1;
  64        else if (v1 > v2)
  65            return 1;
  66        else
  67            return 0;
  68 

   1    #include <stdio.h>
   2    #include <ctype.h>
   3    #include <string.h>
   4 
   5    #define MAXWORD 100
   6 
   7    struct key {
   8        char *word;
   9        int count;
  10    } keytab[] = {
  11        "auto", 0,
  12        "break", 0,
  13        "case", 0,
  14        "char", 0,
  15        "const", 0,
  16        "continue", 0,
  17        "default", 0,
  18        /* ... */
  19        "unsigned", 0,
  20        "void", 0,
  21        "volatile", 0,
  22        "while", 0
  23    };
  24 
  25 
  26    int getword(char *, int);
  27    int binsearch(char *, struct key *, int);
  28 
  29    /* count C keywords */
  30    main()
  31    {
  32        int n;
  33        char word[MAXWORD];
  34 
  35        while (getword(word, MAXWORD) != EOF)
  36            if (isalpha(word[0]))
  37                if ((n = binsearch(word, keytab, NKEYS)) >= 0)
  38                    keytab[n].count++;
  39        for (n = 0; n < NKEYS; n++)
  40            if (keytab[n].count > 0)
  41                printf("%4d %s\n",
  42                    keytab[n].count, keytab[n].word);
  43        return 0;
  44    }
  45 
  46    /* binsearch:  find word in tab[0]...tab[n-1] */
  47    int binsearch(char *word, struct key tab[], int n)
  48    {
  49        int cond;
  50        int low, high, mid;
  51 
  52        low = 0;
  53        high = n - 1;
  54        while (low <= high) {
  55            mid = (low+high) / 2;
  56            if ((cond = strcmp(word, tab[mid].word)) < 0)
  57                high = mid - 1;
  58            else if (cond > 0)
  59                low = mid + 1;
  60            else
  61                return mid;
  62        }
  63        return -1;
  64    }
  65    /* getword:  get next word or character from input */
  66    int getword(char *word, int lim)
  67    {
  68        int c, getch(void);
  69        void ungetch(int);
  70        char *w = word;
  71 
  72        while (isspace(c = getch()))
  73            ;
  74        if (c != EOF)
  75            *w++ = c;
  76        if (!isalpha(c)) {
  77            *w = '\0';
  78            return c;
  79        }
  80        for ( ; --lim > 0; w++)
  81            if (!isalnum(*w = getch())) {
  82                ungetch(*w);
  83                break;
  84            }
  85        *w = '\0';
  86        return word[0];
  87    }

C语言练习5 (2008-02-23 15:34:09由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.