Function Pointers

/*$T Fig08_20.cpp GC 1.140 04/10/12 15:37:25 */
/* Multipurpose sorting program using function pointers. */
#include <iostream>
#include <iomanip>
using namespace std;

/* prototypes */
void                        selectionSort(int[], const int, bool(*) (int, int));
void                        swap(int *const, int *const);
bool                        ascending(int, int);        /* implements ascending order */
bool                        descending(int, int);        /* implements descending order */

/*
 =======================================================================================================================
  2012.04.10  By KeKe
 =======================================================================================================================
 */
void main()
{
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        const int        arraySize = 10;
        int                        order;                /* 1= ascending,2= descending */
        int                        counter;        /* array index */
        int                        a[arraySize] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

        cout << "Enter 1 to sort in ascending order,\n" << "Enter 2 to sort in descending order: ";
        cin >> order;
        cout << "\nData items in originalorder\n";

        /* output original array */
        for(counter = 0; counter < arraySize; ++counter) cout << setw(4) << a[counter];

        /*
         * sort array in ascending order;
         * passfunction ascending ;
         * as an argument to specify ascending sorting order
         */
        if(order == 1)
        {
                selectionSort(a, arraySize, ascending);
                cout << "\nData items in ascending order\n";
        }        /* end if */

        /*
         * sort array in descending order;
         * pass function descending ;
         * as an argument to specify descending sorting order
         */
        else
        {
                selectionSort(a, arraySize, descending);
                cout << "\nData items in descending order\n";
        }        /* end else part of if...else */

        /* output sorted array */
        for(counter = 0; counter < arraySize; ++counter) cout << setw(4) << a[counter];
        cout << endl;
}                /* end main */

/*
 =======================================================================================================================
    multipurpose selection sort;
    the parameter compare is a pointer to ;
    the comparison function that determines the sorting order
 =======================================================================================================================
 */
void selectionSort(int work[], const int size, bool (*compare) (int, int))
{
        int smallestOrLargest;                /* index of smallest (orlargest) element */

        /* loop over size -1 elements */
        for(int i = 0; i < size - 1; ++i)
        {
                smallestOrLargest = i;        /* first index of remaining vector */

                /* loop to find index of smallest (or largest) element */
                for(int index = i + 1; index < size; ++index)
                        if(!(*compare) (work[smallestOrLargest], work[index])) smallestOrLargest = index;

                swap(&work[smallestOrLargest], &work[i]);
        }        /* end if */
}                /* end function selectionSort */

/*
 =======================================================================================================================
    swap values at memory locations to which ;
    element1Ptr and element2Ptr point
 =======================================================================================================================
 */
void swap(int *const element1Ptr, int *const element2Ptr)
{
        int hold = *element1Ptr;
        *element1Ptr = *element2Ptr;
        *element2Ptr = hold;
}        /* end function swap */

/*
 =======================================================================================================================
    determine whether element a is less than ;
    element b for an a scending order sort
 =======================================================================================================================
 */
bool ascending(int a, int b)
{
        return a < b;        /* returns true ifa is less than b */
}        /* end function ascending */

/*
 =======================================================================================================================
    determine whether element a is greater than ;
    element b for a descending order sort
 =======================================================================================================================
 */
bool descending(int a, int b)
{
        return a > b;        /* returns true if a is greater than b */
}        /* end function descending */


                                                                                                                                                                                                                                O(∩_∩)O

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值