常 用 算 法
1.例:文件复制
#include stdio.h
void filecopy(FILE ifp, FILE ofp);
int main(int argc, char argv[]) {
FILE ifp;
char name = argv[0];
if (argc == 1) { 没有参数,从标准输入复制到标准输出
filecopy(stdin, stdout);
return 0;
}
while (++argv != NULL)
if ((ifp = fopen(argv, r)) == NULL)
printf(%s, can't open input file %sn, name, argv);
else {
filecopy(ifp, stdout);
fclose(ifp);
}
return 0;
}
void filecopy(FILE ifp, FILE ofp) {
int c;
while ((c = getc(ifp)) != EOF)
putc(c, ofp);
}
2.例:学生信息的输入的2个程序段
程序1
while (scanf(%ld %s %d %d %d, &number, name, &year, &month, &day) != 5) {
printf(Input error.n);
printf(Format number name year month dayn);
while (gerchar() != 'n')
; 丢掉本输入行剩下的无用字符
}
程序2:
char line[256];
while (1) {
printf();
gets(line);
if (sscanf(line, %ld %s %d %d %d,
&number, name, &year, &month, &day) == 5)
break;
if (sscanf(line, %ld %s %d,%d,%d,
&number, name, &year, &month, &day) == 5)
break;
if (sscanf(line, %ld %s %d%d%d,
&number, name, &day, &month, &year) == 5)
break;
printf(Invalid form %s, line);
}
3.例:读取每个文件里的数值求出平均值并输出
void pAverage(FILE fp, char fname) {
double x, sum = 0.0;
int n = 0, m, c;
printf(nFile %sn, fname);
whlie ((m = fscanf(fp, %lf, &x)) != EOF) {
if (m == 1) {
sum += x;
n++;
continue; 继续循环读入
}
printf(Date error. Discard );
while (!isspace(c = getc(fp)))
putchar(c);
putchar('n');
}
printf(Average %16.8fn, sum n);
}
int main(void) {
char name[256];
FILE fp;
while (1) {
printf(File name (Ctrl-Z for the end) );
if (gets(name) == NULL) break;
if ((fp = fopen(name, r)) == NULL)
printf(Can't open file %sn, name);
else {
pAvarage(fp, name);
fclose(fp);
}
}
printf(Bye!n);
return 0;
}
4.例:函数Sum的完整定义(变动参数的函数)
int sum(int n, ...) {
va_list vap;
int i, s = 0;
va_start(vap, n);
for (i = 1; i = n; i++)
s += va_arg(vap, int);
va_end(vap);
return s;
}
5.例:打印输出身份证结构中身份证号码和姓名
void prtIDCard0(IDCARD ic) {
printf(%sn, ic.id_number);
printf(%snn, ic.name);
}
或者定义下面的函数,调用时应该传身份证记录的地址:
void prtIDCard(IDCARD icp) {
printf(%sn, icp-id_number);
printf(%snn, icp-name);
}
6.例:建立一个动态分配的 POINT 结构
POINT mkpoint2(double x, double y) {
POINT p;
p = (POINT )malloc(sizeof(POINT));
p-x = x;
p-y = y;
return p;
}
7.例:词频统计
#include string.h 程序中需要做字符串复制和比较
#include stdlib.h 程序中要做动态存储分配
#define MAXLEN 20
typedef struct node NODE, LINK; 类型定义
struct node {
char word[MAXLEN];
int count;
LINK next;
};
int getword(char w[], int limit); 有关函数的原型说明
LINK addword(LINK l, char w[]);
void printwords(LINK l);
LINK list = NULL; 全局变量,作为表的头指针
char word[MAXLEN]; 读入用的临时字符数组
int main (void) {
while (getword(word, MAXLEN) != 0)
if (isalpha(word[0]))
list = addword(list, word);
printwords(list);
return 0;
}
void printwords(LINK p) {
for ( ; p != NULL; p = p-next)
printf(%d %sn, p-count, p-word);
}
LINK mknode(char w[]) {
LINK p = (LINK)malloc(sizeof(NODE));
if (p != NULL) {
strncpy(p-word, w, MAXLEN);
p-count = 1;
p-next = NULL;
}
return p;
}
LINK addword(LINK p, char w[]) {
if (p != NULL) {
if(strcmp(p-word, w) == 0)
p-count++;
else
p-next = addword(p-next, w);
return p;
}
else
return mknode(w);
}
8.例:交换两个整形变量的值
void swap(int p, int q) {
int t = p;
p = q;
q = t;
}
9.例:求双精度数组的元素之和
double sum(double a[], int n) {
int i;
double s = 0.0;
for (i = 0; i n; i++)
s += a[i];
return s;
}