STL template & container-Function Point & generic class

Source: Internet
Author: User
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;

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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.