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里面直接编译运行。
***/