Jumat, 24 Juni 2011

Program C++ untuk menghitung penjumlahan Big Integer


At this time I create a program to add up big integer in an array. This is my lab posttest task at that time. It's hard but we have to go through step by step and continue to Study abroad in order to do this.:) Good luck for my friends ...
Anyway please look at the algorithm and source code is below ... check it out ..

Algoritma :
Procedure big_integer ( input : digit , output : jumlah )
Deklarasi
digit        : integer of array (input)
jumlah    : integer of array (output)

Deskripsi
read(digit)
for i <= 0 to digit do
digit = input1[i]
write (input1[i])
for i <= 0 to digit do
digit = input2[i]
write (input2[i])
enfor
endfor
for i<= 0 to digit do
jumlah[i] = 0
endfor
read(jumlah)
for i <= digit-1 to 0 do
jumlah[i] = jumlah[i] + input1[i] + input2[i]
if ( jumlah[i] > 9 && I !=0 )
jumlah[i] = jumlah[i] % 10
jumlah[i – 1] = jumlah[i – 1] + 1
endif
endfor
for i <= 0 to digit do
jumlah[i]
endfor
write(jumlah)
Berikut kode programnya :

#include <cstdlib>
#include <iostream>

using namespace std;

class big{
      public:
big();
             void masuk();
             void beri_nilai();
             void penjumlahan();
             void keluar();
      private:
              int digit;
              int input1[999];
              int input2[999];
              int jumlah[999];
      };

big::big(){
                         cout<<"penjumlahan 2 bilangan dengan banyak digit"<<endl<<endl;
                         }

void big::masuk(){
     cout<<"digit dari kiri"<<endl;
     cout<<"banyaknya digit : ";
     cin>>digit;
     cout<<"\nbilangan pertama"<<endl;
     for(int i=0;i<digit;i++){
             cout<<"digit ke-"<<(i+1)<<" : ";
             cin>>input1[i];
             }
     cout<<"bilangan : ";
     for(int i=0;i<digit;i++){
             cout<<input1[i];
             }
     cout<<endl;
     cout<<"\nbilangan kedua"<<endl;
     for(int i=0;i<digit;i++){
             cout<<"digit ke-"<<(i+1)<<" : ";
             cin>>input2[i];
             }
     cout<<"bilangan : ";
     for(int i=0;i<digit;i++){
             cout<<input2[i];
             }
     cout<<endl;    
     }

void big::beri_nilai(){
     for(int i=0;i<digit;i++){
             jumlah[i]=0;
             }
     }

void big::penjumlahan(){
     for(int i=(digit-1);i>=0;i--){
             jumlah[i]=jumlah[i]+input1[i]+input2[i];
             if (jumlah[i] >9 && i !=0){
                               jumlah[i]=jumlah[i]%10;
                               jumlah[i-1]=jumlah[i-1]+1;
                               }
             }
     }

void big::keluar(){
     cout<<"\nhasil penjumlahan : ";
     for(int i=0;i<digit;i++){
             cout<<jumlah[i];
             }
     cout<<endl<<endl;
     }

int main(int argc, char *argv[])
{
    big x;
    x.masuk();
    x.beri_nilai();
    x.penjumlahan();
    x.keluar();

    system("PAUSE");
    return EXIT_SUCCESS;
}


explanation: 
The above program is a program to add up big integers using an array which we can solve this problem. Because arrays can only accommodate one integer so we can manipulate it where if we have the sum of 12 then the array can only store the digit to 2 for that we can use the state / elections if the sum which if discharged in for 10 then only be raised its number 2 and 1 in the store next to the array.

0 komentar:

Posting Komentar

Template by:

Free Blog Templates