Thursday, March 17, 2016

Building Calculator using HTML5/CSS3 and Javascript


Introduction:

 In this project I will guide you how to build a simple calculator by using HTML5, CSS3 and Javascript. The demo for this project as below:



Requirements:

 In order to understand this project, it requires you to have a basic knowledge of HTML5, CSS3 and Javascript.

Overview:

- The snapshot of calculator as below:

- Functionality:
                       +, -, *, /,  X^3, sqrt(x).
                       C: clear; <--: delete.
Code Analysis:

The project includes 3 files: 
- index.html: Defines textbox and keys.
- design.css: Designs the outline.
- logic.js: Builds functionalities.

index.html (HTML)

<!DOCTYPE html>
<html>
 <head>
  <link rel="stylesheet" type="text/css" href="design.css">
 </head>
 <title> Calculator </title>

 <body>
  <h2>Press Caps lock. A: +; S: -; M: *; D: /; B: <--; C: clear; P: .; E: =</h2>
  <div id="calculator">
  <form>
   <input type="text" id="display" disabled=""> <br/>
   <input type="button" value="C" id="keys" onclick="addtoscreen('C')">
   <input type="button" value="&larr;" id="keys" onclick="backspace()">
   <input type="button" value="X&sup3;" id="keys" onclick="power(3)">
   <input type="button" value="&radic;" id="keys" onclick="power(0.5)"> <br>

   <input type="button" value="9" id="keys" onclick="addtoscreen('9')">
   <input type="button" value="8" id="keys" onclick="addtoscreen('8')">
   <input type="button" value="7" id="keys" onclick="addtoscreen('7')">
   <input type="button" value="+" id="keys" onclick="addtoscreen('+')"> <br>
    
   <input type="button" value="6" id="keys" onclick="addtoscreen('6')">
   <input type="button" value="5" id="keys" onclick="addtoscreen('5')">
   <input type="button" value="4" id="keys" onclick="addtoscreen('4')">
   <input type="button" value="-" id="keys" onclick="addtoscreen('-')"> <br>

   <input type="button" value="3" id="keys" onclick="addtoscreen('3')">
   <input type="button" value="2" id="keys" onclick="addtoscreen('2')">
   <input type="button" value="1" id="keys" onclick="addtoscreen('1')">
   <input type="button" value="*" id="keys" onclick="addtoscreen('*')"> <br>

   <input type="button" value="0" id="keys" onclick="addtoscreen('0')">
   <input type="button" value="." id="keys" onclick="addtoscreen('.')" >
   <input type="button" value="=" id="keys" onclick="answer()">
   <input type="button" value="/" id="keys" onclick="addtoscreen('/')">

  </form>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="logic.js">  </script>
 </body>

</html>

Basically the index.html contains two input types: text for displaying the result on the screen and button for keys input. The text input is disabled. As a result, user is not allowed to type directly to the screen. Besides, depending on which key is clicked, the relevant function will be invoked. These functions are defined in logic.js.
  
design.css (CSS)

h2{
 text-align: center;
 color: lightpink;
}
#calculator {

 width: 250px;
 height: 350px;
 border: 5px solid black;
 text-align: center;
 background: lightgreen;
 margin: 150px auto;
 box-shadow: 0px 0px 30px gray;
}

#display {
 margin-top: 30px;
 margin-bottom: 20px;
 width: 220px;
 height: 30px;
 border: 1px solid red;
 box-shadow: 0px 0px 30px red;
 text-align: right;
 font: 20px bold;
 color: blue;

}

#keys {
 width: 41px;
 height: 35px;
 margin-left: 10px;
 margin-bottom: 20px;
 box-shadow: 0px 0px 20px skyblue;
 cursor: pointer;
}

#keys:hover {
    background: yellow;
    font-weight: bold;
}
Tag, ID Selectors: 

