运算符重载字符串类,经典例题
分类:高并发

#include <iostream>

/*

淮中国人民解放军海军事工业程大学高校Computer科学系

曾在软通引力写过的算法题当中有一小部分是参照英特网的资料,今后拿出去给大家分享!
第3章 调整语句

using namespace std;

规划三个mystring类,蕴含数据成员char * pstr;

试验报告书

/* 1、打字与印刷出装有的“水仙花数”。所谓“金盏银台数”是指一个肆位数,其各位数字立方和万分该数本人。比如,153是一金盏银台数,因为153 = 13 + 53 + 33。 */
#include<iostream.h>
void main()
{
int i, a=0, b=0, c=0;
for(i=100;i<1000;i++)
{
a=i%10;
b=i/10%10;
c=i/100%10;
if(a*a*a+b*b*b+c*c*c==i)
cout<<"i="<<i<<endl;
}
}

#define PI 3.14159
class shape
{
    public:
    virtual int area() const=0;
    virtual void show()=0;
};
class rectangle : public shape
{
    public:
    int _with;
    int _height;
    rectangle(int with,int height):_with(with),_height(height){}
    int area()const
    {
        return _with*_height;
    }
    void show()
    {
        cout<<"rectangle: with:"<<_with<<" height:"<<_height<<endl;
        cout <<"area :"<<area()<<endl;
    }
    bool operator ==(const rectangle &a)
    {
        if(this->area()==a.area())
        return 1;
        else
        return 0;
    }
    bool operator >(const rectangle &a)
    {
        if(this->area()>a.area())
        return 1;
        else
        return 0;
    }
    bool operator <(const rectangle &a)
    {
        if(this->area()<a.area())
        return 1;
        else
        return 0;
    }
    ~rectangle(){}
};
class ellipse: public shape
{
    public:
    int _with;
    int _height;
    ellipse(int with,int height):_with(with),_height(height){}
    int area()const
    {
        return PI*_with*_height;
    }
    void show()
    {
        cout<<"ellipse: with:"<<_with<<" height:"<<_height<<endl;
        cout <<"area :"<<area()<<endl;
    }
    bool operator ==(const ellipse &a)
    {
        if(this->area()==a.area())
        return 1;
        else
        return 0;
    }
    bool operator >(const ellipse &a)
    {
        if(this->area()>a.area())
        return 1;
        else
        return 0;
    }
    bool operator <(const ellipse &a)
    {
        if(this->area()<a.area())
        return 1;
        else
        return 0;
    }
    ~ellipse(){}

和int length; 通过运算符重载落成字符串的输入

 

/* 2、三个数假使恰好等于它的因子之和,这一个数就叫做“完数”。比如,6的因子为1、2、3,而6 = 1 + 2 + 3,由此6是“完数”。编程序找寻1000之内的持有完数,并按下边包车型客车格式输出其因子:
6 -〉1,2,3 */
#include<iostream.h>
void main()
{
int i,j,sum=0,a[50],k,t;
for(i=1;i<=1000;i++)
{
sum=0;
        for(j=1;j<i;j++)
{
if(i%j==0)
{
sum+=j;
a[k++]=j;
}
}
t=k;
if(sum==i)
{
cout<<i<<"->";
for(k=0;k<t;k++)
{
cout<<a[k];
if(k<t-1)cout<<",";
}
cout<<endl;
}
    k=0;
}
}

};

