zoukankan      html  css  js  c++  java
  • C语言-C语言程序设计-Function-Find

    C语言-C语言程序设计-Function-Find

    照着书敲了一遍然后又重新读了一次才发现程序通过while循环识别的 -xn这种输入。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLINE 1000
    
    int getline(char *line, int max);
    
    /* find函数: 打印所有与第一个参数指定的模式相匹配的行*/
    /* find -x -n */
    int main(int argc, char *argv[])
    {
        char line[MAXLINE];
        long lineno = 0;
        int c, except = 0, number = 0,found = 0;
    
        while(--argc > 0 && (*++argv)[0] == '-')
            while( c = *++argv[0]) //这里循环直接识别了 -xn
                switch(c){
                case 'x':
                    except = 1;
                    break;
                case 'n':
                    number = 1;
                    break;
                default:
                    printf("find: illegal option %c
    ", c);
                    argc = 0;
                    found = -1;
                    break;
                }
        if(argc != 1)
            printf("Usage:find -x -n pattern
    ");
        else
            while(getline(line, MAXLINE) > 0){
                lineno++;
                if((strstr(line, *argv) != NULL) != except){
                    if(number)
                        printf("%ld", line);
                    printf("%s", line);
                    found++;
                }
            }
        return found;
    }
    
    int getline(char *line, int max)
    {
        if(fgets(line, max, stdin) == NULL)
            return 0;
        else
            return strlen(line);
    }
    
    
  • 相关阅读:
    poj 2516 Minimum Cost (最小费用流 )
    new start
    关于c语言中的声明和定义
    多态性与虚函数之对象切片
    C专家编程之typedef
    QGroupBox设置边框
    多态性与虚函数之继承中的virtual 晚捆绑
    使用Map
    遍历控件
    C专家编程之枚举
  • 原文地址:https://www.cnblogs.com/yongchao/p/13972487.html
Copyright © 2011-2022 走看看