";
switch(solve(a,b,c,x1,x2))
{
case0:
cout<<"无实根"<break;
case1:
cout<<"x="<break;
case2:
cout<<"x1="<break;
}
}
voidmain()
{disp(2,6,4);
disp(2,4,2);
disp(5,2,3);
system("pause");
}
p237将成绩转化为等级
#include"iostream"
#include"string"
#include"iomanip"
usingnamespacestd;
classstud
{
charname[10];
intdeg;
charlevel[7];
public:
stud(charna[],intd){strcpy(name,na);deg=d;}
char*getname(){returnname;}
friendvoidtrans(stud&s)
{
if(s.deg>=90)
strcpy(s.level,"优");
elseif(s.deg>=80)
strcpy(s.level,"良");
elseif(s.deg>=70)
strcpy(s.level,"中");
elseif(s.deg>=60)
strcpy(s.level,"及格");
elsestrcpy(s.level,"不及格");
}
voiddisp()
{cout<<""<}
};
voidmain()
{
studst[]={stud("王华",78),stud("李明",92),stud("张伟",62),stud("孙强",88)};
cout<<"输出结果"<for(inti=0;i<4;i++)
{
trans(st[i]);
st[i].disp();
}
system("pause");
}
p242银行例题
#include"iostream"
usingnamespacestd;
classBBank;//这里预先说明,类BBank在后面定义
classGBank;//这里预先说明,类GBank在后面定义
classCBank//说明中国银行类CBank
{
private:
intbalance;
public:
CBank(){balance=0;}
CBank(intb){balance=b;}
voidgetbalance(){
cout<<"输入中国银行存款数:
";
cin>>balance;
}
voiddisp(){
cout<<"中国银行存款数:
"<}
friendvoidtotal(CBank,BBank,GBank);
};
classBBank//说明工商银行类Bbank
{
private:
intbalance;
public:
BBank(){balance=0;}
BBank(intb){balance=b;}
voidgetbalance(){
cout<<"输入工商银行存款数:
";
cin>>balance;
}
voiddisp(){
cout<<"工商银行存款数:
"<}
friendvoidtotal(CBank,BBank,GBank);
};
classGBank//说明农业银行类GBank
{
private:
intbalance;
public:
GBank(){balance=0;}
GBank(intb){balance=b;}
voidgetbalance(){
cout<<"输入农业银行存款数:
";
cin>>balance;
}
voiddisp(){
cout<<"农业银行存款数:
"<}
friendvoidtotal(CBank,BBank,GBank);
};
voidtotal(CBankA,BBankB,GBankC)
{
cout<<"总存款数:
"<<
A.balance+B.balance+C.balance<}
voidmain()
{
CBankX(100);
BBankY;
GBankZ;
X.disp();Y.disp();Z.disp();
Y.getbalance();
Z.getbalance();
total(X,Y,Z);
system("pause");
}
p87递归
#include"iostream"
usingnamespacestd;
intn;
intf(intn)
{
if(n==1)//递归出口
return1;
else//递归体
returnn*f(n-1);
}
voidmain()
{
cin>>n;
cout<system("pause");
}
例题词频统计
#include"iostream"
#include"string"
usingnamespacestd;
structa
{char*name;//单词
intval;//出现的次数
};
classassoc
{
private:
a*vex;
intsize;//分配总的单元个数
intused;//已使用的单元个数
public:
assoc(intm)
{size=(m>16)?
m:
16;//至少大于16
used=0;
vex=newa[size];//分配空间
}
int&operator[](char*);
voiddisp();
};
int&assoc:
:
operator[](char*p)
{a*pp;
for(pp=vex;ppif(strcmp(p,pp->name)==0)//若找到,返回原次数
returnpp->val;
if(used==size)//如果单元数已用完,则返回
{staticintdummy=0;
returndummy;
}
pp=&vex[used++];//在已有的单词中未找到,则使用的单元个数增1
pp->name=newchar[strlen(p)+1];//分配一个单元空间
strcpy(pp->name,p);
pp->val=0;
returnpp->val;//返回0
}
voidassoc:
:
disp()
{cout<<"单词出现次数统计"<for(inti=0;i{
cout<<""<"<}
}
voidmain()
{
charbuf[16];
assocvecc(20);//设置20个单元存放单词
intk=10;
cout<<"输入"<for(inti=0;i{
cout<<"第"<
";
cin>>buf;
vecc[buf]++;
//调用“[]“,计数增1,相当于pp->val++
}
vecc.disp();
system("pause");
}
例题点到直线的距离
#include"iostream"
#include
usingnamespacestd;
classPoint
{
public:
doublex,y;
Point(doublex1,doubley1){x=x1;y=y1;}
};
classLine
{
private:
doublea,b,c;
public:
Line(doublea1,doubleb1,doublec1){a=a1;b=b1;c=c1;}
frienddoubledist(Linel,Pointp);
};
doubledist(Linel,Pointp)
{
doubled;
d=abs(l.a*p.x+l.b*p.y+l.c)/(sqrt(l.a*l.a+l.b*l.b));
returnd;
}
voidmain()
{
Pointp(10,10);
Linel(2,4,-3);
cout<<"d="<system("pause");
}
例题点类的运算
#include"iostream"
usingnamespacestd;
classPoint
{
private:
intx,y;
public:
voidsetPoint(intx1,inty1){x=x1;y=y1;}
voidmove(intx1,inty1);
voiddisplay();
};
voidPoint:
:
move(intx1,inty1)
{
x=x+x1;y=y+y1;
}
voidPoint:
:
display()
{
cout<<"x="<}
voidmain()
{
Pointa;
Point*p=newPoint;
a.setPoint(12,6);
a.move(3,4);
cout<<"Firstpoint=>";
a.display();
p->setPoint(5,12);
p->move(3,4);
cout<<"Sencondpoint=>";
p->display();
deletep;
system("pause");
}
例题点线类,计算两点间距离
#include"iostream"
usingnamespacestd;
classPoint
{
private:
intx,y;
public:
Point(intx1=0,inty1=0){x=x1;y=y1;}
Point(constPoint&p){x=p.x;y=p.y;}
intgetx(){returnx;}
intgety(){returny;}
};
classLine
{
private:
Pointp1,p2;
public:
Line(constPoint&pt1,constPoint&pt2);
doubledistance();
};
Line:
:
Line(constPoint&pt1,constPoint&pt2)
:
p1(pt1),p2(pt2)
{//或p1=pt1;
//p2=pt2;
}
doubleLine:
:
distance()
{
doubledx=p2.getx()-p1.getx();
doubledy=p2.gety()-p1.gety();
return(sqrt(dx*dx+dy*dy));
}
voidmain()
{Pointp1(3,4),p2(4,5);
Linel(p1,p2);
cout<system("pause");
}
例题银行还款
#include"iostream"
usingnamespacestd;
floatlv=0.062;//央行房贷利率上限
floatcount(floatloan,floatratio){
floatlv=:
:
lv*ratio;//各个银行重新计算自己的利率
returnloan*(1+lv);
};
intmain(){
floatloan;
floatgh_ratio;//0.9
floatzh_ratio;//0.8
cout<<"贷款总额";
cin>>loan;//10000
cout<<"工行执行%:
";
cin>>gh_ratio;
cout<<"中行执行%:
";
cin>>zh_ratio;
cout<<"工行本月还贷:
"<cout<<"中行本月还贷:
"<system("pause");
}
例题银行排队发号程序
#include"iostream"
usingnamespacestd;
intwelcome()
{
staticintcounter=0;
counter++;
cout<<"welcome!
Youare"<returncounter;
}
voidmain()
{
charch;
cin>>ch;//Q或q为结束
while(ch!
='Q'&&ch!
='q')
{
welcome();
cin>>ch;//Q或q为结束
}
system("pause");
}
判断闰年
#include"iostream"
usingnamespacestd;
voidmain()
{
intyear,rem4,rem100,rem400;
cout<<"输入年份";
cin>>year;
rem400=year%400;
rem100=year%100;
rem4=year%4;
if((rem400==0)||((rem4==0)&&(rem100!
=0)))
cout<else
cout<system("pause");
}
重载+=运算符
#include"iostream"
usingnamespacestd;
classVector//向量(x,y)
{
private:
intx,y;
public:
Vector(){x=0;y=0;};
Vector(intx1,inty1){x=x1;y=y1;};
voidsetvalue(intx1,inty1){x=x1;y=y1;};
voiddisplay(){cout<<"("<Vectoroperator+(Vectorv);
Vectoroperator-(Vectorv);
//友元方式实现
//friendVectoroperator+(Vectorv1,Vectorv2);
//friendVectoroperator-(Vectorv1,Vectorv2);
Vector&operator-=(Vectorv);//+=的重载——成员方式
friendVector&operator+=(Vector&v1,Vectorv2);//-=的重载——友元方式
};
VectorVector:
:
operator+(Vectorv)
{
Vectortmp;//定义一个tmp对象
tmp.x=x+v.x;//相当于tmp.x=this->x+v.x;
tmp.y=y+v.y;
returntmp;//返回tmp对象
}
VectorVector:
:
operator-(Vectorv)
{
Vectortmp;//定义一个tmp对象
tmp.x=x-v.x;
tmp.y=y-v.y;
returntmp;//返回tmp对象
}
/*友元方式实现
Vectoroperator+(Vectorv1,Vectorv2)
{
Vectortmp;//定义一个tmp对象
tmp.x=v1.x+v2.x;//相当于tmp.x=this->x+v.x;
tmp.y=v1.y+v2.y;
returntmp;//返回tmp对象
}
Vectoroperator-(Vectorv1,Vectorv2)
{
Vectortmp;//定义一个tmp对象
tmp.x=v1.x-v2.x;//相当于tmp.x=this->x+v.x;
tmp.y=v1.y-v2.y;
returntmp;//返回tmp对象
}
*/
Vector&Vector:
:
operator-=(Vectorv){
x-=v.x;
y-=v.y;
return*this;
}
Vector&operator+=(Vector&v1,Vectorv2){
v1.x+=v2.x;
v1.y+=v2.y;
returnv1;
};
voidmain()
{Vectorv1(6,8),v2(3,6),v3,v4;
cout<<"v1=";v1.display();
cout<<"v2=";v2.display();
v3=v1+v2;//v3=v1.operator+(v2);
cout<<"v1+v2=";v3.display();
v4=v1-v2;//v4=v1.operator-(v2);
cout<<"v1-v2=";v4.display();
//检验+=/-=
v1.setvalue(6,8);v2.setvalue(3,6);
cout<<"v1=";v1.display();
cout<<"v2=";v2.display();
v3=v1+=v2;//v3=operator+=(v1,v2);
cout<<"v1+=v2后,v1=";v1.display();
v4=v1-=v2;//v4=v1.operator-=(v2);
cout<<"v1-=v2后,v1=";v1.display();
system("pause");
}
重载比较运算符
#include"iostream"
usingnamespacestd;
classLine
{
intlen;
public:
Line(intn):
len(n){}
Lineoperator+(Linel)//"+"运算符重载
{intx=len+l.len;
returnLine(x);//无名临时对象
}
booloperator>(Linel)//“>"运算符重载
{return(len>l.len)?
true:
false;}
};
voidmain()
{Linea(10),b(5),c(14);
if(a+b>c&&a+c>b&&b+c>a)
cout<<"能够构成一个三角形"<else
cout<<"不能够构成一个三角形"<system("pause");
}
重载单目运算符++
#include"iostream"
usingnamespacestd;
classSample
{
private:
intn;
public:
Sample(inti=0):
n(i){}
voiddisplay(){cout<<"n="<voidoperator++(){++n;}//"++"运算符重载
voidoperator++(int){n+=2;}
};
void