>>、输出<<、连接+=、赋值=、关系运算(==、!=、

课程名:《 C++程序设计(二卡塔尔》  

/* 3、求Sn=a+aa+aaa+…+aa…a之值,在那之中a是三个数字。举例:2+22+222+…+22222(这时候n=5),n由键盘输入。*/
#include<iostream.h>
void main()
{
double a,sn=0.0,sum=0.0;
int n,i;
cout<<"please input a number";
cin>>a;
    cout<<"please input n number";
cin>>n;
sn=a;
sum=a;
    for(i=2;i<=n;i++)
    {
sum=sum*10+a;
sn+=sum;
    }
cout<<"Sn="<<sn<<endl;
}

int main()
{
    //cout << "Hello world!" << endl;
    int a=0,b=0;
    cout<<"矩形"<<endl;
    cout<<"宽度 高度"<<endl;

>、<)、下标[]等运算。

题   目:      多态性和虚函数        

/* 4、三个球从100米中度自由落下,每一次落榜后反跳回原中度的五成,再落下。求它在第十三遍一败涂地时,共通过了微微米?第十二次反弹多高?*/
#include<iostream.h>
void main()
{
double h1=100,h2=100,sum=0.0;
int i;
for(i=1;i<=10;i++)
{

    rectangle **p;
    p=new rectangle*[3];
    for(int i=0;i<3;i++)
    {
        cin>>a>>b;
        p[i]=new rectangle(a, b);
    }

*/

                                              

sum+=h2;
h1=h1/2.0;
h2=h1*2;
}
cout<<"sum="<<sum<<"   "<<"h1="<<h1<<endl;
}

    cout<<"椭圆"<<endl;
    cout<<"外接矩形宽度 外接矩形中度"<<endl;

#include <iostream>

班   级:                   

/* 5、猴子吃桃难题。猴子第一天摘下多少个蟠桃,当即吃了八分之四,还不恬适,又多吃了一个。第二天深夜又将剩下的黄桃吃掉了四分之二,又多吃了叁个。今后天天早晨都吃了明天剩余的四分之二零三个。到第10天早晨想再吃时,见只剩叁个黄桃了。求第一天共摘了稍微黄桃。*/
#include<iostream.h>
void main()
{
int number,i;
number=1;
for(i=10;i>1;i--)
number=(number+1)*2;
cout<<"number="<<number<<endl;
}

    ellipse **q;
    q=new ellipse*[3];
    for(int i=0;i<3;i++)
    {
        cin>>a>>b;
        q[i]=new ellipse(a, b);
    }
    p[2]->show(),p[1]->show();
    cout<<p[2]->area()<<endl<<p[1]->area()<<endl;
    cout<<(*(p[2])==*(p[1]))<<endl;
    /*
    ellipse a(5,3);
    ellipse b(7,5),c(9,6);
    cout<<" a=b "<<(a==b)<<" a<b "<<(a<b)<<" a>b "<<(a>b)<<endl;
    */
    return 0;
}

#include <cstring>

学   号:                

8.++程序中使用流格式输入、输出,大家得以怎么办?
答:在程序的上马饱含头文件iostream.h
     cin输入,cout输出。
例如:

标题: 1、抽象类的派生和运算符的重载
时 限: 1000 ms
内存限制: 10000 K
总时限: 3000 ms
描述:

1.抽象类的定义:完成形状抽象类Shape的定义:公有函数:面积(Area);公有函数:显示(Show);实现构造函数和析构函数

2.从形状类Shape派生矩形类Rectangle:

添加公有成员:宽度(Width),高度(Height);
重载比较操作符:==,面积是否相等;
重载比较操作符:>,判面积是否大于某个矩形;
重载比较操作符:<,判面积是否小于某个矩形;
实现公有函数:显示(Show),屏幕打印 宽度,高度。
实现公有函数:面积(Area),计算矩形面积。
实现构造函数和析构函数

3.从形状类Shape派生椭圆类Ellipse:
添加公有成员:椭圆外接矩形宽度(Width),椭圆外接矩形高度(Height);
重载比较操作符:==,面积是否相等;
重载比较操作符:>,判面积是否大于某个椭圆;
重载比较操作符:<,判面积是否小于某个椭圆;
实现公有函数:显示(Show),屏幕打印椭圆外接矩形宽度,椭圆外接矩形高度。
实现公有函数:面积(Area),计算椭圆面积。
实现构造函数和析构函数。

输入:

创建6个实例:

矩形
宽度 高度
23 17
89 25
17 23
椭圆
外接矩形宽度 外接矩形高度
29 17
89 75
17 29

输出: 矩形之间的面积,椭圆间的面积的比较结果
输入样例:
输出样例:
提示: 运算符重载为类的成员函数
来源:

#include<stdlib.h>

姓   名:                     

#include<iostream.h>
void main()
{
   int a;
   cout<<"请输入a的值:";
   cin>>a;
   cout<<"a的值为:"<<a<<endl;
}

using namespace std;

 

第4章 函数

class mystring

 

/* 1、写一函数用“气泡法”对输入的十一个字符按由小到大的顺序排列。*/
#include<iostream.h>
void main()
{
int i,j,temp,a[10];
cout<<"please input ten numbers:n";
for(i=0;i<10;i++)
cin>>a[i];
for(i=0;i<10;i++卡塔尔(قطر‎ //每循环叁次明确数组中多个数的地点
for(j=i+1;j<10;j++State of Qatar //每便循环相比贰个数的分寸
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
cout<<"resort result=";
for(i=0;i<10;i++)
cout<<a[i]<<" ";
}

{

 

/* 2、用递归方法求n阶勒让得多项式的值,递归公式为
         1 (n = 0)
Pn(x) =  x (n = 1)
         ((2n-1)*x*Pn-1(x)-(n-1)*Pn-2(x))/n (n > 1) */
#include<iostream.h>
double fun (double,double);
void main()
{
double n,x,sum;
cout<<"input n and x"<<endl;
cin>>n>>x;
sum=fun(n,x);
cout<<"P"<<n<<"("<<x<<")"<<"="<<sum<<endl;
}
double fun(double n1,double x1)
{
if (n1==0)
return 1;
else if (n1==1)
return  x1;
else if (n1>1)
return ((2*n1-1)*x1*fun(n1-1,x1)-(n1-1)*fun(n1-2,x1))/n1;
}

public:

  

/* 3、编写一函数,由实参传来一字符串,总括此字符串中字母、数字、空格、和别的字符的个数,并在主函数中输入字符串以致出口上述结果。 */
#include<iostream.h>
void judge(char a[]);
void main()
{
const int size=100;
char a[size];
cin.getline(a,size);
judge(a);
}
void judge(char a[100]卡塔尔国//决断字符类型
{
int letter=0,number=0,others=0,i=0;
while(a[i]!='')
{
if ((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='z'State of Qatar卡塔尔letter++;//计算字母个数
else if (a[i]>='0' && a[i]<='9'卡塔尔(قطر‎ number++;//总括数字个数
else others++;//计算其余数个数
i++;
}
cout<<"letter="<<letter<<"  number="<<number<<"  others="<<others<<endl;
}

mystring;

   

/* 4、给出年、月、日,总计该日是该年的第几天。 */
#include<iostream.h>
int lead(int);
void main()
{
int ly,year,month,date,i,sum=0;
cout<<"input year、month、date: ";
cin>>year>>month>>date;
int a[12]={31,0,31,30,31,30,31,31,30,31,30,31};
ly=lead(year);
if (ly==1)
a[1]=29;//366天
else a[1]=28;//365天
for(i=0;i<month-1;i++卡塔尔 //当前月事情发生在此以前全数月天数累积和
sum+=a[i];
sum+=date; //加受骗前月天数
cout<<"你输入的日子是当年的第"<<sum<<"天";
}
int lead(int y卡塔尔国//剖断闰年
{
if((y%4==0&&y%100!=0)||(y%400==0)) return 1;//是闰年
else return 0;//不是闰年
}

//mystring;

   

/* 5、写多个函数,分别求多个整数的最大协议数和最小公倍数,用主函数调用那多个函数,并出口结果,多个整数由键盘输入。 */
#include<iostream.h>
int cdivisor(int,int);
int cmultiple(int,int,int);
void main()
{
int x,y,d,m;
cout<<"input two number: ";
cin>>x>>y;
d=cdivisor(x,y);
m=cmultiple(x,y,d);
cout<<"common divisor is "<<d<<endl<<"common multiple is "<<m<<endl;
}
int cdivisor(int x1,int y1卡塔尔(قطر‎//最大公约数
{
int r,temp;
if (x1<y1)
{
temp=x1;
x1=y1;
y1=temp;
}
while(x1%y1State of Qatar//当不小数除以很小数余数等于0时,相当的小数为最大合同数
{
r=x1%y1;
x1=y1;
y1=r;
}
return y1;
}
int cmultiple(int x2,int y2,int d1卡塔尔//最小公倍数
{
return x2*y2/d1;//两数相乘结果除以它们的最大合同数为最小公倍数
}

~mystring()

   

/* 6、写一函数,将八个字符串连接。 */
#include<iostream.h>
#include<string.h>
void main()
{
const int size=100;
char a[size],b[size];
cout<<"input two string:"<<endl;
cin.getline(a,size);
cin.getline(b,size);
strcat(a,b);
cout<<"a="<<a<<endl;
}

{

评语:

/* 7、写一函数,将一个字符串的元音字母复制到另一个字符串,然后输出。 */
#include<iostream.h>
#include<string.h>
void scpy(char *,char *);
void main()
{
const int size=100;
char a[size]="Hello world";
char b[size]="Net";
cout<<"a= "<<a<<"b= "<<b<<endl;
scpy(a,b);
cout<<"a= "<<a<<endl;
}
void scpy(char *p,char *q)
{
while(*q!='')
{
if (*q=='a'||*q=='A'||*q=='e'||*q=='E'||*q=='i'||*q=='I'||*q=='o'||*q=='O'||*q=='u'||*q=='U')
*p++=*q;
q++;
}
}

}

   

/* 8、写一函数,输入贰个几个人数字,需求输出这4个数字字符,但每多少个数字间空一空格。如输入壹玖捌玖,应输出“1 9 9 0”。 */
#include<iostream.h>
#include<string.h>
void outs(char a[]);
void main()
{
const int size=10;
char a[size];
cin.getline(a,size);
outs(a);
}
void outs(char a[10])
{
int i;
if(strlen(a)<=4)
{
for(i=0;i<4;i++)
cout<<a[i]<<" ";
}
else cout<<"input error."<<endl;
}

mystring& operator=(mystring& s);

 

第5章   数组

void operator+=(mystring & a)

   

/* 1、将多个数组中的值按逆序重新存放,举例,原本逐条为:a、b、c、d。必要改为:d、c、b、a。 */
#include<iostream.h>
void back(char *);
void main()
{
char a[50]="abcdefg";
cout<<"a="<<a<<endl;
back(a);
}
#include<iostream.h>
void back(char *p)
{
int i=0;
while(*p!='')
{
p++;//把指针定位到字符串末尾
i++;//总括字符个数
}
cout<<"a=";
for(;i>0;i--卡塔尔国//逆序输出
{
p--;
cout<<*p;
}
cout<<endl;
}

{

 

/* 2、打印出杨辉三角形(须求打字与印刷出前15行)。(杨辉三角最本色的特点是,它的两条斜边都以由数字1构成的,而其余的数则是相等它肩上的五个数之和。卡塔尔国 */
#include<iostream.h>
void tri(int a[][15]);
void main()
{
int i,j,a[15][15];
tri (a);
cout<<"a= ";
for(i=0;i<15;i++卡塔尔(قطر‎//遍历整个数组
{
for(j=0;j<=i;j++)
{
cout<<a[i][j];
if(a[i][j]>=1&&a[i][j]<=9State of Qatar//当输出个位数之后输出4个空格保持有层有次
cout<<"    ";
else if (a[i][j]>=10&&a[i][j]<=99卡塔尔//当输出11人数之后输出3个空格保持整整齐齐
cout<<"   ";
else if(a[i][j]>=100&&a[i][j]<=999State of Qatar//当输出百位数之后输出2个空格保持有条理
  cout<<"  ";
  else cout<<" ";//当输出百位数之后输出1个空格保持次序分明
}
cout<<endl<<"   ";//每行输出停止后换行
}
}
void tri(int a[15][15])
{
int i,j;
for(i=0;i<15;i++)
for(j=0;j<=i;j++)
{
if(j==0||j==i卡塔尔//三角形第一列和对角线被赋值为1
a[i][j]=1;
else a[i][j]=a[i-1][j-1]+a[i-1][j];//算出此外的数组成分
}
}

this->pstr = realloc(this->pstr, this->length + a.length + 1); strcpy(this->pstr + (this->length), a.pstr);

   

/* 3、编一程序,将五个字符串连接起来,不要用strcat函数。 */
#include<iostream.h>
#include<string.h>
void scat(char *,char *);
void main()
{
const int size=100;
char a[size]="Hello";
char b[size]="Bye";
cout<<"a="<<a<<"   b="<<b<<endl;
scat(a,b);
cout<<"a="<<a<<" after link a and b"<<endl;
}
void scat(char *p,char *q)
{
while(*p!=''卡塔尔(قطر‎//鲜明数组a的插入地方
{
p++;
}
while(*q!='')
{
*p=*q;
p++;
q++;
}
}

}

 

/* 4、打字与印刷“魔方阵”。所谓魔方阵是指那样的方阵,它的每一行、每一列和对角线之和均相等。比如:三阶魔方阵:
      8 1 6
      3 5 7
      4 9 2
渴求打印由1到n2的本来数构成的兼具魔方阵。 */
//方法一:输出N介魔方阵,但每介只输出一种。
#include<iostream.h>
void square(int a[][10],int k,int n);
void main()
{
int n,i,j,k,a[10][10]={0};
cout<<"input an odd number:"<<endl;
cin>>n;
k=n/2;//鲜明第三个数列数
square(a,k,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<"t"<<a[i][j];
cout<<endl;
}
}
void square(int a[][10],int k,int n)
{
int i,j;
for(i=1,j=0;i<=n*n;i++,j--,k++卡塔尔//n为阶数,从1发端给数组赋值
{
if(j<0&&k>=n卡塔尔(قطر‎//当数组行列都越出范围时候,明确数组准确地点
{
j+=2;k-=1;
}
else if(j<0State of Qatar//当数组行越出范围时候,鲜明数组准确地方
j+=n;
else if(k>=nState of Qatar//当数组列越出范围时候,分明数组准确地方
          k-=n;
  else if(a[j][k]!=0卡塔尔//当数组原来的地点置有数时候,鲜明数组地点
  {
j+=2;k-=1;
  }
a[j][k]=i;
}
}
//方法二:输出N介魔方阵全部魔方阵。
#include <string>
#include <iomanip>
#include <iostream>

char & operator [];

   

using namespace std;

bool operator ==(const mystring & s)

 

void printA(int **p,int n卡塔尔国//输出这么些n阶魔方阵
{
cout<<endl<<"上边是二个"<<n<<"阶魔方阵:"<<endl;

{

   

int i,j;

if (strcmp(this->pstr, s.pstr) == 0)

 

for(i = 0;i < n;i++)
{
for(j = 0;j < n;j++)
{
cout<<setw(4)<<p[i][j];
}

return 1;

   

cout<<endl;
}

else

 

cout<<endl<<endl;
}
bool Judge(int **p,int n卡塔尔国//推断是还是不是为n阶魔方阵
{
int i,j,sum = 0,NowSum = 0;
bool YesOrNo = true;

return 0;

   

for(j = 0;j < n;j++State of Qatar//第一行总和
{
sum += p[0][j];
}

}

成就:                            指点老师:                  

for(i = 1;i < n;i++卡塔尔国//推断每行总和是或不是等于
{
NowSum = 0;
for(j = 0;j < n;j++)
{
NowSum += p[i][j];
}

bool operator !=(const mystring & s)

   

if(NowSum != sum)
{
YesOrNo = false;

{

 

goto END;
}
}

if (strcmp(this->pstr, s.pstr) != 0)

   

for(i = 0;i < n;i++卡塔尔(قطر‎//每列是或不是等于
{
NowSum = 0;
for(j = 0;j < n;j++)
{
NowSum += p[j][i];
}

return 1;

                             批阅时间:    年    月     日

if(NowSum != sum)
{
YesOrNo = false;

else

   

goto END;
}
}

return 0;

   

NowSum = 0;
for(i = 0,j = 0;i < n,j < n;i++,j++卡塔尔//主对角线是不是等于
{
NowSum += p[i][j];
}
if(NowSum != sum)
{
YesOrNo = false;

}

  

goto END;
}

bool operator <(const mystring & s)

 

NowSum = 0;
for(i = n-1,j = 0;i >= 0,j < n;i--,j++卡塔尔国//次对角线是还是不是等于
{
NowSum += p[i][j];
}
if(NowSum != sum)
{
YesOrNo = false;

{

 

goto END;
}

if (strcmp(this->pstr, s.pstr)<0)

 

END:
return YesOrNo;
}
void combination(int **p,int n,int *a)//求m = n*n个数(1,2,3.....mState of Qatar的全排列
{
int m = n*n;
static int Num = 0;
int *b_val = new int[m];
int c = 0,k,i,j;

return 1;

 

b_val[c] = -1;//一维数组首地址的值赋-1 c[0]-1
while(b_val[0] < m) //-1
{
if(++b_val[c] < m)//分别从0发轫加多每一种成分值,并约束不超越最大阶数
                 //b[0]0 [1]0 [1]1 [2]0 [2]1 [2]2...[6]6...[7]0...[7]7 [8]0...[8]8 判别魔方 [8]9 012345678
                      //                                               [7]8 [8]0...[8]7 推断魔方 [8]8 [8]9 012345687
     //                                 [6]7...[7]0...[7]6        [8]8 012345768
                                           //         012345786
     //                                                                012345867 ...
           //      876543210
{  
for(k = 0;k < c;k++卡塔尔国//是或不是与前方数字再次,如有重复成分跳出,否则使K下标等于C下标                                 
if(b_val[k] == b_val[c])                                                    
break;
   
if(k == c卡塔尔//若无再度成分,就能够规定当前元素值,并持续排列下贰个下标的数组成分
{
if(c+1 < m卡塔尔 //1 2 3...7 8   倘若不满足条件,则生成了一组排列形式,不然继续排列下二个因素
           //          8
{   
++c; //1 2 3...7 8
//          8
b_val[c] = -1;
//   continue;
}
else  //生成了一组排列方式
{
k = -1;
for(i = 0;i < n;i++)
{
for(j = 0;j < n;j++)
{
p[i][j] = a[b_val[++k]];//a[0]-a[8]
}
}

else

 

//判别是或不是为n阶魔方阵
if(Judge(p,n))
{
printA(p,n);
}
}
}
}
else
{
c--;
}
}

return 0;

 

delete []b_val;
}
void evaluate(int **p,int nState of Qatar//给n阶方阵的要素赋值
{
int i;

}

 

int *AllNum = new int[3*3];
for(i = 1;i <= n*n;i++)
{
AllNum[i - 1] = i;
}

bool operator >(const mystring & s)

 

combination(p,n,AllNum);

{

 

delete []AllNum;
}
void main()
{
int i,n,**a;
string s;

if (strcmp(this->pstr, s.pstr)>0)

 

do
{
//输入n阶方阵的阶数n
cout<<"请输入n阶方阵的阶数n(退出程序按e或E键State of Qatar:";
cin>>s;

return 1;

 

if(!strcmp(s.c_str(),"e") || !strcmp(s.c_str(),"E"))
{
break;
}
else if(s.find_first_not_of("0123456789") != string::npos)
{
cout<<"请输入有效的数字,无法含有非数字的字符。"<<endl;

else

 

continue;
}
else
{
n = atoi(s.c_str());

return 0;

  1. 实验内容或主题材料

if(n < 1)
{
cout<<"请输入有效的数字,必得 >= 1。"<<endl;

}

(1)注脚二维坐标类作为基类派生圆的 类,把派生类圆作为基类,派生圆柱体类。当中,基类二维坐标类有成员数量:x、y坐标值;有成员函数:布局函数完成对基类成员数量的起初化、输出的积极分子函数,需要输出坐标地方。派生类圆类有新扩充成员数量:半径(奥迪Q3);有成员函数:结构函数完成对成员数量的开始化、总结圆面积的积极分子函数、输出半径的成员函数。派生圆柱体类新扩大多稀有高(H);新扩展成员函数有布局函数、总括圆柱体体量的函数和出口全部成员的函数。请达成程序代码的编辑、调试。

continue;
}

friend ostream & operator <<(ostream & os, const mystring & s)

(2)教材393页7-8题。

//分配内部存款和储蓄器
a = new int*[n];
for(i = 0; i < n; i++)
{
a[i] = new int[n];
}

{

(3)教材416页1、4、5题。

cout<<"正在运算,请等待。。。。。。"<<endl;

return os << s.pstr<< strlen;

  1. 尝试指标与需要

//给n阶方阵的要素赋值
evaluate(a,n);

}

(1)明白世袭与派生的概念

cout<<"运算停止!"<<endl;

friend istream & operator >>(istream & is, mystring & s)

(2)驾驭通过持续派生出五个新的类的主意

for(i = 0; i < n; i++)
{
delete []a[i];
}
delete []a;
}
}while(1);

{

(3)领悟多态性的定义

return;
}

/* delete []s.pstr;

(4)驾驭虚函数的魔法与利用方法

/* 5、求贰个3x3矩阵对角线成分之和。*/
#include<iostream.h>
int dia(int a[][3]);
void main()
{
int i,j,sum,a[3][3]={2,3,5,6,2,3,1,9,0};
cout<<"a= ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j];
cout<<endl<<"   ";
}
sum=dia(a);
cout<<"nsum="<<sum<<endl;
}
int dia(int a[3][3])
{
int i,j,sum=0;
for(i=0;i<3;i++卡塔尔//主对角线之和
for(j=i;j<=i;j++)
sum+=a[i][j];
for(j=0;j<3;j++State of Qatar//另贰个对角线之和
for(i=2-j;i<=2-j;i++)
if(i!=j卡塔尔//防止重复累加多少个对焦向重合的成分
sum+=a[i][j];
return sum;//重临对角线员素之和
}

is >>s.length ;

⑵ 源代码

/* 6、编写三个主次,将字符数组s第22中学的全体字符拷贝到字符数组s1中。不用strcpy函数。拷贝时,‘’也要拷贝过去。 ‘’后边的字符不拷贝。*/
#include<iostream.h>
void scopy(char a[],char b[]);
void main()
{
int i;
char a[10];
char b[10]="Hello";
scopy(a,b);
for(i=0;a[i]!='';i++)
cout<<a[i];
cout<<endl;
}
void scopy(char a[],char b[])
{
int i;
for(i=0;b[i]!='';i++)
{
a[i]=b[i];
}
a[i]='';
}

s.pstr = new char[s.length + 1];

(1)#include<iostream>

/* 7、用筛选法求100之内的素数。(所谓素数就是除了1和它自身以外,不能再被其他整数整除,这种数称作素数(也称质数卡塔尔国。)*/
#include<iostream.h>
void main()
{
int i,j;
for(i=1;i<=100;i++)
{
for(j=2;j<i;j++State of Qatar//决断素数
if(i%j!=0);
else break;//不是素数
if(i==j卡塔尔//相等为素数
cout<<" "<<i;
}
cout<<endl;
}

is >> s.pstr;

using namespace std;

/* 8、用采纳法对12个整数排序。*/
#include<iostream.h>
void csort(int a[10]);
void main()
{
int i;
int a[10]={6,4,2,7,9,0,1,6,3,0};
for(i=0;i<10;i++State of Qatar//输出原数组数据顺序
cout<<a[i];
cout<<endl;
csort(a);
for(i=0;i<10;i++卡塔尔(قطر‎//输出排序后的逐条
cout<<a[i];
cout<<endl;
}
void csort(int a[10])//排序
{
int i,j,k,temp=0;
for(i=1;i<10;i++)
{
k=i;
for(j=k+1;j<10;j++卡塔尔//找寻最小数的数组下标
if(a[k]>a[j])k=j;
if(k!=i)
{
temp=a[i];//把数放到正确地点
a[i]=a[k];
a[k]=temp;
}
}
}

*(s.pstr + s.length) = '';

class point 

第6章   指针

*/

{ public: 

/* 1、在主函数中输入11个字符串。用另一函数对它们排序。然后在主函数输出那12个已排好序的字符串。(用指针达成)*/
#include<iostream.h>
void psort(int *p);
void main()
{
int i,a[10];
cout<<"please input ten numbers:n";
for(i=0;i<10;i++)
cin>>a[i];
psort(a);
cout<<"resort result=";
for(i=0;i<10;i++)
cout<<a[i]<<" ";
}
void psort(int *p)
{
int i,j,temp;
for(i=0;i<10;i++卡塔尔国 //每循环二次分明数组中三个数的职位
for(j=i+1;j<10;j++卡塔尔 //每回循环比较三个数的分寸
{
if(p[i]>p[j])
{
temp=p[j];
p[j]=p[i];
p[i]=temp;
}
}

is>>s.pstr;

point(float a=0,float b=0);

}

return is;

void setpoint(float,float); 

/* 2、输入二个字符串,内有数字和非数字字符,如A123x456 1233?8997jhlkll
将个中一而再的数字作为贰个大背头,依次贮存到一数组a中,总括共有多少个整数,并出口那一个数。*/
#include<iostream.h>
#include<string.h>
#include <windows.h>
int charge(int *,char *);
void main()
{
int a[50],i,numb;
char b[50];
cout<<"please input a character string:"<<endl;
cin.getline(b,50);
system("cls");
cout<<"your character string is ";
cout.write(b,strlen(b))<<endl;
numb=charge(a,b);
for(i=0;i<numb;i++)
cout<<" a["<<i<<"]="<<a[i];
cout<<endl<<"total numbers="<<numb<<endl;
}
int charge(int *q,char *p)//*q指向新数组,*p指向早先数组
{
int numb=0;
for(;*p!='';p++卡塔尔(قطر‎//判定每一个字符
{
if(*p>='0'&&*p<='9')
{
*q=(*pState of Qatar-'0';//将字符型整数转换到整型整数赋值给新数组
p++;
while(*p>='0'&&*p<='9'卡塔尔(قطر‎//剖断是或不是有一连字符型整数
{
*q=(*q)*10+((*pState of Qatar-'0'卡塔尔(قطر‎;//将连接字符型整数转变到三个整型整数赋值给新数组
p++;
}
q++;
numb++;//总括整数的个数
}
}
return numb;
}

}

float get_x()const{return x;} 

/* 3、用指向指针的指针的艺术对5个字符串排序并出口。*/
#include <iostream.h>
#include <string.h>
void sort(char **str);
void main()
{
int i;
char *string[5];
cout<<"输入5个字符串:"<<endl;
for(i=0;i<5;i++)
{
string[i] = new char[10];
cin.getline(*(string+i),50);
}
sort(string);
for(i=0;i<5;i++)
delete [] string[i];
}
void sort(char **str)
{
int i=0,j;
char *p=0;
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(*(str+i),*(str+j))<0)
{
p=*(str+i);
*(str+i)=*(str+j);
*(str+j)=p;
}
}
}
cout<<"after sort the chars :"<<endl;
for(i=0;i<5;i++)
{
cout<<*(str+i)<<endl;
}
}

private:

float get_y()const{return y;} 

/* 4、总结一字符串在另八个字符串中冒出的次数。*/
#include<iostream.h>
#include<string.h>
int change(char *,char *);
void main()
{
int sum;
char a[10]="dog";
char b[20]="sdlkdogsddydodog";
cout.write(a,10)<<endl;
cout.write(b,20)<<endl;
sum=change(a,b);
cout<<"sum="<<sum<<endl;
}
int change(char *p,char *q)
{
    int sum=0,i=0;
while(*q!='')
{
while(*p==*q&&*p!=''卡塔尔//相比是或不是含有非凡字符串
{
*p++;
*q++;
i++;
}
if(*p=='')
{
sum+=1;//含有字符串个数
}
p=p-i;//第三个字符串重新定位
q=q-i;//第一个字符串重新定位
i=0;//重新累积移动次数
q++;
}
return sum;
}

char *pstr;

friend ostream &operator<<(ostream &,  point &);

/* 5、有n个整数,使其前面各数顺序向后移m个职位,最终m个数产生最前面包车型地铁m个数.n和m从键盘输入。*/
#include<iostream.h>
#include<string.h>
void charge(int a[],int,int);
void main()
{
int i,n,m,a[50];
cout<<"请输入n的值:";
cin>>n;
cout<<"请输入移动位数:";
cin>>m;
cout<<"请输入整数:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"您输入的整数为:";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
charge(a,n,m);
cout<<"移动后的卡尺头为:";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
void charge(int a[],int n, int m卡塔尔//n为整数个数,向右移动m个地方
{
int i,j;
for(j=0;j<m;j++)//移动m个位置
{
for(i=n-1;i>=0;i--卡塔尔国//移动二个职责将在移动每二个数组成分
{
a[i+1]=a[i];
}
a[0]=a[n];
}
}

int length;

protected:  float x,y;

/* 6、有n个人围成一圈,顺序排号。从第二个人开头报数(从1到3报数),凡报到3的人脱一命归阴界,问最终留给的是原来第几号的那位。*/
#include<iostream.h>
void change(int a[],int n);
void main()
{
int i,a[50],n;
cout<<"输入人数:";
cin>>n;
for(i=0;i<n;i++)
a[i]=i+1;
change(a,n);
}
void change(int a[],int n)
{
int qnumber=0,i=0,k=0;
while(n-qnumber>1State of Qatar//直到只剩余1人时
{
if(a[i]!=0)k++; //报数
if(k==3)
{
a[i]=0; //退出圈子的人
qnumber++; //退出的总人数
k=0; //重新早先报数
}
i++;
if(i==nState of Qatari=0; //当全体人都报过数之后再度每人再一次报数
}
for(i=0;i<n;i++)
if(a[i]!=0)cout<<a[i]<<" ";
}

};

};

/* 7、写一函数,完结八个字符串的可比。即自个儿写八个strcmp函数。*/
#include<iostream.h>
#include<string.h>
int change(char *,char *);
void main()
{
int result;
char a[10]="dog";
char b[20]="sdlkdogsddydodog";
cout.write(a,10)<<endl;
cout.write(b,20)<<endl;
result=change(a,b);
cout<<"result="<<result<<endl;
}
int change(char *p,char *q)
{
    int sum=0,i=0,flag=0;
while(*p!=''&&*q!='')
{
if(*p>*q)
{
flag=1;//第贰个字符串大于第三个字符串再次回到1
break;
}
if(*p<*q)
{
flag=-1;//第二贰个字符串大于第三个字符串再次回到-1
break;
}
if((*p==*q)&&*(p+1)==''&&*(q+1)!='')
{
flag=-1;//第二叁个字符串大于第二个字符串重返-1
break;
}
*p++;
*q++;
}
return flag;
}

mystring::mystring

point::point(float a,float b)

/* 8、将一个5 x 5的矩阵中最大的因素放在宗旨,多个角分别放三个小小的的成分(顺序为从左到右,从上到下顺序依次从小到大寄放),写一函数达成之。用main函数调用。*/
#include<iostream.h>
void psort(int a[]);
void main()
{
int i;
int a[25]={5,3,52,6,2,1,8,0,23,56,7,21,23,4,57,62,15,31,45,6,43,78,12,53,41};
for(i=0;i<25;i++)
{
cout<<" "<<a[i];
}
cout<<endl;
psort(a);
}
void psort(int a[])
{
int i,j,k=0,temp,b[5][5];
for(i=0;i<25;i++卡塔尔(قطر‎ //每循环三次明确数组中叁个数的职分
for(j=i+1;j<25;j++卡塔尔 //每回循环相比较三个数的轻重
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
b[i][j]=a[k++];
}
temp=b[4][4]; //显著5个地点的数值
b[4][4]=b[2][2];
b[2][2]=temp;
temp=b[0][1];
b[0][1]=b[0][4];
b[0][4]=temp;
temp=b[0][2];
b[0][2]=b[4][0];
b[4][0]=temp;
temp=b[0][3];
b[0][3]=b[4][4];
b[4][4]=temp;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
cout<<"t"<<b[i][j];
}
cout<<endl;
}
}

{

{  x=a;

第7章 构造体与共用体

char *p=&s[0];

   y=b;

/* 1、用指针和布局体达成一双向链表,并促成其对应的增、删、遍历作用,并在实例中运用它。*/
#include <iostream.h>

this->pstr = malloc + 1);

}

typedef struct node
{
int number;

strcpy(this->pstr, p);

void point::setpoint(float a,float b)

struct node *next;
struct node *parent;
}Node,*LinkNode;

this->length = strlen;

{  x=a; 

class LinkClass
{
public:
LinkNode first,current;//头指针,当前线指挥部针
void init(卡塔尔;//最早化函数
LinkNode Insert(int data,LinkNode curState of Qatar;//插入函数
void Remove(LinkNode p卡塔尔国;//删除函数
void Prior(LinkNode head卡塔尔国;//遍历函数
};

}

   y=b;

void LinkClass::init()
{
struct node *head=new struct node;
struct node *tail=new struct node;

//有警告

}

head->parent=NULL;
head->next=tail;
tail->parent=head;
tail->next=NULL;
first=current=head;
}

/*

ostream &operator<<(ostream &output,const point&p)

LinkNode LinkClass::Insert(int data,LinkNode cur)
{
struct node *newNode=new struct node;
newNode->number = data;
newNode->next = cur->next;
cur->next = newNode;
newNode->parent = cur;
newNode->next->parent = newNode;
cur=newNode;
return cur;
}

mystring::mystring

void LinkClass::Prior(LinkNode head)
{
LinkNode cur=head->next;
while(cur->next!=NULL)
{
cout<<cur->number<<" ";
cur=cur->next;
}
cout<<""<<endl;
}

{

    output<<"["<<p.get_x()<<","<<p.get_y()<<"]"<<endl; 

void LinkClass::Remove(LinkNode cur)
{
LinkNode temp=cur;
temp->parent->next=temp->next;
temp->next->parent=temp->parent;
delete(temp);
}

this->pstr = malloc + 1);

    return output;

void main()
{
LinkClass lc;
lc.init();
LinkNode cur=lc.current;
for(int i=0;i<=10;i++State of Qatar//用循环来最初化布局体内的number
{
cur=lc.Insert(i,curState of Qatar;//调用插入函数
}
LinkNode head=lc.first;
cout<<"没调用删除函数的遍历:"<<endl;
lc.Prior(head卡塔尔;//遍历函数
for(int j=0;j<=3;j++State of Qatar//删除元素6
{
cur=cur->parent;
}
lc.Remove(cur卡塔尔(قطر‎;//实践删除函数
cout<<"调用删除函数后的遍历:"<<endl;
lc.Prior(head);
}

strcpy(this->pstr, p);

}

/* 2、用指针和协会体落成栈及其方法,并在实例中使用它。*/
#include <stdlib.h>
#include <iostream.h>

this->length = strlen;

class circle:public point

#define STACK_INIT_SIZE 100
#define STACKIN 10

}

{ public: 

struct stack
{
int *top;
int *base;
int stacksize;
    int initstack(stack &s)
{
s.base=(int *)malloc(STACK_INIT_SIZE * sizeof(int));
if(!s.base) return 0;
s.top=s.base;
s.stacksize =STACK_INIT_SIZE;
return 1;

*/

circle(float a=0,float b=0,float r=0):point(a,b),radius(r){} 

}
int gettop(stack s,int &e)
{
if(s.top ==s.base) return 0;
e=*(s.top-1);
return 1;
}
int push(stack &s,int e)
{
if(s.top -s.base>=s.stacksize)
{
s.base=(int *)realloc(s.base,(s.stacksize + STACKIN)*sizeof(int));
if(!s.base) return 0;
s.top =s.base+s.stacksize;
s.stacksize +=STACKIN;
}
*s.top++=e;
return 1;
}
int pop(stack &s,int &e)
{
if (s.top ==s.base)return 0;
e=*--s.top;
return 1;
}
};

mystring& mystring:: operator =(mystring & s)

float area()const{return 3.14*radius*radius;}  

void main()
{
stack s;
int result,e=0;
result=s.initstack(s);
if(result==1)
cout<<"建栈成功!"<<endl;
else
cout<<"建栈失败!"<<endl;
for(int i=0;i<5;i++)
result=s.push(s,i);
if(result==1)
cout<<"开头化栈成功!"<<endl;
else
cout<<"最早化栈失利!"<<endl;
s.gettop(s,e);
cout<<"栈顶成分为:"<<e<<endl;
result=s.pop(s,e);
if(result==1)
cout<<"删除栈顶成分成功!"<<endl;
else
cout<<"删除栈顶成分退步!"<<endl;
s.pop(s,e);
cout<<"删除的栈顶成分为:"<<e<<endl;
}

{

float getradius()const{return radius;}

/* 3、编写一函数print,打字与印刷多少个上学的小孩子的成绩数组,该数组中有5个学子的多少记录,每一种记录包罗num、name、score[3],用主函数输入那几个记录,用print函数输出那个记录。
   4、在上题的幼功上,编写一个函数input,用来输入5个学子的数据记录。*/
#include <string.h>
#include <iostream.h>

delete[]pstr; this->pstr = new char[strlen + 1];

protected:  float radius; };

struct student
{
public:
int num;
char name[20];
int score[3];
};

if strcpy(this->pstr, s.pstr);

class cyl:public circle

class contral
{
public:
void print(struct student *p,int count);
int input(struct student *p,int count);
};

return *this;

{ public: 

void contral::print(struct student *p,int count)
{
for(int i=0;i<=count;i++)
{
cout<<"Num:"<<p->num<<endl;
cout<<"Name:"<<p->name<<endl;
cout<<"JAVA:"<<p->score[0]<<endl;
cout<<"C++:"<<p->score[1]<<endl;
cout<<"English:"<<p->score[2]<<endl;
p++;
}
}

}

cyl(float a=0,float b=0,float r=0,float h=0):circle(a,b,r),height(h){} 

int contral::input(struct student *p,int count)
{
while(true)
{
cout<<"请输入学号:"<<endl;
cin>>p->num;
cout<<"请输入姓名:"<<endl;
cin>>p->name;
cout<<"请输入JAVA分数:"<<endl;
cin>>p->score[0];
cout<<"请输入C++分数:"<<endl;
cin>>p->score[1];
cout<<"请输入English分数:"<<endl;
cin>>p->score[2];
p++;
count++;
cout<<"输入Q退出,输入任何继续"<<endl;
char str[8];
cin>>str;
if(strcmp(str, "q")==0)
{
break;
}
}
return count;
}

char& mystring::operator[]

float volume()const

void main()
{
int count=0;
contral con;
student NewS[100];
student *p=NewS;
count=con.input(p,count);
cout<<count<<endl;
con.print(p,count-1);
}

{

{return 3.14*radius*radius*height;}    

/* 5、将一个链表按逆序排列,就要链头当链尾,链尾当链头。*/
#include<iostream.h>

static char ch = 0;

friend ostream &operator<<(ostream &,  point &); 

struct num
{
int data;
struct num *next;
};

if (n > length || n < 0)

float getheight()const{return height;}

void fan(struct num  node1[])
{

return ch;

protected:  float height;

for(int i=9;i>=0;i--)
{
node1[i].next=&node1[i-1];
}
}

return *;

};

void main()
{
     struct num  node[10];
     for(int i=0;i<10;i++)
{
       node[i].data=i;
}
     for(int j=0;j<10;j++)
cout<<node[j].data;
     fan(node);
cout<<endl;
cout<<node[9].data;
     for(int k=9;k>0;k--)
cout<<(node[k].next)->data;
}
第8章   类和对象

}

ostream &operator<<(ostream &output,const  cyl&cy)

/*1、自定义三个字符串管理类CString,且能促成取子串、删除子串的功力。*/
#include<iostream.h>

int main()

{  output<<"["<<cy.get_x()<<","<<cy.get_y()<<"],"<<"h="<<cy.getheight()<<",volumn="<<cy.volume()<<endl;

#define MAX_LENGTH 100

{

return output;

class CString
{
public:
void cpystring(char *str);
void getSubstring(int beginStr,int endStr);
void delSubstring(int beginStr,int endStr);
void print();
private:
char cstr[MAX_LENGTH];
int length;
};

mystring a, b;

}

#include"CString.h"
#include"string.h"

//cin>>a>>b;

int main()

void CString::cpystring(char *str)
{
int i=0;
while(*str!='')
{
cstr[i++]=*str;
str++;
}
cstr[i]='';
}

cout << a << " " << b << endl;

void CString::getSubstring(int beginStr,int endStr)
{
int i,j=0;
char pstr[MAX_LENGTH];
if(beginStr<0||endStr>MAX_LENGTH||beginStr>endStr)
{
cout<<"error!"<<endl;
}
for(i=beginStr;i<endStr;i++,j++)
{
pstr[j]=cstr[i];
}
pstr[j]='';
cpystring(pstr);
}

a = b;

    point p(1,3); 

void CString::delSubstring(int beginStr,int endStr)
{
int i,j=0;
char pstr[MAX_LENGTH];
if(beginStr<0||endStr>MAX_LENGTH||beginStr>endStr)
{
cout<<"error!"<<endl;
}
for(i=0;i<beginStr;i++,j++)
{
pstr[j]=cstr[i];
}
for(i=endStr+1;i<strlen(cstr);i++,j++)
{
pstr[j]=cstr[i];
}
pstr[j]='';
cpystring(pstr);
}

cout << a << endl;

    circle c(1,2,3);    

void CString::print()
{
cout<<cstr<<endl;
}

a += b;

    cyl cy(1,2,3,4); 

#include<iostream.h>
#include"CString.h"

cout << a << endl;

    cout<<"x="<<p.get_x()<<",y="<<p.get_y()<<endl;

main ()
{
CString str1,str2,str3;
str1.cpystring("Just like before, it's yesterday once more!");//初始化str1
str2=str1;
str3=str1;
str2.getSubstring(5,9卡塔尔;//截取字符串
str3.delSubstring(10,16State of Qatar;//删除字符串
str1.print();
str2.print();
str3.print();
}

cin >> a;

    cout<<"area="<<c.area()<<endl;  cout<<"x="<<cy.get_x()<<",y="<<cy.get_y()<<",h="<<cy.getheight()<<",volume="<<cy. volume()<<endl;

/*2、定义三个循环队列类,且达成其有关的成员操作函数,并实例化调用之。*/
CircularQueue.h文件:
*********************************************************************
#define MAX_SIZE 101

cout << a << endl;

    return 0;

class CircularQueue
{
private:
int queue[MAX_SIZE];
int front;
int rear;

if

}

public:
CircularQueue();
virtual ~CircularQueue();
bool isEmpty();
bool isFull();
bool push(int);
int pop();
};
*********************************************************************

cout << "a字符串越来越大:" << a << endl;

(2)7

CircularQueue.cpp文件:
*********************************************************************
#include "CircularQueue.h"

else if

#include <iostream>

CircularQueue::CircularQueue()
{
front = 0;
rear = 0;
}

cout << "b字符串更加大:" << b << endl;

using namespace std;

CircularQueue::~CircularQueue()
{

else

class A

}

cout << "a,b相等 " << a << endl;

{public:

bool CircularQueue::isFull()
{
if ((rear+1)%MAX_SIZE == front)
{
return true;
}
else
{
return false;
}
}

if

A(){a=0;b=0;}

bool CircularQueue::isEmpty()
{
if (rear == front)
{
return true;
}
else
{
return false;
}
}

cout << "a,b不相等 " << endl;

A(int i){a=i;b=0;}

bool CircularQueue::push(int e)
{
if (isFull())
{
return false;
}

a[1] = 'g';

A(int i,int j){a=i;b=j;}

queue[rear] = e;
rear = ++rear % MAX_SIZE;
return true;
}

cout << a << endl;

void display(){cout<<"a="<<a<<"b="<<b;}

CircularQueue::pop()
{
if (isEmpty())
{
return 0;
}

return 0;

private:

int e = queue[front];
front = ++front % MAX_SIZE;
return e;
}
*********************************************************************

}

    int a;

main.cpp文件
*********************************************************************
#include "CircularQueue.h"
#include <iostream>

    int b;

using namespace std;

};

void main()
{
CircularQueue cQueue;
for (int i=0 ; i<75; i++)
{
cQueue.push(i);
}

class B : public A

for (i=0; i<50; i++)
{
cout << cQueue.pop() << " ";
}
cout << endl;

{public:

for (i=0; i<60; i++)
{
cQueue.push(i);
}

B(){c=0;}

for (i=0; i<85; i++)
{
cout << cQueue.pop() << " ";
}
cout << endl;
}
*********************************************************************
第9章   运算符重载

B(int i):A(i){c=0;}

/*1、定义多少个二维向量类Vector,并在那类中用成员函数情势重载一元运算符++(前、后缀)和二元运算符+。*/
class Vector
{
public:
Vector();
Vector(int x,int y);
operator ++();
Vector operator ++(int);
Vector operator +(const Vector &a);
void display();
private:
int x,y;
};

B(int i,int j):A(i,j){c=0;}

#include<iostream.h>
#include"Vector.h"

B(int i,int j,int k):A(i,j){c=k;}

Vector::Vector(){}
Vector::Vector(int x,int y)
{
this->x=x;
this->y=y;
}
Vector::operator ++() {++x;++y;}
Vector Vector::operator ++(int)
{
Vector s;
s.x=x++;
s.y=y++;
return s;
}
Vector Vector::operator +(const Vector &v)
{
Vector sum;
sum.x=x+v.x;
sum.y=y+v.y;
return sum;
}
void Vector::display()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}

void display1()

#include<iostream.h>
#include"Vector.h"

{display();

void main()
{
Vector v1(3,4),v2(1,2),v3;
cout<<"v1=";v1.display();
cout<<"v2=";v2.display();
++v1;
cout<<"++v1=";v1.display();
cout<<"v2++=";(v2++).display();
cout<<"v2=";v2.display();
v3=v1+v2;
cout<<"v1+v2=";v3.display();
}

cout<<"c="<<c<<endl;

/*2、将率先题中的相关重载项改为用友元函数的章程重载。*/
class Vector
{
public:
Vector();
Vector(int x,int y);
friend void operator ++(Vector &v);
friend void operator ++(Vector &v,int);
friend Vector operator +(const Vector &v1,const Vector &v2);
void display();
private:
int x,y;
};

}

#include<iostream.h>
#include"Vector.h"

private:

Vector::Vector(){}
Vector::Vector(int x,int y)
{
this->x=x;
this->y=y;
}
void operator ++(Vector &v) {++v.x;++v.y;}
Vector operator ++(Vector &v,int)
{
Vector before(v.x,v.y);
v.x++;
v.y++;
return before;
}
Vector operator +(const Vector &v1,const Vector &v2)
{
Vector sum;
sum.x=v1.x+v2.x;
sum.y=v1.y+v2.y;
return sum;
}
void Vector::display()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}

    int c;

#include<iostream.h>
#include"Vector.h"

};

void main()
{
Vector v1(3,4),v2(1,2),v3;
cout<<"v1=";v1.display();
cout<<"v2=";v2.display();
++v1;
cout<<"++v1=";v1.display();
cout<<"v2++=";(v2++).display();
cout<<"v2=";v2.display();
v3=v1+v2;
cout<<"v1+v2=";v3.display();
}

int main()

/*3、重载字符串处理类CString的“=”号和“+”号运算符。*/
class Cstring
{
public:
Cstring(char *pn);
~Cstring();
Cstring& operator=(Cstring &c);
Cstring& operator+(Cstring &c);
void display();
private:
char *p;
};

{

#include<iostream.h>
#include<string.h>
#include"Cstring.h"

    B b1;

Cstring::Cstring(char *pn)
{
p=new char[strlen(pn)+1];
strcpy(p,pn);
}

    B b2(1);

Cstring::~Cstring()
{
delete []p;
}

    B b3(1,3);

Cstring& Cstring::operator=(Cstring &s)
{
delete []p;
p=new char[strlen(s.p)+1];
strcpy(p,s.p);
return *this;
}

    B b4(1,3,5);

Cstring& Cstring::operator+(Cstring &s)
{
char *pp = new char[strlen(p)+strlen(s.p)+1];
strcpy(pp,p);
strcat(pp,s.p);
delete []p;
p = pp;
return *this;
}

    b1.display1();

void Cstring::display()
{
cout<<p<<endl;
}

    b1.display1();

#include<iostream.h>
#include <string.h>
#include"Cstring.h"

    b1.display1();

void main()
{
Cstring s1("first hello");
Cstring s2("second hello");
cout<<"赋值在此以前:"<<endl;
cout<<"s1=";
s1.display();
cout<<"s2=";
s2.display();
cout<<"赋值之后:"<<endl;
s1=s2;
cout<<"s1=";
s1.display();
cout<<"s2=";
s2.display();
cout<<"相加之后:"<<endl;
cout<<"s1+s2"<<endl;
(s1+s2).display();
}
第11章 世袭和派生类

    b1.display1();

/*1、利用虚基类,息灭“两性人”中的冗余数据:姓名、年龄,并编程完成之。*/
class Person 
{
public:
Person(char *name,int age);
Person();
virtual ~Person();

    return 0;

protected:
int age;
char name[20];
};

}

#include "Person.h"
#include <string.h>

(2)8

Person::Person(char *name, int age)
{
strcpy(this->name,name);
this->age = age;
}

#include<iostream>

class Man: virtual public Person
{
public:
Man(char *name,int age,char *sex);
Man();
virtual ~Man();

using namespace std;

protected:
char sex[8];
};

class A

#include "Man.h"
#include "Person.h"
#include <string.h>

{ public: 

Man::Man(char *name,int age,char *sex):Person(name,age)
{
strcpy(this->sex,sex);
}

A(){cout<<"constructing A"<<endl;} 

#include"Person.h"

~A(){cout<<"destrucing A"<<endl;}

class Woman: virtual public Person 
{
public:
Woman(char *name,int age,char *sex);
Woman();
virtual ~Woman();

}; 

protected:
char sex[8];
};

class B:public A 

#include "Woman.h"
#include "Person.h"
#include <string.h>

{  public:  

Woman::Woman(char *name,int age,char *sex):Person(name,age)
{
strcpy(this->sex,sex);
}

B(){cout<<"constructing B"<<endl;}  

class Transexual:public Man,public Woman
{
public:
print();
Transexual(char *name, int age, char *sex1,char *sex2);
Transexual();
virtual ~Transexual();

~B(){cout<<"destrucingB"<<endl;}

};

};

#include "Transexual.h"
#include "Man.h"
#include "Woman.h"
#include "Person.h"
#include <iostream.h>

class C:public B 

Transexual::Transexual(char *name, int age, char *sex1,char *sex2):Person(name,age),Man(name,age,sex1),Woman(name,age,sex2)
{

{  public:  

}

C(){cout<<"constructing C"<<endl;}  

Transexual::print()
{
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"性别:"<<Man::sex<<" "<<Woman::sex<<endl;
}

~C(){cout<<"destrucing C"<<endl;} 

/*2、通过Point类派生出Circle和Rectangle类,再通过Circle和Rectangle派生出“足体育场图形”类Football,并实例化调用Football这些类,且用cout打字与印刷追踪全数类的布局函数和析构函数的调用进度。(注:上面是这多少个类的图样描述,且须求各样类都重载布局函数,且都不用暗中同意构造函数)*/
#include<iostream.h>

}; 

class Point{
public:
Point(double x){
this->x=x;
cout<<"Point Constructor called"<<endl;
}
~Point(){
cout<<"Point Destructor called"<<endl;
}
protected:
double x;
};

int main() 

#include<iostream.h>
#define PI 3.14159265

{

class Circle:virtual public Point{
public:
Circle(double x):Point(x){
cout<<"Circle Constructor called"<<endl;
}
~Circle(){
cout<<"Circle Destructor called"<<endl;
}
void setCarea(){
carea=PI*x/2*x/2;
}
double getCarea(){
return carea;
}
protected:
double carea;

    C c1;  

};

    return 0; 

#include<iostream.h>

}

class Rectangle:virtual public Point{
public:
Rectangle(double x,double y):Point(x){
this->y=y;
cout<<"Rectangle Constructor called"<<endl;
}
~Rectangle(){
cout<<"Rectangle Destructor called"<<endl;
}
void setRarea(){
rarea=x*y;
}
double getRarea(){
return rarea;
}
protected:
double y,rarea;
};

(3) 1

#include<iostream.h>
#define PI 3.14159265

头文件1

class Football:public Circle,public Rectangle{
public:
Football(double x,double y):Point(x),Circle(x),Rectangle(x,y){
cout<<"Football Constructor called"<<endl;
}
~Football(){
cout<<"Football Destructor called"<<endl;
}
void setFarea(){
farea=x*y+PI*x/2*x/2;
}
double getFarea(){
return farea;
}
protected:
double farea;
};

class Point 

#include<iostream.h>
#include"Point.h"
#include"Circle.h"
#include"Rectangle.h"
#include"Football.h"

{ public: 

void main(){
Circle c(10);
Rectangle r(10,20);
Football f(10,20);
c.setCarea();
r.setRarea();
f.setFarea();
cout<<"Circle area:"<<c.getCarea()<<endl;
cout<<"Rectangle area:"<<r.getRarea()<<endl;
cout<<"Football area:"<<f.getFarea()<<endl;
}
第12章   模板

Point(float a=0,float b=0); 

/*1、利用C++的模板机拟订义单向队列类模板、链表类模板,并实例化应用之。*/
/*单向队列类模板*/
#define MAX_SIZE 50

void setPoint(float,float);

template<class T>
class Temqueue
{
private:
T queue[MAX_SIZE];
T front;
T rear;

float get_X()const{return x;} 

public:
Temqueue();
virtual ~Temqueue();
bool isEmpty();
bool isFull();
bool push(T);
T pop();
};

float get_Y()const{return y;} 

template<class T>
Temqueue<T>::Temqueue()
{
front = 0;
rear = 0;
}

friend ostream &operator<<(ostream &,const  point &);

template<class T>
Temqueue<T>::~Temqueue()
{

protected: 

}

float x,y;

template<class T>
bool Temqueue<T>::isFull()
{
if ((rear-MAX_SIZE) == front)
{
return true;
}
else
{
return false;
}
}

};

template<class T>
bool Temqueue<T>::isEmpty()
{
if (rear == front)
{
return true;
}
else
{
return false;
}
}

头文件2

template<class T>
bool Temqueue<T>::push(T e)
{
if (isFull())
{
return true;
}
queue[rear] = e;
rear = ++rear;
return true;
}

class Circle:public point

template<class T>
T Temqueue<T>::pop()
{
if (isEmpty())
{
return true;
}
T e = queue[front];
front = ++front;
return e;
}

{ public: 

#include "Temqueue.h"
#include <iostream>

Circle(float a=0,float b=0,float r=0); 

using namespace std;

void setradius(float); 

void main()
{
int k;
cout<<"请输入要给数列初阶化的长度,队列长度为50。"<<endl;
cin>>k;
Temqueue<int> cQueue;
for (int i=0 ; i<k; i++)
{
cQueue.push(i);
}
if(k>0)
{
for (i=0; i<k; i++)
{
if (i>=MAX_SIZE)
{
cout<<"队列已满!"<<endl;
break;
}
else
cout<<cQueue.pop()<< " ";
}
cout<<endl;
}
else
cout<<"队列为空!"<<endl;
}

float area()const; 

/*链表类模板*/
template<typename T> class List;     //对List类的注解
template<typename T> class Node{     //定义叁个陈诉节点的类
public:
Node();
friend class List<T>;
private:
T data;
Node<T> * next;
};

float getradius()const; 

template<typename T>Node<T>::Node(){
data=0;
next=NULL;
}

friend ostream &operator<<(ostream &,const  Circle &);

template<typename T>class List{                     //定义二个描述链表的类
public:
List(卡塔尔;                                         //空链表的协会(链表中只含表头结点)
~List(卡塔尔国;                                        //析构函数
void MakeEmpty(State of Qatar;                               //清空链表(删除链表中除表头结点以外的具备结点)
Node<T> * Find(T d卡塔尔;                            //查找数据域与d相近的结点(重返所找到结点的指针)
void PrintList(卡塔尔(قطر‎;                               //输出链表中各结点的数据域
void CreateList(卡塔尔国; //初步化链表
private:
Node<T> * head,* rear;
};

protected: 

template<typename T>List<T>::List(){
head=rear=new Node<T>;
}

float radius;

template<typename T>void List<T>::MakeEmpty(){
Node<T> *temp;
while(head->next!=NULL){
temp = head->next ;
    head->next = temp->next ;
   delete temp ;
}
rear = head;
}

 };

template<typename T>List<T>::~List(){
MakeEmpty();
delete head;
}

头文件3

template<typename T>void List<T>::PrintList(){
rear = head->next;
while(rear!=NULL){
cout<<rear->data<<endl ;
rear = rear->next ;
}
}

class Cyl:public circle

template<typename T>void List<T>::CreateList(){
T d;
cout<<"未来上马创办链表,请依次输入数据(以Ctrl+Z停止):"<<endl;
while(cout<<"请输入:" && cin>>d){
rear->next=new Node<T>;
   rear->next->data=d;
   rear=rear->next;
   rear->next=NULL;
}
}

{ public: 

#include <iostream>
#include "Node.h"
#include "List.h"
using namespace std;

Cyl(float a=0,float b=0,float r=0,float h=0); 

void main(){
List <int>l;
    l.CreateList();
l.PrintList();
cout<<"over!"<<endl;
}

void setHeight(float); 

/*2、定义三个类模板,然后生成其模板类并定义该类模板的派生类模板和派生类。*/
using namespace std;

float getHeight()const;    

template <class T>
class Te{
public:
T te;
};

float area()const; 

template <class T>
class Tte:public Te<T>{
public:
T tte;
};

float volume()const;    

class Fte:public Te<int>{
public:
int fte;
};

friend ostream &operator<<(ostream &,  const cyl &); 

#include<iostream>
#include"Te.h"
#include"Tte.h"
#include"Fte.h"

protected: 

void main(){
Te<int> te;
te.te=10;
cout<<"te="<<te.te<<endl;
Tte<int> itte;
itte.tte=20;
cout<<"itte="<<itte.tte<<endl;
Tte<double> dtte;
dtte.tte=3.14;
cout<<"dtte="<<dtte.tte<<endl;
Fte fte;
fte.fte=10;
cout<<"fte="<<fte.fte<<endl;
}
第13章 多态性与虚函数

float height;

/*1、利用虚函数,完成triangle(三角形),square(矩形),circle(圆)的面积总结函数show_area。*/
using namespace std;

};

class Shape{
public:
virtual double show_area()=0;
virtual char* shapeName()=0;
};

源文件1

class Circle:public Shape{
public:
Circle(double radius){
this->radius=radius;
}
virtual double show_area(){
return 3.14*radius*radius;
}
virtual char* shapeName(){
return "Circle: ";
}
protected:
double radius;
};

#include<iostream.h>

class Square:public Shape{
public:
Square(double length,double width){
this->length=length;
this->width=width;
}
virtual double show_area(){
return length*width;
}
virtual char* shapeName(){
return "Square: ";
}
protected:
double length,width;
};

#include"ex_1_1(head).h"

class Triangle:public Shape{
public:
Triangle(double length,double height){
this->length=length;
this->height=height;
}
virtual double show_area(){
return length*height/2;
}
virtual char* shapeName(){
return "Triangle: ";
}
protected:
double length,height;
};

Point::Point(float a,float b)

#include<iostream>
#include"Shape.h"
#include"Circle.h"
#include"Square.h"
#include"Triangle.h"

{x=a;  y=b; }

void main(){
Shape *ptr;
Circle circle(10);
Square square(10,15);
Triangle triangle(10,15);
ptr=&circle;
cout<<ptr->shapeName()<<ptr->show_area()<<endl;
ptr=□
cout<<ptr->shapeName()<<ptr->show_area()<<endl;
ptr=▵
cout<<ptr->shapeName()<<ptr->show_area()<<endl;
}

void Point::setPoint(float a,float b)

/*2、将圆类circle的计量面积和测算周长的效应分别抽象成class area和class perimeter,并在这里三个新类中运用纯虚函数效能达成面积和周长的统一调用接口展现函数Show。*/
using namespace std;

{  x=a;  y=b; }

class Circle{
public:
Circle(double radius){
this->radius=radius;
}
virtual double show()=0;
virtual char* shapeName()=0;
protected:
double radius;
};

float point::get_x()const{return x;}

class Area:public Circle{
public:
Area(double radius):Circle(radius){
}
virtual double show(){
return 3.14*radius*radius;
}
virtual char* shapeName(){
return "Circle Area: ";
}
};

float point::get_y()const{return y;}

class Perimeter:public Circle{
public:
Perimeter(double radius):Circle(radius){
}
virtual double show(){
return 2*3.14*radius;
}
virtual char* shapeName(){
return "Circle Perimeter: ";
}
};

ostream &operator<<(ostream &output,const Point&p) {  output<<"["<<p.x()<<","<<p.y()<<"]"<<endl;

#include<iostream>
#include"Circle.h"
#include"Area.h"
#include"Perimeter.h"

  return output;

void main(){
Circle *ptr;
Area area(10);
Perimeter perimeter(10);
ptr=&area;
cout<<ptr->shapeName()<<ptr->show()<<endl;
ptr=&perimeter;
cout<<ptr->shapeName()<<ptr->show()<<endl;
}
第14章  I/O流

}

1、创立叁个二进制文件,并对其开展种种操作。
struct i_Data //每种学员记录含准考证号、姓名、斯拉维尼亚语成绩共3个字段
{
char ID[30];
char name[30];
int score;
};

源文件2

#include "i_Data.h"
#define NUM 2

#include<iostream.h>

class Io{
public:
void input(){
fstream infile;
int i;
infile.open("score.dat",ios::out|ios::binary);
if(!infile){
cout<<"score.dat can't writed.n";
abort();
}
for(i=0;i<NUM;i++){
cout<<"请输入第"<<i+1<<"位考生消息:n";
cout<<"input 准考证号:";
cin>>data[i].ID;
cout<<"input 姓名:";
cin>>data[i].name;
cout<<"input 英文战绩:";
cin>>data[i].score;
cout<<endl;
infile.write((char *) &data[i], sizeof(data[i]));
}
infile.close();
system("cls");
}

#include"ex_1_1(head).h"

void output(){
fstream outfile;
int i;
outfile.open("score.dat",ios::in|ios::binary);
if(!outfile){
cout<<"score.dat can't opened.n";
abort();
}
for(i=0;i<NUM;i++){
outfile.read((char *) &data[i], sizeof(data[i]));
cout<<"第"<<i+1<<"位考生消息为:n";
cout<<"准考证号:"<<data[i].ID<<endl;
cout<<"姓名:"<<data[i].name<<endl;
cout<<"英语战表:"<<data[i].score<<endl;
cout<<endl;
}
outfile.close();
}
private:
struct i_Data data[NUM];
};

#include"ex_1_1(head1).h"

#include <iostream>
#include <fstream.h>
#include <stdlib.h>
#include "Io.h"
#define NUM 2

Circle::Circle(float a ,float b ,float r ):Point(a,b),radius(r){}

void main() {
Io io;
io.input(卡塔尔(قطر‎; //写入文件
io.output(卡塔尔(قطر‎; //读取文件
}
第15章   非凡管理

void Circle::setRadius(float r){radius=r;}

1、自个儿写贰个主次,在某种条件下抛出各连串型卓殊(如:整数、字符串、类对象、援用等),再捕捉这几个特别,并张开相关管理,保险本人的次第不被暂停,让它继续推行。
#include<iostream>
#include<string>
using namespace std;

float Circle::getRadius( )const{return radius;}

class String{
public:
String(char*, int);
class Range{ //异常类1
public:Range(int j):index(j){}
int index;
};
class Size{}; //异常类2
char& operator[](int k){
if(0<=k && k<len)
return p[k];
        throw Range(k);
}
private:char* p;
int len;
static int max;
};

float Circle::area()const{return 3.14*radius*radius;}

int String::max = 20;
String::String(char* str, int si){
if(si<0 || max<si)
   throw Size();
p=new char[si];
strncpy(p, str, si);
len=si;
}

ostream &operator<<(ostream  &output,const Circle &c)

void g(String& str){
int num=10;
for(int n=0; n<num; n++)
   cout <<str[n];
cout <<endl;
}

void f()
{
//代码区1
try{
//代码区2
String s("abcdefghijklmnop", 10);
g(s);
}
catch(String::Range r)
{
cerr <<"->out of range: " <<r.index <<endl;
//代码区3
}
catch(String::Size)
{
cerr <<"size illegal!n";
}
cout <<"The program will be continued here.nn";
//代码区4
}

output<<"Center ["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area()<<endl;  return output;

void main()
{
//代码区5
f();
cout <<"These code is not effected by probably exception in f().n";
}
/*小张同学见状您的一本Java书上写着“Object无处不在”,真的是如此吧?他一眼扫到写字台上一盏可调亮度的台灯。可以用Java表现台灯吗?他请您尝试看。基本需要:
1、编写台灯类,台灯具备开关状态和“规范”、“巩固”两档可调亮度;台灯具备开采、关闭效用,并能够安装亮度档位;
2、编写主类,展现台灯调控菜单,客户能够凭仗菜单选项决定不住调度一盏台灯的动静;
3、选取调控菜单的“退出”选项能够了结程序运行。*/
using namespace std;

class Light{
public:
Light();
void setStatus(bool status);
bool getStatus();
void setBright(int bright);
int getBright();
void setStyle(int style);
int getStyle();
int input();
void output();
private:
bool status;//表状态:开启也许关闭
int bright;//表亮度
int style;//表亮度类型:普通也许升高
};

源文件3

#include<iostream>
#include"Light.h"

#include<iostream.h>

void Light::setStatus(bool status){
this->status=status;
}

#include"ex_1_1(head).h"

Light::Light(){
status=false;
bright=3;
style=0;
}

#include"ex_1_1(head1).h"

bool Light::getStatus(){
return status;
}

#include"ex_1_1(head2).h"

void Light::setBright(int bright){
this->bright=bright;
}

Cylinder:: Cylinder (float a ,float b ,float r ,float h ):circle(a,b,r),height(h){}

int Light::getBright(){
return bright;
}

void Cylinder::setHeight(float h){height=h;}

void Light::setStyle(int style){
this->style=style;
}

float Cylinder:: getHeight()const{return height;}     

int Light::getStyle(){
return style;
}

float Cylinder::area()const{return 2*Circle::area()+2*3.14*radius*height;}

int Light::input(){
cout<<"请输入你要输入的值:";
int res=-1;//重回输入的值,
cin>>res;
if(cin.fail()){
cout<<"输入的数目超过范围!";
exit(0State of Qatar;//程序退出
}
return res;
}

float Cylinder::volume()const

void Light::output(){
cout<<"当前电灯系统状态为--"<<(this->getStatus(卡塔尔(قطر‎?"开启":"关闭"State of Qatar<<",亮度类型为--"
<<(this->getStyle(卡塔尔国==1?"加强":"规范"卡塔尔国<<",亮度档位为--"<<this->bright<<endl;
}

{return Circle::area()*height;}

#include<iostream>
#include"Light.h"

ostream &operator<<(ostream &output,const  Cyl&cy) {  output<<"Center="<<cy.x<<","<<cy.y<<"],"<<"h="<<cy.height<<"area="<<cy.area()<<",volumn="<<cy.volume()<<endl;

void main(){
Light light;
do{
cout<<"---------------<Control Menu>-----------------"<<endl;
cout<<"1-开灯,2-调整状态,3-调整亮度,4-关灯"<<endl;
cout<<"----------------------------------------------"<<endl;
int answer=light.input();
if(answer==1){
light.setStatus(true);
cout<<"系统启用中。。。"<<endl;
light.output();
}
else if(answer==2){
if(!light.getStatus()){
cout<<"警告!系统未有运营!操作失利!"<<endl;
continue;
}
do{
cout<<"请输入你要调整的情事(0-规范,1-加强卡塔尔:"<<endl;
int style=light.input();
if(style==0 || style==1){
cout<<"操作成功!"<<endl;
light.setStyle(style);
light.output();
break;
}
else{
cout<<"您输入的值超出了增选范围!请重新输入!"<<endl;
continue;
}
}while(true);
}
else if(answer==3){
if(!light.getStatus()){
cout<<"警告!系统并未有运行!操作失利!"<<endl;
continue;
}
do{
cout<<"请输入你要调整的亮度(1--5卡塔尔国:"<<endl;
int bright=light.input();;
if( bright>=0 &&  bright<=5){
cout<<"操作成功!"<<endl;
light.setBright(bright);
light.output();
break;
}
else{
cout<<"您输入的值超过了选择范围!请重新输入!"<<endl;
continue;
}
}while(true);
}
else if(answer==4){
if(!light.getStatus()){
cout<<"警告!系统并未有运转!操作失败!"<<endl;
continue;
}
cout<<"系统将在关张!"<<endl;
light.setStatus(false);
break;
}
else {
cout<<"您输入的值大于了选取范围!请重新输入!"<<endl;
}
}while(true);
}

 return output;

/*万年历*/
#include<iostream.h>
#include<stdlib.h>
#include<iomanip.h>
void main()
{
int year,month,startyear,today,day0,day1,k,j;
char a,b,c;
int M[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int M0[12]={ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    do{
do{
cout<<"请输入年份(壹玖捌柒-2020卡塔尔(قطر‎:";
cin>>year;
if(year<1990||year>2020){
cout<<"您输入的年份越界,是不是再一次输入(Y/N||y/nState of Qatar?";
cin>>a;
if(a=='Y'||a=='y'){
continue;}
else exit(0);
}
else{break;}
}while(true);
do{
cout<<"请输入月份(1-12卡塔尔:";
cin>>month;
if(month<1||month>12){
cout<<"您输入的月份越界,是还是不是再度输入(Y/N||y/nState of Qatar?";
cin>>b;
if(b=='Y'||b=='y'){
continue;}
else{exit(0);}
}
else{break;}
}while(true);
//总计有效天数
day0=0;
day1=0;
j=0;
startyear=1990;
    while (startyear != year)
{
     if ((startyear%4==0&&startyear%100!=0)||(startyear%400==0))
         day0 = day0 + 366;
     else
         day0 = day0 + 365;
     startyear++;
}
if((year%4==0&&year%100!=0)||(year%400==0)){
for (int i = 0; i < (month - 1); i++)
            day1 = day1 + M0[i];
            today = day0 + day1;
}
else{
for (int i = 0; i < (month - 1); i++)
            day1 = day1 + M[i];
            today = day0 + day1;
}
//输出部分

主函数

//闰年出口 
if((year%4==0&&year%100!=0)||(year%400==0)){
cout<<year<<"年"<<month<<"月份的日历如下"<<endl;
cout<<setw(30)<<endl;
cout << setw(8) << "Sun";
        cout << setw(8) << "Mon";
        cout << setw(8) << "Tue";
        cout << setw(8) << "Wed";
        cout << setw(8) << "Thu";
        cout << setw(8) << "Fri";
        cout << setw(8) << "Sat";
        cout << endl;
k=(today%7+1)%7;
for(int i=1; i<=k; i++)
cout<<setw(8)<<"";
for(int g=1; g<=(7-k); g++){
cout<<setw(8)<<g;
}
cout<<endl;
for(int h=8-k; h<=M0[month-1]; h++) {
cout<<setw(8)<<h;
j++;
if(j==7){
j=0;
cout<<endl;
}
}
}
//平年出口
else {
cout<<year<<"年"<<month<<"月份的日历如下"<<endl;
cout<<setw(30)<<""<<endl;
cout << setw(8) << "Sun";
        cout << setw(8) << "Mon";
        cout << setw(8) << "Tue";
        cout << setw(8) << "Wed";
        cout << setw(8) << "Thu";
        cout << setw(8) << "Fri";
        cout << setw(8) << "Sat";
        cout << endl;
k=(today%7+1)%7;
for(int i=1; i<=k; i++)
cout<<setw(8)<<"";
for(int g=1; g<=(7-k); g++){
cout<<setw(8)<<g;
}
cout<<endl;
for(int h=8-k; h<=M[month-1]; h++) {
cout<<setw(8)<<h;
j++;
if(j==7){
j=0;
cout<<endl;
}
}
}
cout<<endl;
cout<<"是或不是连续输入(Y/N||y/n卡塔尔?";
cin>>c;
if(c=='Y'||c=='y') continue;
else exit(0);
}while(true);
}

#include<iostream.h>

#include "ex_1_1(head).h"

#include "ex_1_1(head1).h"

#include "ex_1_1(head2).h"

int main()

{

    Cylinder cy1 (1,2,3,4)

        cout<<"noriginal cylinder:nx="<<cy1.getX()<<"y="<<cy1.getY()<",r="<<

            <<cy1.getRadius()<<",h="<<cy1.getHeight()<<"narea="<<cy1.area()<<",volume="<<cy1.volume()<<endl;

cy1.setHeught(15)

cy1.setRadius(7.5)

cy1.setPoint(5,5)

cout<<"npref cylinder:n"<<cy1;

Point &pRef=cy1;

cout<<"npRef as Point:"<<pRef;

Circle &Ref=cy1;

cout<<"npRef as Circle:"<<cRef;

return 0;

}

(3) 1

#include<iostream.h>

#include "ex_1_1(head).h"

#include "ex_1_1(head1).h"

#include "ex_1_1(head2).h"

int main()

{  point p(1,3); 

circle c(1,2,3);    

cyl cy(1,2,3,4); 

cout<<"x="<<p.get_x()<<",y="<<p.get_y()<<endl; 

cout<<"area="<<c.area()<<endl;  cout<<"x="<<cy.get_x()<<",y="<<cy.get_y()<<",h="<<cy.getheight()<<",area="<<cy.area()<<",volume="<<cy. volume()<<endl; 

return 0;

}

(3)4

#include<iostream>

using namespace std;

class shape

{ public: 

virtual float PrintArea()

const{return 0.0;}

}; 

//

class circle:public shape

{ public:

circle(float r=0); 

void setradius(float); 

float getradius( )const; 

virtual float  PrintArea()const;

protected:  float radius;

};

circle::circle(float r){radius=r;}

void circle::setradius(float r) {radius=r;}

float circle::getradius()const{return radius;}

float circle:: PrintArea() const {return 3.14*radius*radius;}  

int main()

{

    circle cir(5); 

    cir.PrintArea();  

    shape *pt;  pt=&cir ;     

    pt->PrintArea(); 

    cout<<pt->PrintArea()<<endl; 

    return 0;

}

(3)5

#include<iostream>

#include<cmath>

using namespace std;

class shape

{ public: 

virtual float PrintArea() const{return 0.0;}

};  

class circle:public shape { public:  circle(float r=0); 

virtual float  PrintArea()const;

protected:  float radius;

};

circle::circle(float r) {radius=r;}

float circle:: PrintArea()

const {return 3.14*radius*radius;} 

class  rectangle:public shape

{ public:  

rectangle(float a=0,float b=0);

virtual float  PrintArea()const;

protected:  float  x,y;

};

rectangle:: rectangle(float a,float b)

{ x=a;y=b;}

float  rectangle:: PrintArea() const {return  x*y;} 

class  triangle:public shape

{ public:  

triangle(float a=0,float b=0,float c=0); 

virtual float  PrintArea()const;

protected:  float  x,y, z ;

};

triangle::triangle (float a,float b,float c) 

{ x=a;y=b;z=c;}

float  triangle:: PrintArea() const {return   x*y*sqrt(1-(x*x+y*y-z*z)*(x*x+y*y-z*z)/4*x*x*y*y)/2;}  

int main()

{

    circle cir(5);    

    rectangle rec(1,2); 

    triangle  tri(3,4,5); 

    shape *pt[3]; 

    pt[0]=○  

    pt[1]=&rec; 

    pt[2]=&tri;    

    cout<<pt[0]->PrintArea()<<endl;    

    cout<<pt[1]->PrintArea()<<endl;    

    cout<<pt[2]->PrintArea()<<endl; 

    return 0;

}

测量试验数据与试验结果(能够抓图粘贴)

(1)                        

(2)7

(2)8

(3)1

  (3)4、 、

(3)5

结果深入分析与试验心得

这一次作业运用书上给的例题加以结合便可轻松的姣好,然而第二题小编做了相当短日子都并未有做上去,每一回改进之后都会遗留部分微细的不当,最终用尽各样手段,终于把她搞了出去,但是笔者要么有部分地点不精通,都以协和非常不足精心,非常不足认真才坐不上来,未来要勤加学习,把不掌握的搞懂。

 

本文由10bet手机官网发布于高并发,转载请注明出处:运算符重载字符串类,经典例题

上一篇:关于常量成员函数,const用法小结 下一篇:构建一个datatable,通过datatable获得表的主键
猜你喜欢
热门排行
精彩图文