জাভাস্ক্রিপ্ট দিয়ে সহজে বানিয়ে নিন সাইন্টিফিক ক্যালকুলেটর

0
406

আজ আমরা এই টিউন থেকে শিখবো, কীভাবে জাভাস্ক্রিপ্ট দিয়ে সহজে সাইন্টিফিক ক্যালকুলেটর বানানো যায় । আমাদের এই অ্যাপ দিয়ে সাইন্টিফিক  ক্যালকুলেটর  এর মতোই সব ধরনের হিসাব নিকাশ  করা যাবে ।

যদি আপনি আপনার নিজের সাইন্টিফিক ক্যালকুলেটর নিজেই বানাতে পারেন তাহলে কেমন লাগবে আপনার  একবার ভেবে দেখেন ! আমাদের অনেকের  শখ যে, আমরা মোবাইল অ্যাপস, সফটওয়্যার,গেমস তৈরি করবো । চলুন বেশি কথা না বলে দেখা যাক, জাভাস্ক্রিপ্ট দিয়ে সহজে কিভাবে সাইন্টিফিক ক্যালকুলেটর বানানো যায় ।

how to make scientific calculator in javascript

আজ আমরা জাভাস্ক্রিপ্ট ব্যবহার করে একটি সাইন্টিফিক ক্যালকুলেটর তৈরি করতে যাচ্ছি।

এখানে বিভিন্ন বিষয় অন্তর্ভুক্ত রয়েছে:

  • Requirements for building Javascript Scientific Calculator.
  • JavaScript Functions to build Calculator
  • The Visible Section
  • Adding Flavours of CSS

Requirements for building a calculator using JavaScript

  • Integrated Development Environment
  • Local Server/online compiler

আপনি যদি ওয়েবসাইট  জগতে নতুন হন, তবে আপনাকে  জানতে হবে যে  কোথায় গিয়ে  আগে কোডগুলি পরীক্ষা করতে হবে । তার জন্য একটি স্থানীয় সার্ভারের প্রয়োজন ।  আপনি wamp, xampp বা অন্য কোনও সার্ভারের জন্য যেতে পারেন। আপনার কোডগুলি লেখার জন্য, এখানে অনেকগুলি বিকল্প রয়েছে: সাব্লাইম টেক্সট 3, নেটবিনস, বন্ধনী ইত্যাদি  একবার প্ল্যাটফর্ম স্থাপন করার পরে, বাকি কাজগুলি করতে হয় ।

চলুন এখন আমরা সাইন্টিফিক ক্যালকুলেটর বানানোর কোড লিখিঃ–  সাব্লাইম টেক্সট 3 / নেটবিনস /ভিজুয়াল স্টুডিও কোড  যেকোন একটা প্ল্যাটফর্মে ।

 

sublime text editor

প্রথমে আপনি আপনার সাব্লাইম টেক্সট 3 / ভিজুয়াল স্টুডিও কোড / নেটবিনস  যেকোন একটা প্ল্যাটফর্ম ওপেন করবেন । তারপর আমাদের নিচে দেওয়া কোড টা কপি (ctrl+c) করে আপনার প্ল্যাটফর্ম এ  Paste (ctrl+v)  করবেন । তারপর সেভ করবেন (ctrl+s) ।

কোড টি নিচে দেওয়া হলঃ

