版本2和8间的区别 (跳过第6版)
于2006-06-18 16:59:33修订的的版本2
大小: 2310
编辑: czk
备注:
于2006-06-19 09:39:49修订的的版本8
大小: 14785
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 1: 行号 1:
= 典型例题 =

输入行排序
行号 86: 行号 90:

命令行参数指定查找字符串
{{{#!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];
   }

}}}

关键字统计指针版

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

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

   /* count C keywords; pointer version */
   main()
   {
       char word[MAXWORD];
       struct key *p;

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

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

       while (low < high) {
           mid = low + (high-low) / 2;
           if ((cond = strcmp(word, mid->word)) < 0)
               high = mid;
           else if (cond > 0)
               low = mid + 1;
           else
               return mid;
       }
       return NULL;
   }
}}}

单链表自引用结构体

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

#include <stdlib.h>

#include <string.h>



#define MAXWORD 1000

struct node {

    char *word;

    int count;

    struct node *next;

};

struct node*addlist(struct node *head, char *w);

char *strdup(char *s);

void listprint(struct node *p);



main() {

    struct node *head = NULL;

    char word[MAXWORD];

    while(getword(word, MAXWORD)!=EOF)

        if(isalpha(word[0]))

            head = addlist(head, word);

    listprint(head);

    system("pause");

}



void listprint(struct node *p) {

    while(p!=NULL) {

        printf("%4d %s\n", p->count, p->word);

        p = p->next;

    }

}



struct node*addlist(struct node *head, char *w){

    int cond;

    struct node *p = head, *pre = NULL;

    while(p!=NULL) {

        cond=strcmp(w,p->word);

        if(cond == 0) {

            p->count ++;

            return head;

        } else if(cond > 0) {

            pre = p; p = p->next;

        } else

            break;

    }

    p =(struct node*)malloc(sizeof(struct node));

    p->word = strdup(w);

    p->count = 1;

    if(pre == NULL) {

        p->next = NULL;

        head = p;

    } else {

        p->next = pre->next;

        pre->next = p;

    }

    return head;

}



char *strdup(char *s) {

    char *p = (char *)malloc(strlen(s)+1);

    strcpy(p, s);

    return p;

}



#define BUFSIZE 100



char buf[BUFSIZE]; /* buffer for ungetch */

int bufp = 0; /* next free position in buf */

int getch(void) /* get a (possibly pushed-back) character */

{

    return (bufp > 0) ? buf[--bufp] : getchar();

}



void ungetch(int c) /* push character back on input */ {

    if (bufp >= BUFSIZE)

        printf("ungetch: too many characters\n");

    else

        buf[bufp++] = c;

}



int getword(char *word, int lim)

{

    int c;

    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];

}


}}}

变长参数列表
{{{#!cplusplus
   #include <stdarg.h>

   /* minprintf: minimal printf with variable argument list */
   void minprintf(char *fmt, ...)
   {
       va_list ap; /* points to each unnamed arg in turn */
       char *p, *sval;
       int ival;
       double dval;

       va_start(ap, fmt); /* make ap point to 1st unnamed arg */
       for (p = fmt; *p; p++) {
           if (*p != '%') {
               putchar(*p);
               continue;
           }
           switch (*++p) {
           case 'd':
               ival = va_arg(ap, int);
               printf("%d", ival);
               break;
           case 'f':
               dval = va_arg(ap, double);
               printf("%f", dval);
               break;
           case 's':
               for (sval = va_arg(ap, char *); *sval; sval++)
                   putchar(*sval);
               break;
           default:
               putchar(*p);
               break;
           }
       }
       va_end(ap); /* clean up when done */
   }

}}}

