Hướng dẫn how to simplify fractions c++ - cách đơn giản hóa phân số c ++

-2

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Trong lập trình C khi tôi chia 2 số như 2/4, nó cung cấp đầu ra 0,5 nhưng tôi muốn 1/2. Vì vậy, tôi muốn biết cách thực hiện bộ phận để nhận câu trả lời trong các phân số như tử số/mẫu số. Tôi muốn ở C.

Bill Tarbell

4.6382 Huy hiệu vàng33 Huy hiệu bạc49 Huy hiệu đồng2 gold badges33 silver badges49 bronze badges

Đã hỏi ngày 21 tháng 6 năm 2017 lúc 6:53Jun 21, 2017 at 6:53

8

Những gì bạn thực sự muốn là giảm phân số .. không tính toán bộ phận.

Đây là một mẫu nhanh sẽ mang lại phần giảm tỷ lệ:

#include 
#include 

//gcf function - return gcd of two numbers
int gcd[int n, int m]
{
    int gcd, remainder;

    while [n != 0]
    {
        remainder = m % n;
        m = n;
        n = remainder;
    }

    gcd = m;

    return gcd;
}//end gcd function

int main [int argc, const char * argv[]] {
    // insert code here...
    //--declarations
    int number1, number2;
    int newNumber1, newNumber2;

    //--get user input
    printf["Enter a fraction: "];
    scanf["%d/%d", &number1, &number2];

    //--calculations
    //find the gcd of numerator and denominator
    //then divide both the numerator and denominator by the GCD
    newNumber1 = number1 / gcd[number1, number2];
    newNumber2 = number2 / gcd[number1, number2];

    //--results
    printf["In lowest terms: %d/%d", newNumber1, newNumber2];
}

Mẫu lấy từ: //snipplr.com/view/42917/

Đã trả lời ngày 21 tháng 6 năm 2017 lúc 8:34Jun 21, 2017 at 8:34

Bill Tarbellbill TarbellBill Tarbell

4.6382 Huy hiệu vàng33 Huy hiệu bạc49 Huy hiệu đồng2 gold badges33 silver badges49 bronze badges

Đã hỏi ngày 21 tháng 6 năm 2017 lúc 6:53

Những gì bạn thực sự muốn là giảm phân số .. không tính toán bộ phận., 1/2 is not a numeric; it is a string [char array]. So, you can use it for display/print purpose only; you can not use in arithmetic expressions.

Đây là một mẫu nhanh sẽ mang lại phần giảm tỷ lệ:Jun 21, 2017 at 7:00

Mẫu lấy từ: //snipplr.com/view/42917/

#include 
#include 

long GreatestCommonMultiple[long& a, long& b];
std::string SimplifyThis[long& a, long& b];

int main[int argC, char** argV]
{
    long n1 = 3;
    long n2 = 21;
    std::cout 

Bài Viết Liên Quan

Chủ Đề