-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fraction_operation.py
116 lines (98 loc) · 3.4 KB
/
Fraction_operation.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
113
114
115
116
import re,time
def add(first, second):
ans=int(first [1])* int(second[1])
final= int((int(ans)/int(first[1]))*int(first[0]))+int((int(ans)/int(second[1]))*int(second[0]))
for i in range(1,ans):
if ans%i==0 and final%i==0:
ans=int(ans/i)
final=int(final/i)
if ans == final:
ans=1
final=1
time.sleep(1)
print("The addition answer is {}/{}".format(final,ans))
def sub(first, second):
ans=int(first [1])* int(second[1])
final= int((int(ans)/int(first[1]))*int(first[0]))-int((int(ans)/int(second[1]))*int(second[0]))
for i in range(1,ans):
if ans%i==0 and final%i==0:
ans=int(ans/i)
final=int(final/i)
if ans == final:
ans=1
final=1
time.sleep(1)
print("The subtraction answer is {}/{}".format(final,ans))
def multiply(first, second):
ans=int(first [1])* int(second[1])
final= int((first[0]))*(int(second[0]))
for i in range(1,ans):
if ans%i==0 and final%i==0:
ans=int(ans/i)
final=int(final/i)
if ans == final:
ans=1
final=1
time.sleep(1)
print("The multiplication answer is {}/{}".format(final,ans))
def divide(first, second):
ans=int(first [1])* int(second[0])
final= int((first[0]))*int(second[1])
for i in range(1,ans):
if ans%i==0 and final%i==0:
ans=int(ans/i)
final=int(final/i)
if ans == final:
ans=1
final=1
time.sleep(1)
print("The division answer is {}/{}".format(final,ans))
def start():
try:
print("Welcome to fraction operator made by Jovine....")
pattern=r"(\-?\d+)(/)?(\-?\d+)?"
user_frac1=(input("Enter first fraction: "))
user_frac2=input("Enter second fraction: ")
first=[]
second=[]
match1=re.search(pattern,user_frac1)
match2=re.search(pattern, user_frac2)
print("Enter.....")
time.sleep(1)
print("1 for addition........")
print("2 for subtraction.........")
print("3 for multiplication......")
print("4 for division..............")
print("0 for all operations......\n")
user=int(input("Enter option for operation: "))
if match1 and match2:
first.insert(0,int(match1.group(1)))
second.insert(0,int(match2.group(1)))
if match1.group(3)==None:
first.insert(1,1)
if match2.group(3)==None:
second.insert(1,1)
if match1.group(3) != None:
first.insert(1,int(match1.group(3)))
if match2.group(3) != None:
second.insert(1,int(match2.group(3)))
if user==1:
add(first, second)
elif user==2:
sub(first, second)
elif user==3:
multiply (first, second)
elif user==4:
divide(first, second)
elif user==0:
add(first, second)
sub(first, second)
multiply (first, second)
divide (first, second)
else:
print ("Invalid input....")
time.sleep(1)
print("Good bye......")
except (TypeError,IndexError):
print ("Error.....")
start()