课本与课件例题Word格式文档下载.docx

上传人:b****6 文档编号:20377538 上传时间:2023-01-22 格式:DOCX 页数:24 大小:19.76KB
下载 相关 举报
课本与课件例题Word格式文档下载.docx_第1页
第1页 / 共24页
课本与课件例题Word格式文档下载.docx_第2页
第2页 / 共24页
课本与课件例题Word格式文档下载.docx_第3页
第3页 / 共24页
课本与课件例题Word格式文档下载.docx_第4页
第4页 / 共24页
课本与课件例题Word格式文档下载.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

课本与课件例题Word格式文档下载.docx

《课本与课件例题Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《课本与课件例题Word格式文档下载.docx(24页珍藏版)》请在冰豆网上搜索。

课本与课件例题Word格式文档下载.docx

x2=(-b-sqrt(d))/(2*a);

elseif(d==0)

{count=1;

x1=(-b)/(2*a);

elsecount=0;

returncount;

voiddisp(doublea,doubleb,doublec)

{doublex1,x2;

("

a<

"

b<

c<

):

;

switch(solve(a,b,c,x1,x2))

case0:

cout<

无实根"

break;

case1:

x="

x1<

case2:

x1="

x2="

x2<

{disp(2,6,4);

disp(2,4,2);

disp(5,2,3);

p237将成绩转化为等级

#include"

string"

iomanip"

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)

良"

=70)

中"

=60)

