#include <cstdlib>
#include <iostream>
using namespace std;
class hitung{
friend istream& operator>>(istream&, hitung&);
friend ostream& operator<<(ostream&, hitung&);
public:
hitung();
void jumlah();
void kurang();
private:
long int x[50];
long int y[25];
int n;
long int jum,kur;
};
hitung::hitung(){
cout<<"Menghitung big int"<<endl;}
istream& operator>>(istream& in, hitung& a){
cout<<"Masukkan banyaknya inputan :";
in>>a.n;}
ostream& operator<<(ostream& out, hitung& b){
out<<"Banyaknya data :";
out<<b.n<<endl;
}
void hitung::jumlah(){
jum=0;
for(int i=0; i<n; i++){
cout<<"Masukkan angka ke-"<<i+1<<":";
cin>>x[i];
jum=jum+x[i];
}
cout<<jum<<endl;
}
void hitung::kurang(){
kur=0;
for(int j=0; j<n; j++){
cout<<"Masukkan angka ke-"<<j+1<<":";
cin>>y[j];
kur=kur+y[j];
}
cout<<kur<<endl;
}
int main(int argc, char *argv[])
{
hitung c;
cin>>c;
c.jumlah();
c.kurang();
cout<<c;
system("PAUSE");
return EXIT_SUCCESS;
}
OUTPUT:
posting java
import jeliot.io.*;
public class GlobalMembers
{
public static void main()
{
vektor x = new vektor();
vektor y = new vektor();
vektor z = new vektor();
x.masukan();
x.keluar();
y.masukan();
y.keluar();
z.penjumlahan();
System.out.print("hasil 2 penjumlahan 2 vektor adalah :");
z.keluar();
z.perkalian();
System.out.print("hasil perkalian skalar dengan vektor :");
z.keluar();
}
}
public class vektor
{
public vektor()
{
banyak +=3;
try{
for(int i=0; i<banyak; i++){
elemen[i] = 0;
}
}
catch (ArrayIndexOutOfBoundsException e){
System.out.print("aaaa :" +e.getMessage());}
}
public final void beri_nilaiBanyak()
{
System.out.print("masukkan nilai i :");
n = Input.readInt();
banyak=i;
}
public final void masukan()
{
System.out.print("banyak elemen :");
banyakA = Input.readInt();
System.out.print("masukkan data vektor :");
try{
for(int i=0; i<banyak; i++){
System.out.print("data ke-[" +i+1);
System.out.print("] :");
elemen[i] = Input.readInt();}}
catch (ArrayIndexOutOfBoundsException e){
System.out.print("aaaa :" +e.getMessage());}
System.out.print("banyak elemen :");
banyakB = Input.readInt();
System.out.print("masukkan data vektor :");
try{
for(int i=0; i<banyak; i++){
System.out.print("data ke-[" +i+1);
System.out.print( "] :");
elemen[i] = Input.readInt();}}
catch (ArrayIndexOutOfBoundsException e){
System.out.print("aaaa :" +e.getMessage());}
}
public final void keluar()
{
for(int i=0; i<banyak; i++){
System.out.print("s[" +i+1);
System.out.print("=" +elemen[i]);
}
}
public final void penjumlahan()
{
if(banyakA>banyakB) banyak+=banyakA;
else banyak +=banyakB;
try{
for(int i=0; i<banyak; i++){
elemen[i]+=elemenA[i]+elemenB[i];}}
catch (ArrayIndexOutOfBoundsException e){
System.out.print("aaaa :" +e.getMessage());}
}
public final void perkalian(){
System.out.print("masukkan nilai k :");
k = Input.readInt();
banyak+=banyakA;
try {
for(int i=0; i<banyak; i++){
elemen[i]+=k*elemenA[i];
}}
catch (ArrayIndexOutOfBoundsException e){
System.out.print("aaaa :" +e.getMessage());}
}
private int banyak;
private int banyakA;
private int banyakB;
private int n;
private double k;
private int [] elemen = new int[100] ;
}
Tidak ada komentar:
Posting Komentar