Popular Posts

6/25/2012

Sample Math C++


The following code :
===================================

#include<iostream.h>
#include<conio.h>
class Number{
private:
int x,y;
public:
void input(){
cout<<"Input X:";cin>>x;
cout<<"Input Y:";cin>>y;
}
void display(){
cout<<x<<"\t"<<y<<"\t"<<sum()<<"\t"<<pgcd()<<"\t"<<max()<<endl;
}
Number(){x=100;y=200;}
Number(int x1,int y1){
x=x1;
y=y1;
}
int sum(){
return (x+y);
}
  /* int pgcd(){
if(x==y)
return x;
else if(x>y){
x=x-y;
return pgcd();
}
else{
y=y-x;
return pgcd();
}
}*/
int pgcd(){
int a,b;
a=x; b=y;
while(a!=b){
if(a>b) a=a-b;
else b=b-a;
}
return a;
}
int max(){
if(x>y) return x;
else return y;
}
};
void sort(Number a[],int n){
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(a[i].sum()>a[j].sum()){
Number t=a[i];a[i]=a[j];a[j]=t;
}
}
Number *maxsum(Number a[],int n){
Number *p; p=&a[0];
for(int i=1;i<n;i++)
if(p->sum()<a[i].sum()) p=&a[i];
return p;
}
void main(){
int n,i;
Number a[6]={Number(1,2),Number(2,4),
Number(3,6),Number(4,8),
Number(5,10),Number(6,36)};
cout<<"============================================"<<endl;
cout<<"Initialization of Oject:\n";
cout<<"X"<<"\t"<<"Y"<<"\t"<<"SUM"<<"\t"<<"PGCD"<<"\t"<<"Max"<<endl;
for(i=0;i<6;i++)
a[i].display();
cout<<"============================================"<<endl;
cout<<"Input N Value:";cin>>n;
Number *b=new Number[n];
cout<<"Default Data:"<<endl;
cout<<"X"<<"\t"<<"Y"<<"\t"<<"SUM"<<"\t"<<"PGCD"<<"\t"<<"Max"<<endl;
for(i=0;i<n;i++)
b[i].display();
cout<<"============================================"<<endl;
cout<<"Input Data from Keyboard"<<endl ;
for(i=0;i<n;i++)
b[i].input();
cout<<"============================================"<<endl;
cout<<"Data Before sort:"<<endl;
cout<<"X"<<"\t"<<"Y"<<"\t"<<"SUM"<<"\t"<<"PGCD"<<"\t"<<"Max"<<endl;
for(i=0;i<n;i++)
b[i].display();
cout<<"============================================"<<endl;
cout<<"Data After sort:"<<endl;
sort(b,n);
cout<<"X"<<"\t"<<"Y"<<"\t"<<"SUM"<<"\t"<<"PGCD"<<"\t"<<"Max"<<endl;
for(i=0;i<n;i++)
b[i].display();
cout<<"============================================"<<endl;
Number *Ptr;
Ptr=maxsum(b,n);
cout<<"PGCD of MaxSum Data = "<<Ptr->pgcd()<<endl;  
}

0 comments:

Post a Comment