1、 x2=(-b-sqrt(d)/(2*a); else if (d=0) count=1; x1=(-b)/(2*a); else count=0; return count;void disp(double a,double b,double c) double x1,x2;( a , b c ):; switch (solve(a,b,c,x1,x2) case 0: cout无实根 break; case 1:x= x1 case 2:x1=,x2=x2 =90) strcpy(s.level,优 else if(s.deg =80)良=70)中=60) strcpy(s.level ,
2、及格 else strcpy(s.level,不及格 void disp() coutnamedeglevelendl; stud st=stud(王华,78),stud(李明,92),stud(张伟,62),stud(孙强,88);输出结果 for(int i=0;4; trans(sti); sti.disp();p242银行例题class BBank; /这里预先说明,类BBank在后面定义class GBank; /这里预先说明,类GBank在后面定义class CBank /说明中国银行类CBankprivate: int balance; CBank() balance=0; CB
3、ank(int b) balance=b; void getbalance() coutbalance; void disp()中国银行存款数:balancefriend void total(CBank,BBank,GBank);class BBank /说明工商银行类Bbank BBank() balance=0; BBank(int b) balance=b; cout balance;工商银行存款数:balance class GBank /说明农业银行类GBank GBank() balance=0; GBank(int b)balance=b;输入农业银行存款数:农业银行存款数:v
4、oid total(CBank A,BBank B,GBank C) 总存款数: A.balance+B.balance+C.balance f(n);例题词频统计struct a char *name; /单词 int val; /出现的次数class assoc a*vex; int size; /分配总的单元个数 int used; /已使用的单元个数 assoc(int m) size=(m16) ?m:16; /至少大于16 used=0; vex=new asize; /分配空间 int & operator (char *); void disp();int & assoc:op
5、erator (char *p) a *pp; for (pp=vex;ppname)=0) /若找到,返回原次数 return pp-val; if (used=size) /如果单元数已用完,则返回 static int dummy=0; return dummy; pp=&vexused+; /在已有的单词中未找到,则使用的单元个数增1 pp-name=new charstrlen(p)+1; /分配一个单元空间 strcpy(pp-name,p);val=0; return pp- /返回0 void assoc:disp() cout单词出现次数统计 for (int i=0;use
6、d;vexi.name : vexi.val次 char buf16; assoc vecc(20); /设置20个单元存放单词 int k=10;输入k个单词k;第i+1val+ vecc.disp();例题点到直线的距离#includeclass Point double x,y; Point(double x1,double y1) x=x1;y=y1;class Line double a,b,c; Line(double a1,double b1,double c1) a=a1; b=b1; c=c1;friend double dist(Line l,Point p);double
7、 dist(Line l,Point p) double d; d=abs(l.a*p.x+l.b*p.y+l.c)/(sqrt(l.a*l.a+l.b*l.b); return d; Point p(10,10); Line l(2,4,-3);d=dist(l,p)例题点类的运算class Point private: int x,y; public: void setPoint (int x1,int y1) x=x1; void move(int x1,int y1); void display();void Point: move(int x1,int y1) x=x+x1; y=y
8、+y1;display() x ,y= y a.display(); p-setPoint(5,12);move(3,4);Sencond point =display(); delete p;例题点线类,计算两点间距离 Point(int x1=0,int y1=0) x=x1; y=y1; Point(const Point &p) x=p.x; y=p.y; int getx() return x; int gety() return y; Point p1,p2; Line(const Point &pt1,const Point &pt2); double distance();Li
9、ne:Line(const Point &pt2) p1(pt1), p2(pt2) /或p1=pt1; / p2=pt2;double Line:distance() double dx = p2.getx() - p1.getx(); double dy = p2.gety() - p1.gety(); return(sqrt(dx * dx + dy * dy); Point p1(3,4),p2(4,5); Line l(p1,p2);l.distance()例题银行还款float lv=0.062; /央行房贷利率上限float count(float loan, float rat
10、io) float lv=:lv*ratio; /各个银行重新计算自己的利率 return loan*(1+lv);int main() float loan; float gh_ratio; /0.9 float zh_ratio; /0.8贷款总额loan; /10000工行执行:gh_ratio;中行执行:zh_ratio;工行本月还贷:count(loan,gh_ratio)中行本月还贷:count(loan,zh_ratio);例题银行排队发号程序int welcome() static int counter=0; counter+;welcome! You are counter
11、 year; rem400=year%400; rem100=year%100; rem4=year%4; if(rem400=0)|(rem4=0)&(rem100!=0) year 是闰年 else 不是闰年重载+=运算符class Vector /向量(x,y) Vector() x=0; y=0; Vector(int x1,int y1) x=x1; ; void setvalue(int x1,int y1) x=x1; void display() cout x+v.x; tmp.y=y+v.y; return tmp; /返回tmp对象operator -(Vector v)
12、tmp.x=x-v.x; tmp.y=y-v.y;/*友元方式实现Vector operator+(Vector v1,Vector v2) tmp.x=v1.x+v2.x; tmp.y=v1.y+v2.y;Vector operator-(Vector v1,Vector v2) tmp.x=v1.x-v2.x; tmp.y=v1.y-v2.y;*/Vector& Vector:operator -=(Vector v) x-=v.x; y-=v.y; return *this; operator +=(Vector &v1,Vector v2) v1.x+=v2.x; v1.y+=v2.y
13、; return v1; Vector v1(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=
14、v1.operator-=(v2);v1-v2 后,v1=重载比较运算符 int len; Line(int n) :len(n) Line operator +(Line l) /+运算符重载 int x=len+l.len; return Line(x); /无名临时对象 bool operator (Line l) /“ return (lenl.len) ? true:false; Line a(10),b(5),c(14); if (a+bc & a+cb & b+ca)能够构成一个三角形 else不能够构成一个三角形重载单目运算符+class Sample int n; Sample(int i=0) :n(i) void display() cout n= n void operator +() +n; /+ void operator +(int)n+=2;void