binary.c

language: C
license: GPL 2

Code for Snippet:

                
#include <stdio.h>
#include <stdlib.h>
 
#define LENGTH 4
 
double          ADD(double, double);
double          SUB(double, double);
double          MUL(double, double);
double          DIV(double, double);
 
double
ADD(double x, double y)
{
    return x + y;
}
 
double
SUB(double x, double y)
{
    return x - y;
}
 
double
MUL(double x, double y)
{
    return x * y;
}
 
double
DIV(double x, double y)
{
    return x / y;
}
 
typedef double  (*binary_function) (double, double);
 
binary_function myFuncs[] = {&ADD, &SUB, &MUL, &DIV};
char           *myFNames[] = {"ADD", "SUB", "MUL", "DIV"};
 
int
main(int argc, char **argv)
{
    double          x, y;
    if (argc != 3) {
        printf("Usage: binary 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++)
        printf("%s of %8.4f and %8.4f is %8.4f\n", myFNames[i], x, y, (*myFuncs[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: 629
Date Added: 2013-03-07 17:15:51
Last Modified: 2013-04-17 22:12:20

Web Analytics