命令行参数

按照 C 语言的约定,argv[0]的值是启动该程序的程序名,因此 argc 的值至少为 1。
如果 argc 的值为 1,则说明程序名后面没有命令行参数。在上面的例子中,argc 的值为 3,
argv[0]、argv[1]和 argv[2]的值分别为“echo”、 “hello,”,以及“world”
。第一个可选参数为 argv[1],而最后一个可选参数为 argv[argc-1]。另外,ANSI 标准要求argv[argc]的值必须为一空指针.

echo

#include <stdio.h>

/* echo command-line arguments; 1st version */
int main(int argc, char *argv[])
{
    int i;
    for(i = 1; i < argc; i++)
        printf("%s%s", argv[i], i<argc-1 ? " ":"");
    printf("\n");
    return 0;
}

int main(int argc, char *argv[])
{
    while(argc-- > 1)
        printf("%s%s", *++argv, argc > 1? " ":"");
    printf("\n");
    return 0;
}

也可以将 printf 语句写成下列形式:
printf((argc > 1) ? “%s ” : “%s”, *++argv);


find

//通过命令行的第一个参数指定待匹配的模式
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000

int getLine(char *line, int max);
/* find: print lines that match pattern from 1st arg */
int 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;
}

int getLine(char *line, int max)
{
    int i = 0;
    char c;
    for(; i < max-1 && (c = getchar()) != EOF && c != '\n'; i++)
        line[i] = c;
    if(c == '\n')
        line[i++] = c;
    line[i] = '\0';
    return i;
}

//标准库函数 strstr(s, t)返回一个指针,该指针指向字符串 t 在字符串 s 中第一次出现的位置;如果字符串 t 没有在字符串 s 中出现,函数返回 NULL(空指针)。该函数声明在头文件<string.h>中。

UNIX 系统中的 C 语言程序有一个公共的约定:以负号开头的参数表示一个可选标志或参数。假定用-x(代表“除……之外”)表示打印所有与模式不匹配的文本行,用-n(代表“行号”)表示打印行号,那么下列命令:
find -x -n 模式
将打印所有与模式不匹配的行,并在每个打印行的前面加上行号
find -nx 模式

#include <stdio.h>
#include <string.h>
#define MAXLINE 1000

int getLine(char *line, int max);
/* find: print lines that match pattern from 1st arg */
int main(int argc, char *argv[])
{
    char line[MAXLINE];
    int found = 0, except = 0, number = 0;
    int c;
    long lineno = 0;
    while(--argc > 0 && (*++argv)[0] == '-')
        while(c = *++argv[0]){
            switch(c){
            case 'x':
                except = 1;
                break;
            case 'n':
                number = 1;
                break;
            default:
                printf("find: illegal option %c\n", c);
                argc = 0;
                found = -1;
                break;
            }
        }
    if(argc != 1)
        printf("Usage: find -x -n pattern\n");
    else {
        while(getLine(line, MAXLINE) > 0){
            lineno++;
            if((strstr(line, *argv) != NULL) != except){
                if(number)
                    printf("%ld:", lineno);
                printf("%s", line);
                found++;
            }
        }
    }
    return found;
}

int getLine(char *line, int max)
{
    int i = 0;
    char c;
    for(; i < max-1 && (c = getchar()) != EOF && c != '\n'; i++)
        line[i] = c;
    if(c == '\n')
        line[i++] = c;
    line[i] = '\0';
    return i;
}
//编程艺术

练习 5-13

编写程序 tail,将其输入中的最后 n 行打印出来。默认情况下,n 的值为
10,但可通过一个可选参数改变 n 的值,因此,命令
tail -n
将打印其输入的最后 n 行。无论输入或 n 的值是否合理,该程序都应该能正常运行

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINES 1000

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

static char *lineptr[MAXLINES];

int main(int argc, char *argv[])
{
    int n = 10;
    int nlines;
    if(argc > 1 && (*++argv)[0] == '-')
        n = (int)atof(++argv[0]);
    nlines = readlines(lineptr, MAXLINES);
    printf("\n");
    writelines(lineptr, nlines, n);
    return 0;
}

void writelines(char *lineptr[], int nlines, int n)
{
    if(n >= nlines)
        while(nlines-- > 0)
            printf("%s\n", *lineptr++);
    else
        while(n-- > 0)
            printf("%s\n", lineptr[nlines-n-1]);
}       

#define MAXLEN 1000
int getLine(char *p, int maxlen);
char *alloc(int n);

int readlines(char *lineptr[], int maxlines)
{
    int nlines = 0, len;
    char line[MAXLEN], *p;
    while((len = getLine(line, MAXLEN)) > 0){
        if(nlines >= maxlines || (p = alloc(len)) == NULL)
            return -1;
        else{
            line[len-1] = '\0';
            strcpy(p, line);
            lineptr[nlines++] = p;
        }
    }
    return nlines;
}

int getLine(char *p, int maxlen)
{
    int c, i;
    for(i = 0; i < maxlen-1 && (c = getchar()) != EOF && c != '\n'; i++)
        p[i] = c;
    if(c == '\n')
        p[i++] = c;
    p[i] = '\0';
    return i;
}

#define ALLOCSIZE 10000 /* size of available space */
static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

char *alloc(int n)
{
    if(allocbuf + ALLOCSIZE - allocp >= n){
        allocp += n;
        return allocp - n;
    }
    else 
        return 0;
}
测试:
> ./tail -2
qw
er
rt

er
rt

练习 5-14

修改排序程序,使它能处理-r 标记。该标记表明,以逆序(递减)方式排
序。要保证-r 和-n 能够组合在一起使用。

#include <stdio.h>
#include <string.h>

#define MAXLINES 5000
char *lineptr[MAXLINES];

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 *);
void reverse(void *[], int nlines);

int main(int argc, char *argv[])
{
    int nlines, c;
    int numeric = 0, reverses = 0;
    while(--argc > 0 && **++argv == '-')
        while(c = *++argv[0])
            if(c == 'n')
                numeric = 1;
            else if(c == 'r')
                reverses = 1;
            else
                printf("wrong option\n");

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

void reverse(void *v[], int nlines)
{
    void swap(void *v[], int, int);
    int i,j;
    for(i = 0, j = nlines-1; i<j; i++, j--)
        swap(v, i, j);
}

void qSort(void *v[], int left, int right, int (*comp)(void *, void *))
{
    void swap(void *v[], int, int);
    int i, last;

    if(left >= right)
        return;
    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);
}

void swap(void *v[], int i, int j)
{
    void *p;
    p = v[i];
    v[i] = v[j];
    v[j] = p;
}

#include <stdlib.h>

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

void writelines(char *lineptr[], int nlines)
{
    while(nlines-- > 0)
        printf("%s\n", *lineptr++);
}

int getLine(char *p, int maxlen);
#define MAXLEN 1000
int readlines(char *lineptr[], int maxlines)
{
    int len, nlines = 0;
    char line[MAXLEN], *p;
    while((len = getLine(line, MAXLEN)) > 0){
        if(nlines >= maxlines || (p = (char *)malloc(len)) == NULL)
            return -1;
        else{
            line[len-1] = '\0';
            strcpy(p, line);
            lineptr[nlines++] = p;
        }
    }
    return nlines;
}

int getLine(char *p, int maxlen)
{
    int i, c;
    for(i = 0; i < maxlen-1 && (c = getchar()) != EOF && c != '\n'; i++)
        p[i] = c;
    if(c == '\n')
        p[i++] = c;
    p[i] = '\0';
    return i;
}

说明://qsort为<stdlib.h>下函数
    //getline为<stdio.h>下函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值