簡體   English   中英

C -> 如何獲得分數?

[英]C -> How do I get the result as a fraction?

我是使用 C 編程的新手。

代碼現在看起來像這樣。 它是一個用“+”操作的計算器。 第一個分數是 1/1。 第二個分數是 1/2。 結果是 0.50。 我的問題是如何獲得分數和十進制數的結果? 我在底部有一張圖片。 Zähler 是分子,Nenner 在德語中被稱為分子。 非常感謝您的幫助,並為我的英語不好感到抱歉...

#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?**
    }

此致

愛德華

使用正確的說明符

要保存單個非空白字符,請使用" %c"

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

數學

我如何得到分數的結果......?

將第一個分數乘以n2/n2並將第二個分數乘以n1/n1 ,然后相加。

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

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

這可能不是最簡單的形式。 需要減少額外的工作。

我如何得到結果為....一個十進制數?

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

從一個或兩個(或三個)示例開始

0.142857 約為 1/7。

什么是 14/100? 這等於 7/50。 這是 1/7 的近似版本。

什么是 143/1000? 那是 71.5/500... 越來越近了!

什么是 1429/10000? 那是 714.45/500... 更近了!

因此,只需將小數乘以 10^x 並使用 10^x 作為分母,然后嘗試找到公約數,就可以用比率近似小數...

您可以按照user3386109的建議調查 GCD(最大公約數)。

我用英文注釋重新編寫了代碼。 它現在可以工作了,非常感謝。

#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM