第四章 函数与程序结构

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
");
        if(strindex(line, pattern) >= 0){        
            printf("%s", line);
            found++;
        printf("run here2
");
        }
    }
    return found;
}

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

int strindex(char s[], char t[])
{
    int i, j, k;
    printf("run here4
");
    for(i = 0; s[i] != ''; i++){
        printf("run here5
");
        for(j=i, k=0; t[k]!='' && s[j]==t[k]; j++, k++)
            printf("s[%d]=%c, t[%d]=%c
", j,s[j],k, t[k]);
        if(k > 0 && t[k] == ''){
            printf("k:%d
",k);
            return i;    
        }
    }
    printf("run here6
");
    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
", i, sign);
        if (s[i] == '+' || s[i] == '-'){
                i++;
                printf("i=%d run here2
", i);
        }
        for (val = 0.0; isdigit(s[i]); i++){
                printf("i=%d
", i);
                val = 10.0 * val + (s[i] - '0');
                printf("val = %lf run here4
", val);
        }
        printf("i=%d
", i);
        if (s[i] == '.'){
                printf("run here5
");
                i++;
        }
        if(isdigit(s[i]))
                printf("Yes
");
        else
                printf("No
");
        for (power = 1.0; isdigit(s[i]); i++) {
                val = 10.0 * val + (s[i] - '0');
                printf("run here6
");
                power *= 10;
        }
        printf("run here7
");
        return sign * val / power;
}
int main(void)
{
        char s[100] = "-1234";
        double res;
        res = _atof(s);
        printf("res value:%lf
", 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 != ' ')
                s[i++] = c;
        if(c == ' ')
                s[i++] = c;
        s[i] = '';
        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 ", 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
");
    while ((type = getop(s)) != EOF) {
        switch (type) {
        case NUMBER:
            printf("run here2
");
            push(atof(s));
            break;
        case '+':
            printf("run here3
");
            push(pop() + pop());
            break;
        case '*':
            printf("run here4
");
            push(pop() * pop());
            break;
        case '-':
            printf("run here5
");
            op2 = pop();
            push(pop() - op2);
            break;
        case '/':
            printf("run here6
");
            op2 = pop();
            if (op2 != 0.0)
                push(pop() / op2);
            else
                printf("error: zero divisor
");
            break;
        case '
':
            printf("run here7
");
            printf("	%.8g
", pop());
            break;
        default:
            printf("run here8
");
            printf("error: unknown command %s
", s);
            break;
        }
    }
    return 0;
}

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

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

debug3
2debug4

debug3
3debug4

原文地址:https://www.cnblogs.com/try-again/p/4998200.html