文件访问
{{{#!cplusplus
   #include <stdio.h>

   /* cat: concatenate files, version 1 */
   main(int argc, char *argv[])
   {
       FILE *fp;
       void filecopy(FILE *, FILE *)

       if (argc == 1) /* no args; copy standard input */
           filecopy(stdin, stdout);
       else
          while(--argc > 0)
              if ((fp = fopen(*++argv, "r")) == NULL) {
                  printf("cat: can't open %s\n, *argv);
                  return 1;
              } else {
                 filecopy(fp, stdout);
                 fclose(fp);
              }
          return 0;
   }

    /* filecopy: copy file ifp to file ofp */
    void filecopy(FILE *ifp, FILE *ofp)
    {
        int c;

        while ((c = getc(ifp)) != EOF)
            putc(c, ofp);
    }

}}}


= 选择题 =

下列程序执行后的输出结果是 A 6 B 7 C 8 D 9
{{{#!cplusplus
void func(int *a,int b[]) {
    b[0]=*a+6;
}
main() {
    int a,b[5];
    a=0;
    b[0]=3;
    func(&a,b);
    printf("%d\n,b[0]);
}
}}}

下列程序的输出结果是 A 4 B 6 C 8 D 10
{{{#!cplusplus

int b=2;
int func(int *a) {
    b += *a;
    return b;
}
main() {
    int a=2, res=2;
    res += func(&a);
    printf("%d\n",res);
}
}}}

下列程序的输出结果是 A 5 B 6 C 7 D 8
{{{#!cplusplus
struct abc {
    int a, b, c;
};
main() {
    struct abc s[2]={{1,2,3},{4,5,6}};
    int t;
    t=s[0],a+s[1],b;
    printf("%d\n",t);
}
}}}

语句printf("a\bre\'hi\'y\\\bou\n");的输出结果是
{{{ A a\bre\'hi\'y\\\bou B a\bre\'hi\'y\bou C re'hi'you D abre'hi'y\bou}}}

设已有定义: char *st="how are you"; 下列程序段中正确的是
{{{
     A) char a[11], *p; strcpy(p=a+1,&st[4]);
     B) char a[11]; strcpy(++a, st);
     C) char a[11]; strcpy(a, st);
     D) char a[], *p; strcpy(p=&a[1],st+2);
}}}


下列程序执行后的输出结果是 A you&me B you C me D err
{{{#!cplusplus
main() {
    char arr[2][4];
    strcpy(arr[0],"you");
    strcpy(arr[1],"me");
    arr[0][3]='&';
    printf("%s\n",arr[0]);
}
}}}


假定下列程序的可执行文件名为prg.exe,则在该程序所在的子目录下输入命令行: prg hello good<回车>后,程序的输出结果是{{{ A) hello good B) hg C) hel D) hellogood}}}

{{{#!cplusplus
main()(int argc, char *argv[]) {
    int i;
    if(argc<1)
        return;
    for(i=1;i<argc;i++)
        printf("%c", *argv[i]);
}
}}}


以下函数返回a所指数组中最小的值所在的下标值
{{{#!cplusplus
fun(int *a, int n){
    int i,j=0,p;
    p=j;
    for(i=j;i<n;i++)
        if(a[i]<a[p])
            __________;
    return p;
}
}}}
在下划线处应填入的是{{{A) i=p B) a[p]=a[i] C) p=j D) p=i}}}

典型例题

输入行排序

   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    }

关键字统计指针版

   1    #include <stdio.h>
   2    #include <ctype.h>
   3    #include <string.h>
   4    #define MAXWORD 100
   5 
   6    int getword(char *, int);
   7    struct key *binsearch(char *, struct key *, int);
   8 
   9    /* count C keywords; pointer version */
  10    main()
  11    {
  12        char word[MAXWORD];
  13        struct key *p;
  14 
  15        while (getword(word, MAXWORD) != EOF)
  16            if (isalpha(word[0]))
  17                if ((p=binsearch(word, keytab, NKEYS)) != NULL)
  18                    p->count++;
  19        for (p = keytab; p < keytab + NKEYS; p++)
  20            if (p->count > 0)
  21                printf("%4d %s\n", p->count, p->word);
  22        return 0;
  23    }
  24 
  25    /* binsearch: find word in tab[0]...tab[n-1] */
  26    struct key *binsearch(char *word, struck key *tab, int n)
  27    {
  28        int cond;
  29        struct key *low = &tab[0];
  30        struct key *high = &tab[n];
  31        struct key *mid;
  32 
  33        while (low < high) {
  34            mid = low + (high-low) / 2;
  35            if ((cond = strcmp(word, mid->word)) < 0)
  36                high = mid;
  37            else if (cond > 0)
  38                low = mid + 1;
  39            else
  40                return mid;
  41        }
  42        return NULL;
  43    }

单链表自引用结构体

   1 #include <stdio.h>
   2 
   3 #include <stdlib.h>
   4 
   5 #include <string.h>
   6 
   7 
   8 
   9 #define MAXWORD 1000
  10 
  11 struct node {
  12 
  13     char *word;
  14 
  15     int count;
  16 
  17     struct node *next;
  18 
  19 };
  20 
  21 struct node*addlist(struct node *head, char *w);
  22 
  23 char *strdup(char *s);
  24 
  25 void listprint(struct node *p);
  26 
  27 
  28 
  29 main() {
  30 
  31     struct node *head = NULL;
  32 
  33     char word[MAXWORD];
  34 
  35     while(getword(word, MAXWORD)!=EOF)
  36 
  37         if(isalpha(word[0]))
  38 
  39             head = addlist(head, word);
  40 
  41     listprint(head);
  42 
  43     system("pause");
  44 
  45 }
  46 
  47 
  48 
  49 void listprint(struct node *p) {
  50 
  51     while(p!=NULL) {
  52 
  53         printf("%4d %s\n", p->count, p->word);
  54 
  55         p = p->next;
  56 
  57     }
  58 
  59 }
  60 
  61 
  62 
  63 struct node*addlist(struct node *head, char *w){
  64 
  65     int cond;
  66 
  67     struct node *p = head, *pre = NULL;
  68 
  69     while(p!=NULL) {
  70 
  71         cond=strcmp(w,p->word);
  72 
  73         if(cond == 0) {
  74 
  75             p->count ++;
  76 
  77             return head;
  78 
  79         } else if(cond > 0) {
  80 
  81             pre = p; p = p->next;
  82 
  83         } else
  84 
  85             break;
  86 
  87     }
  88 
  89     p =(struct node*)malloc(sizeof(struct node));
  90 
  91     p->word = strdup(w);
  92 
  93     p->count = 1;
  94 
  95     if(pre == NULL) {
  96 
  97         p->next = NULL;
  98 
  99         head = p;   
 100 
 101     } else {
 102 
 103         p->next = pre->next;
 104 
 105         pre->next = p;
 106 
 107     }
 108 
 109     return head;
 110 
 111 }
 112 
 113 
 114 
 115 char *strdup(char *s) {
 116 
 117     char *p = (char *)malloc(strlen(s)+1);
 118 
 119     strcpy(p, s);
 120 
 121     return p;
 122 
 123 }
 124 
 125 
 126 
 127 #define BUFSIZE 100
 128 
 129 
 130 
 131 char buf[BUFSIZE]; /* buffer for ungetch */ 
 132 
 133 int bufp = 0; /* next free position in buf */
 134 
 135 int getch(void) /* get a (possibly pushed-back) character */ 
 136 
 137 {
 138 
 139     return (bufp > 0) ? buf[--bufp] : getchar(); 
 140 
 141 }
 142 
 143 
 144 
 145 void ungetch(int c) /* push character back on input */ {
 146 
 147     if (bufp >= BUFSIZE)
 148 
 149         printf("ungetch: too many characters\n"); 
 150 
 151     else
 152 
 153         buf[bufp++] = c; 
 154 
 155 }
 156 
 157 
 158 
 159 int getword(char *word, int lim)
 160 
 161 {
 162 
 163     int c;
 164 
 165     char *w = word;
 166 
 167 
 168 
 169     while (isspace(c = getch()))
 170 
 171         ;
 172 
 173     if (c != EOF)
 174 
 175         *w++ = c;
 176 
 177     if (!isalpha(c)) {
 178 
 179         *w = '\0';
 180 
 181         return c;
 182 
 183     }
 184 
 185     for ( ; --lim > 0; w++)
 186 
 187         if (!isalnum(*w = getch())) {
 188 
 189             ungetch(*w);
 190 
 191             break;
 192 
 193         }
 194 
 195     *w = '\0';
 196 
 197     return word[0];
 198 
 199 }

