基于python顺序表和链表的图书管理系统

写在前面:在写这个实验的过程中发现网上对于这方面资料挺少的,后觉得值得保存一下。没有用到很高级的方法,大多偏向于暴力解决。(如果你也是cu*t的校友那么欢迎参考和纠错指正,不过最好不要大段复制,因为我也还没交)






class Booklist:
    def __init__(self):
        self.data = []
    def Addbook(self):
        information = input("请输入图书信息,每本图书信息占一行,书号、书名、价格用空格分隔。输入0 0 0结束输入\n")
        try:

            while information != '0':
                information_list = information.split()
                shuhao = information_list[0]
                shuming = information_list[1]
                jiage = information_list[2]
                self.data.append([shuhao,shuming,float(jiage)])
                information = input("请再次输入")
        except ValueError:
            print("输入错误")
        except IndexError:
            print("输入错误")

    def Addbook_2(self):
        num = input("请输入图书数目")
        self.data.append(num)
        information = input("请输入图书信息,每本图书信息占一行,书号、书名、价格用空格分隔。输入0 0 0结束输入\n")
        try:

            while information != '0':
                information_list = information.split()
                shuhao = information_list[0]
                shuming = information_list[1]
                jiage = information_list[2]
                self.data.append([shuhao,shuming,float(jiage)])
                information = input("请再次输入")
        except ValueError:
            print("输入错误")
        except IndexError:
            print("输入错误")

    def Count(self):
        return len(self.data)

    def ShowList(self):
        if len(self.data) > 0:
            print("书号\t书名\t价格")
            for i in range(len(self.data)):
                for j in range(2):
                    print(self.data[i][j],end = '\t')
                print("%.2f" %self.data[i][2])

            print("0\t0\t0")
        else:print("无书籍记录")

    def ShowList_2(self):
        if len(self.data) > 0:
            print("图书数目为", self.data[0][0])
            print("书号\t书名\t价格")
            for i in range(1, len(self.data)):
                for j in range(2):
                    print(self.data[i][j], end='\t')
                print("%.2f" % self.data[i][2])

            print("0\t0\t0")
        else:
            print("无书籍记录")

    def SortBook(self):
        self.data.sort(key=lambda ele:ele[2],reverse = True)
        self.ShowList()

    def revise(self):
        sum = 0
        for i in range(len(self.data)):
            sum = sum+self.data[i][2]
        avg_prise = sum/len(self.data)

        for j in range(len(self.data)):
            if self.data[j][2] < avg_prise:
                self.data[j][2] = self.data[j][2]*1.2
            else:self.data[j][2] = self.data[j][2]*1.1
        print("修改前平均价格为",avg_prise)
        self.ShowList()

    def reverse(self):
        self.Addbook_2()
        i = 0
        j = len(self.data) - 1
        while i < j:
            self.data[i],self.data[j] = self.data[j],self.data[i]
            i = i+1
            j = j-1
        self.ShowList_2()

    def find_exp(self):
        self.Addbook_2()
        self.data.sort(key=lambda ele: ele[2], reverse=True)
        for i in range(len(self.data)-1):
            if self.data[i][2] > self.data[i+1][2]:
                print("价格最高的图书可能有%d"%(i+1)+"本")
                print("书号\t书名\t价格")
                for m in range(i+1):
                    for j in range(3):
                        print(self.data[m][j], end='\t')
                    print("\n")
                print("0\t0\t0")

    def find_like(self):
        target = []
        count = 0
        name_list = []
        self.Addbook_2()
        times = input("请输入查找次数")
        for i in range(int(times)):
            name = input("请输入第%d次待查找的图书名字" %(i+1))
            name_list.append(name)
        for j in range(int(times)):
            for m in range(1, len(self.data)):
                if self.data[m][1] == name_list[j]:
                    count = count + 1
                    target.append(m)
            if count>0:
                print("第%d次查找" %(j+1))
                print("此书有%d本" %count)
                for o in target:
                    for j in range(2):
                        print(self.data[o][j], end='\t')
                    print("%.2f" % self.data[o][2])
                target.clear()
                count = 0

            else:print("抱歉,第%d次查找中没有你的最爱" %(j+1))

    def find_place(self):
        index_list = []
        self.Addbook_2()
        times = input("请输入查找次数")
        for i in range(int(times)):
            index = input("请输入第%d次待查找的图书位置序号" % (i + 1))
            index_list.append(index)
        for p in index_list:
            if int(p) > len(self.data) or int(p)<1:
                print("抱歉,最佳位置上的图书不存在!")
            else:
                for o in range(2):

                    print(self.data[int(p)][o], end='\t')
                print("%.2f" % self.data[int(p)][2])

    def Book_insert(self):
        self.Addbook_2()
        index = int(input("请输入插入位置序号"))
        information = input("请输入插入图书信息")
        information_list = information.split()
        shuhao = information_list[0]
        shuming = information_list[1]
        jiage = information_list[2]
        if index > 0 and index < len(self.data):
            self.data.insert(index, [shuhao, shuming, float(jiage)])
            if len(self.data) > 0:
                print("书号\t书名\t价格")
                for i in range(1, len(self.data)):
                    for j in range(2):
                        print(self.data[i][j], end='\t')
                    print("%.2f" % self.data[i][2])

                print("0\t0\t0")
        else:print("抱歉,入库位置非法")

    def Book_delete(self):
        self.Addbook_2()
        index = int(input("请输入删除位置序号"))
        if index > 0 and index < len(self.data):
            del self.data[index]
            if len(self.data) > 0:
                print("书号\t书名\t价格")
                for i in range(1, len(self.data)):
                    for j in range(2):
                        print(self.data[i][j], end='\t')
                    print("%.2f" % self.data[i][2])

                print("0\t0\t0")
        else:
            print("抱歉,出库位置非法")

    def quchong(self):
        target = []
        target_3 = []
        self.Addbook_2()
        for i in range(1,len(self.data)):
            for j in range(i+1,len(self.data)):
                if self.data[i][0] == self.data[j][0]:
                    target.append(j)
        target_2 = set(target)
        for o in range(len(self.data)):
            if o not in target_2:
                target_3.append(o)
        del target_3[0]
        print("去重后图书数目为",len(target_3))
        if len(self.data) > 0:
            print("书号\t书名\t价格")
            for i in target_3:
                for j in range(2):
                    print(self.data[i][j], end='\t')
                print("%.2f" % self.data[i][2])

            print("0\t0\t0")


 

