-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator3.html
executable file
·162 lines (103 loc) · 2.88 KB
/
calculator3.html
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<html>
<head>
<!-- Name- Payel Bandyopadhyay, Student Number - 014174692 -->
<script type='text/javascript'>
function calculator1()
{
elem1 = document.getElementById('numbers');
var elem2 = elem1.value;
var check1 = elem2.match(/\([^\(\)]*\)/);
if ( check1 != null)
{
var p2 = cal1(check1[0]);
var rep123 = elem2.replace(check1, p2);
var check3 = rep123.match(/\([^\(\)]*\)/);
while (check3 != null)
{
p2 = cal1(check3[0]);
rep123 = rep123.replace(check3, p2);
check3 = rep123.match(/\([^\(\)]*\)/);
}
if(check3 == null)
{
var p2 = cal1(rep123);
}
}
else
{
var p3 = cal1(elem2);
}
}
function cal1(elem)
{
var elemOne = elem;
elemOne = calculator(elemOne);
var check2 = elemOne.match(/^-?[0-9]+\.?[0-9]*[+\-\/\*]\-?[0-9]*\.?[0-9]*/);
while (check2 != null)
{
elemOne = calculator(elemOne);
check2 = elemOne.match(/-?[0-9]+\.?[0-9]*[+\-\/\*]-?[0-9]*\.?[0-9]*/);
}
if(check2 == null)
{
return elemOne;
}
}
function calculator(e1)
{
var result;
var argument1;
var operator1;
var argument2;
var check;
result = e1.match(/-?[0-9]+\.?[0-9]*[+\-\/\*]-?[0-9]+\.?[0-9]*/);
if(result !== null ) {
var res = result[0].match(/^\-/);
if(res !== null)
{
var res1 = result[0].substring(1, result[0].length);
}
else
{
res1 = result[0];
}
argument1 = result[0].match(/^-?[0-9]+\.?[0-9]*/);
operator1 = res1.match(/[^0-9\.]/);
argument2 = res1.match(/[+\-\*\/]([\-?0-9]+\.?[0-9]*)/);
argument2[0] = argument2[0].substring(1, argument2[0].length);
var p1 = ajaxFunction(argument1[0],operator1[0],argument2[0],e1);
e1 = p1.match(/[^()\s]+/).toString();
}
return e1;
}
function ajaxFunction(arg1,op,arg2,element)
{
var xmlhttp= new XMLHttpRequest();
xmlhttp.open("GET","calc.php?arg1="+arg1+"&arg2="+arg2+"&op="+op,false);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState===4 && xmlhttp.status===200)
{
var newDiv = document.createElement("div");
newDiv.innerHTML = xmlhttp.responseText;
var nextElement = document.getElementById("numbers");
parentnode = nextElement.parentNode;
parentnode.insertBefore(newDiv, nextElement);
}
}
xmlhttp.send();
var response1 = xmlhttp.responseText;
var n = response1.indexOf("=");
response1 = response1.substring(n + 1, response1.length );
var rep = element.replace(arg1+op+arg2, response1);
return rep;
}
</script>
</head>
<body>
<form>
</br> <input type='text' id='numbers'/>
<input type='button' onclick="calculator1()" value='Submit' />
</form>
</body>
</html>