第四章 函数与程序结构

4.1  函数的基本知识

#include <stdio.h>
#define MAXLINE 1000
int add_getline(char line[], int max);
int strindex(char source[], char searchfor[]);

char pattern[] = "ray";

int main(void)
{
    char line[MAXLINE];
    int found = 0;

    while(add_getline(line, MAXLINE) > 0){
        printf("run here1\n");
        if(strindex(line, pattern) >= 0){        
            printf("%s", line);
            found++;
        printf("run here2\n");
        }
    }
    return found;
}

int add_getline(char s[], int lim)
{
    int c, i;
    i = 0;
    while(--lim > 0 && (c=getchar()) != EOF && c != '\n') //EOF:ctrl+d,  '\n':enter
        s[i++] = c;
    if(c == '\n')
        s[i++] = c;
    s[i] = '\0';
    printf("run here3 i:%d\n", i);
    return i;
}

int strindex(char s[], char t[])
{
    int i, j, k;
    printf("run here4\n");
    for(i = 0; s[i] != '\0'; i++){
        printf("run here5\n");
        for(j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
            printf("s[%d]=%c, t[%d]=%c\n", j,s[j],k, t[k]);
        if(k > 0 && t[k] == '\0'){
            printf("k:%d\n",k);
            return i;    
        }
    }
    printf("run here6\n");
    return -1;
}
编译后执行,输入:hello world,显示:
hello world
run here3 i:12
run here1
run here4
run here5
run here5
run here5
run here5
run here5
run here5
run here5
run here5
run here5
s[8]=r, t[0]=r
run here5
run here5
run here5
run here6

输入:hello wrayorld,显示:
hello wrayorld
run here3 i:15
run here1
run here4
run here5
run here5
run here5
run here5
run here5
run here5
run here5
run here5
s[7]=r, t[0]=r
s[8]=a, t[1]=a
s[9]=y, t[2]=y
k:3
hello wrayorld
run here2


4.2 返回非整型值的函数

例a.

#include <ctype.h>
#include <stdio.h>
/* atof: convert string s to double */
double _atof(char s[])
{
        double val, power;
        int i, sign;
        for (i = 0; isspace(s[i]); i++) /* skip white space */
                ;
        sign = (s[i] == '-') ? -1 : 1;
        printf("s[%d] = %d run here1\n", i, sign);
        if (s[i] == '+' || s[i] == '-'){
                i++;
                printf("i=%d run here2\n", i);
        }
        for (val = 0.0; isdigit(s[i]); i++){
                printf("i=%d\n", i);
                val = 10.0 * val + (s[i] - '0');
                printf("val = %lf run here4\n", val);
        }
        printf("i=%d\n", i);
        if (s[i] == '.'){
                printf("run here5\n");
                i++;
        }
        if(isdigit(s[i]))
                printf("Yes\n");
        else
                printf("No\n");
        for (power = 1.0; isdigit(s[i]); i++) {
                val = 10.0 * val + (s[i] - '0');
                printf("run here6\n");
                power *= 10;
        }
        printf("run here7\n");
        return sign * val / power;
}
int main(void)
{
        char s[100] = "-1234";
        double res;
        res = _atof(s);
        printf("res value:%lf\n", res);
        return 0;
}
编译后执行,显示:
s[0] = -1 run here1
i=1 run here2
i=1
val = 1.000000 run here4
i=2
val = 12.000000 run here4
i=3
val = 123.000000 run here4
i=4
val = 1234.000000 run here4
i=5
No
run here7
res value:-1234.000000

例子b.

#include <stdio.h>
#define MAXLINE 100
/* rudimentary calculator */
int _getline(char s[], int lim)
{
        int c, i;
        i = 0;
        printf("请输入数字:");
        while(--lim > 0 && (c=getchar()) != EOF && c != '\n')
                s[i++] = c;
        if(c == '\n')
                s[i++] = c;
        s[i] = '\0';
        return i;
}

main()
{
        double sum, atof(char []);
        char line[MAXLINE];
        int _getline(char line[], int max);
        sum = 0;
        while (_getline(line, MAXLINE) > 0)
                printf("相加后输出:%g\n", sum += atof(line));
        return 0;
}
编译执行,显示:
请输入数字:1
相加后输出:1
请输入数字:2
相加后输出:3
请输入数字:10
相加后输出:13
请输入数字:a
相加后输出:13
请输入数字:=
相加后输出:13
请输入数字:7
相加后输出:20
请输入数字:

 4.3 外部变量

#include <stdio.h>
#include <stdlib.h> /* for atof() */
#include <ctype.h>
#include <ctype.h>
#define MAXVAL 100 /* maximum depth of val stack */
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
#define BUFSIZE 100

int getop(char []);
void push(double);
double pop(void);
int getch(void);
void ungetch(int);

int sp = 0;
/* next free stack position */
double val[MAXVAL]; /* value stack */
char buf[BUFSIZE];
int bufp = 0;

/* reverse Polish calculator */
int main(void)
{
    int type;
    double op2;
    char s[MAXOP];
    printf("run here1\n");
    while ((type = getop(s)) != EOF) {
        switch (type) {
        case NUMBER:
            printf("run here2\n");
            push(atof(s));
            break;
        case '+':
            printf("run here3\n");
            push(pop() + pop());
            break;
        case '*':
            printf("run here4\n");
            push(pop() * pop());
            break;
        case '-':
            printf("run here5\n");
            op2 = pop();
            push(pop() - op2);
            break;
        case '/':
            printf("run here6\n");
            op2 = pop();
            if (op2 != 0.0)
                push(pop() / op2);
            else
                printf("error: zero divisor\n");
            break;
        case '\n':
            printf("run here7\n");
            printf("\t%.8g\n", pop());
            break;
        default:
            printf("run here8\n");
            printf("error: unknown command %s\n", s);
            break;
        }
    }
    return 0;
}

/* push: push f onto value stack */
void push(double f)
{
    printf("run here9\n");
    if (sp < MAXVAL)
        val[sp++] = f;
    else
        printf("error: stack full, can't push %g\n", f);
}
/* pop: pop and return top value from stack */
double pop(void)
{
    printf("run here10\n");
    if (sp > 0)
        return val[--sp];
    else {
        printf("error: stack empty\n");
        return 0.0;
    }
}
/* getop: get next character or numeric operand */ int getop(char s[]) { printf("run here11\n"); int i, c; while ((s[0] = c = getch()) == ' ' || c == '\t') printf("run here11-1\n"); s[1] = '\0'; printf("run here11-2\n"); if (!isdigit(c) && c != '.'){ printf("run here11-3\n"); return c; /* not a number */ } i = 0; if (isdigit(c)){ printf("run here11-4\n"); while (isdigit(s[++i] = c = getch())) ; } if(c == '.'){ printf("run here11-5\n"); while(isdigit(s[++i] = c = getch())) ; } s[i] = '\0'; if (c != EOF) ungetch(c); printf("run here11-6\n"); return NUMBER; } int getch(void) { printf("run here12\n"); return (bufp > 0) ? buf[--bufp] : getchar(); } void ungetch(int c) { printf("run here13\n"); if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++] = c; }
编译执行,显示:
run here1
run here11
run here12
3 7 +
run here11-2
run here11-4
run here12
run here13
run here11-6
run here2
run here9
run here11
run here12
run here11-1
run here12
run here11-2
run here11-4
run here12
run here13
run here11-6
run here2
run here9
run here11
run here12
run here11-1
run here12
run here11-2
run here11-3
run here3
run here10
run here10
run here9
run here11
run here12
run here11-2
run here11-3
run here7
run here10
    10
run here11
run here12

 4.10 递归

#include <stdio.h>
/* printd: print n in decimal */
void printd(int n)
{
    if (n < 0) {
        putchar('-');
        n = -n;
    }
    printf("debug1\n");
    if (n / 10){
        printf("debug2\n");
        printd(n / 10);
    }
    printf("debug3\n");
    putchar(n % 10 + '0');
    printf("debug4\n");
    printf("\n");
}

int main()
{
    printd(123);
}
显示:
debug1
debug2
debug1
debug2
debug1
debug3
1debug4

debug3
2debug4

debug3
3debug4

 

posted @ 2015-11-26 16:42  天王星B  Views(193)  Comments(0)    收藏  举报