练习5-12 对程序entab和detab的功能做一些扩充,以接受下列缩写的命令:entab -m +n表示制表符从第m列开始,每隔n列停止

来源:互联网 发布:怎么查看软件注册表 编辑:程序博客网 时间:2024/06/07 17:23

答案所给程序思路简单:

#include <stdio.h>void esettab(int argc,char *argv[],char *tab);void entab(char *tab);int tabpos(int pos,char *tab);#define MAXLINE 100#define TABINC  8#define YES 1#define NO  0main(int argc,char *argv[]){    char tab[MAXLINE+1];    esettab(argc,argv,tab);    entab(tab);    return 0;}void entab(char *tab){    int c,pos;    int nb=0;    int nt=0;    for(pos=1;(c=getchar())!=EOF;pos++){        if(c==' '){            if(tabpos(pos,tab)!=YES)                ++nb;            else{                nb=0;                ++nt;            }        }        else{            for( ;nt>0;--nt)                putchar('\t');            if(c=='\t')                nb=0;            else                for( ;nb>0;--nb)                    putchar(' ');            putchar(c);            if(c=='\n')                pos=0;            else if(c=='\t')                while(tabpos(pos,tab)!=YES)                    ++pos;        }    }}#include <stdlib.h>void esettab(int argc,char *argv[],char *tab){    int i,inc,pos;    if(argc<=1)        for(i=1;i<=MAXLINE;i++)            if(i%TABINC==0)                tab[i]=YES;            else                tab[i]=NO;    else if(argc==3 && *argv[1]=='-' && *argv[2]=='+'){        pos=atoi(&(*++argv)[1]);        inc=atoi(&(*++argv)[1]);        for(i=1;i<=MAXLINE;i++)            if(i!=pos)                tab[i]=NO;            else{                tab[i]=YES;                pos+=inc;            }    }    else{        for(i=1;i<=MAXLINE;i++)            tab[i]=NO;        while(--argc>0){            pos=atoi(*++argv);            if(pos>0 && pos<=MAXLINE)                tab[pos]=YES;        }    }}int tabpos(int pos,char *tab){    if(pos>MAXLINE)        return YES;    else        return tab[pos];}

命令行输入exam5-12 6,回车,再输入6个空格、制表符、a:

        a

输出两个制表符、a:

                a
0 0
原创粉丝点击