vs2005上運行通過!完全按照題意要求整出來的。
狠吧!
#include <iostream>
#include <math.h>
#include <time.h>
#include <iomanip>
using namespace std;
class YuanSuChenFan
{
public:
YuanSuChenFan()
{
a[0] = 0;
a[1] = 0;
a[2] = 0;
}
YuanSuChenFan(int a1,int a2,int a3)
{
a[0] = a1;
a[1] = a2;
a[2] = a3;
}
~YuanSuChenFan()
{
}
private:
int a[3];
public:
void print()
{
cout<<'{'<<a[0]<<','
<<a[1]<<','
<<a[2]<<'}'<<endl;
}
YuanSuChenFan operator ^(YuanSuChenFan yscf)
{
YuanSuChenFan yscf_res;
yscf_res.a[0]=pow((float)a[0],yscf.a[0]);
yscf_res.a[1]=pow((float)a[1],yscf.a[1]);
yscf_res.a[2]=pow((float)a[2],yscf.a[2]);
return yscf_res;
}
};
const double PI = 3.1415926;
class A
{
public:
A(double radio = 1,double height = 1)
{
r = radio;
h = height;
}
~A(){}
private:
double r;
double h;
public:
double Getr()
{
return r;
}
double Geth()
{
return h;
}
public:
double V2()
{
return PI*r*r*h;
}
friend double V1(A a)
{
return PI*a.r*a.r*a.h;
}
};
class Array
{
public:
Array();
Array(int b[][5],int n);
~Array()
{
}
private:
int (*a)[5];
int n; //二維數組的行數。
float ave; //二維數組的平均值。
public:
void f(); //功能函數,求二維數組的平均值。
void fun(); //功能函數,根據題目要求調整二維數組。
void show(); //功能函數,輸出成員數組。
void create(int b[][5],int n);
};
Array::Array(int b[][5],int _n)
{
ave = 0.0f;
a=b;
n=_n;
}
void Array::create(int b[][5],int _n)
{
ave = 0.0f;
a=b;
n=_n;
}
Array::Array()
{
}
void Array::f ()
{
ave = 0.0f;
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
{
ave+=(float)a[i][j];
}
}
ave=ave/(float)(n*5);
show();
cout<<"當前數列Array平均值:"<<ave<<endl;
}
void Array::show ()
{
cout<<endl<<"顯示當前數列Array:"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
{
cout<<setw(10)<<a[i][j];
}
cout<<endl;
}
}
void Array::fun ()
{
int *ptp=new int[5*n];
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
{
ptp[i*5+j]=a[i][j];
}
}
for(int i=0;i<5*n;i++)
{
for(int j=0;j<5*n;j++)
{
if(ptp[i]<ptp[j])
{
int temp=ptp[i];
ptp[i]=ptp[j];
ptp[j]=temp;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
{
a[i][j]=ptp[i*5+j];
}
}
cout<<endl<<" 經過“大於平均值的元素放在數組的後面”處理後。"<<endl;
show();
delete []ptp;
}
void main()
{
cout<<"第壹題:"<<endl;
YuanSuChenFan t1(2,2,2),t2(3,3,3),t3;
t3 = t1^t2;
cout<<"t1=";
t1.print ();
cout<<"t2=";
t2.print ();
cout<<"t3=";
t3.print ();
cout<<endl;
cout<<"第二題:"<<endl;
A circle(10,15);
cout<<"創建圓柱:半徑("<<circle.Getr ()
<<")高度("<<circle.Geth()<<")"<<endl;
cout<<"V1計算面積結果:";
cout<<V1(circle)<<endl;
cout<<"V2計算面積結果:";
cout<<circle.V2 ()<<endl;
cout<<endl;
cout<<"第三題,請選擇顯示方式:"<<endl;
cout<<"1、使用隨機數組顯示本題(按1);"<<endl;
cout<<"2、手動輸入數組顯示本題(按2);"<<endl;
cout<<"3、退出(按3);"<<endl;
int sel;
int cn = 6;
cin>>sel;
if(sel==1)
{
int rand_input[6][5];
Array ar(rand_input,cn);
srand(time(NULL));
for(int i=0;i<cn;i++)
{
for(int j=0;j<5;j++)
{
rand_input[i][j]=rand()%1000;
}
}
ar.show ();
ar.f ();
ar.fun ();
}
else if(sel==2)
{
cout<<"請輸入數組行數(回車確認):"<<endl;
cin>>cn;
int (*psn_input)[5]=new int[cn][5];
if(cn>10)
{
cout<<"大佬,"<<cn*5<<"個數字,妳想輸瘋了麽?"<<endl;
cout<<"好言難勸該死的鬼,輸吧!"<<endl;
}
for(int i=0;i<cn * 5;i++)
{
cout<<"第"<<i/5+1<<"行"<<",第"<<i%5+1<<"列:";
cin >>psn_input[i/5][i%5];
}
Array ar(psn_input,cn);
ar.show ();
ar.f ();
ar.fun ();
delete []psn_input;
}
else
{
return;
}
return;
}
====================================================
補充壹下:
下壹樓的說的也有壹定道理!
那用:
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
{
a[i][j]=b[i][j];
}
}
代替
a=b;
不就可以把b[][]的內容拷貝到a[][]下了麽?
因該是這樣的吧!還沒測試過!妳try壹下!
int (*p)[N] 等價於:
typedef int Nint[N]; //Nint 被定義為三個int單元大小的類型
Nint *p; //然後定義壹個指向Nint類型的指針
其中p又叫做數組指針,主要用於取定長數組。
例如每個漢字占用兩個字符,我們就可以定義int (*p)[2]去指向壹個字符串,當p每次累加就指向下壹個漢字。這樣操作更簡潔方便。
int *p[N]
則是我們常見的壹個大小為N的int型指針數組
他們的用法如下:
int (*p)[3] ,a[2][3]; p=a ; // 則p[][]==a[][]
int *p[2],a[2][3]; p[0]=a[0]; p[1]=a[1]; //則p[][]==a[][]