文档库 最新最全的文档下载
当前位置:文档库 › c++教材练习题参考答案(机械工业出版社)

c++教材练习题参考答案(机械工业出版社)

教材练习题参考答案
第一章
1、
#include
void main()
{
cout<<"**************************\n";
cout<<" 江苏科技大学欢迎您! \n";
cout<<"**************************\n";
}

2、
(1)#include
void main()
{
int a=4,b=5,c=6,z;
z=a+b>c&&b==c||++b>++a ;


cout<<" 表达式的值为: "<cout<<" a="<cout<<" a="<cout<<" a="<cout<<" a="<cin>>a;
cout<cout<}

4、
#include
void main()
{
int a,b,c,v,aver;
cout<< " 输入三个数: "<cin>>a>>b>>c;
v=a*a+b*b+c*c;
aver=(a+b+c)/3;
cout<<" 三个数的平方和是: "<cout<<" 三个数的平均值是: "<
}


第二章
1、
#include
void main()
{
int a,b,c,t;
cout<< " 输入三个数: "<cin>>a>>b>>c;

if(a>b){t=a;a=b;b=t;}
if(a>c){t=a;a=c;c=t;}
if(b>c){t=b;b=c;c=t;}

cout<
}

2、
#include
void main()
{
float x,sum=0.0;
int i=1;
while(i<=10)
{
cin>>x;
sum=sum+x;
i=i+1;
}

cout<<"平均值是: "<}

3、
#include
void main()
{
double y=0.0,t=1.0;
int n;
cin>>n;

for(int i=1;i<=n;i++)
{ t=t*i;
y=y+t;
}


cout<<" n= "<cin>>a;
n=a;
do
{
sum+=n%10;
n/=10;
c++;

}while(n);

cout<cout<
}
6、
#include
void main()
{ int a,b,i;

for(i=1;i<=999;i++)
{
a=i%10;
b=i%100;

if(

i==a*a || i==b*b)
cout<}

}



7、
#include
#include
void main()
{ double a,b,n;
for(n=10000;n<=99999;n++)
{
a=n*10+6;
b=6*100000+n;
if(fabs(b-4*a)<1e-5) { cout<}


}






第三章

//1. 一维数组的输入和输出
#include
void main()
{
int a[10],k=1;
for(int i=0;i<10;i++)
{
cout<<"请输入第"<cin>>a[i];
}
cout<<"数组a为:\n";
for(i=0,k=0;i<10;i++)
{
k++;
cout<if(k%5==0)cout<<'\n';
}
cout<<'\n';
}

//2. 通过指针输出一维数组中值为奇数元素
#include
void main()
{
int a[10]={3,4,5,8,9,11,14,16,19,21},*p=a,k=0;
cout<<"数组a中的奇数有:\n";
for(int i=0;i<10;i++)
if(p[i]%2)
{
k++;
cout<<*(p+i)<<'\t';
if(k%5==0)cout<<'\n';
}
cout<<"\n";
}

//3. 产生一个随机的二维数组b[4][5],并按矩阵的方式输出
#include
#include
void main()
{
int b[4][5];
cout<<"产生的数组为:\n";
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
b[i][j]=rand();
cout<}
cout<<'\n';
}
cout<}

//4. 二维数组的平方赋值
#include
void main()
{
int a[3][4]={3,1,5,2,4,1,7,5,3,6,9,8},b[3][4];
cout<<"数组a为:\n";
for(int i=0;i<3;i++){
for(int j=0;j<4;j++)
{
cout<b[i][j]=a[i][j]*a[i][j];
}
cout<}
cout<cout<<"数组b为:\n";
for(i=0;i<3;i++){
for(int j=0;j<4;j++)
cout<cout<}
cout<}

//5. 小写字母改写为大写字母
#include
void main()
{
char str[100];
cout<<"请输入一个含空格字符的字符串。\n";
cin.getline(str,100);
for(int i=0;str[i];i++)
if(str[i]>='a'&&str[i]<='z')
str[i]-='a'-'A';
cout<<"转换后的字符串为:\n";
cout<}

