| Algoritma IF {nilai x ke rumus x^2 + 1} |
| Deklarasi x : integer hasil : float pilih : void |
| Deskripsi Hitung A; Read(A); A.pilih(); cout << A; |
Listing
| #include <cstdlib> |
| #include <iostream> |
| #include <math.h> |
| #include <conio.h> |
| |
| using namespace std; |
| class Hitung{ |
| friend istream&operator>>(istream&,Hitung&); |
| friend ostream&operator<<(ostream&,const Hitung&); |
| public: |
| void pilih(); |
| private: |
| int x; |
| float hasil; |
| }; |
| |
| istream &operator>>(istream&in,Hitung&masukkan){ |
| cout<<"MASUKKAN ANGKA :"; |
| in>>masukkan.x; |
| return in; |
| } |
| ostream & operator << ( ostream &out,const Hitung &keluar ){ |
| out<<"HASIL :"<<keluar.hasil; |
| return out; |
| } |
| |
| void Hitung::pilih(){ |
| if (x<0)hasil = (float)2/(x*x); |
| else if (x>0) |
| hasil = (float)x*x+1; |
| else |
| hasil = 1; |
| } |
| |
| int main(){ |
| Hitung A; |
| cin >> A; |
| A.pilih(); |
| cout << A; |
| getch(); |
| return 0; |
| system("PAUSE"); |
| return EXIT_SUCCESS; |
| } |

Tidak ada komentar:
Posting Komentar