h2{}: defines h2 element properties: text-align, color.
#calculator{}: defines calculator properties: width, height, border. text-align, margin (auto: center) and box-shadow.
#display{}: defines screen properties: margin, width, height, border, box-shadow, text-align. font, color.
#keys{}: defines key properties: width, height, margin, box-shadow, cursor (pointer)
#keys:hover{}: defines key properties when moving mouse over the keys: change background and bold the text/number as well as cursor turned into pointer.

logic.js (Javascript)

var box=document.getElementById('display');

function addtoscreen(x) {
 box.value +=x; 
 if(x=='C'){
  box.value='';
 }
}

function answer(){
 x=box.value;  
 x=eval(x); 
 box.value=x; 
}
function backspace(){
 var number=box.value;
 var len=number.length-1;
 var newnumber=number.substring(0,len);  
 box.value = newnumber;
}
function power(y){
 x=box.value;
 box.value='';
 x=Math.pow(x,y);
 box.value=x;
}

$(document).keypress(function(e) {
    var key = e.which;
    switch (key)
    {
        case 48:
        //alert("0");
         addtoscreen('0');
          break;
        case 49:
          //alert("1");
        addtoscreen('1');
          break;
        case 50:
         // alert("2");
        addtoscreen('2');
          break;
        case 51:
         // alert("3");
        addtoscreen('3'); 
          break;
        case 52:
         // alert("4");
        addtoscreen('4');  
          break;
        case 53:
         // alert("5");
        addtoscreen('5');  
          break;
        case 54:
        //  alert("6");
        addtoscreen('6'); 
          break;
        case 55:
        //  alert("7");
        addtoscreen('7'); 
          break;
        case 56:
         // alert("8");
        addtoscreen('8');  
          break;
        case 57:
        //  alert("9");
        addtoscreen('9'); 
          break;
        case 67:
        //  alert("C"); 
        addtoscreen('C'); 
          break;  
        case 66:
        //  alert("B"); 
        backspace();
        break; 
        case 65:
        //  alert("A"); 
        addtoscreen('+');
        break;
        case 83:
        //  alert("S"); 
        addtoscreen('-');
        break;
        case 77:
        //  alert("M"); 
        addtoscreen('*');
        break;
        case 68:
        //  alert("D"); 
        addtoscreen('/');
        break;
        case 80:
        //  alert("P"); 
        addtoscreen('.');
        break;
        case 69:
        // alert("E");
        answer(); 
          break;
        default:
          alert("Invalid");
    }
});

var box=document.getElementById('display'); box represents calculator's screen.
addtoscreen(x): when a key is clicked, the value is stored in box.value. "+=" (concatenation) means the key values will be displayed continuously without replacing the previous key value by the current one.
answer(): box.value (existing value on screen) is stored in x, then x is evaluated by javascript API eval(). The eval() method will evaluate the args. For instance, if x=10+2-5 then eval(10+2-5) will evaluate the args and execute 7. Finally the avaluated x or the result will be displayed on screen by box.value=x; 
backspace(): assign the number values on screen to variable named number and len is the length of displayed number on screen subtracted by 1. Then assign this new string to variable named newnumber and display it on screen.
power(y): perform power of 3 and 0.5(sqrt) for any number which is pressed by using Javascript API Math.pow() and display the result on the screen.
$(document).keypress(): detects pressed keyboard and invoking relevant functions.
Caps lock is on. Defined keyboard:
C: clear 
A: +
S: -
M: *
D: /
P: .
B: <--
E: =


Conclusion:

In this project, I helped you build a simple calculator by using HTML5, CSS3 and Javascript. I also described how the project works. Moreover, I also explained code in details and attached the full source code. 

C Quiz

Time: 60 minutes
Question .No: 30
Allowed: pencil + a piece of paper.

1. Given variable declaration below:
int x,y; 
int *a, *b;
    Which is wrong statement?
a.  a=&x;
b.  b=&y;
c.  b=&a;
d.  *b=*a;

2. Given a short code:
int i=1,j=2; 
for(;i<5;i++) 
 i*=j++; 
The given code above is:
a.  Wrong syntax
b.  i=5, j=4
c.  i=10, j=4
d.  Three answers above are wrong.

