Penjumlahan Pecahan (X/Y) di C++

Kali ini penulis membagikan sourch code untuk melakukan penjumlahan pecahan di c++ tanpa mengubahnya menjadi bilangan dengan campuran desimal.

Berikut programnya :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<iostream>
using namespace std;
    
double DMOD(double a,double b){//fungsi kustom modulo untuk melakukan operasi modulo ke tipe data double
    long c=1;
    double d=1;
    c=a/b;
    d=a/b;
    return b*(d-c);
}
    
double KPK(double a,double b){
    double v=0,z=1,FPB=1,e=1,f=1;
    bool c=1,d=1;
    e=a;
    f=b;
    if (a<0){
        a*=-1;
        c=0;
    }
    else if (a==0){b=1;}
    if (b<0){
        b*=-1;
        d=0;
    }
    if (a<=b){v=a;}
    else if (b<a){v=b;}
    for (z=2;z<=v;z++){
        if ((DMOD(b,z)==0)&&(DMOD(a,z)==0)){
            b/=z;
            a/=z;
            FPB*=z;
            if (a<=b){v=a;}
            else if (b<a){v=b;}
        }
        if ((DMOD(b,z)==0)&&(DMOD(a,z)==0)){z--;}
    }
    return (e*f)/FPB;
}
    
int main(){
    kembali:
    int jml=1,pilihan=0,no=0;
    double kpk=1,pmb=0,v=0,z=0,total=0;
    bool c=1,d=1;
    cout<<"-----------------Penjumlahan-----------------" <<endl;
    cout<<"1. Penjumlahan biasa(a+b+c+...)" <<endl;
    cout<<"2. Penjumlahan Pecahan((a/b)+(c/d)+(e/f)+...)" <<endl;
    cout<<"---------------------------------------------" <<endl;
    cout<<"Pilihan = " ;cin>>pilihan;
    system("cls" );
    if (pilihan==1){
        cout<<"Jumlahbilangan : " ;cin>>jml;
        system("cls" );
        double X[jml];
        for (int i=0;i<jml;i++){
            cout<<"\nBilangan-" <<i+1<<" = " ;cin>>X[i];
            total+=X[i];
            system("cls" );
            for (int j=0;j<=i;j++){
                cout<<X[j];
                if (j!=i){
                    cout<<" + " ;
                }
                else if ((j==i)&&(i>=1)){
                    cout<<" = " <<total;
                }
            }
        }
    }
    else if (pilihan==2){
        cout<<"Jumlah pecahan : " ;cin>>jml;
        system("cls" );
        double X[jml];
        double Y[jml];
        for (int i=0;i<jml;i++){
            kpk=1;c=1;d=1;pmb=0;
            cout<<"\nMasukan(X/Y) : " <<endl;
            cout<<"X = " ;cin>>X[i];
            cout<<"Y = " ;cin>>Y[i];
            system("cls" );
            for (int j=0;j<=i;j++){
                cout<<"(" <<X[j]<<"/" <<Y[j]<<")" ;
                if (j!=i){
                    cout<<" + " ;
                }
                else if ((j==i)&&(i>=1)){
                    cout<<" = " ;
                    for (int k=0;k<=j;k++){
                        kpk=KPK(kpk,Y[k]);
                    }
                    for (int k=0;k<=j;k++){
                        pmb+=(X[k]*(kpk/Y[k]));
                    }
                    //Proses penyederhanaan pecahan
                    if (pmb<0){
                        pmb*=-1;
                        c=0;
                    }
                    else if (pmb==0){kpk=1;}
                    if (kpk<0){
                        kpk*=-1;
                        d=0;
                    }
                    if (pmb<=kpk){v=pmb;}
                    else if (kpk<pmb){v=kpk;}
                    for (z=2;z<=v;z++){
                        if ((DMOD(pmb,z)==0)&&(DMOD(kpk,z)==0)){
                            pmb/=z;
                            kpk/=z;
                            if (pmb<=kpk){v=pmb;}
                            else if (kpk<pmb){v=kpk;}
                        }
                        if ((DMOD(pmb,z)==0)&&(DMOD(kpk,z)==0)){z--;}
                    }
                    if (c==0){pmb*=-1;}
                    if (d==0){kpk*=-1;}
                    if ((kpk!=1)&&(pmb>0)&&(kpk>0)){cout<<pmb<<"/" <<kpk;}
                    else if (kpk==1){cout<<pmb;}
                    else if ((kpk==-1)&&(pmb<0)){cout<<pmb*(-1);}
                    else if ((kpk!=-1)&&(pmb<0)&&(kpk<0)){cout<<(-1)*pmb<<"/" <<(-1)*kpk;}
                }
            }
        }
    }
    else {
        cout<<"\nPilihan salah coba lagi...";
        goto kembali;
    }
    return 0;
}

DEMO

Disini penulis menambahkan bonus untuk penjumlahan biasa.Baik itu saja dari penulis, semoga postingan ini dapat membantu, terimakasih sudah berkunjung di blog saya..

Subscribe to receive free email updates:

0 Response to "Penjumlahan Pecahan (X/Y) di C++"

Posting Komentar