<!DOCTYPE html>
<html>
<head>
<title>Scientific Calculator</title>
<meta charset=”utf-8″ />
<meta name=”viewport” content=”initial-scale=1.0, maximum-scale=1.0, width=device-width, user-scalable=true” />
<link rel=”stylesheet” href=”calc.css” type=”text/css”>
<script type=”text/javascript” src=”calc.js”></script>
</head>
<body>
<style>
body{
background-image: linear-gradient(to bottom, #33ccff 0%, #ff99cc 100%);
opacity-bottom: 0.4;
}
.container {
width: 300px;
padding:0px 10px;
margin: 0px auto;
}
.red {
color: #678;
}
table {
margin: 0px 5px 5px 5px;
}
button {
width: 50px;
height: 50px;
font-size: 22px;
background-color: rgba(150, 100, 150, 0.5);
color: #fee;
border: 2px #b9b outset;
border-radius: 5px;
outline: none;
cursor: pointer;
}
.top {
height: 30px;
background-color: #009788;
border-radius: 4px;
border: 1px green outset;
color: #fff;
font-size: 16px;
}
#c {
height: 64px;
color: #fff;
font-weight: bold;
}
button:active{
background-color: #dbc;
box-shadow: none ;
padding-top: 4px;
outline: 1px solid #989;
border: 3px #b9b inset;
}
.top:hover{
background-color: #bab;
box-shadow: inset 5px 5px 5px #cab, inset -5px -5px 5px #a98;
}
button:hover{
background-color: #a9a;
}
.top:active{
background-color: #dbc;
box-shadow: none ;
padding-top: 4px;
outline: 1px solid #989;
border: 3px #b9b inset;
}
#display-container {
border: 3px inset;
margin-bottom: 10px;
}
#h1, #display1 {
width: 260px;
height: 25px;
padding: 2px;
padding-top: 4px;
background-color:powderblue;
font-size: 22px;
font-family: sans-serif;
font-weight: bold;
text-align: right;
overflow: hidden;
font-weight:bold;
color: #245;
text-shadow: 2px 2px 1px #8ac;
}
#display1 {
font-size: 20px;
}
.div {
border: 5px #6bc ridge ;
border-radius: 10px;
margin: 0px 5px 5px 5px;
padding-top: 0px;
width: 286px;
background-color: #689;
font-size: 11px;
color: white;
}
.w {
box-shadow: -10px 10px 10px silver;
border: 5px #6bc groove;
margin: 0px 5px 5px 5px;
border-radius: 10px;
padding-top: 0px;
font-size: 11px;
cursor: pointer;
width: 285px;
color: white;
}
}
.disabled, .disabled:active, .disabled:hover {
padding-top: 0px;
background-color: #999;
box-shadow: 0px 0px 0px grey;
border: none;
}
sup {
font-size: .65em;
}
</style>
<div class=”container”>
<div class=”div”>
<table><caption><pre></bsp></caption>
<tr>
<td colspan=”5″><div id=”display-container”><div id=”display1″><small><small><span></span></small></small></div><div id=”h1″><small><small><span></span></small></small></div></div></td>
</tr>
<tr>
<td><button class=”top” onclick=’getElementById(“h1″).innerHTML = bsp()’>Del</button></td>
<td><button class=”top” onclick=’getElementById(“h1″).innerHTML = mp()’>M+</button></td>
<td><button class=”top” onclick=’getElementById(“h1″).innerHTML = mr()’>MR</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = maths(“rand”)’><small>Rnd</small></button></td>
<td rowspan=”2″><button id=”c” class=”top” onclick=’getElementById(“h1”).innerHTML = c(“”)’>C</button></td>
</tr>
<tr>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = maths(“res”)’><sup>1</sup>/<sub>10</sub></button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = maths(“logTen”)’><small>log10</small></sup></button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = maths(“ln”)’>ln</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = PE(“E”)’>e</button></td>
</tr>
<tr>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = maths(“sine”)’>sin</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = maths(“cosine”)’>cos</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = maths(“tangent”)’>tan</sup></button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = PE(“PI”)’>π</button></td>
<td><button class=”top” onclick=’getElementById(“h1″).innerHTML = percent()’>%</button></td>
</tr>
<tr>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = maths(“fact”)’>n!</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = digit(“%”)’>Mod</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = power(“^”)’>x<sup>y</sup></button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = power(“√”)’><sup>y</sup>√x</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = maths(“cubert”)’><sup>3</sup>√</button></td>
</tr>
<tr>
<td><button class=”top disabled” onclic=’getElementById(“h1”).innerHTML = digit(“(“)’>(</button></td>
<td><button class=”top disabled” onclic=’getElementById(“h1”).innerHTML = digit(“)”)’>)</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = base(“bin”)’>bin</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = base(“oct”)’>oct</button></td>
<td><button class=”top” onclick=’getElementById(“h1”).innerHTML = base(“hex”)’>hex</button></td>
</tr>
<tr>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“7”)’> 7</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“8”)’>8</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“9”)’>9</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = operators(“+”)’>+</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = maths(“negpos”)’>-/+</button> </td>
</tr>
<tr>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“4”)’>4</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“5”)’>5</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“6”)’>6</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = operators(“-“)’>-</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = maths(“Sqrt”)’>√</button></td>
</tr>
<tr>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“1”)’>1</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“2”)’>2</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“3”)’>3</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = operators(“/”)’>/</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = maths(“cube”)’>x<sup>3</sup></button></td>
</tr>
<tr>
<td><button onclick=’getElementById(“h1”).innerHTML = dot(“.”)’>.</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = digit(“0”)’>0</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = equal()’>=</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = operators(“*”)’>X</button></td>
<td><button onclick=’getElementById(“h1”).innerHTML = maths(“sqr”)’>x<sup>2</sup></button></td>
</tr>
</table>
</div>
</div>
<script>
var decimalPoint = enter = entered = operatorSign = rootNpower_Sign = flo = math = M = firstI = first = second = secondI = answer = theanswer = result = peSign = “”;
var opsCheck = dotCounter = 0;
var removeFirstZero = “”;
function dot(b) {
if (decimalPoint == “”) {
enter = entered = b;
first+= enter;
entered+= enter;
decimalPoint = “.”;
dotCounter = 0;
return first;
} else {
return first;
}
}
//To control what happen when Pi and Euler is clicked
function PE(b) {
decimalPoint = “.”;
dotCounter = 15;
var cons = b;
if (peSign == “”) {
if (operatorSign != “” && first == “” + operatorSign) {
first = (cons = “PI”) ? Math.PI: Math.E;
} else if (operatorSign != “” && first > 0 || first < 0) {
first = (cons == “PI”) ? first + “*” + Math.PI: first + “*” + Math.E;
} else if (first != “” && operatorSign != “”) {
first += (cons = “PI”) ? Math.PI: Math.E;
} else if (operatorSign == “” && first != “”) {
first = (cons == “PI”) ? first + “*” + Math.PI: first + “*” + Math.E;
} else {
first = (cons == “PI”) ? Math.PI: Math.E;
}
} else if (first !== “”) {
first = (cons == “PI”) ? first + “*” + Math.PI: first + “*” + Math.E;
} else {
first = (cons == “PI”) ? Math.PI: Math.E;
}
return first;
}
function mp() {
M = first;
first = M;
return first ;
}
function mr() {
first = M;
return first ;
}
//Cancel making all variable empty. All variable = “”
function c(c) {
document.getElementById(“display1”).innerHTML = decimalPoint = operatorSign = rootNpower_Sign = entered = math = first = firstI = second = secondI = answer = theanswer = flo = M = “”;
return “”;
}
//To calculate squares, cubes, factorials e.t.c. For calculations which use only one value. Variable ‘firstI’ was isolated by maths() function to be solved here
function mathematics() {
if (math == “sqr”) {
result = firstI * firstI;
} else if (math == “cube”) {
result = firstI * firstI * firstI;
} else if (math == “Sqrt”) {
result = Math.sqrt(firstI);
} else if (math == “cubert”) {
result = Math.cbrt(firstI);
} else if (math == “negpos”) {
result = firstI * -1;
} else if (math == “sine”) {
result = Math.sin(firstI);
} else if (math == “cosine”) {
result = Math.cos(firstI);
} else if (math == “tangent”) {
result = Math.tan(firstI);
} else if (math == “ln”) {
result = Math.log(firstI);
} else if (math == “logTen”) {
result = Math.log10(firstI);
} else if (math == “rand”) {
result = Math.round(firstI);
}else if (math == “res”) {
result = 1 / firstI;
} else if (math == “fact”) {
n = firstI;
firstI = 1;
while (n > 1){
firstI *= n;
n -= 1;
}
result = firstI;
} decimalPoint = (Math.round(result) == result) ? “”: “.”;
}
//To make variable ‘first’ and ‘second’ keep result of its values. e.g if it was 3 + 2 it must now be 5. This is done to prepare them for use in maths() function
function prep() {
second = eval(second);
first = eval(first);
}
//When sqr, cube e.t.c are clicked, this function, math(), does the following: 1. It extract the last number you entered from variable ‘first’ and keeps it as variable ‘firstI’. 2. It calls mathematics function to work on the on variable ‘firstI’. The answer is kept in ‘result’ variable. 3.It displays the answer of other previous numbers entered and the ‘result’ from mathematics e.g. 5 + 4 which was 3 + 2 + 2^2. Here 3 + 2 has become 5 and because of prep() function.
function maths(a) {
math = a;
try {
if (operatorSign == “+”) {
prep();
firstI = first – second;
mathematics();
first = second + “+” + result;
} else if (operatorSign == “-“) {
prep();
firstI = second – first;
mathematics();
first = second + “-” + “(” + result + “)”;
} else if (operatorSign == “*”) {
prep();
firstI = first / second;
mathematics();
first = second + “*” + result;
} else if (operatorSign == “/”) {
prep();
firstI = second / first;
mathematics();
first = second + “/” + result;
} else {
firstI = first;
mathematics();
first = result;
}
return first;
} catch (first ) {
first = second + operatorSign;
return first;
}
}
//This is just my notes:–> To remove first zero in second number zero must have its own function so that every entry of zero is checked to make sure that no number greater than zero should start with a zero
function digit(b) {
opsCheck = 0;
dotCounter++;
if (first == Infinity || first == NaN) {
first = 0;
}
peSign = “pes”;
entered = b;
if (rootNpower_Sign != “”) {
first = (first === “0” && entered !== “.”) ? entered: first + entered;
return secondI + firstI + rootNpower_Sign + first;
} else {
first = (first === “0” && entered !== “.”) ? entered: first + entered;
theanswer = eval(first) + “”;
if (theanswer.length > 14) {
theanswer = Math.abs((theanswer*1).toPrecision(14));
}
document.getElementById(“display1”).innerHTML = first;
return theanswer;
}
}
var thebase = “”;
function base(a) {
thebase = a;
first *= 1;
if (thebase == “bin”) {
firstII = first.toString(2);
} else if (thebase == “oct”) {
firstII = first.toString(8);
} else if (thebase == “hex”) {
firstII = first.toString(16);
}
return firstII;
}
//Four functions below are for themes
function themes(thm) {
theme = thm;
el = document.getElementsByClassName(“div”);
if (theme == 1) {
el[0].id=”theme1″;
} else if (theme == 2) {
el[0].id=”theme2″;
} else if (theme == 3) {
el[0].id=”theme3″;
} else {
el[0].id=”theme4″;
}
}
//back space
function bsp() {
first += “”;
dotCounter–;
decimalPoint = dotCounter >= 0 ? “.”: “”;
first = first.substr(0, first.length – 1);
document.getElementById(“display1”).innerHTML = first;
try{
eval(first);
return first;
} catch(first) {
eval(first);
return “”;
}
}
//Two functions below calculate power and root
function pow() {
first *= 1;
result = Math.pow(firstI, first);
}
function roots() {
first *= 1;
result = Math.pow(first, 1 / firstI).toPrecision(12);
result = Math.abs(result);
}
//When operators ,+ – / * are clicked op() function does a number of things. It checks if power or root were clicked. If yes, it calculates the previous numbers and the add the new operator. If not it add the operator clicked
function operators(b) {
peSign = “”;
if (opsCheck == 0) {
opsCheck = 1;
document.getElementById(“display1”).innerHTML = first;
try {
if (rootNpower_Sign == “^”) {
if (operatorSign == “+”) {
pow();
answer = result + second;
} else if (operatorSign == “-“) {
pow();
answer = second – result;
} else if (operatorSign == “*”) {
pow();
answer = result * second;
} else if (operatorSign == “/”) {
pow();
answer = second / result;
} else {
pow();
answer = result;
}
} else if (rootNpower_Sign == “√”) {
if (operatorSign == “+”) {
roots();
answer = second + result;
} else if (operatorSign == “-“) {
roots();
answer = second – result;
} else if (operatorSign == “*”) {
roots();
answer = result * second;
} else if (operatorSign == “/”) {
roots();
answer = second / result;
} else {
roots();
answer = result;
}
} else if (a == “%”) {
answer = second % first;
} else {
operatorSign = b;
first += operatorSign;
decimalPoint = “”;
}
rootNpower_Sign = “”;
operatorSign = b;
firstI = “”;
second = answer;
first = answer + operatorSign;
decimalPoint = “”;
document.getElementById(“display1”).innerHTML = first;
return eval(second);
} catch(x) {
if (first != “<span class=’red’>Press ON to start</span>”) {
operatorSign = b;
second = eval(first);
first += operatorSign;
decimalPoint = “”;
} else {
first = “<span class=’red’>Press ON to start</span>” ;
}
document.getElementById(“display1”).innerHTML = first;
return (second == undefined ) ? 0 : eval(second);
}
} else {
operatorSign = b;
first += “”;
first = first.substr(0, first.length – 1);
first = first + operatorSign;
document.getElementById(“display1”).innerHTML = first;
return (second == undefined ) ? 0 : eval(second);
}
}
function percent() {
first = eval(first) * 100;
return first + “%”;
}
//toggles the negative sign
function negpos() {
first = (operatorSign == “”) ? first *= -1: first;
return first ;
}
function power(b) {
rootNpower_Sign = b;
if (operatorSign == “+” && second != “”) {
prep();
firstI = first – second;
first = “”;
secondI = second + “+”;
return second + “+” + firstI + rootNpower_Sign;
} else if (operatorSign == “-” && second != “”) {
prep();
firstI = second – first;
first = “”;
secondI = second + “-“;
return second + “-” + firstI + rootNpower_Sign;
} else if (operatorSign == “*” && second != “”) {
prep();
firstI = first / second;
first = “”;
secondI = second + “*”;
return second + “*” + firstI + rootNpower_Sign;
} else if (operatorSign == “/” && second != “”) {
prep();
firstI = second / first;
first = “”;
secondI = second + “/”;
return second + “/” + firstI + rootNpower_Sign;
} else {
rootNpower_Sign = b;
firstI = first;
first = “”;
return firstI + rootNpower_Sign;
}
}
function equal() {
document.getElementById(“display1”).innerHTML = first;
try {
if (rootNpower_Sign == “^”) {
if (operatorSign == “+”) {
pow();
first = result + second;
} else if (operatorSign == “-“) {
pow();
first = second – result;
} else if (operatorSign == “*”) {
pow();
answer = result * second;
first = answer;
} else if (operatorSign == “/”) {
pow();
first = second / result;
} else {
pow();
first = result;
}
} else if (rootNpower_Sign == “√”) {
if (operatorSign == “+”) {
roots();
first = result + second;
} else if (operatorSign == “-“) {
roots();
first = second – result;
} else if (operatorSign == “*”) {
roots();
first = result * second;
} else if (operatorSign == “/”) {
roots();
first = second / result;
} else {
roots();
first = result;
}
} else if (operatorSign == “%”) {
answer = second % first;
} else {
if (first == “”) {
first = first ;
} else {
try{
first = eval(first) + “”;
if (first.length > 14) {
first = Math.abs((first*1).toPrecision(14));
}
} catch (first) {
first = “<small><small>Incorrect input. Click C to clear</small></small>”;
return first;
first = “”;
}
}
}
rootNpower_Sign = operatorSign = answer = firstI = second = “”;
flo = first;
flo = Math.floor(flo);
decimalPoint = (flo == first) ? “”: “.”;
return first;
} catch(operatorSign) {
operatorSign = “”;
first = eval(first) + “”;
if (first.length > 14) {
first = Math.abs((first*1).toPrecision(14));
}
flo = first;
flo = Math.floor(flo);
decimalPoint = (flo == first) ? “”: “.”;
return first;
}
}
</script>
</body>
</html>

 

