-
Notifications
You must be signed in to change notification settings - Fork 59
/
text_based_calculator.py
130 lines (103 loc) · 2.53 KB
/
text_based_calculator.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
117
118
119
120
121
122
123
124
125
126
127
128
#########################################
# Text based calculator coding project:
# Due: 05/21/2019 at 10:30 am
#
# Write a simple text based calculator
# that prompts the user for two input,
# and prints out the correct calculations.
#
# The operations to model are:
# addition
# subtraction
# multiplication
# division
# modulus
# square root
# exponential
# basic trigonometry: sin(), cos(), tan()
# shows results both in radians and degrees
# logarithm
#
# By Doug Purcell
# http://www.purcellconsult.com
#
#########################################
from math import sin, cos, tan
from math import radians
from math import log
from math import sqrt
'''
addition operation
'''
a1 = float(input('Addition. Enter first number '))
a2 = float(input('Enter second number '))
a3 = a1 + a2
print(a1, '+', a2, '=', a3)
'''
subtraction operation
'''
a1 = float(input('Subtraction: Enter first number '))
a2 = float(input('Enter second number '))
a3 = a1 - a2
print(a1, '-', a2, '=', a3)
'''
multiplication operation
'''
a1 = float(input('Multiplication: Enter first number '))
a2 = float(input('Enter second number '))
a3 = a1 * a2
print(a1, 'x', a2, '=', a3)
'''
division operation
'''
a1 = float(input('Division: Enter first number '))
a2 = float(input('Enter second number '))
a3 = a1 / a2
print(a1, '/', a2, '=', a3)
'''
modulus operation
'''
a1 = float(input('Modulus: Enter first number '))
a2 = float(input('Enter second number '))
a3 = a1 % a2
print(a1, '%', a2, '=', a3)
'''
square root operation
'''
a1 = float(input('Square root: Enter number to take square root of '))
print('√', a1, '=', sqrt(a1))
'''
Exponentiation
'''
b = float(input("Exponentiation. Enter a number for the 'base' "))
n = int(input('Enter the power '))
result = b ** n
print(b, '^', n, '=', result)
'''
basic trigonometry: sin(), cos(), trig:
in both radians and degrees
'''
a = float(input('Enter number to compute, sin, cos, and tan of '))
# calculate degrees
sine_degs = sin(radians(a))
cosine_degs= cos(radians(a))
tangent_degs = tan(radians(a))
# calculate radians
sine_rads = sin(a)
cosine_rads = cos(a)
tangent_rads = tan(a)
print(sine_degs, '° sine')
print(sine_rads, 'r sine')
print(cosine_degs, '° cosine')
print(cosine_rads, 'r cosine')
print(tangent_degs, '° tangent')
print(tangent_rads, 'r tangent')
'''
Implement the logarithm. In mathematics the
logarithm is the inverse function of
exponentiation.
'''
x = float(input('Logarithm. Enter the value of x '))
base = float(input('Enter the base '))
logarithm = log(x, base)
print('log base', base, 'of', x, '=', logarithm)