Hỏi về cộng nhiều phân số

ai giúp em cộng nhiều phân số với ạ

#include <stdio.h>
struct phanso
{
    int tu;
    int mau;
};

void nhapps(phanso a[], int &n)
{
    printf("n="); 
    scanf("%d",&n);
    for (int i = 0; i < n; i++)
    {
        printf("nhap tu:");
        scanf("%d", &a[i].tu);
        printf("nhap mau:");
        scanf("%d", &a[i].mau);
    }
}

void xuatps(phanso a[], int n)
{
    for (int i = 0; i < n; i++)
        printf("%d/%d\n", a[i].tu, a[i].mau);
}

void xuat1ps(phanso ps)
{
    printf("%d/%d\n", ps.tu, ps.mau);
}

int uscln(int a, int b)
{
    while (a != b)
    {
        if (a > b)
            a -= b;
        else
            b -= a;
    }
    return a;
}

phanso tongps(phanso a[], int n)
{
    int z;
    phanso tong;
    tong = a[1];
    for (int i = 1; i < n; i++)
    {
        tong.tu = tong.tu * a[i].mau + tong.mau * a[i].tu;
        tong.mau = tong.mau * a[i].mau;
        z = uscln(tong.tu, tong.mau);
        tong.tu /= z;
        tong.mau /= z;
        printf("tong cac phan so la:");
    }
    return tong;
}

int main()
{
    int n;
    phanso a[100];
    nhapps(a, n);
    xuatps(a, n);
    tongps(a,n);
}

Nếu tính được tổng 2 phân số, thì tổng nhiều phân số chẳng khó gì. Vòng lặp + tổng 2 phân số = tổng nhiều phân số.

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?