首页 > 其他分享 >Programming Abstractions in C阅读笔记:p338-p346

Programming Abstractions in C阅读笔记:p338-p346

时间:2024-03-24 14:33:05浏览次数:25  
标签:p346 Abstractions void Programming break operandStack static printf stack

《Programming Abstractions in C》学习第80天,p338-p346,总计9页。

一、技术总结

栈的实现包括入栈、出栈、判断栈是否为满,判断栈是否为空等。作者结合RPN计算器来实现,稍显无聊。

/*
 * File: rpncalc.c
 * ---------------
 * This program simulates an electronic calculator that uses
 * reverse Polish notation, in which the operators com after
 * the operands to which they apply.
 */

#include <stdio.h>
#include <ctype.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
#include "stack.h"

/* Private function prototypes */

static void ApplyOperator(char op, stackADT operandStack);

static void HelpCommand(void);

static void ClearStack(stackADT operandStack);

static void DisplayStack(stackADT operandStack);

/* Main program */
int main() {
    stackADT operandStack;
    string line;
    char ch;

    printf("RPN Calculator Simulator (type H for help)\n");
    operandStack = NewStack();
    while (TRUE) {
        printf("> ");
        line = GetLine();

        ch = toupper(line[0]);
        switch (ch) {
            case 'Q':
                exit(0);
            case 'H':
                HelpCommand();
                break;
            case 'C':
                ClearStack(operandStack);
                break;
            case 'S':
                DisplayStack(operandStack);
                break;
            default:
                if (isdigit(ch)) {
                    Push(operandStack, StringToReal(line));
                } else {
                    ApplyOperator(ch, operandStack);
                }
                break;

        }
    }

    return 0;
}

/* Private functions */

/*
 * Function: ApplyOperator
 * Usage: ApplyOperator(op, operandStack);
 * ---------------------------------------
 * This function applies the operator to the top two elements on
 * the operand stack. Because the elements on the stack are
 * popped in reverse order, the right operand is popped before
 * the left operand.
 */

static void ApplyOperator(char op, stackADT operandStack) {
    double lhs, rhs, result;

    rhs = Pop(operandStack);
    lhs = Pop(operandStack);
    switch (op) {
        case '+':
            result = lhs + rhs;
            break;
        case '-':
            result = lhs - rhs;
            break;
        case '*':
            result = lhs * rhs;
            break;
        case '/':
            result = lhs / rhs;
            break;
        default:
            Error("Illegal operator %c", op);
    }
    printf("%g\n", result);
    Push(operandStack, result);
}

/*
 * Function: HelpCommand
 * Usage: HelpCommand();
 * ---------------------
 * This function generate a help message for the user;
 */

static void HelpCommand(void) {
    printf("Enter expression in Reverse Polish Notation, \n");
    printf("in which operators follow the operands to which\n");
    printf("they apply. Each line consists of a number, an\n");
    printf("operator, or one of the following commands: \n");
    printf("  Q -- Quit the program\n");
    printf("  H -- Display this help message\n");
    printf("  C -- Clear the calculator stack\n");
    printf("  S -- Display all values in the stack\n");
}


/*
 * Function: ClearStack
 * Usage: ClearStack(stack);
 * -------------------------
 * This function clears the stack by popping elements until it is
 * empty.
 */
static void ClearStack(stackADT stack) {
    while (!StackIsEmpty(stack)) {
        (void)Pop(stack);
    }
}

/*
 * Function: DisplayStack
 * Usage: DisplayStack(stack);
 * ---------------------------
 * This function displays the contents of a stack.
 */

static void DisplayStack(stackADT stack) {
    int i, depth;

    printf("Stack: ");
    depth = StackDepth(stack);
    if (depth == 0) {
        printf("empty\n");
    } else {
        for (i = depth - 1; i >= 0; i--) {
            if (i < depth - 1) {
                printf(", ");
            }
            printf("%g", GetStackElement(stack, i));
        }
    }
}

完整代码见:https://github.com/codists/Programming-Abstractions-In-C/tree/main/chapter8