变长参数列表

   1    #include <stdarg.h>
   2 
   3    /* minprintf: minimal printf with variable argument list */
   4    void minprintf(char *fmt, ...)
   5    {
   6        va_list ap; /* points to each unnamed arg in turn */
   7        char *p, *sval;
   8        int ival;
   9        double dval;
  10 
  11        va_start(ap, fmt); /* make ap point to 1st unnamed arg */
  12        for (p = fmt; *p; p++) {
  13            if (*p != '%') {
  14                putchar(*p);
  15                continue;
  16            }
  17            switch (*++p) {
  18            case 'd':
  19                ival = va_arg(ap, int);
  20                printf("%d", ival);
  21                break;
  22            case 'f':
  23                dval = va_arg(ap, double);
  24                printf("%f", dval);
  25                break;
  26            case 's':
  27                for (sval = va_arg(ap, char *); *sval; sval++)
  28                    putchar(*sval);
  29                break;
  30            default:
  31                putchar(*p);
  32                break;
  33            }
  34        }
  35        va_end(ap); /* clean up when done */
  36    }

文件访问

   1    #include <stdio.h>
   2 
   3    /* cat:  concatenate files, version 1 */
   4    main(int argc, char *argv[])
   5    {
   6        FILE *fp;
   7        void filecopy(FILE *, FILE *)
   8 
   9        if (argc == 1) /* no args; copy standard input */
  10            filecopy(stdin, stdout);
  11        else
  12           while(--argc > 0)
  13               if ((fp = fopen(*++argv, "r")) == NULL) {
  14                   printf("cat: can't open %s\n, *argv);
  15                   return 1;
  16               } else {
  17                  filecopy(fp, stdout);
  18                  fclose(fp);
  19               }
  20           return 0;
  21    }
  22 
  23     /* filecopy:  copy file ifp to file ofp */
  24     void filecopy(FILE *ifp, FILE *ofp)
  25     {
  26         int c;
  27 
  28         while ((c = getc(ifp)) != EOF)
  29             putc(c, ofp);
  30     }

选择题

下列程序执行后的输出结果是 A 6 B 7 C 8 D 9

   1 void func(int *a,int b[]) {
   2     b[0]=*a+6; 
   3 }
   4 main() { 
   5     int a,b[5];
   6     a=0;
   7     b[0]=3;
   8     func(&a,b);  
   9     printf("%d\n,b[0]);
  10 }

下列程序的输出结果是 A 4 B 6 C 8 D 10

   1 int b=2;
   2 int func(int *a) { 
   3     b += *a; 
   4     return b;
   5 }
   6 main() { 
   7     int a=2, res=2;
   8     res += func(&a);
   9     printf("%d\n",res);
  10 }

下列程序的输出结果是 A 5 B 6 C 7 D 8

   1 struct abc { 
   2     int a, b, c; 
   3 };
   4 main() { 
   5     struct abc  s[2]={{1,2,3},{4,5,6}}; 
   6     int t;
   7     t=s[0],a+s[1],b;
   8     printf("%d\n",t);
   9 }

语句printf("a\bre\'hi\'y\\\bou\n");的输出结果是   A a\bre\'hi\'y\\\bou            B a\bre\'hi\'y\bou     C re'hi'you                     D abre'hi'y\bou

设已有定义: char *st="how are you"; 下列程序段中正确的是

     A) char  a[11], *p;  strcpy(p=a+1,&st[4]);
     B) char  a[11];      strcpy(++a, st);
     C) char  a[11];      strcpy(a, st);
     D) char  a[], *p;    strcpy(p=&a[1],st+2);

下列程序执行后的输出结果是 A you&me B you C me D err

   1 main() { 
   2     char  arr[2][4];
   3     strcpy(arr[0],"you");  
   4     strcpy(arr[1],"me");
   5     arr[0][3]='&';
   6     printf("%s\n",arr[0]);
   7 }

假定下列程序的可执行文件名为prg.exe,则在该程序所在的子目录下输入命令行: prg hello good<回车>后,程序的输出结果是  A) hello good   B) hg             C) hel           D) hellogood

   1 main()(int argc, char *argv[]) {
   2     int  i;
   3     if(argc<1)
   4         return;
   5     for(i=1;i<argc;i++) 
   6         printf("%c", *argv[i]);
   7 }

以下函数返回a所指数组中最小的值所在的下标值

   1 fun(int   *a,  int  n){   
   2     int   i,j=0,p;
   3     p=j;
   4     for(i=j;i<n;i++)
   5         if(a[i]<a[p])
   6             __________;
   7     return p;
   8 }

在下划线处应填入的是A) i=p             B) a[p]=a[i]       C) p=j            D) p=i

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

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