K&R的名著:小程序总结5

1readlines函数:读取输入行

int readlines(char *lineptr[], int maxlines)

{

      int len, nlines;

      char *p, line[MAXLEN];

      nlines = 0;

      while ((len = getline(line, MAXLINE)) > 0)

         if (nlines >= maxlines || (p = alloc(len)) == NULL)

                return -1;

         else  {

              line[len-1] = '\0';

             strcpy(p, line);

             lineptr[nlines++] = p;

 }

  return nlines;

           

}

2writelines函数:写输出行

void writelines(char *lineptr[], int nlines)

{

    int i;

    for (i = 0; i < nlines; i++)

       printf("%s\n", lineptr[i]);

}

 

3、二维数组的运用

aday_of_year函数:将某月某日的日期表示形式转换为某一年中的第几天

int day_of_year(int year, int month, int day)

{

        int i, leap;

        //int *ptr;

        leap = year%4 ==0 && year%100 !=0 || year % 400 ==0;

        for (i = 1; i < month; i++)

               day +=daytab[leap][i];

        return day;

}

bmonth_day函数:与上述功能相反

void month_day(int year, int yearday, int *pmonth, int *pday)

{

     int i, leap;

     leap = year%4 ==0 && year%100 !=0 || year % 400 ==0;

     for (i = 1;yearday > daytab[leap][i]; i++)

     yearday -= daytab[leap][i];

     *pmonth = i;

     *pday = yearday;

}

 
#include<stdio.h>
static char daytab[2][13]={
	{0,31,28,31,30,31,30,31,31,30,31,30,31},
	{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
void month_day(int year, int yearday, int *pmonth, int *pday)
{
	int i, leap;
	leap = year%4 ==0 && year%100 !=0 || year % 400 ==0;
	 
	for (i = 1;yearday > daytab[leap][i]; i++)
     yearday -= daytab[leap][i];
	*pmonth = i;
	*pday = yearday;
}

void main(void)
{
	int mon,day;
         int year,yearday;
	
	printf("please input year:");
	scanf("%d",&year);
	printf("please input yearday:");
	scanf("%d",&yearday);
	month_day(year,yearday,&mon,&day);


	printf("it is %d month %d day\n",mon,day);
}


 

c、指针数组的初始化

char *month_name(int n)

{

   static char *name[]={

    "Illegal month",

    "Jan", "Feb", "Mar",

    "Apr", "May","June",

    "July", "Aug", "Sep",

    "Nov", "Dem"

};

return (n < 1 || n > 12)?name[0] : name[n];

}

#include "stdio.h"

char *month_name(int n){
	static char *name[]={
	"Illegal month",
	"Jan", "Feb", "Mar", 
	"Apr", "May","June",
	"July", "Aug", "Sep",
	"Nov", "Dem"};
	return (n<1||n>12)?name[0]:name[n];
}

void main(void){
	int n;
	printf("Please the month:");
	scanf("%d",&n);
    printf("the month is %s\n",month_name(n));
}


 

 

4、模式查找程序,类似Linux平台的grep命令作用

//p100.c

#include<stdio.h>

#include<string.h>

 

#define MAXLINE 1000

 

int getline1(char *line, int max);//因getline函数在linux平台已有定义且与此函数的参数不同,所以改名为getline1()

 

int main(int argc, char *argv[])

{

        char line[MAXLINE];

        int found = 0;

 

        if (argc != 2)

               printf("Usage: find pattern\n");

        else

               while (getline1(line, MAXLINE) > 0)

                       if (strstr(line ,argv[1]) != NULL) {

                       printf("%s",line);

                       found++;

                       }

        return found;

 

}

 

int getline1(char *s, int lim)

{

   int c;

   char *t = s;

 

   while(--lim > 0 && (c =getchar()) !=EOF && c != '\n')

           *s++ = c;

   if (c == '\n')

           *s++ = c;

   *s ='\0';

   return s-t;

 

}



 

 

你可能感兴趣的:(c,linux,null,语言,平台)