strcpy(s.level,"

及格"

elsestrcpy(s.level,"

不及格"

voiddisp()

{cout<

name<

deg<

level<

endl;

};

studst[]={stud("

王华"

78),stud("

李明"

92),stud("

张伟"

62),stud("

孙强"

88)};

输出结果"

for(inti=0;

4;

trans(st[i]);

st[i].disp();

p242银行例题

classBBank;

//这里预先说明,类BBank在后面定义

classGBank;

//这里预先说明,类GBank在后面定义

classCBank//说明中国银行类CBank

private:

intbalance;

CBank(){balance=0;

CBank(intb){balance=b;

voidgetbalance(){

cout<

输入中国银行存款数:

cin>

>

balance;

voiddisp(){

中国银行存款数:

balance<

friendvoidtotal(CBank,BBank,GBank);

classBBank//说明工商银行类Bbank

BBank(){balance=0;

BBank(intb){balance=b;

cout<

输入工商银行存款数:

cin>

balance;

工商银行存款数:

balance<

classGBank//说明农业银行类GBank

GBank(){balance=0;

GBank(intb){balance=b;

输入农业银行存款数:

农业银行存款数:

voidtotal(CBankA,BBankB,GBankC)

总存款数:

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);

p87递归

intn;

intf(intn)

if(n==1)//递归出口

return1;

else//递归体

returnn*f(n-1);

voidmain()

cin>

f(n);

例题词频统计

structa

{char*name;

//单词

intval;

//出现的次数

classassoc

a*vex;

intsize;

//分配总的单元个数

intused;

//已使用的单元个数

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;

pp<

vex+used;

pp++)//在已有的单词中查找

if(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);

val=0;

returnpp->

//返回0

}

voidassoc:

disp()

{cout<

单词出现次数统计"

for(inti=0;

used;

vex[i].name<

:

vex[i].val<

次"

charbuf[16];

assocvecc(20);

//设置20个单元存放单词

intk=10;

输入"

k<

个单词"

k;

第"

i+1<

个单词:

buf;

vecc[buf]++;

//调用“[]“,计数增1,相当于pp->

val++

vecc.disp();

例题点到直线的距离

#include<

math.h>

classPoint

doublex,y;

Point(doublex1,doubley1){x=x1;

y=y1;

classLine

doublea,b,c;

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;

Pointp(10,10);

Linel(2,4,-3);

d="

dist(l,p)<

例题点类的运算

classPoint

private:

intx,y;

public:

voidsetPoint(intx1,inty1){x=x1;

voidmove(intx1,inty1);

voiddisplay();

voidPoint:

move(intx1,inty1)

x=x+x1;

y=y+y1;

display()

x<

y="

y<

Pointa;

Point*p=newPoint;

a.setPoint(12,6);

a.move(3,4);

Firstpoint=>

a.display();

p->

setPoint(5,12);

move(3,4);

Sencondpoint=>

display();

deletep;

例题点线类,计算两点间距离

Point(intx1=0,inty1=0){x=x1;

y=y1;

Point(constPoint&

p){x=p.x;

y=p.y;

intgetx(){returnx;

intgety(){returny;

Pointp1,p2;

Line(constPoint&

pt1,constPoint&

pt2);

doubledistance();

Line:

Line(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));

{Pointp1(3,4),p2(4,5);

Linel(p1,p2);

l.distance()<

例题银行还款

floatlv=0.062;

//央行房贷利率上限

floatcount(floatloan,floatratio){

floatlv=:

lv*ratio;

//各个银行重新计算自己的利率

returnloan*(1+lv);

intmain(){

floatloan;

floatgh_ratio;

//0.9

floatzh_ratio;

//0.8

贷款总额"

loan;

//10000

工行执行%:

gh_ratio;

中行执行%:

zh_ratio;

工行本月还贷:

count(loan,gh_ratio)<

中行本月还贷:

count(loan,zh_ratio);

例题银行排队发号程序

intwelcome()

staticintcounter=0;

counter++;

welcome!

Youare"

counter<

returncounter;

charch;

ch;

//Q或q为结束

while(ch!

='

Q'

&

&

ch!

q'

welcome();

判断闰年

{

intyear,rem4,rem100,rem400;

输入年份"

cin>

year;

rem400=year%400;

rem100=year%100;

rem4=year%4;

if((rem400==0)||((rem4==0)&

(rem100!

=0)))

year<

是闰年"

else

不是闰年"

重载+=运算符

classVector//向量(x,y)

Vector(){x=0;

y=0;

Vector(intx1,inty1){x=x1;

};

voidsetvalue(intx1,inty1){x=x1;

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对象

operator-(Vectorv)

tmp.x=x-v.x;

tmp.y=y-v.y;

/*友元方式实现

Vectoroperator+(Vectorv1,Vectorv2)

tmp.x=v1.x+v2.x;

tmp.y=v1.y+v2.y;

Vectoroperator-(Vectorv1,Vectorv2)

tmp.x=v1.x-v2.x;

tmp.y=v1.y-v2.y;

*/

Vector&

Vector:

operator-=(Vectorv){

x-=v.x;

y-=v.y;

return*this;

operator+=(Vector&

v1,Vectorv2){

v1.x+=v2.x;

v1.y+=v2.y;

returnv1;

{Vectorv1(6,8),v2(3,6),v3,v4;

v1="

v1.display();

v2="

v2.display();

v3=v1+v2;

//v3=v1.operator+(v2);

v1+v2="

v3.display();

v4=v1-v2;

//v4=v1.operator-(v2);

v1-v2="

v4.display();

//检验+=/-=

v1.setvalue(6,8);

v2.setvalue(3,6);

v3=v1+=v2;

//v3=operator+=(v1,v2);

v1+=v2后,v1="

v1.display();

v4=v1-=v2;

//v4=v1.operator-=(v2);

v1-=v2后,v1="

重载比较运算符

intlen;

Line(intn):

len(n){}

Lineoperator+(Linel)//"

+"

运算符重载

{intx=len+l.len;

returnLine(x);

//无名临时对象

booloperator>

(Linel)//“>

{return(len>

l.len)?

true:

false;

{Linea(10),b(5),c(14);

if(a+b>

c&

a+c>

b&

b+c>

a)

能够构成一个三角形"

else

不能够构成一个三角形"

重载单目运算符++

classSample

intn;

Sample(inti=0):

n(i){}

voiddisplay(){cout<

n="

n<

voidoperator++(){++n;

}//"

++"

voidoperator++(int){n+=2;

void

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1