zoukankan      html  css  js  c++  java
  • 员工信息表作业

    dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}
    
    def add_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']]
            if id_max.isdigit():
                id = int(id_max) + 1
                user_info.append(str(id))
    
            name = input('请输入姓名:').strip()
            if name.isspace():
                print('姓名不能为空')
            else:
                user_info.append(name)
    
            age = input('请输入年龄:').strip()
            if age.isspace():
                print('年龄不能为空')
            elif not age.isdigit():
                print('年龄必须是数字')
            else:
                user_info.append(age)
    
            phone = input('请输入电话:').strip()
            if phone.isspace():
                print('电话不能为空')
            elif not phone.isdigit():
                print('电话必须全部由数字组成')
            else:
                user_info.append(phone)
    
            job = input('请输入工作:').strip()
            if job.isspace():
                print('工作不能为空')
            else:
                user_info.append(job)
    
        with open(filename, 'a', encoding='utf-8') as f:
            info = ','.join(user_info)
            if f.writable():
                if f.write(info) > 0:
                    print('用户[%s]的信息添加成功!'%name)
    
    
    def del_info():
        pass
    
    def read_file(filename):
        with open(filename, encoding='utf-8') as f:
            for line in f:
                line = line.strip()
                line_list = line.split(',')
                yield line_list
    
    
    def cond_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]]) < int(val):
                    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')
    
    
    def print_info(col_lst, staff):
        if '*' in col_lst:
            col_lst = dic.keys()
    
        for  i in col_lst:
            print(i, end='			')
        print('')
    
        for staff_info in staff:
            for i in col_lst:
                print(staff_info[dic[i.strip()]], end='			')
            print('')
    
    
    print("请选择你的功能:【1】新增用户信息 【2】删除用户信息 【3】查询用户信息 [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(">>>")
  • 相关阅读:
    JAVA基础-抽象类和接口
    JAVA基础-多态
    JAVA基础-继承机制
    C++(二十七) — 深拷贝、浅拷贝、复制构造函数举例
    C++(二十六) — 构造函数、析构函数、对象数组、复制构造函数
    C++(二十五) — 类的封装、实现、设计
    C++(二十四) — 指向字符的指针为什么可以用字符串来初始化,而不是字符地址?
    C++(二十三) — 内存泄漏及指针悬挂
    C++(二十二) — 指针变量、函数指针、void指针
    C++(二十一) — 引用概念及本质
  • 原文地址:https://www.cnblogs.com/Ryan-Fei/p/12214015.html
Copyright © 2011-2022 走看看