class LinkNode:
    def __init__(self,data = None):
        self.data = data
        self.next = None

class LinkList:
    def __init__(self):
        self.head = LinkNode()
        self.head.next = None

    def CreatList_shou(self,a):
        for i in range(len(a)):
            node = LinkNode(a[i])
            node.next = self.head.next
            self.head.next = node

    def CreatList_wei(self,a):
        t = self.head
        for i in range(len(a)):
            node = LinkNode(a[i])
            t.next = node
            t=node
        node.next = None

    def display(self,a):
        p=self.head.next
        print("书号\t书名\t价格")
        while p is not None:
            for i in range(2):
                print(p.data[i],end='\t')
            print("%.2f" % p.data[2])
            p = p.next

    def ListSort(self,a):
        p = self.head.next
        prise = []
        while p is not None:
            prise.append(p.data)
            p = p.next
        prise.sort(key=lambda ele:ele[2],reverse = True)
        p = self.head.next
        for i in prise:
            p.data = i
            p = p.next

    def revise(self,a):
        p = self.head.next
        count = 0
        sum = 0
        while p is not None:
            sum = sum+p.data[2]
            p = p.next
            count+=1
        avg_prise = sum/count
        p = self.head.next
        print("平均价格为",avg_prise)
        while p is not None:
            if p.data[2] < avg_prise:
                p.data[2] = p.data[2]*1.2
            else:p.data[2] = p.data[2]*1.1
            p = p.next

    def find_exp(self,a):
        self.ListSort(a)
        p = self.head.next
        count = 1

        while p is not None:
            if p.data[2] == p.next.data[2]:
                count = count+1
                p = p.next
            elif p.data[2] > p.next.data[2]:
                break

        print("价格最高的图书可能有%d本" % count)
        m = self.head.next
        print("书号\t书名\t价格")
        for j in range(count):
            for i in range(2):
                print(m.data[i], end='\t')
            print("%.2f" % p.data[2])
            m = m.next

    def reverse(self,a):
        cur = self.head.next.next
        self.head.next.next = None
        while(cur != None):
            next = cur.next
            cur.next = self.head.next
            self.head.next = cur
            cur = next

    def find_like(self,a):
        p = self.head.next
        m = self.head.next                  #用变量储存头指针
        target = []
        count = 0
        name_list = []
        times = input("请输入查找次数")
        for i in range(int(times)):
            name = input("请输入第%d次待查找的图书名字" % (i + 1))
            name_list.append(name)
        for j in range(int(times)):
            while p is not None:
                if p.data[1] == name_list[j]:
                    count = count + 1
                    target.append(p.data)
                    p = p.next
                else:p = p.next
            if count > 0:
                print("第%d次查找" % (j + 1))
                print("此书有%d本" % count)
                for o in target:
                    for j in range(2):
                        print(o[j], end='\t')
                    print("%.2f" %o[2])
                target.clear()
                count = 0
                p = m                           #指针重新指向头结点
            else:
                print("抱歉,第%d次查找中没有你的最爱" % (j + 1))
                p = m

    def find_place(self,a,num):
        p = self.head.next
        m = self.head.next                     #用变量储存头指针
        count = 1
        index_list = []
        times = input("请输入查找次数")
        for i in range(int(times)):
            index = input("请输入第%d次待查找的图书位置序号" % (i + 1))
            index_list.append(index)
        for j in index_list:
            if int(j) > num or int(j) < 1:
                print("抱歉,最佳位置上的图书不存在!")
            else:
                while p is not None:
                    if count == int(j):
                        for i in range(2):
                            print(p.data[i], end='\t')
                        print("%.2f" % p.data[2])
                        p = m                      #让指针重新指向头结点
                        count = 1
                        break
                    else:
                        count = count+1
                        p = p.next

    def book_insert(self,a,num):
        b = []
        m = self.head                                                                  #记录目标节点的前驱节点
        count = 1
        index = int(input("请输入插入位置序号"))
        information = input("请输入插入图书信息")
        information_list = information.split()
        shuhao = information_list[0]
        shuming = information_list[1]
        jiage = information_list[2]
        b.append([shuhao, shuming, float(jiage)])
        node = LinkNode(b[0])
        if index < 1 or index > num+2:
            print("抱歉,入库位置非法")
        else:
            while m is not None:
                if index == count:
                    node.next = m.next
                    m.next = node
                    break

                else:
                    count = count+1
                    m = m.next

        self.display(a)

    def book_delete(self,a,num):

        m = self.head                 #记录目标节点的前驱节点
        count = 1
        index = int(input("请输入删除位置序号"))
        if index < 1 or index > num+1:
            print("抱歉,出库位置非法")
        else:
            while m is not None:
                if index == count:
                    m.next = m.next.next
                    break

                else:
                    count = count+1
                    m = m.next
        self.display(a)

    def quchong(self,a,num):
        target = []
        target_3 = []
        m = self.head.next
        p = self.head.next.next
        index_2 = 2                  #记录p的起点位置
        index = 2
        for i in range(num-2):       #控制m只指向倒数第二个节点,防止p指空
            while p is not None:
                if m.data[0] == p.data[0]:
                    target.append(index)
                    index = index+1
                    p = p.next
                else:
                    index = index+1
                    p = p.next
            m = m.next
            p = m.next
            index_2 = index_2+1
            index = index_2

        target_2 = set(target)
        print("去重后图书数目为", num-len(target_2))
        count_2 = 1
        o = self.head
        for i in target_2:
            while o is not None:
                if i == count_2:
                    o.next = o.next.next
                    break

                else:
                    count_2 = count_2 + 1
                    o = o.next
        self.display(a)

a = []
num = int(input("请输入图书数目"))
information = input("请输入图书信息,每本图书信息占一行,书号、书名、价格用空格分隔。输入0 0 0结束输入\n")
try:

    while information != '0':
        information_list = information.split()
        shuhao = information_list[0]
        shuming = information_list[1]
        jiage = information_list[2]
        a.append([shuhao,shuming,float(jiage)])
        information = input("请再次输入")
except ValueError:
    print("输入错误")
except IndexError:
    print("输入错误")

A=LinkList()
A.CreatList_wei(a)
A.quchong(a,num)












 

你可能感兴趣的:(python)