c语言(2)(函数)

c语言(2)(函数)

1.一个文件中包含了三个函数,其中main函数调用另外的两个函数。
/*
 * hanshu.c
 *
 *  Created on: 2014年11月6日
 *      Author: cdc
  在控制台输入字符,如果字符中含有'ould',这将输入的字符打印出来,如果输入的字符中不含有'ould',
 *      则对输入的字符不做处理。
 */

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
/***声明要调用的函数**/
int getline(char line[], int max);
/***声明要调用的函数**/
int strindex(char source[], char searchfor[]);

char pattern[] = "ould"; /* pattern to search for */

/* find all lines matching pattern */
main() {
	char line[MAXLINE];
	int found = 0;

	while (getline(line, MAXLINE) > 0) {
		if (strindex(line, pattern) >= 0) {
			printf("%s", line);
			found++;
			printf("ould number is==%d\n", found);

		} else {
			printf("%s\n", "the line hao no 'ould'!");
			printf("ould number is==%d\n", found);
		}
	}
	
	return found;
}

/* getline:  get line into s, return length */
int getline(char s[], int lim) {
	int c, i;

	i = 0;
	/**
	 * 回车(\n)=10(ANSI码);
	 * 0=48;
	 *
	 * **/
	while (--lim > 0 && (c = getchar()) != EOF && c != '\n') {

		s[i++] = c;
	}
	if (c == '\n') {
		s[i++] = c;
	}
	/**
	 * '\0' 是字符串的结束符,任何字符串之后都会自动加上'\0'。
	 *
	 * */
	s[i] = '\0';
	return i;
}

/* strindex:  return index of t in s, -1 if none */
int strindex(char s[], char t[]) {
	int i, j, k;

	for (i = 0; s[i] != '\0'; i++) {

		for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++) {
			;
		}
		if (k > 0 && t[k] == '\0') {
			return i;
		}
	}
	return -1;
}

/****
gcc -o hanshu hanshu.c生成hanshu.exe

**/

2.还是三个函数,main函数调用的两个函数是在单独的c文件中的.
/*
 * hanshu.c
 *
 *  Created on: 2014年11月6日
 *      Author: cdc
 *
 *      在控制台输入字符,如果字符中含有'ould',这将输入的字符打印出来,如果输入的字符中不含有'ould',
 *      则对输入的字符不做处理。
 */

#include <stdio.h>

#define MAXLINE 1000 /* maximum input line length */
/***声明要调用的函数**/
int getline(char line[], int max);
/***声明要调用的函数**/
int strindex(char source[], char searchfor[]);

char pattern[] = "ould"; /* pattern to search for */

/* find all lines matching pattern */
main() {
	char line[MAXLINE];
	int found = 0;
	while (getline(line, MAXLINE)> 0) {
		if ( strindex(line, pattern)>= 0) {
			printf("%s", line);
			found++;
			printf("ould number is==%d\n", found);
		} else {
			printf("%s\n", "the line hao no 'ould'!");
			printf("ould number is==%d\n", found);
		}
	}

	return found;
}


/***
文件hanshu.c,strindex.c和文件getline.c在同一个目录下,
可以使用命令gcc hanshu.c strindex.c getline.c,可以编译出来一个a.exe文件;
也可以使用命令gcc -o hanshu hanshu.c strindex.c getline.c,编译出一个文件名为hanshu.exe的文件。
也可以在Eclipse里面直接编译运行。
***/

/*
 * strindex.c

 *
 *  Created on: 2014年11月6日
 *      Author: cdc
 */

int strindex(char s[], char t[]) {
	int i, j, k;

	for (i = 0; s[i] != '\0'; i++) {

		for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++) {
			;
		}
		if (k > 0 && t[k] == '\0') {
			return i;
		}
	}
	return -1;
}




/*

 * getline.c
 *
 *  Created on: 2014年11月6日
 *      Author: cdc
 */


#include <stdio.h>
/* getline:  get line into s, return length */
int getline(char s[], int lim) {
	int c, i;

	i = 0;
	/**
	 * 回车(\n)=10(ANSI码);
	 * 0=48;
	 *
	 * **/
	while (--lim > 0 && (c = getchar()) != EOF && c != '\n') {

		s[i++] = c;
	}
	if (c == '\n') {
		s[i++] = c;
	}
	/**
	 * '\0' 是字符串的结束符,任何字符串之后都会自动加上'\0'。
	 *
	 * */
	s[i] = '\0';
	return i;
}


3.将2中的strindex.c和getline.c文件改成strinfex.h和getline.h文件,把文件名字改掉就可以了。相应的hanshu.c文件改为如下的样式:
/*
 * hanshu.c
 *
 *  Created on: 2014年11月6日
 *      Author: cdc
 *
 *      在控制台输入字符,如果字符中含有'ould',这将输入的字符打印出来,如果输入的字符中不含有'ould',
 *      则对输入的字符不做处理。
 */

#include <stdio.h>
#include "getline.h"
#include "strindex.h"


#define MAXLINE 1000 /* maximum input line length */
/***声明要调用的函数**/
int getline(char line[], int max);
/***声明要调用的函数**/
int strindex(char source[], char searchfor[]);

char pattern[] = "ould"; /* pattern to search for */

/* find all lines matching pattern */
main() {
	char line[MAXLINE];
	int found = 0;
	while (getline(line, MAXLINE)> 0) {
		if ( strindex(line, pattern)>= 0) {
			printf("%s", line);
			found++;
			printf("ould number is==%d\n", found);
		} else {
			printf("%s\n", "the line hao no 'ould'!");
			printf("ould number is==%d\n", found);
		}
	}

	return found;
}


/***
文件hanshu.c,strindex.h和文件getline.h在同一个目录下,
可以使用命令gcc hanshu.c,可以编译出来一个a.exe文件;
也可以使用命令gcc -o hanshu hanshu.c ,编译出一个文件名为hanshu.exe的文件。
也可以在Eclipse里面直接编译运行。
***/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值