zoukankan      html  css  js  c++  java
  • 函数式编程(练习题)

    作业

    现要求你写一个简单的员工信息增删改查程序,需求如下:

    当然此表你在文件存储时可以这样表示

    1,Alex Li,22,13651054608,IT,2013-04-01
    2,Jack Wang,28,13451024608,HR,2015-01-07
    3,Rain Wang,21,13451054608,IT,2017-04-01
    4,Mack Qiao,44,15653354208,Sales,2016-02-01
    5,Rachel Chen,23,13351024606,IT,2013-03-16
    6,Eric Liu,19,18531054602,Marketing,2012-12-01
    7,Chao Zhang,21,13235324334,Administration,2011-08-08
    8,Kevin Chen,22,13151054603,Sales,2013-04-01
    9,Shit Wen,20,13351024602,IT,2017-07-03
    10,Shanshan Du,26,13698424612,Operation,2017-07-02
    

    1.可进行模糊查询,语法至少支持下面3种查询语法:

    find name,age from staff_table where age > 22
    
    find * from staff_table where dept = "IT"
    
    find * from staff_table where enroll_date like "2013"
    

    2.可创建新员工纪录,以phone做唯一键(即不允许表里有手机号重复的情况),staff_id需自增

    语法: add staff_table Alex Li,25,134435344,IT,2015-10-29
    

    3.可删除指定员工信息纪录,输入员工id,即可删除

    语法: del from staff where  id=3
    

    4.可修改员工信息,语法如下:

    UPDATE staff_table SET dept="Market" WHERE  dept = "IT" 把所有dept=IT的纪录的dept改成Market
    UPDATE staff_table SET age=25 WHERE  name = "Alex Li"  把name=Alex Li的纪录的年龄改成25
    

    5.以上每条语名执行完毕后,要显示这条语句影响了多少条纪录。 比如查询语句 就显示 查询出了多少条、修改语句就显示修改了多少条等。

    注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码

    import os
    import re
    FILE = 'staff_table.txt'
    PROJECT = ['staff_id','name','age','phone','dept','enroll_date']
    def date():
        date_dic ={
            'staff_id':[],
            'name':[],
            'age':[],
            'phone':[],
            'dept':[],
            'enroll_date':[]
        }
        with open(FILE,'r',encoding='utf-8') as f:
            for line in f.readlines():
                line = line.split(',')
                date_dic['staff_id'].append(line[0])
                date_dic['name'].append(line[1])
                date_dic['age'].append(line[2])
                date_dic['phone'].append(line[3])
                date_dic['dept'].append(line[4])
                date_dic['enroll_date'].append(line[5].strip())
            return date_dic
    def save_date(STAFF_DB):
        with open('new.%s'%FILE,'w',encoding='utf-8')as  f_new:
            line =[]
            num =len(STAFF_DB.get('staff_id'))
            (STAFF_DB).get('staff_id')[num-1] =  str((STAFF_DB).get('staff_id')[num-1])
            for n in range(num):
                rew = STAFF_DB['staff_id'][n]+','+STAFF_DB['name'][n]+','+STAFF_DB['age'][n]+','+STAFF_DB['phone'][n]+','+STAFF_DB['dept'][n]+','+STAFF_DB['enroll_date'][n]
                line.append(rew)
                for v in line:
                    pass
                f_new.write(v+'
    ')
            f_new.close()
            os.replace('new.%s'%FILE,FILE)
    
    def add(cmd):
        for index,k in enumerate(cmd):
            s = cmd[3]
        s = str(s).split(',')
        cmd_2 = []
        name_1 = cmd[2]+s[0]
        cmd_2.append(name_1)
        cmd_2.append(s[1])
        if s[2] not in STAFF_DB['phone']:
            cmd_2.append(s[2])
            cmd_2.append(s[3])
            cmd_2.append(s[4])
        else:
            print('手机号已存在')
            exit(1)
    
        STAFF_DB['staff_id'].append((len(date()['staff_id'])+1))
        STAFF_DB['name'].append(name_1)
        STAFF_DB['age'].append(cmd_2[1])
        STAFF_DB['phone'].append(cmd_2[2])
        STAFF_DB['dept'].append(cmd_2[3])
        STAFF_DB['enroll_date'].append(cmd_2[4])
        save_date(STAFF_DB)
        print('添加成功')
    def find(cmd):
        if cmd[1] == 'name'or'age':
            for index,k in enumerate(STAFF_DB['age']):
                if '<' in cmd:
                    if k < cmd[7]:
                        print(STAFF_DB['name'][index],k)
                elif '>' in cmd:
                    if k> cmd[7]:
                        print(STAFF_DB['name'][index],k)
                elif '=' in cmd:
                    if k == cmd[7]:
                        print(STAFF_DB['name'][index],k)
        else:
            pass
    
    def where(cmd):
        choice = cmd[5]
        result = cmd[7]
        if  '=' in cmd:
            for index,k in enumerate(STAFF_DB[choice]):
                if result in '"'+k+'"':
                    print(STAFF_DB['staff_id'][index],STAFF_DB['name'][index],STAFF_DB['age'][index],STAFF_DB['phone'][index],STAFF_DB['enroll_date'][index],k)
        elif 'like' in cmd:
            for index, k in enumerate(STAFF_DB[choice]):
                if result in '"'+k[:4]+'"':
                    print(STAFF_DB['staff_id'][index],STAFF_DB['name'][index],STAFF_DB['age'][index],STAFF_DB['phone'][index],STAFF_DB['enroll_date'][index])
    
    def delete(cmd):
        #del from staff where  id=3
        cmd_del = cmd[4]
        cmd_del_2 = str(cmd_del).split('=')[1]
        cmd_del = int(str(cmd_del).split('=')[1])
        print(cmd_del_2)
        line = []
        line = STAFF_DB.get('staff_id')
        index = line.index(cmd_del_2)
        print(index)
    
        if STAFF_DB.get('staff_id')[index]== cmd_del_2:
            print('oooooooooo')
            del (STAFF_DB.get('staff_id')[index])
            del (STAFF_DB.get('name')[index])
            del (STAFF_DB.get('age')[index])
            del (STAFF_DB.get('phone')[index])
            del (STAFF_DB.get('dept')[index])
            del (STAFF_DB.get('enroll_date')[index])
    
        save_date(STAFF_DB)
    def analysis(cmd):
        '''解析语句'''
        cmd_list = cmd.split(re.match(*' ',cmd))
        cmd = cmd_list
        if cmd [1] == '*':
            return where(cmd)
        elif cmd [0] == 'find':
            return  find(cmd)
        elif cmd[0] == 'add':
            return  add(cmd)
        elif cmd[0] == 'del':
            return delete(cmd)
    
    def mian():
        '''程序入口'''
        while True:
            cmd =input('输入sql语句:  ').strip()
            if not cmd:continue
            analysis(cmd)
    STAFF_DB = date()
    mian()


    未完待续请大家批评 谢谢
  • 相关阅读:
    多测师肖sir__分享考试连接___轻速云
    软件测试___多测师肖sir____非技术面试之离职原因
    软件测试 ___ 接口测试__面试题
    软件测试____多测师肖sir____app测试面试题
    软件测试 ____杭州多测师肖sir___面试题模板收集
    软件测试___接口测试面试题____接口超时场景设置和接口超时处理
    软件测试 ____杭州多测师肖sir__po设计面试题
    c#使用Socket获取网页(含自动跳转, 解压网页)
    查找类似图片(Find Similar Image)
    C# .net Remoting最简单的例子
  • 原文地址:https://www.cnblogs.com/mjiu/p/8654778.html
Copyright © 2011-2022 走看看