temp=b*b-4*a*c;
if(temp>0)
equation_1(a,b,c);
if(temp==0)
equation_2(a,b,c);
if(temp<0)
equation_3(a,b,c);
}
2.定义函数up(ch),如字符变量ch是小写字母就转换成大写字母并通过up返回,否则字符ch不改变。
要求在短小而完全的程序中显示这个程序是怎样被调用的。
#include
usingnamespacestd;
charup(charc)
{
if(c>=97&&c<=123)
return(c-32);
else
returnc;
}
voidmain()
{
inti;
charc[15]={'A','v','e','t','E','T','%','&','4','Y','e','i','@','9','^'};
for(i=0;i<15;i++)
cout<cout<}
3.编写主程序条用带实数r和整数n两个参数的函数并输出r的n次幂。
#include
#include
doublepower(doublea,intb)
{
inti;
doubleresult=1.0;
for(i=0;i
result=result*a;
returnresult;
}
voidmain()
{
doubler;
intn;
cout<<"r=";
cin>>r;
cout<<"n=";
cin>>n;
cout<"<}
4.编写有字符型参数C和整形参数N的函数,让他们显示出由字符C组成的三角形。
其方式为第1行有1个字符C,第2行有2个字符C,等等。
#include
usingnamespacestd;
voidprint_triangle(charc,intn)
{
inti,j;
for(i=0;i{
for(j=0;j<=i;j++)
{
cout<}
cout<}
}
voidmain()
{
print_triangle('a',10);
}
5.编写一个ieqiu字符串长度的函数,strlen(),再用strlen()函数编写一个函数revers(s)的倒序递归程序,使字符串s逆序。
#include
#include
usingnamespacestd;
intstrlen(char*str)
{
intlen=0;
while(str[len]!
='\0')
{
len++;
}
returnlen;
}
voidrevers(char*b)
{
charc;
intj,len;
len=strlen(b);
j=len/2-1;
while(j>=0)
{
c=*(b+j);
*(b+j)=*(b+len-j-1);
*(b+len-j-1)=c;
j--;
}
b[len]='\0';
}
voidmain()
{
charstr[]={"1234567890"};
cout<"<cout<revers(str);//
cout<}
6.用函数模板实现3个数值中按最小值到最大值排序的程序。
#include
usingnamespacestd;
template
voidsort(Ta,Tb,Tc)
{
Tarray[3],temp;
inti,j;
array[0]=a;
array[1]=b;
array[2]=c;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
cout<}
voidmain()
{
sort(5,1,9);
}
7.利用函数模板设计一个求数组元素中和的函数,并检验之。
#include
usingnamespacestd;
template
Tsum(Ta[],intn)
{
inti;
Ts=0;
for(i=0;is=s+a[i];
returns;
}
voidmain()
{
inta[5]={1,2,3,4,5};
ints=sum(a,5);
cout<
}
8.重载上题中的函数模板,使他能够进行两个数组的求和。
#include
usingnamespacestd;
template
Tsum(Ta[],intn)
{
inti;
Ts=0;
for(i=0;is=s+a[i];
returns;
}
template//重载上面的模板
Tsum(Ta[],intn,Tb[],intm)
{
returnsum(a,n)+sum(b,m);
}
voidmain()
{
inta[5]={1,2,3,4,5};
intb[10]={1,2,3,4,5,6,7,8,9,10};
ints1=sum(a,5);
ints2=sum(b,10);
ints3=sum(a,5,b,10);
cout<cout<cout<}
[color=#EE1111]第四章[/color]
一、填空题
1.数据成员、成员函数;
2.类、重载、1;
3.fun:
fun(fun&)、fun:
fun(constfun&);
二、单项选择题
1.C。
2.C。
3.没又答案,应该是A:
:
~A(void)、或A:
:
~A()。
4.B。
5.C。
6.C。
7.D
三、改错题
1.returnm;---错误,没又定义变量m;
2.A.init(24,56);---错误,应该先定义A对象:
PointA;
四、完成程序题
1.
#include
usingnamespacestd;
classbase
{
private:
//私有数据成员
inta,b;
public:
voidinit(intx,inty)//公有函数
{
a=x;
b=y;
}
voidprint()
{
cout<<"2*"<}
};
voidmain()
{
basea;
a.init(68,55);
a.print();
}
2.
#include
usingnamespacestd;
classPoint
{
private:
intm,n;
public:
Point(int,int);//整型变量,为参数的构造函数
Point(Point&);//复制构造函数的原型
print()
{
cout<<"m="<}
};
Point:
:
Point(inta,intb)
{
m=a;
n=b;
}
Point:
:
Point(Point&t)//复制构造函数的定义
{
m=t.m;
n=t.n;
}
voidmain()
{
Pointa(10,89);
Pointb(a);
a.print();
b.print();
}
五、程序分析题
1.没有结果,因为没有main函数
如果加main函数
voidmain()
{
baseb(10,20);
}
输出:
初始化...10,20
Destory...10,20
2.
输出:
55
六、编程题
1.设计一个点类Point,再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形的对角顶点。
并可以输出4个坐标值和面积。
使用测试程序验证程序。
#include
usingnamespacestd;
classPoint//点类
{
private:
intx,y;//私有成员变量,坐标
public:
Point()//无参数的构造方法,对xy初始化
{
x=0;
y=0;
}
Point(inta,intb)//又参数的构造方法,对xy赋值
{
x=a;
y=b;
}
voidsetXY(inta,intb)//设置坐标的函数
{
x=a;
y=b;
}
intgetX()//得到x的方法
{
returnx;
}
intgetY()//得到有的函数
{
returny;
}
};
classRectangle//矩形类
{
private:
Pointpoint1,point2,point3,point4;//私有成员变量,4个点的对象
public:
Rectangle();//类Point的无参构造函数已经对每个对象做初始化啦,这里不用对每个点多初始化了
Rectangle(Pointone,Pointtwo)//用点对象做初始化的,构造函数,1和4为对角顶点
{
point1=one;
point4=two;
init();
}
Rectangle(intx1,inty1,intx2,inty2)//用两对坐标做初始化,构造函数,1和4为对角顶点
{
point1.setXY(x1,y1);
point4.setXY(x2,y2);
init();
}
voidinit()//给另外两个点做初始化的函数
{
point2.setXY