0

I´m new at programming with C.

The code looks like this now. Its a calculator which operates with "+". The first fraction is 1/1. The second fraction is 1/2. The result of this is 0.50. My question is how do I get the result as a fraction and a decimal number? I have a picture at the bottom. Zähler is numerator and Nenner is called denumerator in german. Thank you very much for your help and sorry for my bad english...

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

int main(void)

{
    // Eingabe der ersten und zweiten rationalen Zahl sowie des Operators


    system("chcp 1252 > nul");
    double z1, n1, z2, n2;
    char op;


    // z1 = Zähler 1 , n1 = Nenner 1, z1/n1 = Bruch 1
    printf("Eingabe der ersten rationalen Zahl\n");
    printf("Zähler 1:");
    scanf("%lf", &z1);
    printf("Nenner 1:");
    scanf("%lf", &n1);


    // op = Operator
    printf("Eingabe des Operators +, -, *, /:\n");
    scanf("%s", &op);

    // z2 = Zähler 2,  n2 = Nenner 2, z2/n2 = Bruch 2
    printf("Eingabe der zweiten rationalen Zahl\n");
    printf("Zähler 2:");
    scanf("%lf", &z2);
    printf("Nenner 2:");
    scanf("%lf", &n2);


    // Addition

    if (op == '+')
    {
        printf("Summe aus Bruch 1 und Bruch 2:\n");
        printf("%.lf/%.lf + %.lf/%.lf = %.lf/%.lf", z1, n1, z2, n2, (z1/n1) + (z2/n2));
            -**> do I need to change something up here to get a fraction after the decimal number?**
    }

Best regards

Eduard

Ôrel
  • 7,044
  • 3
  • 27
  • 46
Edi
  • 1
  • 1
  • oh sorry guys i meant the operator is a "-". – Edi Aug 10 '22 at 19:30
  • 1
    The screenshot is tiny and entirely useless. Can you post the output as plain text? – tadman Aug 10 '22 at 19:34
  • Note: `%s` is a C string, and `op` is a *single character*. You need `%c` for input there. You're writing two bytes to that single character, which is out of bounds. – tadman Aug 10 '22 at 19:34
  • Are you really asking "How can I add fractions in C?" – tadman Aug 10 '22 at 19:36
  • I know its so stupid to ask. But I dont know yet how to get the result as a fraction and a decimal number... haha... – Edi Aug 10 '22 at 19:40
  • There is no built-in fractional number system in C. You need to find a library or write your own. You only get integer values and floating point values, that's it. – tadman Aug 10 '22 at 19:41
  • To get the result as a fraction, compute the numerator and denominator separately. Then print the fraction with `printf("%lf/%lf\n", numerator, denominator)`. Compute the numerator by cross multiplying and subtracting `Z = z1*n2 - z2*n1`. The denominator is the product `N = n1*n2`. To reduce the fraction, divide the numerator and denominator by the `GCD(Z, N)`. Handling improper fractions can be done with the division `/` and remainder `%` operators, with special handling for negative numbers. Special handling is also needed if the denominator is 1 after dividing by the GCD. – user3386109 Aug 10 '22 at 20:01
  • here is c# code - quite easy to translate to C https://stackoverflow.com/questions/7564906/convert-double-to-fraction-as-string-in-c-sharp – 0___________ Aug 10 '22 at 20:29
  • `a/b + c/d ==> ad/bd + bc/bd ==> (ad + bc)/bd`, so `5/7 + 2/3` is `(5*3 + 7*2)/21` or `29/21` – pmg Aug 11 '22 at 09:35
  • @Eduard, I need to ask you to translate the program messages and the comments into english. I'm so sorry of not being able to speak German, and I think you would not want to receive my answer in spanish either. Thanks in advance. – Luis Colorado Aug 13 '22 at 12:08

3 Answers3

2

Use correct specifier

To save an single non-white-space character, use " %c"

char op;
....
// scanf("%s", &op);
scanf(" %c", &op);

Math

how do I get the result as a fraction ...?

Multiply the first fraction by n2/n2 and the 2nd by n1/n1, then add.

double top = z1*n2 + z2*n1;
double bottom = n1*n2;

printf("%g/%g\n", top, bottom);

This may not be the simplest form. Additional work needed to reduce.

how do I get the result as .... a decimal number?

printf("%g\n", top / bottom);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Start with an example or two (or three)

0.142857 is approximately 1/7.

What is 14/100? That's equal to 7/50. That's an approximate version of 1/7.

What is 143/1000? That's 71.5/500... Getting closer!

What is 1429/10000? That's 714.45/500... Even closer!

So, just by multiplying the decimal by 10^x and using 10^x as the denominator, then trying to find common divisors, you approximate the decimal with a ratio...

You could investigate GCD (Greatest Common Divisor) as suggested by user3386109.

Fe2O3
  • 6,077
  • 2
  • 4
  • 20
0

I have written the code all over again with english comments. It works now thank you very much.

To get the result as a fraction I followed the instruction of Chux - Reinstate Monica.

Multiply the first fraction by n2/n2 and the 2nd by n1/n1, then add.

double top = z1*n2 + z2*n1;
double bottom = n1*n2;

printf("%g/%g\n", top, bottom);

This may not be the simplest form. Additional work needed to reduce.

how do I get the result as .... a decimal number?

 printf("%g\n", top / bottom);

Code:

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


int main(void)

{
    // input of the first rational and the second number such as the operator


    system("chcp 1252 > nul");
    double z1, n1, z2, n2;
    char op;


    // z1 = numerator 1 , n1 = denominator 1
    printf("First rational number\n");
    printf("Numerator 1:");
    scanf("%lf", &z1);
    printf("Denominator 1:");
    scanf("%lf", &n1);


    // op = operator
    printf("Input of the operator +, -, *, /:\n");
    scanf("%s", &op);

    // z2 = numerator 2,  n2 = denominator 2
    printf("Second rational number\n");
    printf("Numerator 2:");
    scanf("%lf", &z2);
    printf("Denominator 2:");
    scanf("%lf", &n2);


    // addition

    if (op == '+')
    {
        double top = z1*n2 + z2*n1;
        double bottom = n1*n2;
        printf("Total of fraction 1 und fraction 2:\n");
        printf("%.lf/%.lf + %.lf/%.lf = %.2lf\n", z1, n1, z2, n2, top, bottom);
        printf("Result as a fraction: %g/%g\n:", top, bottom);
    }



    // subtraction
    else if (op == '-')
    {
        double top = z1*n2 - z2*n1;
        double bottom = n1*n2;
        printf("Difference of fraction 1 und fraction 2:\n");
        printf("%.lf/%.lf - %.lf/%.lf = %g\n", z1, n1, z2, n2, top, bottom);
        printf("Result as a fraction: %g/%g\n", top, bottom);
    }






    // multiplication
    else if (op == '*')
    {
        double top = z1*z2;
        double bottom = n1*n2;
        printf("Product of fraction 1 und fraction 2:\n");
        printf("%.lf/%.lf * %.lf/%.lf = %.2lf\n", z1, n1, z2, n2, top, bottom);
        printf("Result as a fraction: %g/%g\n", top, bottom);
    }



    // division
    else if (op == '/')
    {
        double top = z1*n2;
        double bottom = n1*z2;
        printf("Quotient of fraction 1 und fraction 2:\n");
        printf("%.lf/%.lf / %.lf/%.lf = %.2lf\n", z1, n1, z2, n2, top, bottom);
        printf("Result as a fraction: %g/%g\n ", top, bottom);

    }




    return 0;
}
Edi
  • 1
  • 1
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Aug 20 '22 at 01:08