第四章 类
运算符重载(问题不会利用友元)
#include<iostream>
using namespace std;
class Complex{
public:
Complex operator++(int);
/*Complex operator++();*/
Complex operator/=(const Complex &);
Complex operator*=(const Complex &);
Complex operator+=(const Complex &);
Complex operator-(const Complex &);
Complex(double r=0,double i=0):real(r),image(i){}
void show(){
cout<<real<<"+"<<image<<"i"<<endl;
}
private:
double real;
double image;
};
Complex Complex::operator++(int){
this->real++;
this->image++;
return *this;
}
//Complex Complex::operator++(){
// this->real++;
// this->image++;
// return *this;
//}
Complex Complex::operator/=(const Complex &c){
double x;
x=this->real;
this->real=(x*c.real+this->image*c.image)/(c.real*c.real+c.image*c.image);
this->image=(this->image*c.real-x*c.image)/(c.real*c.real+c.image*c.image);
return *this;
}
Complex Complex::operator*=(const Complex &c){
double x;
x=this->real;
this->real=x*c.real-this->image*c.image;
this->image=x*c.image+this->image*c.real;
return *this;
}
Complex Complex::operator+=(const Complex &c){
Complex t;
this->real+=c.real;
this->image+=c.image;
return *this;
}
Complex Complex::operator-(const Complex & c){
Complex t;
t.real=this->real-c.real;
t.image=this->image-c.image;
return t;
}
int main(){
Complex c1(2,3),c2(1,4),c3;
cout<<"c1=";
c1.show();
cout<<"c2=";
c2.show();
cout<<"c3=";
c3.show();
cout<<"(c3=c1-c2)=";
c3=c1-c2;
c3.show();
/*cout<<"(c1+=c2)=";
c1+=c2;
c1.show();
cout<<"(c2*=c1)=";
c2*=c1;
c2.show();*/
/*c1/=c2;
cout<<"(c1/=c2)=";
c1.show();*/
cout<<"c1++=";
c1++.show();
return 0;
}
第五章 数组与指针
标准c++string类(问题无法理解,无从下手)
#include<iostream>
using namespace std;
const int n=21;
class mystring{
public:
bool operator==(mystring &);
bool operator>(mystring &);
bool operator<(mystring &);
mystring &operator+=(char * s);
mystring &operator+(char * s);
mystring &operator=(char * s);
char operator[](int);
mystring(){
last=0;
maxsize=n;
str[0]='\0';
}//当c字符串过长时,初始化要采用结尾处理
mystring(char *s){
last=-1;
maxsize=n;
do{
last++;
str[last]=s[last];
}while(s[last]!='\0' && last<maxsize-1);
str[last]='\0';
}
~mystring(){} //析构函数
void show(){
cout<<str<<'\t'; }
private:
char str[n];
int maxsize;
int last;
};
char mystring::operator[](int){
int i;
cin>>i;
return i;
}
mystring & mystring::operator=(char *s){//这里返回值为引用,不调用复制构造函数
last=-1;
do{
last++;
str[last]=s[last];
}while(s[last]!='\0' && last<maxsize-1);
str[last]='\0';
return *this;
}
mystring &mystring::operator+(char *s){
mystring t;
last=-1;
do{
last ++;
t.str[last]=str[last]+s[last];
}while(s[last]!='\0' && last<maxsize-1);
str[last]='\0';
return t;
}
mystring &mystring::operator+=(char *s){
last=-1;
do{
last ++;
str[last]+=s[last];
}while(s[last]!='\0' && last<maxsize-1);
str[last]='\0';
return *this;
}
bool mystring::operator<(mystring & ms){
int i=0,k;
do{
k=str[i]-ms.str[i];
i++;
}while(k==0 && i<last && i<ms.last);
if(k<0) return true;
if(i==last && i!=ms.last) return true;
return false;
}
bool mystring::operator>(mystring & ms){
int i=0,k;
do{
k=ms[last]-str[last];
i++;
}while(k==0 && i<last && i<ms.last);
if(k<0) return true;
if(i==last && i!=ms.last) return true;//此处不太理解!!
return false;
}
bool mystring::operator==(mystring & ms){
int i=0,k;
do{
k=str[i]-ms.str[i];
i++;
}while( k==0 && i<last && i<ms.last);
if(k==0) return true;
if(i==last && i!=ms.last) return true;
return false;
}
int main(){
mystring str0;
mystring();
str0.show();
return 0;
}//无法理解
关于日历的制作(只能对已知的日期进行编程)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int year,month,day;
year=2012;
month=2;
cout<<setw(16)<<year<<"年"<<month<<"月"<<endl;
cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)<<"四"<<setw(4)<<"五"<<setw(4)<<"六"<<endl;
for(day=1;day<=29;day++)
{
if(day==1)
cout<<setw(16)<<day;
else
cout<<setw(4)<<day;
if((day+3)%7==0)
cout<<endl;
}
}
杨辉三角(虽然程序正确,但对循环还是不太熟练)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n[10][10],i,j,k;
for(i=0;i<10;i++)
for(j=0;j<=i;j++)
if(j==0 || j==i)
n[i][j]=1;
else
n[i][j]=n[i-1][j-1]+n[i-1][j];
for(i=0;i<10;i++)
{
for(k=10;k>=i;k--)
cout<<" ";
for(j=0;j<=i;j++)
cout<<setw(4)<<n[i][j];
cout<<endl;
}
}
第四章 类
运算符重载(问题不会利用友元)
#include<iostream>
using namespace std;
class Complex{
public:
Complex operator++(int);
/*Complex operator++();*/
Complex operator/=(const Complex &);
Complex operator*=(const Complex &);
Complex operator+=(const Complex &);
Complex operator-(const Complex &);
Complex(double r=0,double i=0):real(r),image(i){}
void show(){
cout<<real<<"+"<<image<<"i"<<endl;
}
private:
double real;
double image;
};
Complex Complex::operator++(int){
this->real++;
this->image++;
return *this;
}
//Complex Complex::operator++(){
// this->real++;
// this->image++;
// return *this;
//}
Complex Complex::operator/=(const Complex &c){
double x;
x=this->real;
this->real=(x*c.real+this->image*c.image)/(c.real*c.real+c.image*c.image);
this->image=(this->image*c.real-x*c.image)/(c.real*c.real+c.image*c.image);
return *this;
}
Complex Complex::operator*=(const Complex &c){
double x;
x=this->real;
this->real=x*c.real-this->image*c.image;
this->image=x*c.image+this->image*c.real;
return *this;
}
Complex Complex::operator+=(const Complex &c){
Complex t;
this->real+=c.real;
this->image+=c.image;
return *this;
}
Complex Complex::operator-(const Complex & c){
Complex t;
t.real=this->real-c.real;
t.image=this->image-c.image;
return t;
}
int main(){
Complex c1(2,3),c2(1,4),c3;
cout<<"c1=";
c1.show();
cout<<"c2=";
c2.show();
cout<<"c3=";
c3.show();
cout<<"(c3=c1-c2)=";
c3=c1-c2;
c3.show();
/*cout<<"(c1+=c2)=";
c1+=c2;
c1.show();
cout<<"(c2*=c1)=";
c2*=c1;
c2.show();*/
/*c1/=c2;
cout<<"(c1/=c2)=";
c1.show();*/
cout<<"c1++=";
c1++.show();
return 0;
}
第五章 数组与指针
标准c++string类(问题无法理解,无从下手)
#include<iostream>
using namespace std;
const int n=21;
class mystring{
public:
bool operator==(mystring &);
bool operator>(mystring &);
bool operator<(mystring &);
mystring &operator+=(char * s);
mystring &operator+(char * s);
mystring &operator=(char * s);
char operator[](int);
mystring(){
last=0;
maxsize=n;
str[0]='\0';
}//当c字符串过长时,初始化要采用结尾处理
mystring(char *s){
last=-1;
maxsize=n;
do{
last++;
str[last]=s[last];
}while(s[last]!='\0' && last<maxsize-1);
str[last]='\0';
}
~mystring(){} //析构函数
void show(){
cout<<str<<'\t'; }
private:
char str[n];
int maxsize;
int last;
};
char mystring::operator[](int){
int i;
cin>>i;
return i;
}
mystring & mystring::operator=(char *s){//这里返回值为引用,不调用复制构造函数
last=-1;
do{
last++;
str[last]=s[last];
}while(s[last]!='\0' && last<maxsize-1);
str[last]='\0';
return *this;
}
mystring &mystring::operator+(char *s){
mystring t;
last=-1;
do{
last ++;
t.str[last]=str[last]+s[last];
}while(s[last]!='\0' && last<maxsize-1);
str[last]='\0';
return t;
}
mystring &mystring::operator+=(char *s){
last=-1;
do{
last ++;
str[last]+=s[last];
}while(s[last]!='\0' && last<maxsize-1);
str[last]='\0';
return *this;
}
bool mystring::operator<(mystring & ms){
int i=0,k;
do{
k=str[i]-ms.str[i];
i++;
}while(k==0 && i<last && i<ms.last);
if(k<0) return true;
if(i==last && i!=ms.last) return true;
return false;
}
bool mystring::operator>(mystring & ms){
int i=0,k;
do{
k=ms[last]-str[last];
i++;
}while(k==0 && i<last && i<ms.last);
if(k<0) return true;
if(i==last && i!=ms.last) return true;//此处不太理解!!
return false;
}
bool mystring::operator==(mystring & ms){
int i=0,k;
do{
k=str[i]-ms.str[i];
i++;
}while( k==0 && i<last && i<ms.last);
if(k==0) return true;
if(i==last && i!=ms.last) return true;
return false;
}
int main(){
mystring str0;
mystring();
str0.show();
return 0;
}//无法理解
关于日历的制作(只能对已知的日期进行编程)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int year,month,day;
year=2012;
month=2;
cout<<setw(16)<<year<<"年"<<month<<"月"<<endl;
cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)<<"四"<<setw(4)<<"五"<<setw(4)<<"六"<<endl;
for(day=1;day<=29;day++)
{
if(day==1)
cout<<setw(16)<<day;
else
cout<<setw(4)<<day;
if((day+3)%7==0)
cout<<endl;
}
}
杨辉三角(虽然程序正确,但对循环还是不太熟练)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n[10][10],i,j,k;
for(i=0;i<10;i++)
for(j=0;j<=i;j++)
if(j==0 || j==i)
n[i][j]=1;
else
n[i][j]=n[i-1][j-1]+n[i-1][j];
for(i=0;i<10;i++)
{
for(k=10;k>=i;k--)
cout<<" ";
for(j=0;j<=i;j++)
cout<<setw(4)<<n[i][j];
cout<<endl;
}
}