正規表示式作業 計算器

2022-07-26 16:57:11 字數 4501 閱讀 1601

dic = 

defadd_info(filename):

user_info =

with open(filename, 'r

', encoding='

utf-8

') as f:

lines =f.readlines()

last_line = lines[-1]

id_max = last_line[dic['id'

]]

ifid_max.isdigit():

name = input('

請輸入姓名:

').strip()

ifname.isspace():

print('

姓名不能為空')

else

: age = input('

請輸入年齡:

').strip()

ifage.isspace():

print('

年齡不能為空')

elif

notage.isdigit():

print('

年齡必須是數字')

else

: phone = input('

').strip()

ifphone.isspace():

print('

**不能為空')

elif

notphone.isdigit():

print('

**必須全部由數字組成')

else

: job = input('

請輸入工作:

').strip()

ifjob.isspace():

print('

工作不能為空')

else

: with open(filename, 'a

', encoding='

utf-8

') as f:

info = ','

.join(user_info)

iff.writable():

if f.write(info) >0:

print('

使用者[%s]的資訊新增成功!

'%name)

defdel_info():

pass

defread_file(filename):

with open(filename, encoding='

utf-8

') as f:

for line in

f: line =line.strip()

line_list = line.split(','

)

yield

line_list

defcond_filter(condition, filename):

condition =condition.strip()

if'>='in

condition:

condition = condition.split('

>=')

col =condition[0].strip()

val = condition[1].strip()

g =read_file(filename)

for line_list in

g:

if int(line_list[dic[col]]) >=int(val):

yield

line_list

elif

'<='in

condition:

condition = condition.split('

<=')

col =condition[0].strip()

val = condition[1].strip()

g =read_file(filename)

for line_list in

g:

if int(line_list[dic[col]]) <=int(val):

yield

line_list

elif

'>'in

condition:

condition = condition.split('

>')

col =condition[0].strip()

val = condition[1].strip()

g =read_file(filename)

for line_list in

g:

if int(line_list[dic[col]]) >int(val):

yield

line_list

elif

'<'in

condition:

condition = condition.split('

<')

col =condition[0].strip()

val = condition[1].strip()

g =read_file(filename)

for line_list in

g:

if int(line_list[dic[col]]) yield

line_list

elif

'like'in

condition:

condition = condition.split('

like')

col =condition[0].strip()

val = condition[1].strip()

g =read_file(filename)

for line_list in

g:

if val in

line_list[dic[col]]:

yield

line_list

else

:

print('

查詢暫時只支援》=、<=、>、<、like')

defprint_info(col_lst, staff):

if'*'

incol_lst:

col_lst =dic.keys()

for i in

col_lst:

print(i, end='

\t\t\t')

print(''

)

for staff_info in

staff:

for i in

col_lst:

print(staff_info[dic[i.strip()]], end='

\t\t\t')

print(''

)print("

請選擇你的功能:新增使用者資訊 刪除使用者資訊 查詢使用者資訊 [exit] 退出")

choose = input("

請選擇你的功能:

").strip()

while

true:

ret = input("

>>>

").lower()

if'exit

'.lower() ==ret.strip():

break

if choose == '3'

: if'

where'in

ret:

select, where = ret.split('

where')

select, table = select.split('

from')

cols = select.replace('

select

', ''

).strip()

cols = cols.split(','

) g =cond_filter(where.strip(), table.strip())

print_info(cols, g)

else

: select, table = ret.split('

from')

cols = select.replace('

select

', ''

).strip()

cols = cols.split(','

) g =read_file(table.strip())

print_info(cols, g)

ret = input("

>>>")

elif choose == '1'

: add_info(

'userinfo')

ret = input("

>>>

")

計算器 (正規表示式

寫個計算器,輸入完全符合要求的 數字組成的表示式,計算相應結果。思路分先後順序遞迴拆解式子。先對 符合進行遞迴,直到全是 表示式,再對 表示式進行遞迴直到只有數字。最後返回計算。include using namespace std define maxn 100005 define maxm 10...

練習 正規表示式計算器

coding utf 8 import re 正規表示式計算器 檢測錯誤 1 括號匹配 2 運算子檢查 3 非法字元 查詢括號,乘除,加減。運算 主函式替換 ret re.findall s 審核小括號 def check k s tag 0 for x in s if x tag 1 elif x...

正規表示式實現計算器

author 來童星 date 2019 11 10 import re source 1 2 60 30 40 5 9 2 5 3 7 3 99 4 2998 10 568 14 4 3 16 3 2 做檢查 def check s flag true if re.findall a za z s...