3. Given a short code:
 int x[]={0x4161} 
char *p1, *p2; 
p1=(char*) x; 
p2=p1+1; 
printf(“%c %d”,*p1, *p2); 
Result:
a.  A 97
b.  a 65
c.  A 98
d.  a 66

4. Given a short code:
 int x=1,y=2; 
 while(x<=10) 
 { 
  x++; 
  if (x==6) 
   break; 
  y+=++x; 
 } 
 printf("%d %d",x,y); 
Result:
a.  6 10
b.  11 37
c.  12 40
d.  Three answers above are wrong.

5. Given a short code:
 int x=1,y=2; 
 while(x<=10) 
 { 
  x++; 
  if (x==6); 
   y+=++x; 
 } 
 printf("%d %d",x,y); 
Result:
a.  6 10
b.  11 37
c.  12 40
d.  Three answers above are wrong.

6. Given a short code:
 int x=1,y=2; 
 while(x<=10) 
 { 
  x++; 
  if (x==6) 
   continue; 
  y+=++x; 
 } 
 printf("%d %d",x,y); 
Result:
a.  6 10
b.  11 37
c.  12 40
d.  Three answers above are wrong.

7. Given a short code:
int i=1,j=0; 
for(i=1;i<10;i+=2) 
 for(;i<8;i++) 
  j+=i; 
printf("%d %d",i,j); 
Result:
a.  10 28
b.  19 81
c.  11 0
d.  Three answers above are wrong.

8. Given a short code:
int i=1,j=0; 
for(i=1;i<10;i+=2); 
 for(;i<8;i++) 
  j+=i; 
printf("%d %d",i,j); 
Result:
a.  10 28
b.  19 81
c.  11 0
d.  Three answers above are wrong.

9.  Given a short code:
int i=1,j=0; 
if (i>j); 
if (i<5) 
 i+=3; 
else 
i+=2; 
Result of i:
a.  1
b.  2
c.  3
d.  4

Given a short code( is used for 10,11,12,13,14,15):
#include<stdio.h> 
double a,b; 
void f1(int a); 
int f2(int a); 
void f1(int a) 

 int s, i; 
 for(s=i=0; i<=a; s+=++i) 
  i+=2; 
 printf(“%d”,s); 

int f2(int a) 

 int i; 
 for(i=2;a%i;i++); 
 printf (“%d”, i); 
if (i==a) 
  return a; 
 else 
  return a/i; 


10. Invoke f1(15)
a.  Print 57
b.  Print 63
c.  Print 51
d.  Three answers above are wrong.

11. Invoke f2(15):
a.  Print 3
b.  Print 15
c.  Print nothing
d.  Three answers above are wrong.

12. Invoke f1(f2(39))
a.  Print 3 35
b.  Print 35
c.  Print 335
d.  Three answers above are wrong.

