Thursday 16 August 2012

Example for usage of Big Integer: Use of Big Integer to do factorial.


import java.math.BigInteger;
import java.util.ArrayList;


public class TextFactorial {

public static synchronized BigInteger bigNumber(int num){

ArrayList list = new ArrayList();
list.add(BigInteger.valueOf(1));

for (int i=list.size();i<(num+1);i++){
BigInteger lastfact = (BigInteger)list.get(i-1);
BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(i));
list.add(nextfact);
}
return (BigInteger)list.get(num);

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int num = 23;
System.out.println(TextFactorial.bigNumber(num));
}

}


Big Integer: 任意无限大。 As long as computer support, it could support. 

Java by Example Study Note - Chapter 2


Java by Example 

Study Note - Chapter 2

1. A method with same but different parameter - polymorphism(多态): overloaded (重载)

Example 2.1: 

Class Sub{
}

public class TextConstant {
Int number =0; 
public void run(Object obj){
System.out.println("is Object class: "+ obj);
}
public void run(Sub sub){
System.out.println("is sub class: "+ sub);
}
public static void main(String [] args){
TextConstant constant = new TextConstant();
Constant.run(null);
}
}

Output: Is sub class: null
Reason: When invoke run() method, it will first consider a parameter with class object. 
If the parameter is not class object, only it will consider object class. 
* null will not use for primitive variable like int. 

2. Primitive variable :  int, short, long, byte, float (4byte), double (8 byte), char (2 byte), boolean

Type conversion among primitive variable: can be auto change or force change. 

Rule for auto change: two type must be compatible, and target type must have range bigger than the initial type. 

Example 2.2: 
float f = 1.7f;
double dou = f;
system.out.println("dou: "+dou);

Output:  dou=1.7000000476837158

Rule for force change: put a cast in front with the new type. Will lost accuracy.  

Example 2.3: 
double d = 123.456d; 
byte b = (byte)d;
system.out.println("d: "+d+" b: "+b);
double d = 567.89d; 
byte b = (byte)d;
system.out.println("d: "+d+" b: "+b);

Output: 
d: 123.456 b=123
d: 567.89 b=55
Reason: 
The first d is within one byte (less than 256 (0x100)). So it take all the value in front the decimal. 
The second d is more than one byte (more than 256 (0x100)). So it will first convert 567.89 to 567. Then, 567 is mod with 256 and get the value and assign it to b. 

3. Operate on a variety of operators: 

Calculate operate: involve the problem on lost of accuracy when involve operation with different type of data. To ensure more accurate result, system will auto convert all the data to the type have more accuracy level. 

Example 2.4: 
int a=6, b=5; 
int divideA = a/b; 
float divideB=a/b;
float divideC=a/(b*2f);
System.out.println(divideA +","+divideB+","+divideC); 

Output: 1,1.0,0.6
Reason: 
For divideB: a = int type, b = int type. Largest accuracy : int. All the data will maintain at int type. And final result will convert from int to float. (6/5=1 in int. 1 int to float = 1.0)
For divideC: a=int, b = int, 2F=float. Largest accuracy: float. All the data will upgrade to float type. And final result will maintain in float type. (5*2f=10.0 in float, 6/10.0=0.6 in float too)

Note: 
~ : NOT 
^: XOR 
A+=1; : A=A+1; 
&& (Logical AND): When both side are true, then only true is return. Else, return false. 
If left side=false, right side is not executed. Return false directly. 
|| (Logical OR): When left side = true, return true, right side is not executed. 
If left side = false, return result as result of right side. 
& (Bitwise AND): When left side = false, right side is executed. 
| (Bitwise OR): When leftside = true, right side is executed but the result not concern, return true. 
Logical AND, OR: return type can be boolean type 
Bitwise AND, OR: return type can be boolean type or int type
For bit operator: <<(move left, LSB=0), >>(signed move right, if MSB=1 then MSB=1, if MSB=0, MSB=0), >>> (unsigned move right. MSB=0) 
Bit operator only suitable for the following data type: byte, char, short, int, long. 
<< : left move, multiple by 2. 
>>: right move, divide by 2. 
For int type right move:  a >> b,  if b > 31 . B will be mod with 324*8
a>>32 = a>>0
a>>33 = a>>1
For long type right move: a>>b, if b> 63. B will be mod with 648*8
a>>1 : a value will not change since it not assigned to a. 
a=a>>1: a value will change. 

