2 Replies - 16414 Views - Last Post: 15 March 2009 - 06:00 AM

#1 dreamygirl  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Simple Calculator program

Post icon  Posted 14 March 2009 - 12:47 PM

Good morning ,


I'm supposed to create a simple calculator program that performs the following operations : (=,-,/,*)


I have done the layout but i don't really understand the concept of using this method (document.value = .... ) and how can i apply this to the buttons i've created and relating them to the functions they use,Because its nt explained in the book .

anyway i wish anyone could give an idea about that and how can i acces the functions and use them.

This my code :

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">



<head>
<title> Simple Calculation</title>
<script type = "text/javascript">
<!--
<head>
<title> Simple Calculation</title>
<script type = "text/javascript">
<!--

function number() {






		 }


function add() {



}


function subtract() {


}


function division() {


}


function multiply() {


}


function clear() {

}


function prevArg() {


}


			// -->
			  </script>
		   </head>
		






<p>

<form action = "">
		 <div>
			<label>
			   <input type = "text" /></label></br>

<input type = "submit" value = "9" onclick = "number(9)" /> 
<input type = "submit" value = "8" onclick = "number(8)" /> 
<input type = "submit" value = "7" onclick = "number(7)" /></br>
<input type = "submit" value = "6" onclick = "number(6)" /> 
<input type = "submit" value = "5" onclick = "number(5)" /> 
<input type = "submit" value = "4" onclick = "number(4)" /></br>
<input type = "submit" value = "3" onclick = "number(3)" /> 
<input type = "submit" value = "2" onclick = "number(2)" /> 
<input type = "submit" value = "1" onclick = "number(1)" /></br>

<input type = "reset" value = "C"  />
<input type = "submit" value = "0" onclick = "x0" />

<input type = "submit" value = "="onclick = "displayresult( )" /> </br>

 

<input type = "submit" value = "+" onclick = "add( )" /> 
<input type = "submit" value = "*" onclick = "multiply( )" /> 
<input type = "submit" value = "/" onclick = "divide( )" /> 
<input type = "submit" value = "-" onclick = "subtract( )" /> 


   
  
				 </p>   
			  
		   </body>
		</html>




thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Calculator program

#2 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 138
  • Joined: 28-November 08

Re: Simple Calculator program

Posted 14 March 2009 - 03:01 PM

Hi, you dont really need to worry about + / - * because the Javascript eval() will take care of the caluculations.
You can just add these characters to the value of the calculator screen.

This might help...

<HTML xmlns = "http://www.w3.org/1999/xhtml">
	<HEAD>
	<TITLE> Simple Calculation</TITLE>
	<script type = "text/javascript">
	<!--
	function update(value){
		document.getElementById("screen").value += value;
	}
	
	function result(){
		document.getElementById("screen").value = eval(document.getElementById("screen").value);
	}
	
	function reset(){
		document.getElementById("screen").value = '';
	}
	-->
	</SCRIPT>
	</HEAD>
	<BODY>
				<FORM NAME="myForm">
			<TABLE BORDER=2>
				<TR>
					<TD align="center">
						<INPUT TYPE="text" ID="screen" NAME="screen" style="width:99%"><br>
				</TD>
				</TR>
				<TR>
					<TD>
						<INPUT TYPE="button" NAME="7"   VALUE="  7  " onclick="update(7)">
						<INPUT TYPE="button" NAME="8"   VALUE="  8  " onclick="update(8)">
						<INPUT TYPE="button" NAME="9" VALUE="  9  " onclick="update(9)">
						<INPUT TYPE="button" NAME="+"  VALUE="  +  " onclick="update('+')">
						<br>
						<INPUT TYPE="button" NAME="4"  VALUE="  4  " onclick="update(4)">
						<INPUT TYPE="button" NAME="5"  VALUE="  5  " onclick="update(5)">
						<INPUT TYPE="button" NAME="6"   VALUE="  6  " onclick="update(6)">
						<INPUT TYPE="button" NAME="-" VALUE="  -  " onclick="update('-')">
						<br>
						<INPUT TYPE="button" NAME="1" VALUE="  1  " onclick="update(1)">
						<INPUT TYPE="button" NAME="2" VALUE="  2  " onclick="update(2)">
						<INPUT TYPE="button" NAME="3"  VALUE="  3  " onclick="update(3)">
						<INPUT TYPE="button" NAME="*" VALUE="  x  " onclick="update('*')">
						<br>
						<INPUT TYPE="button" NAME="c" VALUE="  c  " onclick="reset();">
						<INPUT TYPE="button" NAME="0"  VALUE="  0  " onclick="update(0)">
						<INPUT TYPE="button" NAME="="  VALUE="  =  " onclick="result();">
						<INPUT TYPE="button" NAME="/"   VALUE="  /  " onclick="update('/')">
					</TD>
				</TR>
			</TABLE>
		</FORM>
	</BODY>
</HTML>



Hope it helps :)

This post has been edited by JohnWaters: 14 March 2009 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 dreamygirl  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 67
  • Joined: 08-October 08

Re: Simple Calculator program

Posted 15 March 2009 - 06:00 AM

ooh i didnt't know about that , it really helped me.

thanks for your effort and time :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1