zoukankan      html  css  js  c++  java
  • 第五章 指针与数组

    5.2 指针与函数参数

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
     
    int getch(void);
    void ungetch(int);
     
    /*getint 函数:将输入中的下一个整型数赋值给*pn */
    int getint(int* pn)
    {
        int c, sign;
     
        while (isspace(c = getch())) /*跳过空白符*/
            ;
        if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
            ungetch(c); /*输入不是一个数字 */
            return 0;
        }
        sign = (c == '-') ? -1 : 1;
        if (c == '+' || c == '-')
            c = getch();
        for (*pn = 0; isdigit(c); c = getch())
            * pn = 10 * *pn + (c - '0');
        *pn *= sign;
        if (c != EOF)
            ungetch(c);
        return c;
    }
     
    #define BUFSIZE 100
     
    char buf[BUFSIZE];
    int bufp = 0;
     
    int getch(void)
    {
        return (bufp > 0) ? buf[--bufp] : getchar();
    }
     
    void ungetch(int c)
    {
        if (bufp >= BUFSIZE)
            printf("ungetch: too many characters
    ");
        else
            buf[bufp++] = c;
    }
     
    int main()
    {
        int n, array[10];
        for (n = 0; n < 10 && getint(&array[n]) != EOF; n++)
            ;
        for (n = 0; n < 10; n++)
            printf("%d
    ", array[n]);
        return 0;
    }

    5.6 指针数组以及指向指针的指针

    #include <stdio.h>  
    #include <string.h>  
    #include <stdio.h>
    #define MAXLINES 5000 /*最多可以输入5000行*/  
    #define MAXLEN 1000 /*max length of any input line*/  
    #define ALLOCSIZE 10000 /*size of available space */  
    
    char *lineptr[MAXLINES]; /*指向所有行的指针*/  
    static char allocbuf[ALLOCSIZE]; /*storage for alloc*/  
    static char *allocp = allocbuf; /*next free position */       
    
    int readlines(char *lineptr[], int nlines);  
    void writelines(char *lineptr[], int nlines);  
    int add_getline(char *, int);  
    char *alloc(int);  
    void qsort(char *lineptr[], int left, int right);  
    
    /* sort input lines*/  
    int main()
    {  
        int nlines; /*number of input lines read*/  
    
        if ((nlines = readlines(lineptr, MAXLINES)) > 0) {  
            qsort(lineptr, 0, nlines - 1);  
            writelines(lineptr, nlines);  
            return 0;  
        }  
        else{  
            printf(" error : input too big to sort 
    ");  
            return 1;  
        }     
    }    
    
    /*readlines : read input lines*/  
    int readlines(char *lineptr[], int maxlines)
    {  
        int len, nlines;  
        char *p, line[MAXLEN];  
    
        nlines = 0;  
        printf("please input the strings need to sort
    ");
        while ((len = add_getline(line, MAXLEN)) > 0) {  
            if (nlines >= maxlines || (p = alloc(len)) == NULL) {  
            return -1;  
            }  
            else{  
            line[len - 1] = '';/*删除换行符*/  
            strcpy(p, line);  
            lineptr[nlines++] = p;  
            }  
        }  
        return nlines;  
    }  
    
    /*writelines : write output lines */  
    void writelines(char *lineptr[], int nlines)
    {  
        int i ;  
        printf("
    ");
        printf("output the sorted strings is:
    ");
        for ( i = 0; i < nlines; i ++) {  
            printf("%s
    ", lineptr[i]);  
        }  
    }  
    
    /************************************************************************/  
    /* qsort : sort v[left]...v[right] into increasing order 
    ************************************************************************/  
    void qsort(char *v[], int left, int right)
    {  
        int i , last;  
        void swap(char *v[], int i , int j);//声明交换函数  
    
        if (left >= right) {  
            return;  
        }  
    
        swap(v, left, (left + right) / 2);  
        last = left;  
        for (i = left + 1; i <= right; i ++) {  
            if (strcmp(v[i], v[left]) < 0) {  
            swap(v, ++last, i);  
            }  
        }  
        swap(v, left, last);  
        qsort(v,left, last - 1);  
        qsort(v, last + 1, right);  
    }  
    
    void swap(char *v[], int i , int j)
    {  
        char *temp;  
    
        temp = v[i];  
        v[i] = v[j];  
        v[j] = temp;  
    }  
    
    /************************************************************************/  
    /* getline : get line into s, return length 
    ************************************************************************/  
    int add_getline(char s[], int lim)
    {  
        int c, i;  
    
        i = 0;  
        while ( --lim > 0 && (c = getchar()) != EOF && c != '
    ') {  
            s[i++] = c;  
        }  
    
        if (c == '
    ') {  
            s[i++] = c;  
        }  
        s[i] = '';  
        return i;  
    }  
    
    char *alloc(int n)
    {/* return pointer to n characters*/  
        if (allocbuf + ALLOCSIZE - allocp >= n) {  
            allocp += n;  
            return allocp - n;/*old p*/  
        }  
        else{ /* not enough room*/  
            return 0;  
        }  
    }  
    编译执行,显示:
    please input the strings need to sort
    paul
    ray
    allen
    kobe
    abi
    bill

    output the sorted strings is:
    abi
    allen
    bill
    kobe
    paul
    ray
  • 相关阅读:
    稳扎稳打Silverlight(13) 2.0交互之鼠标事件和键盘事件
    稳扎稳打Silverlight(17) 2.0数据之详解DataGrid, 绑定数据到ListBox
    再接再厉VS 2008 sp1 + .NET 3.5 sp1(2) Entity Framework(实体框架)之详解 Linq To Entities 之一
    稳扎稳打Silverlight(8) 2.0图形之基类System.Windows.Shapes.Shape
    稳扎稳打Silverlight(11) 2.0动画之ColorAnimation, DoubleAnimation, PointAnimation, 内插关键帧动画
    稳扎稳打Silverlight(21) 2.0通信之WebRequest和WebResponse, 对指定的URI发出请求以及接收响应
    稳扎稳打Silverlight(16) 2.0数据之独立存储(Isolated Storage)
    稳扎稳打Silverlight(9) 2.0画笔之SolidColorBrush, ImageBrush, VideoBrush, LinearGradientBrush, RadialGradientBrush
    稳扎稳打Silverlight(23) 2.0通信之调用WCF的双向通信(Duplex Service)
    游戏人生Silverlight(1) 七彩俄罗斯方块[Silverlight 2.0(c#)]
  • 原文地址:https://www.cnblogs.com/try-again/p/5011214.html
Copyright © 2011-2022 走看看