Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.4 kB
5
Indexable
Never
try: #Start of try and exception
    print()
    class StudentRecord: #Start of declaring Student Record Structure
        def __init__(self, name, marks, school):
            self.name = name
            self.marks = marks
            self.school = school
    
    stack = ['' for i in range(2)] #Declaring length of array
    stackfull = len(stack)
    basePointer = 0
    topPointer = -1
    
    def push(item): #Start of function
        global topPointer #Declaring pointer globally for assigning
        if topPointer < stackfull - 1:
            topPointer = topPointer + 1
            stack[topPointer] = item
        else:
            print("Stack is full, cannot push more than stack length")
    
    yesno = int(input("Would you like to push any records into the stack?\n1 for Yes\n2 for No\nEnter here: ")) #Asking if any records need to be pushed
    if yesno == 1: #Start of iteration
        print()
        lenitems = int(input("How many records do you want to push?: ")) #Asking how many records need to be pushed
        while lenitems > 0: #Start of while iteration to input user data
            print()
            name = input("Enter name: ")
            marks = input("Enter marks: ")
            school = input("Enter school: ")
            item = StudentRecord(name, marks, school) #Data assignment
            push(item) #Calling function
            lenitems = lenitems - 1
    
    if yesno == 1: #Start of iteration to re-print pushed data
        print()
        print("Stack after item push --->")
        for item in stack:
            if item != '':
                print()
                print("Name:", item.name)
                print("Marks:", item.marks)
                print("School:", item.school)
                print()
    
    def pop(): #Start of function
        global topPointer #Declaring pointer globally for assigning
        if topPointer == basePointer - 1:
            print("Stack is empty, cannot pop more records")
        else:
            item = stack[topPointer]
            stack[topPointer] = ''
            topPointer = topPointer - 1
            print()
            print("Popped record --->")
            print()
            print("Name:", item.name)
            print("Marks:", item.marks)
            print("School:", item.school)
    
    yesno2 = int(input("Would you like to pop any records in the stack?\n1 for Yes\n2 for No\nEnter here: ")) #Asking user whether any data needs to be popped or not
    if yesno2 == 1: #Iteration
        lenitems2 = int(input("How many records do you want to pop?: "))
        for i in range(lenitems2):
            pop()
    
    if yesno2 == 1: #Iteration after pop
        print()
        print("Stack after record pop --->")
        for item in stack:
            if item != '':
                print()
                print("Name:", item.name)
                print("Marks:", item.marks)
                print("School:", item.school)
                print()
    
    with open("C:\\Users\A2-B2-CS12\Desktop\mainfile.txt", "w") as file: #Opening file for write
        for item in stack: #Item in index array
            if item != '': #Iteration if not empty
                file.write("Name: " + item.name + "\n") #Writing if true
                file.write("Marks: " + item.marks + "\n")
                file.write("School: " + item.school + "\n")
                file.write("\n")
                
    f = open("C:\\Users\A2-B2-CS12\Desktop\mainfile.txt", "r") #Opening file for read
    print()
    print("The final record/s written in the file is output below")
    print()
    print(f.read())  #Outputting file data

    with open("C:\\Users\A2-B2-CS12\Desktop\datafile.txt", "w") as sortingfile: #Opening file for write
        file.write("\n")

    # def insertionSort(arraysort):
    #     n = len(arraysort)      
    #     if n <= 1:
    #         return   
    #     for i in range(1, n): 
    #         key = arraysort[i]  
    #         j = i-1
    #         while j >= 0 and key < arraysort[j]:  
    #             arraysort[j+1] = arraysort[j]  
    #             j -= 1
    #         arraysort[j+1] = key  
    
    # arraysort = [1]
    # insertionSort(arraysort)
    # print(arraysort)
    
except Exception as err: #End of try and except
    print("An error occurred:", str(err))