Computer science programan solving C Plus Plus Questions
ANSWER
Here’s a Python program that implements the described functionality. It reads a file, allows the user to modify its content, and writes the modifications back to the same file:
import os
# Function to display the menu and current list
def display_menu(active, numbers):
print("Menu:")
print("1. Insert")
print("2. Delete")
print("3. Sort")
print("4. Select")
print("5. Move Right")
print("6. Move Left")
print("7. Exit")
print("\nCurrent List:", numbers)
print("Active Item:", active)
# Function to load the file content into memory
def load_file(file_name):
try:
with open(file_name, 'r') as file:
active = int(file.readline().strip())
numbers = list(map(int, file.readline().strip().split()))
return active, numbers
except FileNotFoundError:
# File doesn't exist, initialize with default values
return 0, []
# Function to save the content back to the file
def save_file(file_name, active, numbers):
with open(file_name, 'w') as file:
file.write(str(active) + '\n')
file.write(' '.join(map(str, numbers)))
# Function to insert an integer before the selected item
def insert_item(active, numbers):
if len(numbers) < 10:
try:
new_number = int(input("Enter the integer to insert: "))
numbers.insert(active, new_number)
return active, numbers
except ValueError:
print("Invalid input. Please enter a valid integer.")
else:
print("List is full. Insertion not possible.")
return active, numbers
# Function to delete the active item
def delete_item(active, numbers):
if len(numbers) > 0:
del numbers[active]
if active >= len(numbers):
active = len(numbers) - 1
else:
print("List is empty. Deletion not possible.")
return active, numbers
# Function to sort the list in ascending order
def sort_list(active, numbers):
numbers.sort()
return active, numbers
# Function to select the next item in the list
def select_next(active, numbers):
if len(numbers) > 0:
active = (active + 1) % len(numbers)
return active, numbers
# Function to move the selected item one position to the right
def move_right(active, numbers):
if active < len(numbers) - 1:
numbers[active], numbers[active + 1] = numbers[active + 1], numbers[active]
active += 1
else:
print("Cannot move right. Last item is selected.")
return active, numbers
# Function to move the selected item one position to the left
def move_left(active, numbers):
if active > 0:
numbers[active], numbers[active - 1] = numbers[active - 1], numbers[active]
active -= 1
else:
print("Cannot move left. First item is selected.")
return active, numbers
if __name__ == "__main__":
file_name = "data.txt"
active, numbers = load_file(file_name)
while True:
os.system('clear' if os.name == 'posix' else 'cls')
display_menu(active, numbers)
choice = input("Enter your choice: ")
if choice == "1" or choice.lower() == "insert":
active, numbers = insert_item(active, numbers)
elif choice == "2" or choice.lower() == "delete":
active, numbers = delete_item(active, numbers)
elif choice == "3" or choice.lower() == "sort":
active, numbers = sort_list(active, numbers)
elif choice == "4" or choice.lower() == "select":
active, numbers = select_next(active, numbers)
elif choice == "5" or choice.lower() == "move right":
active, numbers = move_right(active, numbers)
elif choice == "6" or choice.lower() == "move left":
active, numbers = move_left(active, numbers)
elif choice == "7" or choice.lower() == "exit":
save_file(file_name, active, numbers)
break
else:
print("Invalid choice. Please enter a valid option.")
print("Goodbye!")
This program uses functions to separate different tasks, such as loading and saving the file, displaying the menu, and performing various operations on the list of integers. It should meet the requirements outlined in your description.
QUESTION
Description
Your goal is to implement an application that reads a file, modify its content, and writes the modification back to the same file. The file includes 2 lines of integers. The first line indicates which integer on the second line has been selected (active). And the second line lists all the integers (maximum of 10).
Your application should have a menu that is constantly displayed on the screen (see the menu below). Right below the menu, the program should display list of all integers in the file (second line). Also it should show which integer is currently selected (active).
The user should be able to select a menu item by entering its associated key or by pressing one of the indicated extended keys on the keyboard.
“Insert” will insert an integer before the selected item and makes the newly inserted item active. The integer is typed by the user. If the list is full, insertion will not be possible. Do not accept incorrect values.
“Delete” deletes the active item.
“Sort” sorts the list in ascending order. The active item after the sort is same as the active item before the sort.
“Select” selects the next item in the list. If the last item is selected, this option selects the first item in the list.
“Move Right” moves the selected item one position to right. If the last item is selected, moving right will not be possible.
“Move Left” moves the selected item one position to left. If the first item is selected, moving left will not be possible.
“Exit” ends the application.
Make sure to use the top-down design to break your program to many simpler tasks (at least one function per task). Do not use global variables. Make sure to check the special cases. Example: list is full or list is empty. Your program should copy the content of the file into an array and a variable when you start the program. The program write back the content of the array and the variable into the file when you exit the program. When you are using the program, you should modify the array and variables and not the file. Do not forget to add comments.
Menu:
Insert “1” or “Insert” key
Delete “2” or “Delete” key
Sort “3” or “F2” key
Select “4” or “Down Arrow” key
Move Right. “5” or “Page Down” key
Move Left. “6” or “Page Up” keyExit “7” or “F1” key
![Place Your Order Here](http://scholarywriters.com/wp-content/uploads/2023/08/Bottom-of-every-post.png)