-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·112 lines (94 loc) · 2.91 KB
/
main.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python3.4m
import Big_integer
import sys
import argparse
def getFromFiles (fileA, fileB, fileMod, binary):
A = Big_integer.Big_integer()
B = Big_integer.Big_integer()
modulus = Big_integer.Big_integer()
if (binary):
if not A.ReadFromBin(fileA):
print("Can't get number from:", fileA)
return False, A, B, modulus
if not B.ReadFromBin(fileB):
print("Can't get number from:", fileB)
return False, A, B, modulus
if fileMod:
if not modulus.ReadFromBin(fileMod):
print("Can't get number from:", fileMod)
return False, A, B, modulus
else:
if not A.ReadFromTextFile(fileA):
print("Can't get number from:", fileA)
return False, A, B, modulus
if not B.ReadFromTextFile(fileB):
print("Can't get number from:", fileB)
return False, A, B, modulus
if fileMod:
if not modulus.ReadFromTextFile(fileMod):
print("Can't get number from:", fileMod)
return False, A, B, modulus
return True, A, B, modulus
def process(A, B, modulus, operation):
zero = Big_integer.Big_integer("0")
res = zero
if (modulus < zero):
print("Negative modulus!")
return False, res
if operation == '^':
res = Big_integer.Pow(A, B, modulus)
return True, res
if (modulus > zero):
A %= modulus
B %= modulus
if operation == '+':
res = A + B
elif operation == '-':
res = A - B
elif operation == '*':
res = A * B
elif operation == '/':
if B == zero:
print("Division by zero")
return False, res
res = A / B
elif operation == '%':
if (B == zero):
print("Division by zero")
return False, res
res = A % B
if (modulus > zero):
res %= modulus
while (res < zero):
res += modulus
return True, res
def saveRes(fileRes, binary, res):
if binary:
if not res.WriteToBin(fileRes):
print("Can't save result to ", fileRes)
return False
else:
if not res.WriteToTextFile(fileRes):
print("Can't save result to ", fileRes)
return False
return True
def main():
parser = argparse.ArgumentParser(prog = "Big_integer")
parser.add_argument('A_filename', help = "filename of file with 1-st big integer")
parser.add_argument('operation', choices=['+', '-', '*', '/', '%', '^'], help = "operations")
parser.add_argument('B_filename', help = "filename of file with 2-nd big integer")
parser.add_argument('result_filename', help = "filename of result file")
parser.add_argument('-b', action='store_const', const=True, default=False, help = "perform binary files")
parser.add_argument('mod_filename', help = "filename of file with modulus", nargs='?')
arguments = parser.parse_args()
isRead, A, B, modulus = getFromFiles (arguments.A_filename, arguments.B_filename, arguments.mod_filename, arguments.b)
if not isRead:
sys.exit(-1)
isProcess, res = process(A, B, modulus, arguments.operation)
if not isProcess:
sys.exit(-1)
isWrite = saveRes(arguments.result_filename, arguments.b, res)
if not isWrite:
sys.exit(-1)
if __name__ == '__main__':
main()