Friday 29 June 2012

How to get TCK check byte of a ATR command

How to get TCK check byte of a ATR command?


Answer: 

TCK check byte of a ATR command is calculated with the XOR operation from byte T0-T7(The byte before TCK byte).


The following diagram illustrated an examples of ATR from TS 102.221:
Example of ATR and description

The following diagram illustrated program to do the XOR operation:

Program to do XOR operation


Development Engineer Interview Question


、请填写BOOL, float, 指针变量 零值比较的 if 语句。(本题5分)
Please write down the if statement for BOOL, float, pointer variables and null. (5 marks)
提示:这里零值可以是0, 0.0 , FALSE或者空指针。例如 int 变量 n 零值比较的 if 语句为:
    if ( n == 0 )
    if ( n != 0 )
以此类推。










、描述以下C语言中类型描述的含义:(本题5分)
Describe the meaning of the following C language type: (5 marks)
            #define n  100
              A           int *p ;
              B           int *p[n] ;
              C           int (*p)[n] ;
              D           int (*p)() ;
              E           int *p();
              F           int **p ;
              G     typedef int (*P)(char *(*)()) ;









三、以下为Windows NT下的32C程序请计算sizeof的值,(本题10
Following is the 32-bit Windows NT in C language. Please calculate the value of sizeof.(10 marks)
char      str[] = “Hello”;
char      *p = str;
int       n = 10;
请计算
Please calculate:
   sizeof(str) =
   sizeof(p)  =
   sizeof(n)  =
void Func(char str[100])
{
请计算
Please calculate:
   sizeof(str) =
}


void *p = malloc(100);
请计算
Please calculate:
   sizeof(p) =



、简答题 (每题4分,共20分)
Short answer question (4 marks for each question, total 20 marks)

1、头文件中的 ifndef/define/endif 干什么用?
What is the usage of  ifndef/define/endif in the header?





2#include  <filename.h>     #include  “filename.h” 有什么区别?
What is the difference between #include <filename.h> and #include “filename.h”?




3const 有什么用途?
What is the usage of const?





4 变量可以同时具有constvolatile属性吗?为什么?
Variable can be both const and volatile attributes? Why?




5、请简述以下AB 两个for循环的优缺点
Please describe in briefly the advantages and disadvantages of the Loop A and Loop B.

Loop A.
for (i=0; i<N; i++)
{
      if (condition)
{
             DoSomething();
}
else
{
  DoOtherthing();
}
}
Loop B
if (condition)
{
      for (i=0; i<N; i++)
{
  DoSomething();
}
}
else
{
      for (i=0; i<N; i++)
      {
  DoOtherthing();
}
}







五、有关内存的思考题, 请问运行下面4Test后有什么结果。(本题20分)
Problem about memory. What is the result of execute the following 4 Test?
A
void GetMemory(char *p)
{
   p= (char *)malloc(100);
}

void Test(void)
{
   char *str = NULL;
   GetMemory(str);
   strcpy(str, “Hello word”);
   printf(str);
}




B
char *GetMemory(void)
{
   char p[] = “hello world”;
   return p;
}

void Test(void)
{
   char *str = NULL;
   str = GetMemory();
   printf(str);
}




C:
void GetMemory(char **p, int num)
{
   *p = (char *) malloc(num);
}
void Test(void)
{
   char *str = NULL;
   GetMemory(&str, 100);
   strcpy(str, “hello”);
   printf(str);
}




D:
void Test(void)
{
   char *str = (char *) malloc(100);
   strcpy(str, “hello”);
   free(str);
   if (str != NULL)
   {
      strcpy(str, “world”);
      printf(str);
}
}






六、编写strcpy函数 (本题20分)
Write the function of strcpy (20 marks)

已知strcpy函数的原型是   char *strcpy(char *strDest, const char *strSrc);
It is known that prototype of  function strcpy is char *strcpy(char*strDest, const char *strSrc);

返回为目标字符串的地址
Return as target string address

其中strDest是目的字符串,strSrc是源字符串。
 strDest is the purpose string, strSrc is the source string.

1       不调用任何库函数,请编写函数 strcpy
 Write the function of strcpy without using any library function.
 (2)   此函数返回目标字符串的地址,为什么要返回它?
 This function returns the address of target string. Why should it return?
3试想一下,strcpy在什么情况下会出现不期望的结果,为什么会这样?请重写一个函数
char * strmove (char *strDest, const char *strSrc),解决这个问题。
Imagine, in what circumstances strcpy will output an unexpected result, why is it so? Please rewrite a function
char * strmove (char *strDest, const char*strSrc), solve this question.





七、测试题(本题10分)
Quiz (10 marks)

已知函数atoi,其函数原型是int atoi(char *str)
It is known that prototype of function atoi is int atoi(char*str)

函数用途:
Usage of function:
 
将字符串转换成一个整数值
  Convert a string into a integer value
输入参数:
Input parameters;
  str
待转换为整型数的字符串
  str to be converted to a string of integers
返回值:
Return value:
 
成功返回转换后的数值,失败则返回0.
  If success, return the converted value. If fail, return to 0.
请设计出这个函数的测试用例。
  Please design a test case for this function.



















----------------------解答-------------------------
一、请填写BOOL , float, 指针变量 与“零值”比较的 if 语句。
提示:这里“零值”可以是0, 0.0 , FALSE或者“空指针”。例如 int 变量 n 与“零值”比较的 if 语句为: 
    if ( n == 0 ) 
    if ( n != 0 ) 
以此类推。 
答案:
  BOOL型变量:
     if(!var)
 int型变量:
     if(var==0)
 float型变量: 
     const float EPSINON = 0.00001;
      if ((x >= - EPSINON) && (x <= EPSINON)
 指针变量:  
    if(var==NULL)
  注意:浮点型变量并不精确,所以不可将float变量用“==”或“!=”与数字比较,应该设法转化成“>=”或“<=”形式。如果写成if (x == 0.0),则判为错。
比如 float a = 1.0; 
         a /= 7; 
         a *= 7; 
         a -= 1.0; 这时结果就不等于0.0,所以只要精度够了就行

二、描述以下C语言中类型描述的含义:
       #define n  100
        A     int *p ;  定义p, p是一个指向int的指针
        B     int *p[n] ; 定义p, p是一个具有n个元素的数组,每个元素的类型是 指向int的指针
        C     int (*p)[n] ; 定义p, p是一个指针,指向一个具有n个元素的数组,数组的每个元素类型为int
        D     int (*p)() ; 定义p, p是一个指针,指向一个函数,这个函数的类型为 返回值为int, 无参数
        E     int *p(); 声明p, p是一个函数返回类型为指向int的指针,无参数
        F     int **p ; 定义p, p是一个“指向int型指针”的指针
        G     typedef int (*P)(char *(*)()) ; 定义类型P, P是一个函数指针类型,此类函数的返回类型为int, 参数的类型为函数指针(返回值为指向char的指针无参数)

三、以下为Windows NT下的32C程序,请计算sizeof的值
char       str[] = Hello;
char       *p = str;
int   n = 10;
请计算:
      sizeof(str) = 6  要注意最后还有一个'\0'
      sizeof(p)  = 4   32位中所有的指针都是32位的
      sizeof(n)  = 4   32位中int32位的
       ----------------------------------------------------------
void Func(char str[100])
{
请计算:
      sizeof(str) = 数组作为形参时,会退化成同类型的指针
}
       ----------------------------------------------------------
void *p = malloc(100);
请计算:
      sizeof(p) = 4 32位中所有的指针都是32位的,malloc没有关系


四、简答题 
1、头文件中的 ifndef/define/endif 干什么用? 
ifndef 当标识符已经被定义过
define 定义标识符
endif  结束if

2#include  <filename.h>     #include  filename.h 有什么区别? 
使用<>,编译器从标准库路径开始搜索
使用""  编译器从用户的工作路径开始搜索

3const 有什么用途?
   c中,const用来修饰变量,说明它是一个只读类型的,不可以对它进行赋值操作


4 变量可以同时具有constvolatile属性吗?为什么?
 可以,比如说键盘的接口数据,它对于pc来说就是既不可赋值,又是易变的

5、请简述以下A两个for循环的优缺点 
Loop A 判断在循环中,执行效率不高,但代码量少
for (i=0; i<N; i++)
{
if (condition)
{
DoSomething();
}
else
{
DoOtherthine();
}
}
Loop B判断在循环外,执行效率高,但代码量大
if (condition)
{
 for (i=0; i<N; i++)
{
 DoSomethine();
}
}
else
{
for (i=0; i<N; i++)
{
DoOtherthing();
}
}

五、有关内存的思考题请问运行下面4Test后有什么结果。 
A
void GetMemody(char *p)
{
  p= (char *)malloc(100);
}
void Test(void)
{
  char *str = NULL;
  GetMemory(str);
  strcpy(str, Hello word);
  printf(str);
}
GetMemory( char *p )函数的形参为字符串指针,在函数内部修改形参并不能真正的改变传入形参的值,执行完 char *str = NULL; GetMemory( str );后的str仍然为NULL
p指向的内存没有释放。
B
char *GetMemory(void)
{
  char p[] = hello world;
  return p;
}
void Test(void)
{
  char *str = NULL;
  str = GetMemory();
  printf(str);
}
p[]数组为函数内的局部自动变量,在函数返回后,指向的内存已经无效。(变量的生存期问题)
C:
void GetMemory2(char **p, int num)
{
  *p = (char *) malloc(num);
}
void Test(void)
{
  char *str = NULL;
  GetMemory(&str, 100);
  strcpy(str, hello);
  printf(str);
}
GetMemory中执行申请内存及赋值语句 p = (char *) malloc( num );后未判断内存是否申请成功:且最后str指向的内存也没有释放。
D:
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, hello);
free(str);
if (str != NULL)
{
strcpy(str, world);
printf(str);
}
}
在执行 char *str = (char *) malloc(100);后未进行内存是否申请成功的判断;另外,在free(str)后未置str为空,导致后面的判断出现错误