//6. 通过指针将字符串逆序
#include
#include
void main()
{
char str[]="abc321",*s1=str,*s2,ch;
cout<<"原字符串为:\n"<s2=str; //A
while(*s2)s2++;
s2--; //B
while(s1ch=*s1;
*s1=*s2;
*s2=ch;
s1++;
s2--;
}
cout<<"逆序后的字符串为:\n"<}
/*其中A到B行可改为:
s2=str+strlen(str)-1;
*/


第四章习题答案
4-1
#include
void swap(int *p1,int *p2)
{
int t;
t=*p1;*p1=*p2;*p2=t;
}

void main()
{
int a,b,c;
int *s1,*s2,*s3;
cout<<"输入三个数a,b,c:";
cin>>a>>b>>c;
s1=&a;
s2=&b;
s3=&c;
if(a>b)swap(s1,s2);
if(a>c)swap(s1,s3);
if(b>c)swap(s2,s3);
cout<<"输出排好序的三个数:";
cout<}
4-2
#include
void trans(int,int);
void main()
{
int n,sel;
cout<<"输入一个整数:";
cin>>n;
do
{
cout<<"\n输出进制:2,8,16,32(输入0时退出):";
cin>>s

el;
switch (sel)
{
case 2:trans(n,2);break;
case 8:trans(n,8);break;
case 16:trans(n,16);break;
case 32:trans(n,32);break;
case 0:break;
default:cout<<"进制输入错误!"<}
}while(sel!=0);
}
void trans(int n,int base)
{
int c;
if(n==0)return;
c=n%base;
trans(n/base,base);
if(c<=9)
cout<else
cout<
}
4-3
#include
#include
#include
void fun(int b[],int n)
{
for(int i=0;ifor(int j=i+1;j{
if(b[i]!=0&&b[j]!=0)
if(b[j]%b[i]==0)
b[j]=0;
}
}
void main()
{
int a[210]={0},n=0,k=5;
for(int i=0;i<48;i++) //给数组赋初值
a[i]=k,k+=2;

fun(a,48);

for(i=0;i<49;i++)
{
if(a[i]!=0)
{
cout<n++;
if(n%5==0)
cout<}

}
cout<}

4-4
#include
int gcd(int,int);
int lcm(int,int);
void main()
{
int num1,num2,den1,den2,a,l,g;
cin>>num1>>den1>>num2>>den2;
cout<cout<<"求分母的最小公倍数: ";
l=lcm(den1,den2);
cout<cout<<"两分数的和为:";
a=(l/den1)*num1+(l/den2)*num2;
cout<cout<<"求分子分母的最大公约数:";
g=gcd(a,l);
cout<cout<<"输出最简分数:";
cout<
}
int gcd(int a,int b)
{
int i;
for(i=a;i>=1;i--)
if(!((a%i)||(b%i)))
break;
return (i);
}
int lcm(int a,int b)
{
int i;
i=b;
while(i%a)i+=b;
return(i);
}

4-5
#include
#include
double sin(double x,double eps)
{
double term,sum,y;
int n=1; //n=0
y=x*x;
term=x;
sum=0;
while(fabs(term)>=eps)
{
sum+=term;
n++;
term=term*y/((2*n-2)*(2*n-1)); //((2*n)*(2*n-1))
term*=-1;

}
return sum;
}
void main()
{
double x,y;
cout<<"输入x的值(角度):";
cin>>x;
while(x>360)x-=360;
y=3.1415926*x/180;
cout<<"度数为:"<}

4-6
#include
#include
float solut(float a,float b,float c,float d)
{
float x=1,x0,f,f1;
do{
x0=x;
f=((a*x0+b)*x0+c)*x0+d;
f1=(3*a*x0+2*b)*x0+c; //对f函数的导数
x=x0-f/f1;
}while(fabs(x-x0)>=1e-3);
return x;
}
void main()
{
float a,b,c,d;
cout<<"input a,b,c,d:";
cin>>a>>b>>c>>d;
cout<cout<<"x="<}

4-7
#include
#include

prime(int n)
{ int i=2;
int flag=1;
for(;iif (n%i==0)
flag=0;
return(flag);
}

void main()
{
int m,j,k,n=0;
for(m=6;m<=100;m+=2)
{for(j=3;j<=m/2;j+=2)
if (prime(j))
{ k=m-j;
if (prime(k))
{
cout<if (++n%5==0) cout<if(m==j+

k)break;
} }

}
cout<}

4-8
#include
void f1(int n)
{
int j=n%10;
if(n/10)f1(n/10);
cout<}

void f2(int n)
{
cout<if(n/10)f2(n/10);
}
void main()
{
int x;
cout<<"输入一个整数:";
cin>>x;
cout<<"输入的整数为:"<cout<<"该整数的反序输出为:";
f2(x);
cout<<'\n';
cout<<"该整数的正序输出为:";
f1(x);
cout<<'\n';
}



第五章 习题参考解答
1.
1 3 1
2 3 1
1 3 2
2 3 2
1 3 3
2 3 3
1 2 3

2.
正确程序之一为:
#include
#define SQR(x) ((x)*(x))
void main(){
int counter;
for(counter=0;counter<5;++counter){
cout<<"x="<<(counter+1)<<" "<<"x的平方为"<}
}

3.
#define true 1
#define false 0
#define ok 2
#define error –1
#define overflow -2

4.
#define IFF(x) if(x%10==0)return true;else retrue false;

5.
#define EXCHANGE(x,y) x=x+y;y=x-y;x=x-y;



第六章
1.[程序如下]
#include
struct student
{
float CPPscore;
float eng;
};
void main()
{
student s1; float sum;
cin>>s1.CPPscore>>s1.eng;
sum=s1.CPPscore+s1.eng;
cout<}

2.[程序如下]
# include
# include
struct score
{
char name[8] ;
int no ;
float c, english, maths, average ;
} ;
score input(score st) ;
float average( float a, float b, float c ) ;
void sort(score st[], int n) ;
void print (score st[], int n) ;

void main()
{
score student[30] ;
for (int i=0 ; i<5 ; i++)
student[i]=input(student[i]) ;
sort(student, 5) ;
print(student, 5) ;
}
score input(score st)
{
cout<<"请输入学生姓名:" ; cin>>https://www.wendangku.net/doc/ba9570652.html, ;
cout<<"请输入学生学号:" ; cin>>st.no ;
cout<<"请输入C++成绩:" ; cin>>st.c ;
cout<<"请输入英语成绩:" ; cin>>st.english ;
cout<<"请输入数学成绩:" ; cin>>st.maths ;
cout<st.average=average(st.c, st.english, st.maths) ;
return st ;
}

float average( float a, float b, float c )
{ return (a+b+c)/3 ; }

void sort(score st[], int n)
{ score temp ;
for ( int i=0 ; ifor ( int j=i ; jif (st[i].average < st[j].average )
{ temp=st[i] ; st[i]=st[j] ; st[j]=temp ; }
}

void print (score st[], int n)
{
cout<<"No. "<<" 姓名 "<<" 平均 "<<" C++ "<<" 英语 "<<" 数学 "<for ( int i=0 ; i{
cout<cout<<'\t'<cout<<'\t'<cout<<'\t'<cout<<'\t'<cout<<'\t'<}
}

3.[程序如下]
#include
struct student
{
long int num;
float CPPmid;
float CPPend;
float ave;
};
void main()
{student s[3];
student *p;
for(p=s;p{

cout<<"请输入学生学号:" ; cin>>p->num;
cout<<"请输入C++期中成绩:" ; cin>>p->CPPmid;
cout<<"请输入C++期末成绩:" ; cin>>p->CPPend;
p->ave=(p->CPPmid+p->CPPend)/2;
}
cout<<"No. "<<" 平均 "<<" C++期中 "<<" C++期末 "<for(p=s;p{
cout<num ;
cout<<'\t'<ave;
cout<<'\t'<CPPmid ;
cout<<'\t'<CPPend;
cout<}
}

4.[程序如下]
struct link
{
int data;
link*next;
};
int sum(link *head)
{
link *p;
int s=0;
p=head->next;
while(p)
{
s+=p->data;
p=p->next;
return s;
}
}

5.[程序如下]
#include
struct node
{
int data;
node *next;
};

node *create( )
{
node *head ; //头指针
node *p , *pend ;
int a;
cout<<"请输入数据(为零表示结束输入):" ;
cin>>a;
head=0 ;
while(a!=0)
{
p=new node ;
p->data=a ;
if(head==0)
{
head=p ;
pend=p ;
}
else {
pend->next=p ;
pend=p ;
}
cout<<"请输入数据(为零表示结束输入):" ;
cin>>a;

}
if(head) pend->next=0 ;
return head ;
}

node * invert(node *head)
{
node *p,*q;
p=head->next;
if(p!=NULL)
{
head->next=NULL;
do
{
q=p->next;
p->next=head;
head=p;
p=q;;
}while(p!=NULL);
}
return head;
}

void print(node *head )
{
if(head==0) { cout<<" 链表为空!\n" ; return ; }
node *p=head ;
cout<<"链表上各个结点的值为:\n";
while(p!=0)
{
cout < data<<'\t' ;
p=p->next ;
}
}

void release(node *head )
{
if(head==0) { cout<<" 链表为空!\n" ; return ; }
node *p ;
while( head )
{
p=head;
head=head->next ;
delete p;
}
cout<<"\n结点空间释放完毕!";
}

void main( )
{
node *head;
head=create( );
print(head);
head=invert(head);
cout<<"\n逆置后";
print(head);
release(head);
}

6.[程序如下]
#include
struct Node
{
int x; //围成一圈时,人的序号
Node *next;
};
Node * DelNode(Node *head, int m) //依次输出环形链表中凡报到m者的序号
{ Node *p;
int count;
if(head==NULL) return head;;
while( head != head->next) //直到链表上只有一个结点为止
{
count=0;
while(count < m-2)
{
count++;
head = head->next;
}
p=head->next; //删除p所指向的结点
head->next = p->next;
head = head->next ;
cout<x<delete p;
}
return head;
}
void main(void)
//在主函数中,构造环形链表,调用DelNode函数依次输出报到m的人的序号
{ Node *head, *p; //最后输出留在圈中最后的人的序号
int i;
head = new Node;
head->x = 1;
head->next = NULL;
p=head;
for(i=2; i<=10; i++)
{
p->next = new Node; //新结点加入链尾
p =p->next;
p->x = i;
}
p->next =head;

// 构成循环链
head = DelNode(head, 5);
cout << "最后的一个人为: " << head->x << endl;
}

7.[程序如下]
node *Merge(node *h1, node *h2)
{
node *newHead,*p1,*p2;
if(h1->datadata)
{
newHead=h1;
p=h2;
}
else
{ newHead=h2;
p=h1;
}
node *q= newHead
while(q->next)
{
if(q->next->datadata)
q=q->next;
else
{
p1=q->next;
q->next=p;
q=p;
p=p1;
}
}
q->next=p;
return head;
}

8.[程序如下]
#include
struct Node
{
char c;
int count;
Node * next;
};

Node *search(Node *head, char ch)
{
Node *p;
p=head;
while(p)
{
if(p->c==ch) { p->count++; break; }
p=p->next;
}
if(p==0 )
{
p=new Node;
p->c=ch;
p->count=1;
if(head) p->next=head;
else p->next=0;
head=p;
}
return head;
}

void print(Node *head)
{ while(head)
{
cout<<"字符:"<< head->c<<" \t出现"<< head->count<<"次"<head=head->next;
}
}

void dele(Node *head)
{ Node *p;
while(head)
{
p=head;
head=head->next;
delete p;
}
}

void main(void)
{
Node *h=0;
char s[300],*p=s;
char c;
cout<<"输入一行字符串:";
cin.getline(s,300);
while( c=*p++ )
h = search(h,c);
print(h);
dele(h);
}


第七章
1.
#include
class C{
int m,n;
int c;
public:
C(int x,int y);
int f(int n);
void fun();
void print();
};
C::C(int x,int y){
n=m=x,n=y;
}
int C::f(int n){
int s=1;
for(int i=n;i>=1;i--)
s*=i;
return s;
}
void C::fun(){
c=f(m)/(f(n)*f(m-n));
}
void C::print(){
cout<<"m为:"<cout<<"组合数为:";
cout<}

void main(){
cout<<"请输入两个整整:";
int m,n;
cin>>m>>n;
if(m>n){
C a(m,n);
a.fun();
a.print();
}
else cout<<"输入数据有误!";
}

2.
#include
class Num
{
int a[5];
int n;
public:
Num(int[5]);
void fun();
void show();
};
Num::Num(int b[5]){
for(int i=0;i<5;i++)a[i]=b[i];
n=0;
}
void Num::fun(){
for(int i=0;i<5;i++)
n=n*10+a[i];
}
void Num::show(){
for(int i=0;i<5;i++)cout<cout<<'\n';
cout<}
void main(){
int b[5];
cout<<"请输入5个数字:";
for(int i=0;i<5;i++)
cin>>b[i];
Num m(b);
m.fun();
m.show();
}

3.
#include
#include
class String
{
char a[100],b[100];
int n;
public:
String(char c[],char d[],int m);//构造函数,以参数初始化数据成员;
void fun();//功能函数,将b插入a中第n个字符处;
void show ();//功能函数,输出数据成员。
};
String::String(char c[],char d[],int m){
int s=strlen(c),k=strlen(d),i;
n=m;
for(i=0;i<=s;i++)a[i]=c[i];
for(i=0;i<=k;i++)b[i]=d[i];
}
void String::fun(){
int s=strlen(a),k=strlen(b),i,j;
for(i=s;i>=n-1;i--)
a[i+k]=a[i];
for(i=n-1,j=0;ia[i]=b[j];
}
void String::show(){


cout<cout<}
void main(){
char c[100],d[100];
cout<<"请输入两个字符串:";
cin.getline(c,100);
cin.getline(d,100);
cout<<"请输入插入位置:";
int n;
cin>>n;
String aa(c,d,n);
aa.show();
aa.fun();
aa.show();
}

4.
#include
class An
{
float a0,d; //分别为首项和公差
int n; //项数
float *pa,s; //分别存放数列前n项及前n项的和
public:
An(float b0,float c,int m);//构造函数:根据给定的数据初始化成员数据;
void setdata( ); //求前n项及前n项的和;
void show(); //输出所有数据成员;
~An(); //析构函数。
};
An::An(float b0,float c,int m){
a0=b0;
d=c;
n=m;
pa=new float[n+1];
s=0;
}
void An::setdata(){
pa[0]=a0;s=pa[0];
for(int i=1;ipa[i]=pa[i-1]+d;
s+=pa[i];
}
}
void An::show(){
float *p=pa;
for(int i=0;i}
An::~An(){
if(pa)delete[]pa;
}
void main(){
float b0,c;
int m;
cout<<"请输入数列的首项与公差:";
cin>>b0>>c;
cout<<"请输入项数:";
cin>>m;
An aa(b0,c,m);
aa.setdata();
aa.show();
}

5.#include
#include
class String
{
char *str; //存放含数字字符的字符串
int len; //成员字符串的长度
public:
String(char* ptr); //构造函数,根据给定的字符串初始化成员数据;
char getc(int n);//根据给定的序号取成员字符串中的第n个字符;
void setc(int n,char c);//根据给定的序号把成员字符串中的第n个字符修改为c
void setlen();//求成员字符串的长度
void show();//输出数据成员。
~String();
};
String::String(char* ptr){
str=new char[strlen(ptr)+1];
strcpy(str,ptr);
len=strlen(ptr);
}
char String::getc(int n){
return str[n-1];
}
void String::setc(int n,char c){
str[n-1]=c;
}
void String::setlen(){
len=strlen(str);
}
void String::show(){
cout<}
String::~String(){
if(str)delete[]str;
}
void main(){
cout<<"请输入一个字符串:";
char ptr[100];
cin.getline(ptr,100);
String ss(ptr);
ss.show();
cout<<"请输入取第几个字符:";
int n;
cin>>n;
ss.getc(n);
cout<<"请输入替换成什么字符:";
char c;
cin>>c;
ss.setc(n,c);
ss.show();
}


6.#include
class Array
{
int (*a)[5]; // 表示二维数组
float ave; // 二维数组的平均值
public:
Array(int b[][5],int n); //构造函数,用n行5列的数组初始化成员数据;
void f(); //功能函数,求二维数组的平均值;
void fun(); //功能函数,根据题目要求调整二维数组;
void show(); //功能函数,输出成员数组;
~Array();
};
Array::Array(int b[][5],int n){
int i,j;
a=new int[n][5];
for(i=0;ifor(j=0;j<5;j++)
a[i][j]=b[i][j];
}
ave=0;
}
void Array

::f(){
int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
ave+=a[i][j];
ave/=(5*5);
}

void Array::fun(){
int k;
int *p=*a,*q=*a+24;
while(pwhile(*pwhile(*q>ave)q--;
if(pk=*p;
*p=*q;
*q=k;
}
}
}
void Array::show(){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<5;j++)
cout<cout<<'\n';
}
cout<}
Array::~Array(){
}
void main(){
cout<<"请输入5行5列矩阵:";
int b[5][5],i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)cin>>b[i][j];
Array aa(b,5);
aa.f();
aa.show();
cout<<'\n';
aa.fun();
aa.show();
}


第八章
1.当用派生类定义对象时,派生类的构造函数除了对新定义的数据成员初始化外,还必须调用基类的构造函数对从基类继承的数据成员进行初始化。初始化的方法是在派生类的构造函数的初始化成员列表中给出对基类的构造函数的调用。
派生类中有对象成员时,在派生类的构造函数的初始化成员列表中给出对象成员名对其初始化。即:
派生类构造函数名(形参表列):基类名(实参表列1),对象成员名(实参表列2)
{
派生类中数据成员初始化
}
若基类中的构造函数是缺省的构造函数,则在定义派生类的过程中,可以不在派生类的构造函数的初始化成员列表中给出对基类的构造函数的调用。

2.#include
#include
class point
{ float x,y;
public:
void setx (float xx) {x=xx;}
void sety (float yy) {y=yy;}
protected:
float getx(){return x;}
float gety(){return y;}
};
class line:public point
{ public:
float x1,y1;
void len();
};
void line::len()
{ double s;
s=sqrt(pow(getx()-x1,2)+pow(gety()-y1,2));
cout<<"两点间线段的长度为:"<}
void main( )
{ float a1,b1,a2,b2;
cout<<"(a1,b1)=\n";
cin>>a1>>b1;
cout<<"(a2,b2)=\n";
cin>>a2>>b2;
line line1;
line1.setx(a1);
line1.sety(b1);
line1.x1=a2;
line1.y1=b2;
line1.len( );
}

3. 程序运行结果如下:
1 inside a
2 inside b
3 inside c
10 inside A::I

4. 若虚基类有缺省的构造函数,则在派生的每一个派生类的构造函数的初始化成员列表中不必都列出对虚基类构造函数的调用,系统在派生类的构造函数中自动调用虚基类缺省构造函数。
若虚基类中没有缺省的构造函数,则在派生类的构造函数的初始化成员列表中都必须列出对虚基类构造函数的调用。
把基类A定义成虚基类,类B和类C继承了类A,类D继承了类B和类C,当定义派生类D的对象时,虚基类A的构造函数仅调用一次,由派生类D的构造函数来调用虚基类的构造函数。

5. 静态数据成员是在类定义中用关键字static修饰的数据成员。与一般数据成员不同的是必须在类体中对静态数据成员作引用性说明,同时在类体外对其作定义性说

明,只有对其作定义说明时系统才为其分配存储空间。一个类的静态数据成员被它的多个对象共享。对类的静态成员操作时用类名代替对象名,使用格式为“类名::静态成员名”。
与静态数据成员一样,可通过类名加上作用域运算符直接调用静态成员函数。

6. #include
const float PI=3.1415 ;
class A
{
float r,h;
public:
A(float a,float b){r=a;h=b;}
float Getr()
{ return r; }
float Geth()
{ return h; }
friend float V1(A &);
};
float V1(A &a)
{ return PI*a.r*a.r*a.h; }
double V2(double r,double h)
{ return PI*r*r*h;}
void main()
{
A a1(25,40);
cout<<"用友元函数求得体积为:"<cout<<"用成员函数求得体积为:"<cout<<"用普通函数求得体积为:"<
}

7. 程序运行结果如下:
class A
class B
class C
class D




第九章
//1.求表面积和体积
#include
#define PAI 3.1415927
class shape{
protected:
float x,y,are,vol;
public:
shape(float a=0,float b=0)
{ x=a; y=b;are=0;vol=0; }
virtual void print()=0;
virtual void disp()=0;
};
class square:public shape{
public:
square(float a):shape(a){}
void disp()
{ are=6*x*x; vol=x*x*x; }
void print()
{ cout<<"S_ARE="<};
class circle:public shape{
public:
circle(float a):shape(a){}
void disp()
{ are=float(PAI*x*x*4/3); vol=float(PAI*x*x*x); }
void print()
{ cout<<"C_ARE="<};
class yz:public shape{
public:
yz(float a,float b):shape(a,b){}
void disp()
{
are=float(PAI*x*x*2+2*PAI*x*y);
vol=float(PAI*x*x*y);
}
void print()
{ cout<<"Y_ARE="<};
void main()
{
shape *ps;
square sq(2); circle ci(3); yz y(2,3);
ps=&sq; ps->disp(); ps->print();
ps=&ci; ps->disp(); ps->print();
ps=&y; ps->disp(); ps->print();
}

//2.点到坐标原点的距离
#include
#include
class Point{
float x,y;
public:
Point(float a,float b)
{
x=a;
y=b;
}
operator double()
{
return sqrt(x*x+y*y);
}
void print()
{
cout<<"点("<}
};
void main()
{
float a,b;
cin>>a>>b;
Point p(3,4);
p.print();
double d=p;
cout<<"到原点的距离为:"<}

//3.动态数组的自增和自减
#include
class Array{
int *p;
int n;
public:
Array() { p=0;n=0; }
Array(Array &t)
{ n=t.n; p=new int[n];
for(int i=0;i}
Array(int pp[],int m)
{ n=m; p=new int[n];
for(int i=0;i}
Array & operator=(Array &t)
{ n=t.n;
if(p)delete []p;
if(t.p){
p=new int [n];
for(int i=0;i}
else p=0;
return *this;
}
void print()
{ for(int i=0;icout<}
Array operator++(

)
{ for(int i=0;ireturn *this;
}
Array operator++(int)
{ Array ar=*this; //A
++(*this);
return ar;
}
friend Array operator--(Array &t)
{ for(int i=0;ireturn t;
}
friend Array operator--(Array &t,int)
{ Array ar=t;
--t; //for(int i=0;ireturn ar;
}
~Array() { if(p)delete []p; }
};
void main()
{
int a[]={1,2,3,4,5,6,7};
Array arr(a,7),ar;
arr.print();
ar=++arr; ar.print(); arr.print();
ar=arr++; ar.print(); arr.print();
ar=--arr; ar.print(); arr.print();
ar=arr--; ar.print(); arr.print();
}
注:由于程序中使用了拷贝功能的构造函数(如A行),无参的对象,以及对象之间的赋值,且对象使用了动态内存,故必须显式定义拷贝构造函数Array(Array &t).无参的构造函数Array(),并重载赋值运算符Array & operator=(Array &t),以避免在释放动态内存时出错。

//4. 重载运算符“^”求元素的n次方
#include
class Array{
int ar[3];
public:
Array(){}
Array(int *aa)
{
for(int i=0;i<3;i++)
ar[i]=aa[i];
}
void show( )
{
for(int i=0;i<3;i++)
cout<cout<<'\n';
}
Array operator ^(Array);
} ;
Array Array::operator ^(Array arr)
{
int t;
for(int i=0;i<3;i++)
{
t=ar[i];
if(arr.ar[i]==0)ar[i]=1;
else for(int j=1;jar[i]*=t;
}
return *this;
}
void main( )
{
int a[3]={2,2,2},b[3]={3,3,3};
Array ar1(a),ar2(b),ar3;
ar1.show(); ar2.show();
ar3=ar1^ar2; ar3.show();
}

//5. 重载运算符“!”,实现字符串逆序赋值
#include
#include
class String{
char *s;
public:
String(char *str=0)
{
if(str==0)s=0;
else{
s=new char[strlen(str)+1];
strcpy(s,str);
}
}
String & operator !()
{
char *p1=s,*p2=s,ch;
while(*p1++);
p1-=2;
while(p1>=p2)
{
ch=*p1;
*p1--=*p2;
*p2++=ch;
}
return *this;
}
String & operator =(String & str)
{
if(s)delete[]s;
s=new char[strlen(str.s)+1];
strcpy(s,str.s);
return *this;
}
void print(){cout<~String(){ delete []s; }
};
void main()
{
String t1("abcdefg"),t2;
t1.print();
t2=!t1;
t2.print();
}




相关文档
相关文档 最新文档