C - Mathematics - Fraction simplifier

Today, I inaugurate a new C section: mathematics.

The first tutorial is a way to simplify a fraction.

I put all data in a structure that I typedefined as t_bp.
I also made 3 files: main.c, misc.c and h.h.
There is also an error manager inside the code.

For example, the numbers allowed are 1 until 9. So if you type something else, it will generate an error.

And of course, you have to enter 2 arguments: the numerator and the denominator.
Notice that in this program you can use negative numbers.

Let's see this in details, with this full example.

Simplifying a fraction

main.c

/* main.c */

#include <stdio.h>

#include "h.h"

int     main(int ac, char *av[])
{
    t_bp b;

    init(ac, av, &b);
    debug(&b);
    return 0;
}

misc.c

/* misc.c  */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "h.h"

void init(int ac, char *av[], t_bp *b)
{

    if (ac < 3)
    {
        printf("Usage: ./math [numerator] [denominator]\n");
        exit(1);
    }
    check_number(av[1]);
    check_number(av[2]);
    b->numerator = atoi(av[1]);
    b->numeratorAbs = abs(b->numerator);
    b->denominator = atoi(av[2]);
    b->denominatorAbs = abs(b->denominator);
    if (b->numerator == 0 || b->denominator == 0)
    {
        fprintf(stderr, "Error: %s\n", "Allowed characters: [1-9]");
        exit(1);
    }
}

void check_number(char *av)
{
    unsigned int i;

    i = 0;
    if (av[0] == '-')
    {
        i = 1;
    }
    while (i < strlen(av))
    {
        if (av[i] < 48 || av[i] > 57)
        {
            fprintf(stderr, "Error: %s\n", "Allowed characters: [1-9]");
            exit(1);
        }
        ++i;
    }
}

void debug(t_bp *b)
{
    printf("\n===== START =====\n\n");
    printf("Numerator = %d\n", b->numerator);
    printf("Denominator = %d\n", b->denominator);
    compare_number(b);
    calculate_result(b);
    manage_negative(b);
    printf("Result simplified = %c(%d / %d)\n", b->negative, b->numeratorAbs, b->denominatorAbs);
    printf("\n===== END =====\n\n");
}

void compare_number(t_bp *b)
{
    if (b->numeratorAbs > b->denominatorAbs)
    {
        b->smallest = b->denominatorAbs;
    }
    else if (b->numeratorAbs < b->denominatorAbs)
    {
        b->smallest = b->numeratorAbs;
    }
    else
    {
        b->smallest = b->numeratorAbs;
    }
}

void calculate_result(t_bp *b)
{
    b->result = b->numeratorAbs / b->denominatorAbs;
    if ((b->result * b->denominatorAbs) == b->numeratorAbs)
    {
        simplify_simple(b);
    }
    else
    {
        simplify_remainder(b);
    }
}

void simplify_simple(t_bp *b)
{
    b->numeratorAbs = b->numeratorAbs / b->denominatorAbs;
    b->denominatorAbs = 1;
}

void simplify_remainder(t_bp *b)
{
    int i;
    int tmp;

    tmp = b->smallest;
    i = 1;
    while (tmp > i)
    {
        if (((b->numeratorAbs % tmp) == 0) &&
            ((b->denominatorAbs % tmp) == 0))
        {
            b->simplifier = tmp;
            b->numeratorAbs /= b->simplifier;
            b->denominatorAbs /= b->simplifier;
            break;
        }
        else
        {
            --tmp;
        }
    }
}

void manage_negative(t_bp *b)
{
    b->negative = '+';
    if (b->numerator > 0 && b->denominator < 0)
    {
        b->negative = '-';
    }
    else if (b->numerator < 0 && b->denominator > 0)
    {
        b->negative = '-';
    }
}

h.h

#ifndef H_H_
#define H_H_

/**
 * Structure
 */

typedef struct badprog
{
    int numerator;
    int numeratorAbs;
    int denominator;
    int denominatorAbs;
    int simplifier;
    int smallest;
    char negative;
    int result;
} t_bp;


/**
 * Prototype
 */
void init(int ac, char *av[], t_bp *b);
void debug(t_bp *b);
void compare_number(t_bp *b);
void calculate_result(t_bp *b);
void simplify_simple(t_bp *b);
void simplify_remainder(t_bp *b);
void check_number(char *av);
void manage_negative(t_bp *b);

#endif /* H_H_ */

Compiling

I've chosen 29623692 as numerator and 39498256 as denominator.

$ gcc main.c misc.c -o fraction-simplifier ; ./fraction-simplifier 29623692 39498256

Result

The last line tells us the result as a simplified fraction.

===== START =====

Numerator = 29623692
Denominator = 39498256
Result simplified = +(3 / 4)

===== END =====

Who said math was a complicated science? cool

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.