第四章代碼

練習4-1 (60頁)

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

練習4-2(62頁)

#include <stdio.h>
#include <ctype.h>

double atof(char[]);

int main()
{
    double b = atof("1.2e+2");
    printf("%f\n", b);
    return 0;
}

double atof(char s[])
{
    double val, power;
    int sign, k = 0, j, i= 0;
    sign = (s[i] == '-') ? -1 : 1;
    if (s[i]== '-' || s[i] == '+')
        i++;
    for(val = 0.0; isdigit(s[i]); i++) {
        val = val * 10 + s[i] - '0';
    }
    if (s[i] == '.')
        i++;
    for(power = 1.0; isdigit(s[i]); i++) {
        val = val * 10 + s[i] - '0';
        power *= 10;
    }
    if (s[i] == 'e' || s[i] == 'E') {
        i++;
        if (s[i] == '-') {
            i++;
            for(; isdigit(s[i]); i++) {
                k = k * 10 + s[i] - '0';
            }
            for (j = 0; j < k; j++) {
                power *= 10;
            }
        } else if (s[i] == '+') {
            i++;
            for (; isdigit(s[i]); i++) {
                k = k * 10 + s[i] - '0';
            }
            for (j = 0; j < k; j++){
                power /= 10;
            }
        } else {
            for (; isdigit(s[i]); i++) {
                k = k * 10 + s[i] - '0';
            }
            for (j = 0; j < k; j++){
                power /= 10;
            }
        }
    }
    return sign * val / power;
}

習題4-3、4-4、4-5(還是有一些細節問題要考慮)

代碼155行左右,使用getchar()函數而沒有使用程序本身實現的函數getch()。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAXLEN  100 /* 讀取字符串s的最大長度 */
#define NUMBER '0'  /* 標識找到的是數字      */
#define FUNCTION 'f'/* 標識找到的是函數      */
#define MAXVAL  100 /* 棧的深度              */
#define BUFSIZ 100

int sp = 0;         /* 棧中下一個空閒位置 */
double val[MAXVAL];

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

void clear_stack(void);  /* 清空棧 */
void print_op(void);/* 打印棧頂元素 */
void copy_op(void); /* 複製棧頂元素 */
void swap_op(void); /* 交換棧頂兩個元素 */
void mathfunction(char[]);
void ungetch(int);
int getch(void);

int main()
{
    int tpye;
    char s[MAXLEN];
    double op2;
    while ((tpye = getop(s)) != EOF) {  /* 處理各種類型數據 */
        switch (tpye) {
        case FUNCTION:
            mathfunction(s);
            break;
        case NUMBER:
            push(atof(s));
            break;
        case '+':
            push(pop() + pop());
            break;
        case '-':
            op2 = pop();
            push(pop() - op2);
            break;
        case '*':
            push(pop() * pop());
            break;
        case '/':
            op2 = pop();
            if (op2 == 0.0)
                printf("error: 除數爲0\n");
            else
                push(pop() / op2);
            break;
        case '\n':
            printf("\t%.8g\n", pop());
            break;
        case '%':
            op2 = pop();
            if (op2 != 0.0)
                push(fmod(pop(), op2));
            else
                printf("error: 0不能做除數\n");
        case 'c':       /* 清空棧 */
            clear_stack();
            break;
        case 'p':       /* 複製棧頂元素 */
            copy_op();
            break;
        case '?':       /* 打印棧頂元素 */
            print_op();
            break;
        case ']':       /* 交換棧頂的兩個元素 */
            swap_op();
            break;
        default:
            printf("error: 不能處理\n");
            break;
        }
    }
    return 0;
}

/* 判斷函數類型 */
void mathfunction(char s[])
{
    double op2;
    if (strcmp(s, "sin") == 0)
        push(sin(pop()));
    else if (strcmp(s, "cos") == 0)
        push(cos(pop()));
    else if (strcmp(s, "exp") == 0)
        push(exp(pop()));
    else if (strcmp(s, "pow") == 0) {
        op2 = pop();
        push(pow(pop(), op2));
    } else
        printf("error: function %s is not find\n", s);

}

void push(double f) /* 入棧 */
{
    if (sp < MAXVAL) {
        val[sp++] = f;
    }

    else
        printf("error: stack full, can't push %g\n", f);
}

double pop()        /* 出棧 */
{
    if (sp > 0)
        return val[--sp];
    else {
        printf("error: stack empty, can't pop");
        return 0.0;
    }
}

void print_op()     /* 打印棧頂元素 */
{
    int i = sp - 1;
    printf("\t%.g\n", val[i]);
}

void copy_op()    /* 複製棧頂元素 */
{
    int i = sp - 1;
    push(val[i]);
}

void swap_op()      /* 交換棧頂兩個元素 */
{
    int i = sp -1;
    double f = val[i];
    val[i] = val[i - 1];
    val[i - 1] = f;
}

void clear_stack()  /* 清空棧 */
{
    sp = 0;
}


int getop(char s[])    /* 獲取一個輸入,將數值以字符串形式存到數組s中 */
{
    int c, i= 0;
    while ((c = getchar()) == ' ' || c == '\t')  /* 跳過空格和製表符 */
        ;
    if (islower(c)) {   /* 如果是小寫字母,以函數名存入字符串s中 */
        s[i] = c;
        while (islower(s[++i] = c = getchar()))
            ;
        s[i] = '\0';
        if (c != EOF)
            ungetch(c);
        if (strlen(s) > 1)
            return FUNCTION;
        else
            return c;
    }

    if ( c != '.' && !isdigit(c) && c != '-')
        return c;

    if (c == '-') {     // 處理'-'號
        if (isdigit((c = getch())) || c == '.')
            s[++i] = c;
        else {
            if (c != EOF) {
                ungetch(c);
                return c;
            }
        }
    }


    if (isdigit(c)) {   /* 遇到數字,整數部分 */
        for (; isdigit(c); i++) {
            s[i] = c;
            c = getchar();
        }
    }
    if (c == '.') {     /* 遇到小數點,然後處理小數部分*/
        s[i++] = '.';
        if (isdigit((c = getchar())))
            for (; isdigit(c); i++) {
                s[i] = c;
                c = getchar();
            }
    }
    s[i] = '\0';
    if (c != EOF)
        ungetch(c);   /* 放到輸入緩衝中 */
    return NUMBER;
}

char buf[BUFSIZ];    /* 輸入緩衝區大小 */
int bufp = 0;        /* 輸入緩衝區指針 */
int getch(void)          /* 獲取輸入       */
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c)  /* 將字符放到緩衝 */
{
    if (bufp < BUFSIZ)
        buf[bufp++] = c;
    else
        printf("ungetch: too many characters\n");
}



發佈了37 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章