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
  • 相关阅读:
    MYSQL查询练习 1
    Mysql语句练习记录
    博客园背景样式修改
    MYSQL安装与卸载(一)
    IDEA 使用与总结
    解决layui弹窗提示刷新页面一闪而逝的问题
    System.Xml.XmlException: 分析 EntityName 时出错
    PS快速把倾斜的图片调正
    iis添加asp.net网站,访问提示:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射
    c# 递归查找父类的子类
  • 原文地址:https://www.cnblogs.com/try-again/p/5011214.html
Copyright © 2011-2022 走看看