binblock.c

language: C
license: GPL 2

Code for Snippet:

                
#include <stdio.h>
#include <stdlib.h>
 
#define LENGTH 4
 
char           *myFNames[] = {"ADD", "SUB", "MUL", "DIV"};
 
typedef double  (^binary_block) (double, double);
 
binary_block ADD = ^(double x, double y){return x + y;};
binary_block SUB = ^(double x, double y){return x - y;};
binary_block MUL = ^(double x, double y){return x * y;};
binary_block DIV = ^(double x, double y){return x / y;};
 
binary_block *myBlocks[] = {&ADD, &SUB, &MUL, &DIV};
 
typedef void (^print_block) (int, double, double); 
 
print_block myPrint = ^(int i, double x, double y) { printf("%s of %8.4f and %8.4f is %8.4f\n", myFNames[i], x, y, (*myBlocks[i]) (x, y));};
 
int
main(int argc, char **argv)
{
    double          x, y;
    if (argc != 3) {
        printf("Usage: binblock x y, with x and y numbers\n");
        return EXIT_FAILURE;
    }
    x = atof(argv[1]);
    y = atof(argv[2]);
    for (int i = 0; i < LENGTH; i++)
        myPrint (i, x, y);
    return EXIT_SUCCESS;
}
 
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 533
Date Added: 2013-03-07 17:14:31
Last Modified: 2013-04-17 22:12:19

Web Analytics