六、编写strcpy函数 
已知strcpy函数的原型是   char *strcpy(char *strDest, const char *strSrc); 
返回为目标字符串的地址
 其中strDest是目的字符串,strSrc是源字符串。 
1)不调用任何库函数,请编写函数 strcpy 
 (2) 此函数返回目标字符串的地址,为什么要返回它?
3)试想一下,strcpy在什么情况下时会出现不期望的结果,为什么会这样?请重写一个函数
char * strmove (char *strDest, const char *strSrc),解决这个问题。
(1)
char* strcpy(char * dst, const char * src)
{
  char * cp = dst;
  while( *cp++ = *src++ ); /* Copy src over dst */
  return( dst );
}
char * strcpy( char *strDest, const char *strSrc )
{
  assert( (strDest != NULL) && (strSrc != NULL) );
  char *address = strDest;
  while( (*strDest++ = * strSrc++) != \0 );
  return address;
}
(2)
     这是为了链式操作,即可以串起来写
strcpy(str_c, strcpy(str_b, str_c));
(3)
srcdst的范围重合时,且dst的开始点位于src之后时,原操作会src被破坏且无限copy直到"死机"的情况出现
char * strmove (char *dst, const char *src)
{
  char * cp = dst;
  int len;
  if (dst <= src) {
    while( *cp++ = *src++ );
  } else {
    len = strlen(src);
    src += len;
    cp += len;
    len++;
    while (len--) {
      *cp-- = *src--;
    }
  }
  return( dst );
}
七、测试题
已知函数atoi,其函数原型是int atoi(char *str)
函数用途:
 
将字符串转换成一个整数值
输入参数:
  str
待转换为整型数的字符串
返回值:
 
成功返回转换后的数值,失败则返回0.
请设计出这个函数的测试用例。

参考答案:
(注:预期结果对不对无所谓,主要看设计的输入条件考虑的是否合理和充分)
Str
预期结果
1
1
“0”
0
abc”(或其他非数字字符串)
0
1a” (或其他数字和非数字的组合字符串)
0
-1”
-1
“01”
1
1\\”
0
“1\0”
1
“1\n”
1
1.0”
1
“1234567890” (或注明较长字符串或int型长的字符串)
“123456789012345678901234567890” (或注明返回较长字符串或int型长的字符串)
“123456789045677775”(或注明超过int型的字符串)
0
“”(空字符串)
0
NULL
0

八、阅读理解
1. 智能卡定义:有运算能力的,支持多种存储方式,支持ISO7816系列规范的,内嵌芯片的塑料卡片。

2. 使用APDU协议,采用主从方式同CAD通信。