Thread: keep getting two errors when working with fractions

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    keep getting two errors when working with fractions

    Maybe someone can help me here too. The programm is meant to do basic calculation operations with fractions. Got everything done now, but I still get two errors when I use the programme.
    Weirldy, some fractions dont make me problems at all, but on some the program just randomly stops doing anything in the division part, nothing happens anymore.
    The other one is when I use the loop. I made it, that you can go for several loops until you tipe a "0" to stop the program. But when I go for the second loops it says Stack Overflow and I dont know why.

    This is the main .c
    Code:
    Code:
    #include "rational.h"
    
    int main(void)
    {
        int test;
    
    
        do {
            fraction1.denominator=0;
            fraction1.numerator=0;
            fraction2.denominator = 0;
            fraction2.numerator = 0;
            result.denominator = 0;
            result.numerator = 0;
            LMC = 0;
    
    
    
    
            read_rational();
            getch();
            addition(fraction1, fraction2);
            if (result.denominator < 0)
            {
                result.denominator = -result.denominator;
                result.numerator = -result.numerator;
            }
            printf("Das Ergebnis der Addition beider Brüche ist:\n");
            print_rational(result);
    
    
    
    
    
    
    
    
    
    
            result = subtraction(fraction1, fraction2);
            if (result.denominator < 0)
            {
                result.denominator = -result.denominator;
                result.numerator = -result.numerator;
            }
            printf("Das Ergebnis der Subtraktion beider Brüche ist:\n");
            print_rational(result);
    
    
    
    
    
    
    
    
            result = division(fraction1, fraction2);
            if (result.denominator < 0)
            {
                result.denominator = -result.denominator;
                result.numerator = -result.numerator;
            }
            printf("Das Ergebnis der Division beider Brüche ist:\n");
            print_rational(result);
    
    
    
    
    
    
            result = multiplication(fraction1, fraction2);
            if (result.denominator < 0)
            {
                result.denominator = -result.denominator;
                result.numerator = -result.numerator;
            }
            printf("Das Ergebnis der Multiplikation beider Brüche ist:\n");
            print_rational(result);
    
    
            printf("Wenn sie aus der Schleife aussteigen wollen, geben sie '0' ein. Ansonsten etwas beliebiges anderes\n");
            scanf_s("%d\n\n\n", &test);
        } while (test != 0);
    
    
    
    
        return 0;
    }
    
    here is the header, but there shouldnt be a problem
    Code:
    Code:
    #ifndef rational_h#define rational_h
    
    
    #include <stdio.h>
    
    
    
    
    typedef struct {
        long numerator, denominator;
    }rational;
    
    
    rational fraction1;
    rational fraction2;
    rational result;
     long LMC;
    
    
    rational addition(rational, rational);
    rational subtraction(rational, rational);
    rational multiplication(rational, rational);
    rational division(rational, rational);
    void print_rational(rational);
    rational read_rational(void);
    long ggT(long, long);
    long ggT1(long, long);
    
    
    #endif
    
    and here is the .c for the module. there is the division part also where the program stops. and when I get the other error with stack overflow its at the ggT function (at least there is where the cursor stops
    Code:
    Code:
    #include "rational.h"
    
    
    
    
    
    rational addition(rational fraction1, rational fraction2)
    {
    
    
        result.numerator = fraction1.numerator + fraction2.numerator;
        result.denominator = fraction1.denominator;
        if (result.numerator < result.denominator)
        {
            LMC = ggT1(result.numerator, result.denominator);
        }
        else
        {
            LMC = ggT1(result.denominator, result.numerator);
        }
    
    
        result.numerator /= LMC;
        result.denominator /= LMC;
        return result;
    }
    
    
    
    
    
    
    rational subtraction(rational fraction1, rational fraction2)
    {
        result.numerator = fraction1.numerator-fraction2.numerator;
        result.denominator = fraction1.denominator;
        if (result.numerator < result.denominator)
        {
            LMC = ggT1(result.numerator, result.denominator);
        }
        else
        {
            LMC = ggT1(result.denominator, result.numerator);
        }
    
    
        result.numerator /= LMC;
        result.denominator /= LMC;
        return result;
    }
    
    
    
    
    
    
    rational multiplication(rational fraction1, rational fraction2)
    {
        result.numerator = fraction1.numerator * fraction2.numerator;
        result.denominator = fraction1.denominator*fraction2.denominator;
        if (result.numerator < result.denominator)
        {
            LMC = ggT1(result.numerator, result.denominator);
        }
        else
        {
            LMC = ggT1(result.denominator, result.numerator);
        }
    
    
        result.numerator /= LMC;
        result.denominator /= LMC;
    
    
        return result;
    }
    
    
    
    
    
    
    rational division(rational fraction1, rational fraction2)
    {
    
    
        result.numerator = fraction1.numerator * fraction2.denominator;
        result.denominator = fraction1.denominator*fraction2.numerator;
    
    
        if (result.numerator < result.denominator)
        {
            LMC=ggT1(result.numerator, result.denominator);
        }
        else
        {
            LMC = ggT1(result.denominator, result.numerator);
        }
    
    
        result.numerator /= LMC;
        result.denominator /= LMC;
        
        return result;
    }
    
    
    
    
    
    
    long ggT(long a, long b)
    {
        static long helper = 0;
        helper += b;
    
    
    
    
    
    
        if (helper%a == 0 && helper%b == 0)
        {
            return helper;
        }
        else 
        {
            return ggT(a, b);
        }
    
    
        
        }
    
    
    long ggT1(long a, long b)
    {
        int r = 0;
    
    
        if (a == 0)
        {
            return b;
        }
        while (b != 0)                       
        {
            if (a > b)
            {
                a = a - b;             
            }
            else
            {
                b = b - a;            
            }
        }
        return a;                           
    }
    
    
    
    
    rational read_rational(void)
    {
        printf("Geben sie den Zaehler des ersten Bruchs ein\n");
        scanf_s("%x", &fraction1.numerator);
    
    
        printf("Geben sie den Nenner des ersten Bruchs ein\n");
        scanf_s("%x", &fraction1.denominator);
    
    
        printf("Geben sie den Zaehler des zweiten Bruchs ein\n");
        scanf_s("%x", &fraction2.numerator);
    
    
        printf("Geben sie den Nenner des zweiten Bruchs ein\n");
        scanf_s("%x", &fraction2.denominator);
    
    
        LMC=ggT(fraction1.denominator, fraction2.denominator);
        fraction1.numerator *= (LMC / fraction1.denominator);
        fraction1.denominator = LMC;
    
    
        fraction2.numerator *= (LMC / fraction2.denominator);
        fraction2.denominator = LMC;
    
    
        return fraction1;
    }
    
    
    void print_rational(rational result)
    {
        printf("%ld/%ld\n\n", result.numerator, result.denominator);
    }
    

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You asked this question on your other thread, let's keep it there -> keep getting an to me unknown error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with fractions in C++
    By Agnes Roguska in forum C++ Programming
    Replies: 43
    Last Post: 02-19-2012, 05:26 PM
  2. Fractions
    By madmax2006 in forum C Programming
    Replies: 4
    Last Post: 06-09-2010, 11:58 PM
  3. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  4. No script errors. Why isnt my idea working?
    By Queatrix in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 06:31 PM
  5. Counter Errors for otherwise working program???
    By jereland in forum C Programming
    Replies: 7
    Last Post: 03-22-2004, 08:37 PM

Tags for this Thread