Sabtu, 23 April 2011


Ini adalah program untuk mencari nilai interaktif dalam bentuk Dev dimana seperti yang kita ketahui bahwa program dalam bentuk Dev tidak begitu berbeda dengan program dalam bentuk C++, dapat kita bandingkan dari program sebelumnya yaitu dalam bentuk C++, setelah kita mengamati kedua program tersebut kita akan mengetahui letak perbedannya bukan?. Dan berikut adalah pendeklarasiannnya:
Deklarasinya
Deklarasi:
        Proses, n: integer
Rumus: 1-(1/2)+(1/3)-(1/4)+...+(1/n)
for(int j=1; j<=n; j++);
total = rumus / j;
rumus = (rumus * (-1));
jumlah+= total;
                if(j==1)
                if( j > 1)
output: nilai interaktifnya

 seperti kita lihat dalam program dapat debedakan/dibagi dalam 3 file yaitu:
1.       dalam Definisi class

2.         class hitung
3.         {      
4.         public:
5.                int proses();
6.                void input();
7.         private:
8.                 int n;
9.                 float rumus,jumlah,total;
10.     };

2. Implementasi class

void hitung::input()
{
     cin >> n;
     cout << endl;
}             
int hitung::proses()
{
    jumlah = 0;
    total = 0;
    rumus = -1;
          for(int j=1; j<=n; j++){
                  rumus = (rumus * (-1));
                  total = rumus / j;
                  jumlah+= total;
                  if(j==1)
                  cout << " ( " << total << " ) ";
                       if( j > 1)
                           cout << " + ( " << total << " ) ";
}
cout << endl << endl << " Jumlah Interaktifnya = " << jumlah;
cout << endl;
return jumlah;
}
int main(int argc, char *argv[])
{
    cout << "Rumus 1-(1/2)+(1/3)-(1/4)+...+(1/n) ";
    cout << endl;
    cout << endl;
    cout << "Nilai n : "; 

3. Main function

#include <cstdlib>
#include <iostream>
using namespace std;
hitung baris;
 baris.input();
 baris.proses();
system("PAUSE");
return EXIT_SUCCESS; }

berikut adalah program utuhnya:

#include <cstdlib>
#include <iostream>
using namespace std;
class hitung
{               
public:
       int proses();
       void input();
private:
        int n;
        float rumus,jumlah,total;
};
void hitung::input()
{
     cin >> n;
     cout << endl;
}             
int hitung::proses()
{
    jumlah = 0;
    total = 0;
    rumus = -1;
          for(int j=1; j<=n; j++){
                  rumus = (rumus * (-1));
                  total = rumus / j;
                  jumlah+= total;
                  if(j==1)
                  cout << " ( " << total << " ) ";
                       if( j > 1)
                           cout << " + ( " << total << " ) ";
}
cout << endl << endl << " Jumlah Interaktifnya = " << jumlah;
cout << endl;
return jumlah;
}
int main(int argc, char *argv[])
{
    cout << "Rumus 1-(1/2)+(1/3)-(1/4)+...+(1/n) ";
    cout << endl;
    cout << endl;
    cout << "Nilai n : "; 
    hitung baris;
    baris.input();
    baris.proses();
    system("PAUSE");
    return EXIT_SUCCESS;
}                                              

Tidak ada komentar:

Posting Komentar