-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineEditor.py
More file actions
68 lines (56 loc) · 2.11 KB
/
LineEditor.py
File metadata and controls
68 lines (56 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from ArrayList import ArrayList
# 배열구조의 리스트를 이용한 라인 편집기 프로그램
list = ArrayList(1000)
while True :
command = input("[메뉴선택] i-입력, d-삭제, r-변경, p-출력, l-파일읽기, s-저장, q-종료=> ")
if command == 'i' :
pos = int( input(" 입력행 번호: ") )
str = input(" 입력행 내용: ")
list.insert(pos, str)
elif command == 'd' :
pos = int( input(" 삭제행 번호: ") )
list.delete(pos)
elif command == 'r' :
pos = int( input(" 변경행 번호: ") )
str = input(" 변경행 내용: ");
list.replace(pos, str)
elif command == 'p' :
print('Line Editor')
for line in range (list.size) :
print('[%2d] '%line, end='')
print(list.getEntry(line))
print()
elif command == 'q' : exit()
elif command == 'l' :
# filename = input(" 읽어들일 파일 이름: ")
filename = 'test.txt'
infile = open(filename , "r")
lines = infile.readlines();
for line in lines:
list.insert(list.size, line.rstrip('\n'))
infile.close()
elif command == 's' :
# filename = input(" 저장할 파일 이름: ")
filename = 'test.txt'
outfile = open(filename , "w")
len = list.size
for i in range(len) :
outfile.write(list.getEntry(i)+'\n')
outfile.close()
elif command == 'm':
word_count = {}
for i in range(list.size):
words = list.getEntry(i).split() # 공백을 기준으로 단어 분리
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
filename = 'dic.txt'
with open(filename, "w") as outfile:
for word, count in word_count.items():
outfile.write(f"{word}: {count}\n")
# dic.txt 파일에 있는 내용을 출력
print("\n[dic.txt 파일 출력]")
with open(filename, "r") as infile:
print(infile.read())