二、英语总结

1.insulation是什么意思?

答:

(1)insulate > insulation

(2)insulate: from insula“island”(isle)。 vt. to cover and surround sth with materials。

(3)insulation: u. the substance used in insulating。

p345, By using StackDepth(stack) form, you provide a layer of insulation between the client and the implementation.

2.auxiliary是什么意思?

答:*aug-"to increase"。adj. giving help, especially to a more import person(辅助的)。

三、其它

今日没有什么想说的。

四、参考资料

1. 编程

(1)Eric S.Roberts,《Programming Abstractions in C》:https://book.douban.com/subject/2003414

2. 英语

(1)Etymology Dictionary:https://www.etymonline.com

(2) Cambridage Dictionary:https://dictionary.cambridge.org

欢迎搜索及关注:编程人(a_codists)

标签:p346,Abstractions,void,Programming,break,operandStack,static,printf,stack
From: https://www.cnblogs.com/codists/p/18092398

相关文章

  • 『The Go Programming Language』二、程序结构
    2.1名称名称开头通常为字母或下划线且区分大小写。存在25个关键字只能用于合法处不能用作名称:breakdefaultfuncinterfaceselectcasedefergomapstructchanelsegotopackageswitchconstfallthroughifrangetypecontinueforimportreturnvar此外还存在预声明的常量、类型和函......
  • The 14th Jilin Provincial Collegiate Programming Contest
    The14thJilinProvincialCollegiateProgrammingContest-Codeforces队友太猛了,我整场就只写了D,其他题给队友开完了,预计补一下M,FProblemD.Trie(AC自动机+树状数组)大概就是给定一颗Trie树操作一是给Trie树的fail树上一个集合中的点的所有子节点打上一个......
  • Programming Abstractions in C阅读笔记:p327-p330
    《ProgrammingAbstractionsinC》学习第78天,p327-p330,总计4页。一、技术总结1.ADT(抽象数据类型)p328,Atypedefinedintermofitsbehaviorratherthanitsrepresnetationiscalledanabstractdatatype(如果一种数据类型使用它们的行为而不是表示来定义,那么这样的......
  • JB Loves Math(The 19th Zhejiang Provincial Collegiate Programming Contest)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • JB Wants to Earn Big Money(The 19th Zhejiang Provincial Collegiate Programming Co
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • JB Loves Comma(The 19th Zhejiang Provincial Collegiate Programming Contest)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • Monoxer Programming Contest 2024(AtCoder Beginner Contest 345)
    MonoxerProgrammingContest2024(AtCoderBeginnerContest345)\(A\)Leftrightarrow\(100pts\)按照题意模拟即可。点击查看代码intmain(){strings;inta=0,b=0,c=0,i;cin>>s;for(i=0;i<s.size();i++){a+=(s[i]=='<&#......
  • Dwango Programming Contest 6th D 题解
    正好测试一下专栏的题解系统。我省选寄了都怪洛谷/fn/fn/fn/fn/fn/fn/fn题解显然可以对于所有关系建有向边,显然是基环外向树森林。由于是字典序最小,因此找到最小的上一个点没有直接连向边的点一定最优。但是有时取最优会导致最后无法选完,我们考虑无法选完的情况。第一种是......
  • Toyota Programming Contest 2024#3(AtCoder Beginner Contest 344)
    C先预处理出三个数组能拼出的数,存放到map中。查询的时候只需要看这个数是否出现在map里即可。时间复杂度\(O(n^3\logv+Q\logv)\),\(n\leq100\),\(\logv\)是map的时间复杂度。#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=3e......
  • Programming Abstractions in C阅读笔记:p312-p326
    《ProgrammingAbstractionsinC》学习第77天,p312-p326,总计15页,第7章完结。一、技术总结第7章主要讲算法分析——引入时间复杂度这一概念来评估算法的快慢。时间复杂度使用大O符号来表示。第7章以排序算法为示例,包含:选择排序,归并排序以及快速排序,这些基本的排序算法都是我们要......