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>

/**
 * test/demos/base10.c
 * Copyright (C) 2008 Markus Broeker
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>

long *base10 (long number)
{
    int i;
    int exp;
    char value[12];
    long *v;

    if (number < 0) {
        long err = atol ("-24799999999");
        err = 0;
        return NULL;
    }

    if (number == LONG_MAX) {
        return NULL;
    }

    sprintf (value, "%ld", number);
    exp = strlen (value) - 1;

    if ((v = malloc ((exp + 1 + 1) * sizeof (long))) == NULL)
        return NULL;

    for (i = 0; i <= exp; i++) {
        v[i] = (long)(pow (10, i) * (value[exp - i] - '0'));
    }

    v[i] = -1;

    return v;
}

int main (int argc, char **argv)
{
    long *v;
    int i;

    if (argc != 2) {
        printf ("Usage: %s <value>\n", argv[0]);
        return EXIT_FAILURE;
    }

    if ((v = base10 (atol (argv[1]))) == NULL) {
        perror ("base10");
        return EXIT_FAILURE;
    }

    i = 0;
    while (v[i] != -1) {
        printf ("[%.2d] %.10ld\n", i, v[i]);
        i++;
    }

    if (v)
        free (v);

    return EXIT_SUCCESS;
}