第五章

練習5-1

如果符號-或+後面跟的不是數字,getint函數將把符號視爲0的有效表達方式。修改該函數,將這個形式的+或者-符號重新寫回到輸入流。

說明:題目理解起來不是特別清晰,不知道是翻譯過來的問題還是我理解的問題,參考了一下答案。

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

int getch(void);
void ungetch(int);
int getint(int);


int getint(int *pn)
{
    int c, i, sign;
    while (isspace(c = getch()))    /* 跳過空白 */
        ;
    if (!isdigit(c) && c != '+' && c != '-' && c != EOF){   /* 不是數字,+,-,結束 */
        ungetch(c);
        return 0;
    }
    sign = (c == '-') ? -1 : 1;
    if (c == '+' || c == '-') {
        i = c;
        c = getch();
        if (!isdigit(c)) {  /* 符號後面的輸入不是數字 */
            if (c != EOF)
                ungetch(c);
            ungetch(i);
            return c;
        }
    }
    for (*pn = 0; isdigit(c); c = getch()) /* 字符串轉換成整數 */
        *pn = *pn * 10 + c - '0';
    *pn *= sign;
    if (c != EOF)
        ungetch(c);
    return c;
}


練習5-2

模仿函數getint的實現,編寫一個讀取浮點數的getfloat函數。getfloat函數的返回值應該是什麼類型?

實現:

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

int getch(void);
void ungetch(int);
int getint(float *);


int getint(double *pn)
{
    int c, i, sign;
    float power;
    while (isspace(c = getch()))    /* 跳過空白 */
        ;
    if (!isdigit(c) && c != '+' && c != '-' && c != EOF){   /* 不是數字,+,-,結束 */
        ungetch(c);
        return 0;
    }
    sign = (c == '-') ? -1 : 1;
    if (c == '+' || c == '-') {
        i = c;
        c = getch();
        if (!isdigit(c)) {  /* 符號後面的輸入不是數字 */
            if (c != EOF)
                ungetch(c);
            ungetch(i);
            return c;
        }
    }
    for (*pn = 0; isdigit(c); c = getch()) /* 字符串轉換成整數 */
        *pn = *pn * 10 + c - '0';
    if (c == '.') {
        power = 1.0;
        c = getch();
        for (; isdigit(c); c = getch()) {
            *pn = *pn * 10 + c - '0';
            power *= 10;
        }
        *pn /= power;
    }
    *pn *= sign;
    if (c != EOF)
        ungetch(c);
    return c;
}

練習5-3

用指針方式實現第二章中的函數strcat。函數strcat(s,t)將t指向的字符串複製到s指向的字符串的尾部。

代碼:

void strcat(char *s, char *t)
{
    while (*s++)
        ;
    while(*s++ = *t++)
        ;
}

練習5-4

編寫函數strend(s,t)。如果字符串t出現在字符串s的尾部,該函數返回1,否則返回0。

代碼:

int strend(char *s, char *t)
{
    int i = strlen(s) - strlen(t);
    s += i;
    for (; *s == *t; s++, t++)
        ;
    if (*s == '\0')
        return 1;
    else
        return 0;
}

練習5-5

實現庫函數strncpy、strncat和strncmp它們最多對參數字符串中的前n個字符經行操作。

void strncpy(char *s, char *t, int n)
{
    int i;
    if (strlen(t) >= n)
        for (i = 0; i < n; i++)
            *s++ = *t++;
    else
        while (*s++ = *t++)
        ;
    *s = '\0';
}

void strncat(char *s, char *t, int n)
{
    int i;
    while (*s++)
        ;
    if (strlen(t) >= n) {
        for (i = 0; i < n; i++)
        *s++ = *t++;
    } else
        for (i = 0; *t != '\0'; i++)
            *s++ = *t++;
    *s = '\0';
}

int strncmp(char *s, char *t, int n)
{
    for (; *s == *t; s++, t++)
        if (*s == '\0' || --n <= 0)
            return 0;
    return *s - *t;
}


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