zoukankan      html  css  js  c++  java
  • 第十三周知识总结

    1.括号匹配问题

    部分代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "stack_list_parenthesis.h"

    // 检查字符串 str 是否全部是全括号 '(', ')', '[', ']', '{', 或 '}'。
    int allParenthesis(char *str) {
    int i; // 循环变量。

    for (i=0; i<strlen(str); i++)
    if (!(str[i]=='(' || str[i]==')' || // 找到一个非括号字符,返回 0。
    str[i]=='[' || str[i]==']' ||
    str[i]=='{' || str[i]=='}')) return 0;

    return 1; // 所有字符都是括号,返回 1。
    }

    // 检查两个字符是否匹配。
    int isMatching(char e, char c) {
    // 返回匹配的圆括号、方括号、或花括号。
    return (e=='(' && c==')') || (e=='[' && c==']') || (e=='{' && c=='}');
    }

    // 检查字符串 str 是否括号匹配。
    int parenthesisMatching(char *str) {
    Stack S; // 检查括号匹配的栈。
    char e; // 存放出栈的字符。
    int matching = 1; // 括号匹配旗标。
    int i; // 循环变量。

    initial(&S);

    for (i=0; i<strlen(str) && matching; i++) { // 扫描字符串 str。
    if (str[i]=='(' || str[i]=='[' || str[i]=='{') // 若是左括号,
    push(&S, str[i]); // 将字符入栈。
    else {
    e = pop(&S); // e 是从 S 出栈的字符。
    // 若出栈的左括号和目前的右括号不匹配,旗标设为假值。
    if (!isMatching(e, str[i])) matching = 0;
    }
    }
    if (matching==1 && is_empty(S)) matching = 1; // 若栈已空,旗标设为真值。
    else matching = 0; // 否则,旗标设为假值。

    clear(&S); // 清空栈。
    return matching; // 返回旗标。
    }

    int main(void) {
    char str[100]; // 括号字符串指针。

    do { // 重复括号匹配检查,直至 str 为空字符串。
    do { // 读入一个全括号的字符串。
    printf("读入一个全括号的字符串 ('(', ')', '[', ']', '{', '}'):");
    scanf("%s", &str);
    if (!strcmp(str, "stop")) return 0; // 若字符串是 "stop",则终止程序。
    } while (!allParenthesis(str));

    if (parenthesisMatching(str)) printf("**** 字符串 %s 是全括号匹配。 ", str);
    else printf("**** 字符串 %s 不是全括号匹配。 ", str);

    printf("---------------------------------------- "); // 打印分隔线。
    } while (strlen(str)!=0);
    }

    2.单链表

    部分代码:

    // 将元素 e 插入到 L 适当的位置。插入完成时,返回 e 的位置。
    int insert(List *L, ElemType e) {
    Link current = *L; // 指向目前的节点。
    Link previous = NULL; // 指向前一个节点,开始时为空。
    Link newNode; // 新节点的指针。
    int position = 0; // 目前节点位置。

    if (*L==NULL) { // Case 1:线性表是空的。
    newNode = (Link) malloc(sizeof(Node)); // 要求一个新节点的内存。
    newNode->elem = e; // 设定头节点的数据。
    newNode->next = NULL; // 设定头节点的链。
    *L = newNode; // 线性表指向第一个节点。
    return 0; // 返回头节点位置。
    }

    do { // 检查目前的节点。
    if (current->elem>=e) { // Cases 2 & 3: 找到插入位置,插在这个节点之前。
    newNode = (Link) malloc(sizeof(Node)); // 要求一个新节点的内存。
    newNode->elem = e; // 设定新节点的数据。
    newNode->next = current; // 设定新节点的链。
    if (previous==NULL) *L = newNode; // Case 2: 插入线性表的头节点。
    else previous->next = newNode; // Case3:修改前一节点的链。
    return position; // 返回位置。
    }
    else {
    previous = current; // 将目前的节点设为前一个节点。
    current = current->next; // 将下一个节点设为下一步骤的目前节点。
    position++; // 位置加 1。
    }
    } while (current!=NULL); // 若线性表还有节点,继续。

    // Case 4:while 循环结束,没有执行返回,移到链结表的最后节点。
    newNode = (Link) malloc(sizeof(Node)); // 要求一个新节点的内存。
    newNode->elem = e; // 设定新节点的数据。
    newNode->next = NULL; // 最后的节点,将其下一个节点设为空值。
    previous->next = newNode; // 将新增节点放在线性表的最后。
    return position; // 返回位置。
    }

    3.一元多项式

    部分代码:

    // 将一个项加到多项式线性表 ,依照项次递减顺序。 若项次不存在,
    // 插入该项次;若项次已存在,判断最后的参数 flag 旗标,flag==1 时,
    // 将系数的值相加;为 0 时,取代原系数。最后,返回系数的值。
    Coefficient addTerm(Poly *P, ElemType e, int flag) {
    Link current = *P; // 指向目前的节点。
    Link previous = NULL; // 指向前一个节点,开始时为空。
    Link newNode; // 新节点的指针。

    if (*P==NULL) { // 线性表是空的。
    newNode = (Link) malloc(sizeof(Term)); // 要求一个新节点的内存。
    newNode->elem.degr = e.degr; // 设定头节点的项次。
    newNode->elem.coef = e.coef; // 设定头节点的系数。
    newNode->next = NULL; // 设定头节点的链。
    *P = newNode; // 线性表指向第一个节点。
    return 0; // 返回头节点位置。
    }

    do { // 检查目前的节点。
    if (current->elem.degr==e.degr) { // 找到同次项,修改这个项的系数。
    if (flag==1) // 旗标是累加系数
    current->elem.coef += e.coef; // 累加系数。
    else current->elem.coef = e.coef; // 替换系数。
    return current->elem.coef; //返回系数。
    }
    if (current->elem.degr<e.degr) { // 找到插入新项的位置,插在这个项之前。
    newNode = (Link) malloc(sizeof(Term)); // 要求一个新节点的内存。
    newNode->elem.degr = e.degr; // 设定新项的项次。
    newNode->elem.coef = e.coef; // 设定新项的系数。
    newNode->next = current; // 设定新项的链。
    if (previous==NULL) *P = newNode; // Case 2: 插入多项式的头节点。
    else previous->next = newNode; // Case3:修改前项的链。
    return newNode->elem.coef; // 返回系数。
    }
    else {
    previous = current; // 将目前的节点设为前一个项。
    current = current->next; // 将下一个节点设为下一步骤的目前的项。
    }
    } while (current!=NULL); // 若线性表还有节点,继续。

    // Case 4:while 循环结束,没有执行返回,移到最末项。
    newNode = (Link) malloc(sizeof(Term)); // 要求一个新节点的内存。
    newNode->elem.degr = e.degr; // 设定新项的项次。
    newNode->elem.coef = e.coef; // 设定新项的系数。
    newNode->next = NULL; // 最后一项,将其下一个项设为空值。
    previous->next = newNode; // 将新增节点放在线性表的最后。
    return newNode->elem.coef; // 返回系数。
    }

    // 从多项式线性表移除一个项次。若该项次存在,移除项次,并返回该
    // 项次的系数;否则,返回 0。
    Coefficient removeTerm(Poly *P, Degree degree) {
    Link current = *P; // 指多项式的首项。
    Link previous; // 指向前一项。
    Coefficient coef; // 移除项的系数。

    while (current!=NULL) { // 当多项式还有项。
    if (current->elem.degr==degree) { // 找到要移除的项。
    coef = current->elem.coef; // 移除项的系数。
    if (current==*P) { // 移除项是头首项。
    *P = current->next; // 设定下一项为首项。
    free(current); // 释放移除的项。
    return coef; // 返回移除项的系数。
    }
    else {
    previous->next = current->next; // 将前一个项的链指向目前的下一项。
    free(current); // 放移除的项。
    return coef; // 返回移除项的系数。
    }
    }
    else if (current->elem.degr>degree) {
    previous = current; // 将目前的节点设为前一个节点。
    current = current->next; // 将下一个节点设为下一步骤的目前节点。
    }
    else return 0.0; // 目前项次小于移除的项次;删除失败。
    }
    return 0.0; // 已经没有项,移除失败。
    }

  • 相关阅读:
    Chrome浏览器扩展开发系列之十四:本地消息机制Native messaging
    Chrome浏览器扩展开发系列之十三:消息传递Message
    Chrome浏览器扩展开发系列之十二:Content Scripts
    Chrome浏览器扩展开发系列之十一:NPAPI插件的使用
    Chrome浏览器扩展开发系列之十:桌面通知Notification
    《转载》使用CSS3 Flexbox布局
    CSS display属性的值及作用
    CSS中的各种居中方法总结
    《转载》详解 CSS 属性
    CSS3中新增的内容
  • 原文地址:https://www.cnblogs.com/mju3197103150/p/13021334.html
Copyright © 2011-2022 走看看