13. Invoke f2(f1(39)
a.  Print 287
b.  Print 35
c.  Print 2870
d.  Three answers above are wrong.

14. Invoke f2(f2(39))
a.  Print 133
b.  Print 13 3
c.  Print 313
d.  Three answers above are wrong.

15. Which one is correct statement?
a.  a=b+f1(a);
b.  a=b+f2(a);
c.  a=f1(f2(b));
d.  a=f2(f1(b));

Given a short code:
int a=5, b=6, y; 
y=a&&(a>b++)&&--b; 
16. Value of b, y are:
a.  b=5, y=0
b.  b=6, y=1
c.  b=7, y=0
d. Three answers above are wrong.

17. Given a short code:
int x=11, y=5, b; 
b=(x-y)?(x&&y):(x||y); 
    Value of b:
a. 14
b. 15
c. 1
d. 0

18. Given a short code:
int a=10, b=7; float y; 
y=(b+1)*(a/b); 
printf(“%5.2f”,y); 
    Result:
a. 11.43
b. 11.42
c. 8
d. 8.00

19. Given a variable declaration:
int a,b, *p1, *p2; 
    Which one is wrong statement?
a.  p1=a;
b.  p2=&b+1;
c.  p1=p2+a;
d.  p1=&a+1;

20. Given a variable declaration:
int *p1, *p2, *p3; 
    Which one is correct statement?
a.  p3=p1+p2;
b.  p3=p2-p1;
c.  *p3=p1-p2;
d.  p3=2*p1;

21. Given a short code:
int x=1,i; 
for(i=1; i<10; i++) 

 x+=++i; 
 if(i==5) 
  break;

printf(“%d”,x); 
    Result:
a. 31
b. 7
c. 55
d. Three answers above are wrong.

22. Given a short code:
int i=1, j=2; float S=0; 
do { 
 S+=i/j; 
 i++; 
 j*=2; 
} while(i<15); 
printf(“%3.2f”, S); 
    Result:
a. 2.0
b. 2.00
c. 0.00
d. 0.0

23. Given a short code:
int a=1, b=0; 
while(a<=b&&++b) 
 if(a==b) 
  break; 
printf(“%d %d”, a,b); 
    Result:
a. 1 0
b. 1 1
c. 11
d. Three answers above are wrong.

24. Given a short code:
#include<stdio.h> 
int a,b; 
main() 

 switch(a-b?5:b++) 
 { 
   default: a=b+=2; 
   case 5: b+=5; 
   case 1: a--; 
 } 

    Result:
a.  a=2, b=8
b.  a= -1, b=1
c.  a= -1, b=6
d.  Three answers above are wrong.

25. Given a short code:
#include<stdio.h> 
int a=1,b=2; 
main() 

 switch(a=3) 
 { 
   case 3: a+=2; 
  default: b++; 
  break; 
  case 0: a--; 


   Result:
a.  a=1, b=3
b.  a=5, b=3
c.  a=4, b=3
d. Three answers above are wrong.

26. Given a short code:
#include<stdio.h> 
int change(int *a); 
main() 

 int x=3, y=0; 
 while(x>change(&y)) ; 
 printf(“%d”,y); 

int change(int *a) 

 static int temp=1; 
 *a+=temp++; 
 return temp; 

    Result:
a.  Print 0
b.  Wrong syntax
c.  Print 1
d.  Print 3

Given a short code(is used for 27, 28, 29,30)
#include<stdio.h> 
void func1(int *a, int b); 
void func2(int *a, int *b); 
main() 

 int a=15, b=8; 
 func1(&b,a); 
printf(“%d %d”, a,b); 
 func2(&a,&a); 
 printf(“%d %d”,a,b); 

void func1(int *a, int b) 

 *a+=b++; 
 func2(&b,a); 
 printf(“%d %d”, *a, b); 
 return; 

void func2(int *a, int *b) 

 if(*a>*b) 
   *a+=*b; 
 else *a-=2; 
 printf(“%d %d”, *a, *b); 
 return; 

27.  The function func2 outputs:
a. 14 23
b. 13 13
c. Both a,b are correct.
d. Both a, b are  wrong.

28. The function func1 outputs:
a. 23 14
b. 14 23
c. 23 23
d. Both a,b are correct.

29. The output of printf in main function BEFORE invoking func2:
a. 15 8
b. 15 23
c. 13 8
d. Three answers above are wrong.

30. The output of printf in main function AFTER invoking func2:
a. 15 8
b. 13 23
c. 13 8
d. Three answers above are wrong.
Answer: 1.C 2.C 3.B 4.A 5.B 6.C 7.A 8.A 9.D 10.B 11.A 12.D 13.D 14.C 15.B 16.C 17.C 18.D 19.A 20.C 21.A 22.C 23.A 24.A 25.B 26.D 27.C 28.D  29.B 30.B

PHP Quiz

Time: 20 minutes
Question .No: 18
Allowed: pencil + a piece of paper

1. What will the following PHP script display?
<?php 
function get_sum(){
global $var;
$var = 5;
 }
 $var = 10;
 get_sum();
 echo $var;
 ?>
A. 15
B. NULL
C. 5
D. 10
 The same question if ignoring the line global $var; 

2. Which of the following structures are not supported in PHP5? (more than one can be selected) 
A. Multiple inheritance
B. Abstract classes
C. Interfaces
D. Public and private method

3. Is it true that functions in PHP cannot be overloaded or redefined?
A. Yes
B. No

4. How could the expression in "echo" below is fixed?
<?php 
$arr['one'] = 14;
echo "Event happended $arr['one'] days ago";
 ?>
A. " .$arr['one']. "
B. $arr{one}
C. {$arr['one']}

5. What does the following PHP script do? (more than one can be selected) 
<?php 
$id = 3;
$str = "active$id";
$$str = 1;
 ?>
A. Sets the variable $str to 1
B. Declares $active3 as a new variable
C. Sets the variable $str to "active3"
D. Nothing. The script is invalid

6. How many parameters can the following function accept?
<?php 
function get_sum()
{
//...
}
 ?>
A. 0
B. 1
C. Any number

7. What is "herodoc" used for?
A. Creating multi-line strings with quotation marks
B. Creating single-line strings with quotation marks
C. Creating multi-line strings without quotation marks
D. Creating single-line strings without quotation marks

8. Can "echo" accept more than 1 parameter?
A. No
B. Yes

9. What is "<<<" operator used for?
A. To declare a regular expression
B. It is similar to "echo"
C. To declare a string variable

10. What will the following PHP script display?
<?php 
abstract class Foo 
{
abstract function bar(array $params=NULL); 
}
class Moo extends Foo
{
public function bar(array $params)
{
echo count($params);
}
}
$c = new Moo();
$c->bar(array(5)); 
 ?>
A. 1
B. 5
C. 0
D. Fatal error

11. How do you start a PHP session?
A. Using session_start()
B. Using session_set()
C. Using session_register()
D. Using the variable $_SESSION

12. Which of the following statements about abstract classes are true?  (more than one can be selected) 
A. It is necessary to instantiate at least one object of an abstract class
B. Methods declared as abstract define only their signature and cannot contain any functionality
C. Methods declared as abstract contain functionality which cannot be overridden by the classes which inherit them
D. It is not possible to instantiate an object of an abstract class

13. What will the following PHP script display?
<?php 
$str = 123;
echo "Value of variable - \$str"; 
 ?>  
A. Value of variable - $str
B. Value of variable - 123
C. Value of variable - \$str
The same question with:
echo "Value of variable - $str"; 
echo 'Value of variable - \$str';
echo 'Value of variable - $str'; 

14. Which function returns a list of reponse headers?
A. header_getAll()
B. headers()
C. headers_list()
D. headers_send()

15. What is the following PHP script display?
<?php 
$number = array(0.57,"21.5", 40.52);
if (in_array(21.5, $number,true)) echo "The value 21.5 is found";
else echo "21.5 is not found";
 ?>
A. "21.5 is not found"
B. "The value 21.5 is found"
C. Nothing. The script is invalid

16. What is the difference between accessing class methods via "->" and accessing via "::"?
A. "->" is for static methods, "::" is for non-static methods.
B. "->" is for static methods, "::" is for all methods.
C. "->" is for all methods, "::" is for static methods.
D. "->" is for non-static methods, "::" is for static methods.
E. There is no difference.

17. Which change will make the following PHP script display the string "Hello, World!"?
<?php 
function myfunction()
{
???
     print $string;
}
myfunction("Hello, World!");
 ?>
A.  Replacing ??? with $string = $argv[1];
B. Replacing ??? with $string = get_function_args();
C. Replacing ??? with list($string) = func_get_args();
D. Nothing can be replaced.

18. Which function is best suited for removing markup tags from a string?
A. reg_replace()
B. strip_markup()
C. strip_tags()
D. str_replace()
Answer: 1.C 2.A 3.A 4.A 5.B,C 6.C 7.C 8.B 9.A 10.D 11.A 12.B,D 13.A 14.C 15.A 16.D 17.C 18.C
Ref:
http://www.zendexam.com/
http://php.net/