cout<<’\n’;
}
cout<<”SUM=”<
}
voidSUM:
:
process(){
intt=0,t1=0;
for(inti=0;i<5;i++)
for(intj=0;j<5;j++)
t+=array[i][j];
for(i=1;i<4;i++)
for(intj=1;j<4;j++)
t1+=array[i][j];
s=t-t1;
}
voidmain(){
intaa[5][5];
inta=1;
for(inti=0;i<5;i++)
for(intj=0;j<5;j++)
aa[i][j]=a++;
SUMss(aa);
ss.process();
ss.print();
}
例5:
定义一个字符串类String,具体要求如下:
(1)私有成员数据
char*str1,*str2;
char*str;//str为str1与str2之和,如str1为“dba”,str2为“41”,则str为“dba41”
(2)公有成员函数
构造函数:
根据给定的两个字符串初始化成员数据str1和str2,由str1和str2得到str;
voiddele():
删除str中的空格字符;
voidsort():
对str中的字符按升序进行排列;
voidshow():
输出所有成员数据;
析构函数。
(3)设计一个完整的程序对该类进行测试。
以字符串“dba”和“41”初始化str1和str2(b与a间一个空格,4与1间2个空格);
输出删除空格和排序前的字符串str1、str2和str;
输出删除空格和排序后的字符串str1、str2和str。
#include
#include
classString{
char*str1,*str2;
char*str;
public:
String(char*ptr1,char*ptr2);
voiddele();
voidsort();
voidshow();
~String(){
delete[]str1;
delete[]str2;
delete[]str;}
};
String:
:
String(char*ptr1,char*ptr2){
str1=newchar[strlen(ptr1)+1];
strcpy(str1,ptr1);//对内存空间赋值
str2=newchar[strlen(ptr2)+1];
strcpy(str2,ptr2);
str=
newchar[strlen(ptr1)+strlen(ptr2)+1];
strcpy(str,ptr1);strcat(str,ptr2);
}
voidString:
:
dele(){
char*ptr1=str,*ptr2=str;
while(*ptr1){
if(*ptr1!
=‘’)*ptr2++=*ptr1;
ptr1++;
}
*ptr2=’\0’;
}
voidString:
:
sort(){
char*ptr1=str,*ptr2,temp;
//ptr1和ptr2保证成员指针不移动
while(*ptr1){
for(ptr2=ptr1;*ptr2;ptr2++)
if(*ptr1>*ptr2){
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
}
ptr1++;
}
}
voidString:
:
show(){
cout<<”str1=”<cout<<”str2=”<cout<<”str=”<}
voidmain(){
Stringstr(“dab”,”41”);
str.show();
str.dele();
str.sort();
str.show();
}
例6:
定义一个数组类Array,求整型一维组中值为素数的元素的平均值。
具体要求如下:
(1)私有成员数据
int*p,k;//p表示维组,k为数组大小
float*s;//一维组中素数元素的平均值
(2)公有成员函数
Array(int*ptr,intn):
根据参数初始化p和k,并为s赋初值;
intfun(intn):
判断n是否为素数,若是返回1,否则返回0;
voidsum():
求素数元素的平均值;
voidshow():
输出所有成员数据,数组每行5个元素,其他成员每行一个;
析构函数。
(3)设计一个完整的程序对该类进行测试。
以数组{5,2,7,4,8,23,65,1,40}说明Array类型对象arr,并对其测试。
#include
classArray{
int*p,k;
float*s;
public:
Array(int*ptr,intn);
intfun(intn);
voidsum();
voidshow();
~Array();
};
Array:
:
Array(int*ptr,intn){
k=n;
p=newint[k];//分配空间
for(inti=0;ip[i]=ptr[i];}//初始化
s=newfloat(0);
}
intArray:
:
fun(intn){
if(n==0||n==1)return0;
for(inti=2;iif(n%i==0)return0;
return1;
}
voidArray:
:
sum(){
for(inti=0,j=0;iif(fun(p[i])){
*s+=p[i];
j++;
}
*s/=j;
}
voidArray:
:
show(){
cout<<”数组个数为:
”<cout<<”数组为:
”<for(inti=0,j=1;icout<
if(j%5==0)cout<}
cout<cout<<”和为:
”<<*s<}
~Array(){
delete[]p;
delete[]s;
}
voidmain(){
inta[20]={5,2,7,4,8,23,65,1,40};
Arrayarr(a,9);
arr.sum();
arr.show();
}
例7:
假定有如下“孪生质数对”的定义:
如果两个质数(即素数),相差为2,则这两个质数称为“孪生质数对”。
如(3,5)、(11,13)、(827,829)等。
试建立一个类Prime,求出指定范围内孪生质数对数,具体要求如下:
(1)私有数据成员
●intb[50][2];存放求出的孪生质数对。
●intm,n,count;m、n存放孪生质数对的取值范围,count存放求出的孪生质数对的对数。
(2)公有成员函数
●构造函数Prime(int,int):
初始化成员数据m,n。
●intisprime(intx):
判断x是否是素数。
●voidprocess():
根据给定的m,n的值,求出介于m,n之间(包括m,n本身)的所有孪生质数对及其对数,并输出。
(3)在主程序中对该类进行测试。
使用测试数据m=3,n=1000。
#include
#include
#include
classPrime{
public:
intb[50][2];
intm,n,count;
public:
Prime(ints,intt);
intisprime(intx);
voidprocess();
};
Prime:
:
Prime(ints,intt){
m=s;
n=t;
}
intPrime:
:
isprime(intx){
intk,i;
k=(int)sqrt(x);
for(i=2;i<=k;i++)
if(x%i==0)
return(0);
return
(1);
}
voidPrime:
:
process(){
inta[500],k=0;
for(inti=m;iif(isprime(i))a[k++]=i;
count=0;
for(i=0;iif(a[i+1]-a[i]==2){
b[count][0]=a[i];
b[count++][1]=a[i+1];}
}
for(i=0;iif(i%5==0)cout<cout<<'('<<<','<cout<}
};
voidmain(){
Primetest(3,1000);
cout<test.process();
}
例8:
设计一个日期类Date,包括年、月、日等私有数据成员。
要求实现日期的基本运算,如一日期加上天数、一日期减去天数、两日期相差的天数等。
分析:
在Date类中设计如下重载运算符函数:
Dateoperator+(intdays);//返回一日期加一天数得到的日期
Dateoperator-(intdays);//返回一日期减去天数得到的日期
intoperator-(Date&b);//返回两日期相差的天数
实现这些重载运算符函数调用以下私有成员函数:
leap(int);//判断指定的年份是否为闰年
dton(Date&);//将指定日期转换为从0年0月0日起的天数
ntod(int);//将指定的0年0月0日起的天数转换为对应的日期
#include
intday_tab[2][12]={{31,28,31,30,31,30,31,31,30,
31,30,31},{31,29,31,30,31,31,30,31,30,31}};
//day_tab二维数组存放各月天数,
//第一行对应非闰年,第二行对应闰年
classDate{
intyear,month,day;
intleap(int);
intdton(Date&);
Datentod(int);
public:
Date(){}
Date(inty,intm,intd);
voidsetday(intd);
voidsetmonth(intm);
voidsetyear(inty);
intgetday();
intgetmonth();
intgetyear();
Dateoperator+(intdays);
Dateoperator-(intdays);
intoperator-(Date&b);
voiddisp();
};
Date:
:
Date(inty,intm,intd)
{
year=y;month=m;day=d;
}
voidDate:
:
setday(intd){day=d;}
voidDate:
:
setmonth(intm){month=m;}
voidDate:
:
setyear(inty){year=y;}
intDate:
:
getday(){returnday;}
intDate:
:
getmonth(){returnmonth;}
intDate:
:
getyear(){returnyear;}
DateDate:
:
operator+(intdays)
{
staticDatedate;
intnumber=dton(*this)+days;
date=ntod(number);
returndate;
}
DateDate:
:
operator-(intdays)
{
staticDatedate;
intnumber=dton(*this);
number-=days;
date=ntod(number);
returndate;
}
intDate:
:
operator-(Date&b)
{
intda