拓展:
Ainm]是一个n行m列的矩阵a表示A的第i行列的元素,定义为A的第1行和第列除了a,之外所有元素(共n+m-2个)的乘积a]a.2"aj-I**alim*al1]a2]a-1]al+a现输入非负整形的矩阵A[nm,求MAX(4小即所有的x门中的最大值。输入描述、第一行两个整数n和m。之后n行输入矩阵,均为非负整数输出描述一行输出答案。
示例
输入3 5
5 1 8 5 2
1 3 10 3 3
7 8 5 5 16
输出358400
(花式while if组合,隐藏循环层数)
#include <iostream>
using namespace std;
int main()
{
int row=1,colmn=1;
int i=0,j=0;
//输入
cin>>row>>colmn;
unsigned int arr[row][colmn];
for (i=0;i<row;i++) {
for (j=0;j<colmn;j++) {
cin>>arr[i][j];
}
}
//打印
cout << endl;
for (i=0;i<row;i++) {
for (j=0;j<colmn;j++) {
cout<<arr[i][j]<<'\t';
}
cout << endl;
}
cout << endl;
int r,c;
//运算出结果并将结果存于mul[row][colmn]数组
i=0;
j=0;
unsigned int mul[row][colmn];
unsigned int max=0;
while(i<row)
{
if(j>=colmn)
{
j=0;
i++;
continue;
}
mul[i][j]=1;
r=0;
c=0;
while (r<row)
{
if(c>=colmn)
{
c=0;
r++;
continue;
}
if(r!=i)
{
mul[i][j]=mul[i][j]*arr[r][j];
r++;
continue;
}
if(c==j&&r==i)
{
c++;
continue;
}
else if(c==j||r==i)
{
mul[i][j]=mul[i][j]*arr[r][c];
}
c++;
}
max=max>mul[i][j]?max:mul[i][j];
j++;
}
//输出打印
for (int i=0;i<row;i++) {
for (int j=0;j<colmn;j++) {
cout<<mul[i][j]<<'\t';
}
cout << endl;
}
cout << endl;
cout << "MAX = " << max <<endl;
return 0;
}
构造函数:
无参构造,有参构造,拷贝构造,拷贝赋值构造
#include <iostream>
#include <cstring>
using namespace std;
class Stu
{
public:
string name;
int age;
int score;
int *high;
public:
//无参构造
Stu ()
{
// int *my_score = &this->score;
// my_score=new int;
this->high = new int;
this->name = new char[10];
cout << "无参构造" << endl;
}
//析构函数
~Stu()
{
//cout << "调用析构函数" << endl;
delete high;
cout << "调用析构函数" << endl;
}
//拷贝函数
Stu (const Stu &a):name(a.name),age(a.age),score(a.score)//浅拷贝(有危害),high(a.high)
{
cout << "调用拷贝函数" << endl;
//深拷贝
this->high = new int;
memcpy(this->high,a.high,sizeof(*(a.high)));
}
//拷贝赋值函数
Stu &operator=(const Stu &a)
{
if(this==&a)
{
return *this;
}
this->age=a.age;
this->name=a.name;
this->score=a.score;
cout << "调用拷贝赋值函数" << endl;
//深拷贝
*high=*a.high;
// //浅拷贝(有危害)
// high=a.high;
// //深拷贝
// this->high = new int;
// memcpy(this->high,a.high,sizeof(*(a.high)));
return *this;
}
// //(错误)拷贝赋值函数
// Stu operator=(const Stu &a)
// {
// if(this==&a)
// {
// return *this;
// }
// this->age=a.age;
// this->name=a.name;
// this->score=a.score;
// cout << "调用拷贝赋值函数" << endl;
// //深拷贝
// *high=*a.high;
//浅拷贝(有危害)
high=a.high;
//深拷贝
this->high = new int;
memcpy(this->high,a.high,sizeof(*(a.high)));
// return *this;
// }
void show(string a);
};
void Stu::show(string a)
{
cout << endl;
cout << "***********" << a << "***********" << endl;
cout << "* 姓名 :" << name << endl;
cout << "* 年龄 :" << age << endl;
cout << "* 分数 :" << score << endl;
cout << "* 身高 :" << *high << endl;
cout << "* high :" << high << endl;
cout << "* size :" << sizeof(Stu) << endl;
cout << "******************************" << endl;
}
int main()
{
//cout << "Hello World!" << endl;
Stu student1;
student1.age=18;
*student1.high=178;
student1.name="小明";
student1.score=98;
student1.show("student1");
Stu student2=student1;
Stu student3;
student3=student2;
student2.show("student2");
student3.show("student3");
return 0;
}
运算符重载:
算数运算符重载 :(+、-、()++、++() 、()--、--())
#include <iostream>
using namespace std;
class complex //虚数运算
{
int real; //实数
int imag; //虚数
friend complex *operator+(const complex &p1,const complex &p2);
friend const complex operator-(const complex &p1);
friend complex operator++(complex &p1,int);
friend complex operator--(complex &p1,int);
friend complex operator++(complex &p1);
friend complex operator--(complex &p1);
public:
complex()
{
}
complex(int i,int j)
{
real=i;
imag=j;
}
void show()
{
cout<<this->real<<"+"<<this->imag<<'i'<<endl;
cout<<" this addr : " <<this<<endl;
}
// //类内局部引用
// //+运算符重载
// complex *operator+(const complex &p2)
// {
// //堆区申请
// complex *p3=new complex;
// p3->real=this->real+p2.real;
// p3->imag=this->imag+p2.imag;
// cout <<"p3 : "<< p3 << endl;
// cout <<"this : "<< this << endl;
// cout <<"p2 : "<< &p2 << endl;
// return p3;
// // //栈区申请
// // static complex p3;
// // p3.real=this->real+p2.real;
// // p3.imag=this->imag+p2.imag;
// // cout <<"p3 : "<< &p3 << endl;
// // cout <<"this : "<< this << endl;
// // cout <<"p2 : "<< &p2 << endl;
// // return &p3;
// }
// //-运算符重载
// const complex operator-()
// {
// return complex(-this->real,-this->imag);
//堆区申请
complex p3;
p3.real = -this->real;
p3.imag = -this->imag;
cout <<"p3 : "<< &p3 << endl;
return p3;
// }
// //()++运算符重载
// complex operator++(int)
// {
// complex temp=*this;
// this->real++;
// this->imag++;
// return temp;
// }
// //()--运算符重载
// complex operator--(int)
// {
// complex temp;
// temp.real=this->real--;
// temp.imag=this->imag--;
// return temp;
// }
// //++()运算符重载
// complex operator++()
// {
// this->real++;
// this->imag++;
// return *this;
// }
// //--()运算符重载
// complex operator--()
// {
// this->real--;
// this->imag--;
// return *this;
// }
};
//全局函数
//+运算符重载
complex *operator+(const complex &p1,const complex &p2)
{
//堆区申请
complex *p3=new complex;
p3->real=p1.real+p2.real;
p3->imag=p1.imag+p2.imag;
cout <<"p1 : "<< &p1 << endl;
cout <<"p2 : "<< &p2 << endl;
cout <<"p3 : "<< p3 << endl;
return p3;
// //栈区申请
// static complex p3;
// p3.real=this->real+p2.real;
// p3.imag=this->imag+p2.imag;
// cout <<"p3 : "<< &p3 << endl;
// cout <<"this : "<< this << endl;
// cout <<"p2 : "<< &p2 << endl;
// return &p3;
}
//-运算符重载
const complex operator-(const complex &p1)
{
return complex(-p1.real,-p1.imag);
// complex p3;
// p3.real = -p1.real ;
// p3.imag = -p1.imag;
// cout <<"p3 : "<< &p3 << endl;
// return p3;
}
//()++运算符重载
complex operator++(complex &p1,int)
{
complex temp = p1;
p1.real++;
p1.imag++;
return temp;
}
//()--运算符重载
complex operator--(complex &p1,int)
{
complex temp = p1;
p1.real--;
p1.imag--;
return temp;
}
//++()运算符重载
complex operator++(complex &p1)
{
complex temp = p1;
p1.real++;
p1.imag++;
return p1;
}
//--()运算符重载
complex operator--(complex &p1)
{
p1.real--;
p1.imag--;
return p1;
}
int main()
{
cout << "Hello World!" << endl;
complex p1(7,3);
p1.show();
complex p2(5,2);
p2.show();
complex *p3=p1+p2;
p3->show();
p1.show();
complex p4=-p1;
p4.show();
p1.show();
p1++.show();
p1.show();
p2--.show();
p2.show();
(++p2).show();
p2.show();
(--p2).show();
p2.show();
return 0;
}
赋值(输入输出)运算符重载 :(+=、-=、>>、<<)
#include <iostream>
using namespace std;
class complex //虚数运算
{
int real; //实数
int imag; //虚数
friend complex &operator += (complex &p1,const complex &p2);
friend complex &operator -= (complex &p1,const complex &p2);
friend ostream &operator << (ostream &cout_,complex &p);
friend istream &operator >> (istream &cin_,complex &p);
public:
complex()
{
}
complex(int i,int j)
{
real=i;
imag=j;
}
void show()
{
cout<<endl;
cout<<real<<"+"<<imag<<'i'<<endl;
cout<<" this addr : " <<this<<endl;
}
//类内重载
//+=运算符重载
// complex &operator+=(const complex &p2)
// {
// this->real+=p2.real;
// this->imag+=p2.imag;
// cout <<"this : "<< this << endl;
// cout <<"p2 : "<< &p2 << endl;
// return *this;
// }
//-=运算符重载
// complex &operator-=(const complex &p2)
// {
// this->real-=p2.real;
// this->imag-=p2.imag;
// cout <<"this : "<< this << endl;
// cout <<"p2 : "<< &p2 << endl;
// return *this;
// }
// //<<输出运算符重载 (不能够类内构造)
// ostream &operator << (ostream &cout_)
// {
// cout_ << this->real << "+" << this->imag << endl;
// return cout_;
// }
};
//全局重载
//+=运算符重载
complex &operator += (complex &p1,const complex &p2)
{
p1.real+=p2.real;
p1.imag+=p2.imag;
cout <<"p1 : "<< &p1 << endl;
cout <<"p2 : "<< &p2 << endl;
return p1;
}
//-=运算符重载
complex &operator -= (complex &p1,const complex &p2)
{
p1.real-=p2.real;
p1.imag-=p2.imag;
cout <<"p1 : "<< &p1 << endl;
cout <<"p2 : "<< &p2 << endl;
return p1;
}
//<<输出运算符重载
ostream &operator << (ostream &cout_,complex &p)
{
cout_ << p.real << "+" << p.imag <<"i"<< '\t';
return cout_;
}
//>>输入运算符重载
istream &operator >> (istream &cin_,complex &p)
{
cin_ >> p.real>> p.imag;
return cin_;
}
int main()
{
cout << "Hello World!" << endl;
complex p1(7,3);
p1.show();
complex p2(5,2);
p2.show();
p1+=p2;
p1.show();
p1-=p2;
p1.show();
complex p3;
cin>>p3;
cout<<p1<<p2<<p3;
return 0;
}
判断运算符重载 : (>、<、==、!)
#include <iostream>
using namespace std;
class complex //虚数运算
{
int real; //实数
int imag; //虚数
friend bool operator ! (const complex &p1);
friend bool operator == (const complex &p1,const complex &p2);
friend bool operator > (const complex &p1,const complex &p2);
friend bool operator < (const complex &p1,const complex &p2);
public:
complex()
{
}
complex(int i,int j)
{
real=i;
imag=j;
}
void show()
{
cout<<endl;
cout<<real<<"+"<<imag<<'i'<<endl;
cout<<" this addr : " <<this<<endl;
}
//类内构造函数
//>运算符重载
// bool operator>(const complex &p2)
// {
// if(this->real==p2.real)
// return this->imag>p2.imag;
// else
// return this->real>p2.real;
// }
//<运算符重载
// bool operator<(const complex &p2)
// {
// if(this->real==p2.real)
// return this->imag<p2.imag;
// else
// return this->real<p2.real;
// //==运算符重载
// bool operator==(const complex &p2)
// {
// if(this->real==p2.real)
// return this->imag==p2.imag;
// else
// return this->real==p2.real;
// }
// }
//!运算符重载
// bool operator!()
// {
// return !this->real&&!this->imag;
// }
};
//全局函数
//>运算符重载
bool operator>(const complex &p1,const complex &p2)
{
if(p1.real==p2.real)
return p1.imag>p2.imag;
else
return p1.real>p2.real;
}
//<运算符重载
bool operator<(const complex &p1,const complex &p2)
{
if(p1.real==p2.real)
return p1.imag<p2.imag;
else
return p1.real<p2.real;
}
//==运算符重载
bool operator==(const complex &p1,const complex &p2)
{
if(p1.real==p2.real)
return p1.imag==p2.imag;
else
return p1.real==p2.real;
}
//!运算符重载
bool operator!(const complex &p1)
{
return !p1.real&&!p1.imag;
}
int main()
{
cout << "Hello World!" << endl;
complex p1(7,3);
p1.show();
complex p2(5,2);
p2.show();
cout<<"p1>p2 bool = "<<(p1>p2)<<endl;
p1.show();
cout<<"!p1 bool = "<<!p1<<endl;
p1.show();
cout<<"p1==p1 bool = "<<(p1==p1)<<endl;
p1.show();
return 0;
}
拓展:
已知三角新三条边求周长面积
#include <iostream>
#include <cmath>
using namespace std;
class triangle
{
double ptr1;
double ptr2;
double ptr3;
public:
void show()
{
double L=ptr1+ptr2+ptr3;
cout << "周长 L = " << L << endl;
double p = L/2;
double S = (sqrt(p*(p-ptr1)*(p-ptr2)*(p-ptr3)));
cout << "面积 S = " << S << endl;
}
int set_()
{
cout << "边1 ptr1 = ";
cin >> ptr1;
cout << "边1 ptr2 = ";
cin >> ptr2;
cout << "边1 ptr3 = ";
cin >> ptr3;
if(ptr1+ptr2<ptr3||ptr3+ptr2<ptr1||ptr1+ptr3<ptr2)
{
cout << "不符合三角形的结构" << endl;
return -1;
}
return 0;
}
};
int main()
{
cout << "Hello World!" << endl;
cout << endl;
triangle trr1;
if(!trr1.set_())
trr1.show();
return 0;
}