STL template&Container--function point & generic class
please wirte a program to realize the model described in the figure. you shoudl design your program as negeric as possible so that we can enhance the model in the future ealily without making too much change in your program(SAP 2005)
A1:
//using function point
#include<stdio.h>
int jug(int x,int y)
{
if(x>=0)
return x;
else if(y==0)
return y;
else
return x/y;
}
int sun(int x,int y)
{
return x+y;
}
int minus(int x,int y)
{
return x-y;
}
//use function point to call alternative function
void test(int (*p)(int,int),int a,int b)
{
int result;
result=(*p)(a,b);
printf("a=%d,b=%d,result=%d",a,b,result);
}
int main()
{
int a=1,b=2,c=3,d=4,c=-5;
test(sub,a,b);
test(minus,c,d);
test(jug,e,b);
return 0;
}
A2:
//using template
#include <iostream>
template<class T>
class operate{
public:
static T add(T a, T b){
return a+b;
}
static T Mul(T a,T b){
return a*b;
}
static T Jug(T a,T b){
if(a>=0)
return a;
else
return a/b;
}
};
int main(){
int a,b,c,d,e,x,y,z;
a=1,b=2,c=3,d=4,e=-5;
x=operate<int>::Add(a,b);
y=operate<int>::Mul(c,d);
z=operate<int>::Jug(e,b);
printf("%d,%d,%d",x,y,z);
return 0;
}
A1:
//using function point
#include<stdio.h>
int jug(int x,int y)
{
if(x>=0)
return x;
else if(y==0)
return y;
else
return x/y;
}
int sun(int x,int y)
{
return x+y;
}
int minus(int x,int y)
{
return x-y;
}
//use function point to call alternative function
void test(int (*p)(int,int),int a,int b)
{
int result;
result=(*p)(a,b);
printf("a=%d,b=%d,result=%d",a,b,result);
}
int main()
{
int a=1,b=2,c=3,d=4,c=-5;
test(sub,a,b);
test(minus,c,d);
test(jug,e,b);
return 0;
}
A2:
//using template
#include <iostream>
template<class T>
class operate{
public:
static T add(T a, T b){
return a+b;
}
static T Mul(T a,T b){
return a*b;
}
static T Jug(T a,T b){
if(a>=0)
return a;
else
return a/b;
}
};
int main(){
int a,b,c,d,e,x,y,z;
a=1,b=2,c=3,d=4,e=-5;
x=operate<int>::Add(a,b);
y=operate<int>::Mul(c,d);
z=operate<int>::Jug(e,b);
printf("%d,%d,%d",x,y,z);
return 0;
}