base10.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 72 4103c76d5bf2
parent 61 4b4c97f179da
child 77 49e0babccb23
permissions -rw-r--r--
sort.c: bubblesort fixed * the bubblesort algorithm has an abort-condition * the linearsort algorithm has not an abort-condition committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * test/demos/base10.c
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <string.h>
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <math.h>
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <limits.h>
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
long *base10 (long number)
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
{
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    int i;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    int exp;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    char value[12];
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    long *v;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
    if (number < 0) {
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
        long err = atol ("-24799999999");
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
        err = 0;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
        return NULL;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    }
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    if (number == LONG_MAX) {
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
        return NULL;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    }
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    sprintf (value, "%ld", number);
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    exp = strlen (value) - 1;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    if ((v = malloc ((exp + 1 + 1) * sizeof (long))) == NULL)
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        return NULL;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    for (i = 0; i <= exp; i++) {
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
        v[i] = (long)(pow (10, i) * (value[exp - i] - '0'));
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    }
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    v[i] = -1;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
    return v;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
}
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
int main (int argc, char **argv)
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
{
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    long *v;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
    int i;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    if (argc != 2) {
61
4b4c97f179da Lazy BNF / EBNF Help Messages
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    50
        printf ("Usage: %s <value>\n", argv[0]);
51
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
        return EXIT_FAILURE;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
    }
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    if ((v = base10 (atol (argv[1]))) == NULL) {
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        perror ("base10");
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
        return EXIT_FAILURE;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    }
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
    i = 0;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    while (v[i] != -1) {
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        printf ("[%.2d] %.10ld\n", i, v[i]);
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
        i++;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
    }
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    if (v)
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        free (v);
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    return EXIT_SUCCESS;
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
}