#include "iostream.h"
#include "math.h"
#define bits ( sizeof( int ) * 8 - 1 )
static int CheckFlag( int x, int y )
{
int s1 = x >> bits;
int s2 = y >> bits;
s1=abs(s1);
s2=abs(s2);
return ( s1 * 2 + s2 );
}
static int SameSign( int Flag, int x, int y )
{
int t[2] = { x, y };
long z = x - y;
int s = z >> (bits+32);
s=abs(s);
return t[s];
}
static int DiffSign(int Flag,int x,int y) 比較
{
int t[2] = { x, y };
return t[Flag - 1];
}
static int (*FuncList[4])(int Flag,int x,int y)={SameSign, DiffSign, DiffSign, SameSign};
static int Max( int x, int y )
{
int Flag = CheckFlag( x, y );
return (*FuncList[Flag])(Flag,x,y);
}
void main()
{
int x,y,z;
cout<<"請輸入整型x的值(-32768~32767):";
cin>>x;
cout<<"請輸入整型y的值(-32768~32767):";
cin>>y;
z=Max(x,y);
cout<<"最大的數:"<<z<<endl;
}
#define bits ( sizeof( int ) * 8 - 1 ) //移動的位數
static int SameSign( int Flag, int x, int y ) //函數功能:x,y同號時的比較
static int DiffSign(int Flag,int x,int y) //函數功能:x,y異號時的比較
static int (*FuncList[4])(int Flag,int x,int y)={SameSign, DiffSign, DiffSign, SameSign}; //定義函數指針數組
static int Max( int x, int y ) //函數功能:求最大值
static int CheckFlag( int x, int y ) //函數功能:檢測x,y的符號
/*
Flag 的值
+ +:0
- +;1
+ 1:2
- -:3
*/