/*
 * Soubor:  20110318-Fri-odkaz-na-funkce.c
 * Datum:   18.03.2011
 * Autor:   Marek Nožka, nozka <@t> spseol <d.t> cz
 * Licence: GNU/GPL 
 * Úloha: 
 * Popis:   
 ****************************************************/
#define _ISOC99_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <limits.h>
#include <math.h>

#include <unistd.h>             /* rozlišení stdio */

/* ***************    Makra    ******************** */
#define PROMPT " > "
#define ARRAYC 1024

/****************************************************
 *                 Funkce
 ****************************************************/

double deg2rad(double deg)
{
    return deg * M_PI / 180;
}

double rad2rad(double rad)
{
    return rad;
}


/****************************************************
 *               Hlavní program.
 ****************************************************/
int main(void)
{
    char line[ARRAYC];

    /* f_convert je odkaz na funkci, která přebírá double
     * a vrací double                          */
    double (*f_convert) (double);
    double from, to, step;
    char choice[ARRAYC];

    f_convert = deg2rad;
    /* Čte řádky až do konce souboru */
    while (printf(PROMPT), (fgets(line, ARRAYC, stdin) != NULL)) {
        if (sscanf(line, "%s %lg %lg %lg", choice, &from, &to, &step) == 4) {
            printf("%g\n", sin(f_convert(from)));
        } else if (sscanf(line, "%s %lg", choice, &from) == 2) {
            ;
        } else if (sscanf(line, "%s", choice) == 1) {
            if (strcmp(choice, "r") == 0) {
                f_convert = rad2rad;
            } else if (strcmp(choice, "d") == 0) {
                f_convert = deg2rad;
            } else {
                fprintf(stderr, "toto neni platny vstup\n");
            }
        }
    }
    return 0;
}