এই কোড টি আপনার প্লাটফর্ম এ সেভ করার পর  । কোড এর উপর রাইট বাটন ক্লিক করে,   কপি ফাইল পাথ (কপি ফাইল পাথ)  এ ক্লিক করেন ।

তারপর আপনার ব্রাউজার(google crome,mozila,internet explore ) এ পেস্ট করুন  এবং এন্টার(Enter) ক্লিক করুন ।

browser

 

 

 

আউটপুটঃ

scientific calculator

ধন্যবাদ সবাইকে । সব সময় আমাদের  আইটি ডক্টর ২৪.কম এর সাথেই থাকুন ।  বাংলা টেকনোলজি ব্লগিং প্লাটফর্ম ও কমিউনিটি। টেকনোলজিকে যারা ভালবাসে তাদের জন্য দারুন ব্লগ আইটি ডক্টর ২৪.কম বাংলাদেশের শীর্ষ স্থানীয় ব্লগ গুলোর মধ্যে একটি। সকলকে টেকনোলজি সম্পর্কে সচেতনে করা এবং টেকনোলজির সুবিধা যাতে সবাই আইটি ডক্টর ২৪.কম ওয়েবসাইটের মাধ্যমে নিতে পারে এটাই আমদের কাম্য। আমরা কাজ করে যাচ্ছি তাই আপনারা সবাই আইটি ডক্টর ২৪.কম এর সাথে থাকুন।

 

 

 

Please comment Here (ভাল লাগলে কমেন্ট করুন)