Skip to content

Instantly share code, notes, and snippets.

@exclusiveTanim
Created April 5, 2014 15:49
Show Gist options
  • Save exclusiveTanim/9993594 to your computer and use it in GitHub Desktop.
Save exclusiveTanim/9993594 to your computer and use it in GitHub Desktop.
MY CALCULATOR
/*ALl Logical and graphical codes for Calculator*/
var self = Ti.UI.createWindow({
title : "GENERAL CALCULATOR",
backgroundColor : '#000',
layout : 'vertical',
orientationModes : [Ti.UI.PORTRAIT]
});
// Create a TextField.
var main = Ti.UI.createTextArea({
height : 60,
width : 320,
textAlign : Ti.UI.TEXT_ALIGNMENT_RIGHT,
backgroundColor : 'gray',
font : {
fontSize : 30,
fontWeight : 'bold'
},
value : "0",
maxLength : 16,
//name:"display",
//softKeyboardOnFocus : Ti.UI.Android.SOFT_KEYBOARD_DEFAULT_ON_FOCUS, // Android only
//keyboardType : Ti.UI.KEYBOARD_DEFAULT,
//returnKeyType : Ti.UI.RETURNKEY_DEFAULT,
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
});
self.add(main);
// Listen for return events.
main.addEventListener('click', function(e) {
main.blur();
});
/// AGAIN FUNCTION
function addChar(input, character) {
//alert(input); alert(character);
if (main.value == null || main.value == "0")
main.value = character;
else
main.value += character;
}
function deleteChar(input) {
main.value = main.value.substring(0, main.value.length - 1);
}
function compute(form) {
var str=main.value;
if(ch=="^")
alert("power");
else
main.value = eval(main.value);
main.blur();
}
function expr(form) {
main.value = Math.exp(main.value);
main.blur();
}
function square(form) {
main.value = eval(main.value) * eval(main.value);
main.blur();
}
function sqrt(form) {
main.value = Math.sqrt(main.value);
main.blur();
}
function changeSign(input) {
if (input.substring(0, 1) == "-")
main.value = input.substring(1, main.value.length);
else
main.value = "-" + main.value;
main.blur();
}
function percentval() {
main.value = eval(main.value) / (100);
main.blur();
}
function fact() {
var n = eval(main.value);
var k=1;
for (var i = 1; i <= n; i++) {
k=k*i;
}
main.value=k;
main.blur();
}
function coss(form) {
main.value = Math.cos(main.value);
}
function sins(form) {
main.value = Math.sin(main.value);
}
function tans(form) {
main.value = Math.tan(main.value);
}
function lns(form) {
main.value = Math.log(main.value);
}
function powerval() {
}
function checkNum(str) {
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i + 1);
if (ch < "0" || ch > "9") {
if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "." && ch != "(" && ch != ")" && ch != "^") {
alert("invalid entry!");
return false;
}
}
}
return true;
}
//--------VIEW sin cos-------------
var viewlogic = Titanium.UI.createView({
//borderRadius : 10,
backgroundColor : 'black',
height : 50,
top : 10,
//left:10,
layout : 'horizontal'
});
self.add(viewlogic);
// Create a Button.
var sin = Ti.UI.createButton({
title : 'sin',
height : 50,
width : 64,
value : 'sin'
});
sin.addEventListener('click', function(e) {
if (checkNum(main.value)) {
sins(main);
}
});
// Add to the parent view.
viewlogic.add(sin);
// Create a Button.
var cos = Ti.UI.createButton({
title : 'cos',
height : 50,
width : 64,
value : 'cos'
});
cos.addEventListener('click', function(e) {
if (checkNum(main.value)) {
coss(main);
}
});
// Add to the parent view.
viewlogic.add(cos);
// Create a Button.
var tan = Ti.UI.createButton({
title : 'tan',
height : 50,
width : 64,
value : 'tan'
});
tan.addEventListener('click', function(e) {
if (checkNum(main.value)) {
tans(main);
}
});
// Add to the parent view.
viewlogic.add(tan);
// Create a Button.
var ln = Ti.UI.createButton({
title : 'ln',
height : 50,
width : 64,
//backgroundColor:'red',
value : 'ln'
});
ln.addEventListener('click', function(e) {
if (checkNum(main.value)) {
lns(main);
}
});
// Add to the parent view.
viewlogic.add(ln);
// Create a Button.
var log = Ti.UI.createButton({
title : 'log',
height : 50,
width : 64,
//backgroundColor:'red',
value : 'log'
});
log.addEventListener('click', function(e) {
});
// Add to the parent view.
viewlogic.add(log);
//---------END VIEW sin cos----------
//--------VIEW NpR-------------
var viewnCr = Titanium.UI.createView({
//borderRadius : 10,
backgroundColor : 'black',
height : 50,
top : 5,
//left:10,
layout : 'horizontal'
});
self.add(viewnCr);
// Create a Button.
var yx = Ti.UI.createButton({
title : ' y^x',
height : 50,
width : 64,
value : 'y^x'
});
yx.addEventListener('click', function(e) {
powerval();
addChar(main.value, '^');
main.blur();
});
// Add to the parent view.
viewnCr.add(yx);
// Create a Button.
var fac = Ti.UI.createButton({
title : 'n!',
height : 50,
width : 64,
value : 'n!'
});
fac.addEventListener('click', function(e) {
fact();
});
// Add to the parent view.
viewnCr.add(fac);
// Create a Button.
var nc = Ti.UI.createButton({
title : 'nCr',
height : 50,
width : 64,
value : 'nCr'
});
nc.addEventListener('click', function(e) {
});
// Add to the parent view.
viewnCr.add(nc);
// Create a Button.
var np = Ti.UI.createButton({
title : 'nPr',
height : 50,
width : 64,
value : 'nPr'
});
np.addEventListener('click', function(e) {
});
// Add to the parent view.
viewnCr.add(np);
// Create a Button.
var pai = Ti.UI.createButton({
title : 'π',
height : 50,
width : 64,
//backgroundColor:'red',
value : 'π'
});
pai.addEventListener('click', function(e) {
//percentval();
});
// Add to the parent view.
viewnCr.add(pai);
// Create a Button.
//---------END VIEW NpR----------
//--------VIEW Zero-------------
var view0 = Titanium.UI.createView({
//borderRadius : 10,
backgroundColor : 'black',
height : 50,
top : 5,
//left:10,
layout : 'horizontal'
});
self.add(view0);
// Create a Button.
var sqr = Ti.UI.createButton({
title : '√',
height : 50,
width : 64,
value : '√'
});
sqr.addEventListener('click', function(e) {
//sqrrootval();
if (checkNum(main.value)) {
sqrt(main);
}
});
// Add to the parent view.
view0.add(sqr);
// Create a Button.
var x2 = Ti.UI.createButton({
title : 'x^2',
height : 50,
width : 64,
value : 'x^2'
});
x2.addEventListener('click', function(e) {
if (checkNum(main.value)) {
square(main);
}
});
// Add to the parent view.
view0.add(x2);
// Create a Button.
var pr = Ti.UI.createButton({
title : '%',
height : 50,
width : 64,
//backgroundColor:'red',
value : '%'
});
pr.addEventListener('click', function(e) {
percentval();
});
// Add to the parent view.
view0.add(pr);
var fb = Ti.UI.createButton({
title : '(',
height : 50,
width : 64,
//backgroundColor:'red',
value : '('
});
fb.addEventListener('click', function(e) {
addChar(main.value, '(');
main.blur();
});
// Add to the parent view.
view0.add(fb);
// Create a Button.
var bra = Ti.UI.createButton({
title : ')',
height : 50,
width : 64,
//backgroundColor:'red',
value : 'bra'
});
bra.addEventListener('click', function(e) {
//alert(ac.value);
//reset();
addChar(main.value, ')');
main.blur();
});
// Add to the parent view.
view0.add(bra);
//---------END VIEW0-----------
//--------VIEW ONE-------------
var view1 = Titanium.UI.createView({
//borderRadius : 10,
backgroundColor : 'black',
height : 50,
top : 5,
//left:10,
layout : 'horizontal'
});
self.add(view1);
// Create a Button.
var num7 = Ti.UI.createButton({
title : '7',
height : 50,
width : 64,
value : '7'
});
num7.addEventListener('click', function(e) {
addChar(main.value, '7');
});
// Add to the parent view.
view1.add(num7);
// Create a Button.
var num8 = Ti.UI.createButton({
title : '8',
height : 50,
width : 64,
value : '8'
});
num8.addEventListener('click', function(e) {
addChar(main.value, '8');
main.blur();
});
// Add to the parent view.
view1.add(num8);
// Create a Button.
var num9 = Ti.UI.createButton({
title : '9',
height : 50,
width : 64,
value : '9'
});
num9.addEventListener('click', function(e) {
addChar(main.value, '9');
main.blur();
});
// Add to the parent view.
view1.add(num9);
// Create a Button.
var del = Ti.UI.createButton({
title : 'DEL',
height : 50,
width : 64,
//backgroundColor:'red',
value : 'del'
});
del.addEventListener('click', function(e) {
deleteChar(main.value);
main.blur();
});
// Add to the parent view.
view1.add(del);
// Create a Button.
var ac = Ti.UI.createButton({
title : 'AC',
height : 42,
width : 58,
backgroundColor : 'red',
value : 'ac'
});
ac.addEventListener('click', function(e) {
main.value = 0;
main.blur();
});
// Add to the parent view.
view1.add(ac);
num7.addEventListener('click', function(e) {
main.blur();
});
//---------END VIEW1-----------
//--------VIEW2 START-------------
var view2 = Titanium.UI.createView({
//borderRadius : 10,
backgroundColor : 'black',
height : 50,
top : 5,
//left:10,
layout : 'horizontal'
});
self.add(view2);
// Create a Button.
var num4 = Ti.UI.createButton({
title : '4',
height : 50,
width : 64,
value : '4'
});
num4.addEventListener('click', function(e) {
addChar(main.value, '4');
main.blur();
});
// Add to the parent view.
view2.add(num4);
// Create a Button.
var num5 = Ti.UI.createButton({
title : '5',
height : 50,
width : 64,
value : '5'
});
num5.addEventListener('click', function(e) {
addChar(main.value, '5');
main.blur();
});
// Add to the parent view.
view2.add(num5);
// Create a Button.
var num6 = Ti.UI.createButton({
title : '6',
height : 50,
width : 64,
value : '6'
});
num6.addEventListener('click', function(e) {
addChar(main.value, '6');
main.blur();
});
// Add to the parent view.
view2.add(num6);
// Create a Button.
var mul = Ti.UI.createButton({
title : '*',
height : 50,
width : 64,
value : '*'
});
mul.addEventListener('click', function(e) {
addChar(main.value, '*');
main.blur();
});
// Add to the parent view.
view2.add(mul);
// Create a Button.
var div = Ti.UI.createButton({
title : '/',
height : 50,
width : 64,
value : '/'
});
div.addEventListener('click', function(e) {
addChar(main.value, '/');
main.blur();
});
// Add to the parent view.
view2.add(div);
//---------END VIEW2-----------
//--------VIEW3 START-------------
var view3 = Titanium.UI.createView({
//borderRadius : 10,
backgroundColor : 'black',
height : 50,
top : 5,
//left:10,
layout : 'horizontal'
});
self.add(view3);
// Create a Button.
var num1 = Ti.UI.createButton({
title : '1',
height : 50,
width : 64,
value : '1'
});
num1.addEventListener('click', function(e) {
addChar(main.value, '1');
main.blur();
});
// Add to the parent view.
view3.add(num1);
// Create a Button.
var num2 = Ti.UI.createButton({
title : '2',
height : 50,
width : 64,
value : '2'
});
num2.addEventListener('click', function(e) {
addChar(main.value, '2');
main.blur();
});
// Add to the parent view.
view3.add(num2);
// Create a Button.
var num3 = Ti.UI.createButton({
title : '3',
height : 50,
width : 64,
value : '3'
});
num3.addEventListener('click', function(e) {
addChar(main.value, '3');
main.blur();
});
// Add to the parent view.
view3.add(num3);
// Create a Button.
var add = Ti.UI.createButton({
title : '+',
height : 50,
width : 64,
value : '+'
});
add.addEventListener('click', function(e) {
addChar(main.value, '+');
main.blur();
});
// Add to the parent view.
view3.add(add);
// Create a Button.
var min = Ti.UI.createButton({
title : '-',
height : 50,
width : 64,
value : '-'
});
min.addEventListener('click', function(e) {
//alert(min.value);
//operator("-");
addChar(main.value, '-');
main.blur();
});
// Add to the parent view.
view3.add(min);
//---------END VIEW3-----------
//--------VIEW4 START-------------
var view4 = Titanium.UI.createView({
//borderRadius : 10,
backgroundColor : 'black',
height : 50,
top : 5,
//left:10,
layout : 'horizontal'
});
self.add(view4);
// Create a Button.
var num0 = Ti.UI.createButton({
title : '0',
height : 50,
width : 64,
value : '0'
});
num0.addEventListener('click', function(e) {
addChar(main.value, '0');
main.blur();
});
// Add to the parent view.
view4.add(num0);
// Create a Button.
var pm = Ti.UI.createButton({
title : '+/-',
height : 50,
width : 64,
value : '+/-'
});
// Add to the parent view.changeSign(this.form.display)
pm.addEventListener('click', function(e) {
changeSign(main.value);
main.blur();
});
view4.add(pm);
// Create a Button.
var dot = Ti.UI.createButton({
title : '.',
height : 50,
width : 64,
value : '.'
});
dot.addEventListener('click', function(e) {
//displaynum(".");
addChar(main.value, '.');
main.blur();
});
// Add to the parent view.
view4.add(dot);
// Create a Button.
var exp = Ti.UI.createButton({
title : 'EXP',
height : 50,
width : 64,
value : 'exp'
});
// Add to the parent view.
exp.addEventListener('click', function(e) {
//equals();
if (checkNum(main.value)) {
expr(main);
}
});
view4.add(exp);
var eq = Ti.UI.createButton({
title : '=',
height : 50,
width : 64,
value : '='
});
eq.addEventListener('click', function(e) {
//equals();
if (checkNum(main.value)) {
compute(main);
}
main.blur();
});
// Add to the parent view.
view4.add(eq);
//---------END VIEW4-----------
self.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment