69,374
社区成员
发帖
与我相关
我的任务
分享
/***********************************************************************
Copyright (c) 2009,liushac
All rights no reserved.
Name: <<C程序设计语言>>例程5-10
ID: hello.c
功能: 允许程序有两个可选变元。其中一个表示“与模式串不匹配的文本行”,
另一个表示“每个被打印文本行前面都有相应的行号”。
-x(x代表except)表示打印所有与模式串不匹配的文本行;
-n(n代表number)表示打印行号。
Version: 0.1
Author: c.
Date: Sep 25, 2009
***********************************************************************/
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getline(char *line,int max);
main(int agrc,char *argv[])
{
char *line;
long lineno=0;
int c,except=0,number=0,found=0;
while(--argc>0 && (* ++argv)[0]=='-')
{
while(c=*++argv[0])
switch(0)
{
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("%d:",lineno);
printf("%s",line);
found++;
}
}
return found;
}
}
/* getline: 将一行读入s中并返回其长度 */
int getline(char *s, int lim)
{
char *p;
int c;
p = s;
while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
*p++ = c;
if (c == '\n')
*p++ = c;
*p = '\0';
return (int)(p - s);
}
/***********************************************************************
Copyright (c) 2009,liushac
All rights no reserved.
Name: <<C程序设计语言>>例程5-10
ID: hello.c
功能: 允许程序有两个可选变元。其中一个表示“与模式串不匹配的文本行”,
另一个表示“每个被打印文本行前面都有相应的行号”。
-x(x代表except)表示打印所有与模式串不匹配的文本行;
-n(n代表number)表示打印行号。
Version: 0.1
Author: c.
Date: Sep 25, 2009
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 1000
int getline(char *line,int max);
main(int agrc,char *argv[])
{
char *line=(char *)malloc(512);
long lineno=0;
int c,except=0,number=0,found=0;
while(--agrc>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);
agrc=0;
found=-1;
break;
}
if (agrc !=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++;
}
}
free(line);
return found;
}
}
/* getline: 将一行读入s中并返回其长度 */
int getline(char *s, int lim)
{
char *p;
int c;
p = s;
while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
*p++ = c;
if (c == '\n')
*p++ = c;
*p = '\0';
return (int)(p - s);
}
while(--agrc>0 && (* ++argv)[0]=='-')
{ // 删掉这个
while(c=*++argv[0])
......
free(line);
return found;
} // 删掉这个
}
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getline(char line[],int max);
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])
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: hello -x -n pattern\n");
else
while(getline(line,MAXLINE)>0)
{
lineno++;
if ((strstr(line,*argv)!=NULL)!=except)
{
if(number)
printf("%d:",lineno);
printf("%s",line);
found++;
}
}
return found;
}
/* getline:将一行读入s中并返回其长度 */
int getline (char s[],int maxlim)
{
int c;
int i;
i=0;
while (i<(maxlim-1) && (c=getchar())!=EOF && c!='\n'){
s[i]=c;
++i;
}
if (c=='\n'){
s[i]=c;
++i;
}
s[i]='\0';
return i;
}