《程序设计》函数题|041-048

习题11-1 输出月份英文名 – 浙大版《C语言程序设计(第4版)》题目集

// 月份->英文名
const char *months[] = {
        "wrong input!",    // ERROR
        "January",   // 1月
        "February",  // 2月
        "March",     // 3月
        "April",     // 4月
        "May",       // 5月
        "June",      // 6月
        "July",      // 7月
        "August",    // 8月
        "September", // 9月
        "October",   // 10月
        "November",  // 11月
        "December"   // 12月
};

// 返回:月份英文名
char *getmonth(int n){
    char *result;

    if(n>=1 && n<=12){
        result=months[n];
    }else{
        result=months[0];
    }
    
    return result;
}

习题11-2 查找星期 – 浙大版《C语言程序设计(第4版)》题目集

// 返回:查找字符串对应的星期数
int getindex(char *s){
    char str[7][10]=
    {"Sunday\0","Monday\0"
     ,"Tuesday\0","Wednesday\0"
     ,"Thursday\0","Friday\0"
     ,"Saturday\0"};
    for(int i=0;i<7;++i){
        if(strcmp(str[i],s)==0){
            return i;
        }
    }
    return -1;
}

习题11-3 计算最长的字符串长度 – 浙大版《C语言程序设计(第4版)》题目集

#include <limits.h>

// 返回:最长的字符串长度
int max_len(char *s[],int n){
    int result=0;

    for(int i=0;i<n;++i){
        if(strlen(s[i])>result){
            result=strlen(s[i]);
        }
    }
    
    return result;
}

习题11-4 字符串的连接 – 浙大版《C语言程序设计(第4版)》题目集

// 处理:将字符串t复制到字符串s的末端
// 返回:字符串s的首地址
char *str_cat(char *s,char *t){
    int lenS=strlen(s);
    int lenT=strlen(t);
    for(int i=0;i<=lenT;++i){
        s[lenS+i]=t[i];
    }
    return s;
}

习题11-5 指定位置输出字符串 – 浙大版《C语言程序设计(第4版)》题目集

// 输出:s中从ch1到ch2之间的所有字符
// 返回:ch1的地址
char *match(char *s, char ch1, char ch2) {
    int flag = 0, d = -1, count = 0;
    for (int i = 0; i < strlen(s); i++) {
        if (s[i] == ch1 && count == 0) {
            flag = 1;
            d = i;
            count++;
        }
        if (flag == 1)
            printf("%c", s[i]);
        if (s[i] == ch2 && count == 1) {
            flag = 0;
            count++;
        }
    }
    printf("\n");
    if (d != -1)
        return &s[d];
    else
        return s + strlen(s);
}

习题11-6 查找子串 – 浙大版《C语言程序设计(第4版)》题目集

// 返回:子串t在s中的首地址
char *search(char *s, char *t){
    int i, j, k;
    
    // 如果t是空字符串,返回s的开始位置
    if (t[0] == '\0') {
        return s;
    }
    
    // 遍历字符串s
    for (i = 0; s[i] != '\0'; i++) {
        // 检查从s[i]开始是否匹配t
        j = 0;
        k = i;
        while (t[j] != '\0' && s[k] != '\0' && s[k] == t[j]) {
            j++;
            k++;
        }
        
        // 如果t的所有字符都匹配了,返回起始位置
        if (t[j] == '\0') {
            return &s[i];
        }
    }
    
    // 没有找到匹配的子串
    return NULL;
}

习题11-7 奇数值结点链表 – 浙大版《C语言程序设计(第4版)》题目集

// 处理:建立链表
// 返回:头节点指针
struct ListNode *readlist(){
    struct ListNode *head = NULL, *tail = NULL;
    int num;
    
    while (scanf("%d", &num) == 1 && num != -1) {
        struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
        newNode->data = num;
        newNode->next = NULL;
        
        if (head == NULL) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
    }
    
    return head;
}

// 处理:将单链表L中奇数值的结点分离出来,重新组成一个新的链表
// 返回:头节点指针
struct ListNode *getodd( struct ListNode **L ){
    struct ListNode *oddHead = NULL, *oddTail = NULL;
    struct ListNode *evenHead = NULL, *evenTail = NULL;
    struct ListNode *curr = *L;
    
    while (curr != NULL) {
        if (curr->data % 2 == 1) {
            // 奇数节点,加入奇数链表
            if (oddHead == NULL) {
                oddHead = curr;
                oddTail = curr;
            } else {
                oddTail->next = curr;
                oddTail = curr;
            }
        } else {
            // 偶数节点,加入偶数链表
            if (evenHead == NULL) {
                evenHead = curr;
                evenTail = curr;
            } else {
                evenTail->next = curr;
                evenTail = curr;
            }
        }
        curr = curr->next;
    }
    
    // 断开两个链表的末尾
    if (oddTail != NULL) {
        oddTail->next = NULL;
    }
    if (evenTail != NULL) {
        evenTail->next = NULL;
    }
    
    // 更新原链表为偶数链表
    *L = evenHead;
    
    return oddHead;
}

习题11-8 单链表结点删除 – 浙大版《C语言程序设计(第4版)》题目集

// 处理:将读入的数据存储为单链表
// 返回:头节点指针
struct ListNode *readlist(){
    struct ListNode *head = NULL, *tail = NULL;
    int num;
    
    while (scanf("%d", &num) == 1 && num != -1) {
        struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
        newNode->data = num;
        newNode->next = NULL;
        
        if (head == NULL) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
    }
    
    return head;
}

// 处理:删除链表中所有存储了m的节点
// 返回:头节点指针
struct ListNode *deletem( struct ListNode *L, int m ){
    struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
    dummy->next = L;
    struct ListNode *prev = dummy, *curr = L;
    
    while (curr != NULL) {
        if (curr->data == m) {
            prev->next = curr->next;
            free(curr);
            curr = prev->next;
        } else {
            prev = curr;
            curr = curr->next;
        }
    }
    
    struct ListNode *newHead = dummy->next;
    free(dummy);
    return newHead;
}

《《程序设计》函数题|041-048》有1条评论

发表评论