Home about IT Motivation Course Sales Project About Me

Saturday, December 12, 2009

C++: menjumlahkan, mengurangi dan mengkali vector 3D

nyari nayari dapet dan di modify menjadi seperti berikut. silakan dimodify sesuai kebutuhan.

#include<iostream.h>
#include<math.h>

class vector3dimensi{
float x, y, z;
double s;
public :

void set_vector3dimensi(float xx, float yy, float zz)
{
x= xx;
y= yy;
z= zz;
}


vector3dimensi operator*(vector3dimensi t2)
{
vector3dimensi temp;
temp.x=(y*t2.z)-(z*t2.y);
temp.y=-((x*t2.z)-(z*t2.x));
temp.z=(x*t2.y)-(y*t2.x);
return temp;
}
void showkali(){
cout <<"kalinya adalah "<<x<<" i + "<<y<<" j + "<<z<<" k\n";
}






vector3dimensi operator+(vector3dimensi t2)
{
vector3dimensi temp;
temp.x=x+t2.x;
temp.y=y+t2.y;
temp.z=z+t2.z;
return temp;
}
void showplus(){
cout <<"Jumlahnya adalah "<<x<<" i + "<<y<<" j + "<<z<<" k\n";
}





vector3dimensi operator-(vector3dimensi t2)
{
vector3dimensi temp;
temp.x=x-t2.x;
temp.y=y-t2.y;
temp.z=z-t2.z;
return temp;
}
void showmin(){
cout <<"kurangnya adalah "<<x<<" i + "<<y<<" j + "<<z<<" k\n";
}





};


void main(){
float u1, u2, u3, v1, v2, v3;
vector3dimensi s1, s2, s3;
int n;
cout<<"----PROGRAM VECTOR----\n\n";
cout<<"Pilih (1) untuk penjumlahan dua vector 3D\n";
cout<<"Pilih (2) untuk pengurangan dua vector 3D\n";
cout<<"Pilih (3) untuk perkalian skalar dua vector 3D\n";

back:
cout<<"Pilihan anda: ";
cin>>n;

if (n==1){

cout<<"vector s1= U1i + U2j + U3k\n";
cout << "Masukkan nilai U1: ";
cin>>u1;
cout<<"Masukkan nilai U2: ";
cin>>u2;
cout<<"Masukkan nilai U3: ";
cin>>u3;
cout<<"\nvector s2= V1i + V2j + V3k\n";
cout << "Masukkan nilai V1: ";
cin>>v1;
cout<<"Masukkan nilai V2: ";
cin>>v2;
cout<<"Masukkan nilai V3: ";
cin>>v3;

s1.set_vector3dimensi(u1, u2, u3);
s2.set_vector3dimensi(v1, v2, v3);
s3=s1+s2;
s3.showplus();




}
else if (n==2){


cout<<"vector s1= U1i + U2j + U3k\n";
cout << "Masukkan nilai U1: ";
cin>>u1;
cout<<"Masukkan nilai U2: ";
cin>>u2;
cout<<"Masukkan nilai U3: ";
cin>>u3;
cout<<"\nvector s2= V1i + V2j + V3k\n";
cout << "Masukkan nilai V1: ";
cin>>v1;
cout<<"Masukkan nilai V2: ";
cin>>v2;
cout<<"Masukkan nilai V3: ";
cin>>v3;

s1.set_vector3dimensi(u1, u2, u3);
s2.set_vector3dimensi(v1, v2, v3);
s3=s1-s2;
s3.showmin();



}
else if (n==3){

cout<<"vector s1= U1i + U2j + U3k\n";
cout << "Masukkan nilai U1: ";
cin>>u1;
cout<<"Masukkan nilai U2: ";
cin>>u2;
cout<<"Masukkan nilai U3: ";
cin>>u3;
cout<<"\nvector s2= V1i + V2j + V3k\n";
cout << "Masukkan nilai V1: ";
cin>>v1;
cout<<"Masukkan nilai V2: ";
cin>>v2;
cout<<"Masukkan nilai V3: ";
cin>>v3;

s1.set_vector3dimensi(u1, u2, u3);
s2.set_vector3dimensi(v1, v2, v3);
s3=s1*s2;
s3.showkali();
}
else{
cout<<"Masukan salah, silahkan ulangi lagi!\n";
goto back;
}
}

No comments: