C/C++教程

2021 fall cs61lab11

本文主要是介绍2021 fall cs61lab11,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

网址 https://inst.eecs.berkeley.edu/~cs61a/fa21/lab/lab11/#problem-3
problem1:

    class Buffer:
        """A Buffer provides a way of accessing a sequence of tokens across lines.

        Its constructor takes an iterator, called "the source", that returns the
        next line of tokens as a list each time it is queried, or None to indicate
        the end of data.

        The Buffer in effect concatenates the sequences returned from its source
        and then supplies the items from them one at a time through its pop_first()
        method, calling the source for more sequences of items only when needed.

        In addition, Buffer provides a current method to look at the
        next item to be supplied, without sequencing past it.

        The __str__ method prints all tokens read so far, up to the end of the
        current line, and marks the current token with >>.

        >>> buf = Buffer(iter([['(', '+'], [15], [12, ')']]))
        >>> buf.pop_first()
        '('
        >>> buf.pop_first()
        '+'
        >>> buf.current()
        15
        >>> buf.current()   # Calling current twice should not change buf
        15
        >>> buf.pop_first()
        15
        >>> buf.current()
        12
        >>> buf.pop_first()
        12
        >>> buf.pop_first()
        ')'
        >>> buf.pop_first()  # returns None
        """

        def __init__(self, source):
            self.index = 0
            self.source = source#source是每一次next是一个列表
            self.current_line = ()
            self.current()

        def pop_first(self):
            """Remove the next item from self and return it. If self has
            exhausted its source, returns None."""
            # BEGIN PROBLEM 1
            "*** YOUR CODE HERE ***"
            current = self.current()
            self.index += 1#只有每次pop_first才会改变index也就是改变current()
            return current
            # END PROBLEM 1

        def current(self):
            """Return the current element, or None if none exists."""
            while not self.more_on_line():#当more_on_line()函数False时超过了当前索引,就需要进入下一个列表
                self.index = 0
                try:
                    # BEGIN PROBLEM 1
                    "*** YOUR CODE HERE ***"
                    self.current_line = next(self.source)#进入下一个列表
                    # END PROBLEM 1
                except StopIteration:
                    self.current_line = ()
                    return None
            return self.current_line[self.index]

        def more_on_line(self):
            return self.index < len(self.current_line)

problem 2 and 3:
这两道题目就是要对给定的src进行解释,两个函数相互递归,有一些规则
scheme_read遇到左括号就调用read_line函数来生成Pair,遇到'就是引用即解释为quote,
read_line函数就是如果遇到右括号就结束,如果是其他的数字或者字母,布尔值就正常储存,在调用scheme_read函数进入递归

    def scheme_read(src):
        """Read the next expression from SRC, a Buffer of tokens.
        """
        if src.current() is None:
            raise EOFError
        val = src.pop_first()  # Get and remove the first token
        if val == 'nil':
            return nil
        elif val == '(':
            return read_tail(src)
        elif val == "'":
            return Pair('quote', Pair(scheme_read(src), nil))
        elif val not in DELIMITERS:
            return val
        else:
            raise SyntaxError('unexpected token: {0}'.format(val))


    def read_tail(src):
        """Return the remainder of a list in SRC, starting before an element or ).
        """
        try:
            if src.current() is None:
                raise SyntaxError('unexpected end of file')
            elif src.current() == ')':
                src.pop_first()
                return nil
            else:
                return Pair(scheme_read(src), read_tail(src))
        except EOFError:
            raise SyntaxError('unexpected end of file')
这篇关于2021 fall cs61lab11的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!