第一关
#include <iostream>
using namespace std;
/********* Begin *********/
class Complex
{
//复数类的声明
public:
float real;
float image;
Complex(float r,float i){
real=r;
image=i;
}
void Print(){
if(image<0){
cout<<real<<image<<'i'<<endl;
}else{
cout<<real<<'+'<<image<<'i'<<endl;
}
}
};
//复数类的定义
Complex operator+ (Complex& t1,Complex& t2){
Complex t(0,0);
t.real=t1.real+t2.real;
t.image=t1.image+t2.image;
return t;
}
Complex operator-(Complex& t1,Complex& t2){
Complex t(0,0);
t.real=t1.real-t2.real;
t.image=t1.image-t2.image;
return t;
}
Complex operator*(Complex &t1,Complex &t2){
Complex t(0,0);
t.real=t1.real*t2.real-t1.image*t2.image;
t.image=t1.image*t2.real+t1.real*t2.image;
return t;
}
/********* End *********/
第二关
class Virus
{
//病毒类的声明
public:
int Gen;
Virus(){
Gen=0;
}
Virus (const Virus &v){
this->Gen=v.Gen+1;
}
};
//病毒类的定义以及其他内容
bool operator==(const int& g,const Virus &v){
if(v.Gen==g){
return true;
}else{
return false;
}
}
第三关
#include <iostream>
#include <string>
using namespace std;
/********* Begin *********/
// 前置声明 Teacher 类
class Teacher;
class Student
{
//学生类的声明
public:
friend class Teacher;
Student(int num,string nam,string se){
number=num;
name=nam;
sex=se;
}
void Print(){
cout<<"学生:"<<name<<",编号:"<<number<<",性别:"<<sex<<endl;
}
private:
int number;
string name;
string sex;
};
//学生类的定义
class Teacher
{
//教师类的声明
public:
Teacher(const Student &t){
this->number=t.number;
this->name=t.name;
this->sex=t.sex;
}
Teacher(int num,string nam,string se){
number=num;
name=nam;
sex=se;
}
void Print(){
cout<<"教师:"<<name<<",编号:"<<number<<",性别:"<<sex<<endl;
}
private:
int number;
string name;
string sex;
};
//教师类的定义
第四关
注意矩阵计算的一些知识,特别是乘法
#include <iostream>
#include <string>
using namespace std;
/********* Begin *********/
class Matrix
{
//矩阵类的声明
public:
friend Matrix operator+(Matrix& m1,Matrix &m2);
friend Matrix operator-(Matrix& m1,Matrix &m2);
friend Matrix operator*(Matrix& m1,Matrix &m2);
Matrix(int r=0,int c=0){
row=r;
col=c;
/*for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
data[i][j]=0;
}
}*/
}
void Fill(int val){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
data[i][j]=val;
}
}
}
void Set(int r,int c,int val){
data[r][c]=val;
}
void Print(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cout<<data[i][j]<<' ';
}
cout<<endl;
}
}
private:
int row;
int col;
int data[100][100];
};
//矩阵类的定义
Matrix operator+(Matrix& m1,Matrix &m2)
{
//实现矩阵加法
Matrix t;
t.row=m1.row;
t.col=m1.col;
for(int i=0;i<m1.row;i++){
for(int j=0;j<m1.col;j++){
t.data[i][j]=m1.data[i][j]+m2.data[i][j];
}
}
return t;
}
Matrix operator-(Matrix& m1,Matrix &m2)
{
//实现矩阵减法
Matrix t;
t.row=m1.row;
t.col=m1.col;
for(int i=0;i<m1.row;i++){
for(int j=0;j<m1.col;j++){
t.data[i][j]=m1.data[i][j]-m2.data[i][j];
}
}
return t;
}
Matrix operator*(Matrix& m1,Matrix &m2)
{
//实现矩阵乘法
Matrix t;
t.row=m1.row;
t.col=m2.col;
for(int i=0;i<m1.row;i++){
for(int j=0;j<m2.col;j++){
int temp=0;
for(int k=0;k<m2.row;k++){
temp+=m1.data[i][k]*m2.data[k][j];
}
t.data[i][j]=temp;
}
}
return t;
}