|
发表于 2022-6-26 11:46:47
|
显示全部楼层
本楼为最佳答案
 1. 你没有说要使用的编程语言,而且把问题发在了 新手乐园 版块
所以我不知道你要求用哪个语言写,看你的问题中出现了 input 这个单词,初步判断是用python
2. python中的这个input函数每一次都会得到一行输入,但是我需要一次得到一个字符,而且有时候还需要把这个字符退回去,用input函数就需要自己写一个字符一个字符读取和退回字符的代码,C语言中有fgetc和ungetc函数,这两个函数就是我想要的,这里就直接调用C语言的这两个函数
3. 我干脆帮你把这两个要求合并一下吧,毕竟两个要求有太多相同的地方
合并成这样
输入一个字符串,如果是“表达式”,那就计算结果然后输出,如果不是“表达式”,那就报告错误
- #!/usr/bin/env python
- #coding=utf-8
- from ctypes import cdll
- from ctypes.util import find_library
- from ctypes import c_void_p, c_int, byref
- import sys
- libc = cdll.LoadLibrary(find_library('c'))
- stdin = c_void_p.in_dll(libc, 'stdin')
- def fgetc(stream):
- return libc.fgetc(stream)
- def ungetc(ch, stream):
- return libc.ungetc(ch, stream)
- # token
- # [[symbol, value, rows, cols], lchild, rchild]
- last_token = None
- file_rows = 1
- file_cols = 1
- ERROR = 256
- NUM = 257
- EOF = -1
- def error(value):
- return [[ERROR, value[1], value[2], value[3]], None, None]
- def isdigit(ch):
- return ch >= ord('0') and ch <= ord('9')
- def get_token():
- global last_token
- if last_token:
- result = last_token
- last_token = None
- return result
- global file_cols, file_rows
- last_position = [file_rows, file_cols]
- ch = fgetc(stdin)
- file_cols += 1
- if ch in [ord(i) for i in [' ', '\t', '\v', '\f']]:
- return get_token()
- if isdigit(ch):
- ungetc(ch, stdin)
- file_cols -= 1
- num = c_int()
- libc.fscanf(stdin, b'%d', byref(num))
- file_cols += len(str(num.value))
- return [[NUM, num.value, *last_position], None, None]
- if ch == ord('\n'):
- file_rows += 1
- file_cols = 1
- return [[ch, ch, *last_position], None, None]
- def unget_token(token):
- global last_token
- last_token = token
- def factor():
- token = get_token()
- if token[0][0] == NUM: return token
- if token[0][0] == ord('-'):
- token[1] = factor()
- if token[1][0][0] == ERROR: return token[1]
- return token
- if token[0][0] != ord('('): return error(token[0])
- token = expression()
- if token[0][0] == ERROR: return token
- temp = token
- token = get_token()
- if token[0][0] != ord(')'): return error(token[0])
- return temp
- def term():
- token = factor()
- if token[0][0] == ERROR: return token
- a = token
- token = get_token()
- if token[0][0] != ord('*') and token[0][0] != ord('/'):
- unget_token(token)
- return a
- op = token
- token = term()
- if token[0][0] == ERROR: return token
- b = token
- op[1] = a
- op[2] = b
- return op
- def expression():
- token = term()
- if token[0][0] == ERROR: return token
- a = token
- token = get_token()
- if token[0][0] != ord('+') and token[0][0] != ord('-'):
- unget_token(token)
- return a
- op = token
- token = expression()
- if token[0][0] == ERROR: return token
- b = token
- op[1] = a
- op[2] = b
- return op
- def do_error(info):
- print('错误的表达式!', file = sys.stderr)
- print(f'(\'{chr(info[1])}\',{info[2],info[3]})', file = sys.stderr)
- while True:
- token = get_token()
- if token[0][0] == ord('\n'): break
- def val(tree):
- if tree[0][0] == NUM: return tree[0][1]
- if tree[0][0] == ord('+'): return val(tree[1]) + val(tree[2])
- if tree[0][0] == ord('*'): return val(tree[1]) * val(tree[2])
- if tree[0][0] == ord('/'): return val(tree[1]) / val(tree[2])
- if tree[0][0] == ord('-'):
- return -val(tree[1]) if tree[2] == None else val(tree[1]) - val(tree[2])
- raise "未知的运算符!"
- def line():
- token = get_token()
- if token[0][0] == ord('\n'): return
- unget_token(token)
- token = expression()
- if token[0][0] == ERROR:
- do_error(token[0])
- return
- result = token
- token = get_token()
- if token[0][0] == ord('\n'):
- #print(result)
- print('>>> ' + str(val(result)))
- return
- do_error(token[0])
- def lines():
- token = get_token()
- unget_token(token)
- if token[0][0] == EOF:
- return
- line()
- lines()
- return
- def start():
- lines()
- token = get_token()
- if token[0][0] == EOF: return True
- return False
- print(start())
复制代码
参考: https://fishc.com.cn/thread-213764-1-1.html
|
|