1)

main.c: In the main function, you are expected to read two fractional numbers (a fractional
number is composed of two integers representing the numerator and denominator of the
fraction) and perform the four basic arithmetic operations on these, namely, addition,
subtraction, multiplication and division. You are expected to use the function provided in utils.h.

2)

utils.c: There are six functions in this file. Two of them are fully implemented for you. The other
four, you are expected to complete them to provide the correct functionality. Note that two
fractions after going through an arithmetic operaction may need to be simplified. The
simplification is done by dividing both the numerator and denominator by their greatest common
divisor. This means that you will need to find the GCD of two integers (you do not need to write
a separate function for this).

main.c

#include <stdio.h>
#include "util.h"


int main() {

    /* A fractional number: 13/7 */
    int num1 = 13, den1 = 7;
    /* A fractional number: 13/7 */
    int num2 = 30, den2 = 11;
    /* An unitilized fractional number */
    int num3, den3;

    printf("First number: \n");
    printf("numerator\n");
    scanf("%d",&num1);//getting input from user
    printf("denominator\n");
    scanf("%d",&den1);//getting input from user
    fraction_print(num1, den1);//calling the print function
    printf("\n");

    printf("Second number:\n");
    printf("numerator\n");
    scanf("%d",&num2);//getting input from user
    printf("denominator\n");
    scanf("%d",&den2);//getting input from user
    fraction_print(num2, den2);//calling the print function
    printf("\n");

    printf("Addition: ");
    fraction_add(num1, den1, num2, den2, &num3, &den3);//addition function is calling
    fraction_print(num3, den3);//calling the print function
    printf("\n");

    printf("Subtraction: ");
    fraction_sub(num1, den1, num2, den2, &num3, &den3);//subtraction function is calling
    fraction_print(num3, den3);//calling the print function
    printf("\n");

    printf("Multiplication: ");
    fraction_mul(num1, den1, num2, den2, &num3, &den3);//multiplication function is calling
    fraction_print(num3, den3);//calling the print function
    printf("\n");

    printf("Division: ");
    fraction_div(num1, den1, num2, den2, &num3, &den3);//division function is calling
    fraction_print(num3, den3);//calling the print function
    printf("\n");
    printf("\n");

    return(0);
}

util.c

#include <stdio.h>
#include "util.h"

void fraction_print(int numerator, int denominator) {
    printf("%d//%d", numerator, denominator);
}  /* end fraction_print */

void fraction_add(int n1, int d1, int n2, int d2, int * n3, int * d3) {
    *n3 = n1*d2 + n2*d1;//multiply the numerator and denominator and find the total numerator
    *d3 = d1*d2;//multiply the denominators and find the common denominator
    fraction_simplify(n3, d3);
}
void fraction_sub(int n1, int d1, int n2, int d2, int * n3, int * d3) {
    *n3 = n1*d2 - n2*d1;//multiply the numerator and denominator and find the total numerator
    *d3 = d1*d2;//multiply the denominators and find the common denominator
    fraction_simplify(n3, d3);//Calling the simplification function
} 

void fraction_mul(int n1, int d1, int n2, int d2, int * n3, int * d3) {
    *n3 = n1*n2;//numerator are multiplied
    *d3 = d1*d2;//denominators are multiplied
    fraction_simplify(n3, d3);//Calling the simplification function
} /* end fraction_mul */

void fraction_div(int n1, int d1, int n2, int d2, int * n3, int * d3) {
    *n3 = n1*d2;//multiplying the first numerator by the second denominator
    *d3 = n2*d1;//multiplying the first denominator by the second numerator
    fraction_simplify(n3, d3);//Calling the simplification function
} /* end fraction_div */

/* Simplify the given fraction such that they are divided by their GCD */
void fraction_simplify(int *n, int *d) {
    int i,gcd,x0= *n,x1= *d;
    if(x0<=0){//checking for negativity of the numerator
        x0=x0*(-1);//multiply numerator to minus one
    }
    if(x1<=0){//checking for negativity of the denominator
        x1=x1*(-1);//multiply denominator to minus one
    }
    for(i=1; i <= x0 && i <= x1; ++i){//gcd find cycle starts
        if(x0%i==0 && x1%i==0){//numerator and denominator have the greatest divisor 
            gcd=i;
        }
    }
    if(*n <=0 && *d <=0){//the state of being both negative 
        gcd=gcd*(-1);//gcd multiply to minus one
    }
    *n = *n /gcd;//simplification done here 
    *d = *d /gcd;//simplification done here 
} /* end fraction_div */

util.h

#ifndef _UTIL_H_
#define _UTIL_H_

void fraction_print(int numerator, int denominator);

void fraction_add(int n1, int d1, int n2, int d2, int * n3, int * d3);

void fraction_sub(int n1, int d1, int n2, int d2, int * n3, int * d3);

void fraction_mul(int n1, int d1, int n2, int d2, int * n3, int * d3);

void fraction_div(int n1, int d1, int n2, int d2, int * n3, int * d3);

void fraction_simplify(int * n, int * d);

#endif /* _UTIL_H_ */

makefile

all: clean compile run

compile: main.c util.c util.h
	@echo "-------------------------------------------"
	@echo "Compiling..."
	@gcc -o test main.c util.c

run:
	@echo "-------------------------------------------"
	@echo "Running the tests...."
	@echo "================================================================================="
	./test
	@echo "================================================================================="
	@echo "Completed tests...."

clean:
	@echo "-------------------------------------------"
	@echo "Removing compiled files..."
	@rm -f *.o
	@rm -f test

1 Reply to “C programming fractional numbers calculator”

Leave a Reply

Your email address will not be published. Required fields are marked *