#include <iostream.h>
#include <iomanip.h>
class vektor{
friend ostream& operator << (ostream& , vektor& );
friend istream& operator >> (istream& , vektor& );
public:
vektor();
void penjumlahan(const vektor&, const vektor&);
void perkalian(float, const vektor&);
void nilai(int);
private:
int elemen[100];
int banyak;
};
vektor::vektor(){
banyak=3;
for(int i=0;i<banyak;i++)
elemen[i];
}
istream& operator >> (istream& in, vektor& a){
cout<<"\nbanyak elemen : "; in>>a.banyak;
cout<<"masukkan data vektor\n : ";
for(int i=0;i<a.banyak;i++){
cout<<"data["<<i+1<<"] :";
cin>>a.elemen[i];
}
return in;
}
void vektor::nilai(int i){
banyak= i;
}
ostream& operator << (ostream& out, vektor& a){
cout<<endl;
for(int i=0;i<a.banyak;i++)
cout<<"s["<<i+1<<"]="<<setw(5)<<a.elemen[i]<<"\n";
return out;
}
void vektor::penjumlahan(const vektor& a, const vektor& b){
if(a.banyak>b.banyak)banyak=a.banyak;
else banyak=b.banyak;
for(int i=0;i<banyak;i++)
elemen[i]=a.elemen[i]+b.elemen[i];
}
void vektor::perkalian(float k, const vektor& a){
banyak = a.banyak;
for(int i=0;i<banyak;i++)
elemen[i]=k*a.elemen[i];
}
#include <iostream.h>
#include <iomanip.h>
#include "vektor.h"
void main(){
vektor x, y, z;
cin>>x;
cout<<x;
cin>> y;
cout<<y;
z.penjumlahan(x,y);
cout<<"\nhasil penjumlahan 2 vektor\n"<<z;
cout<<z;
// getch();
}
0 komentar:
Post a Comment