C++程序设计 课后作业
我们专业开了这门课,教材是 谭浩强的 C++程序设计。除了课后习题,还布置了一些习题,这里给大家分享一下。
占楼编辑 目录 占楼编辑 目录 #include<iostream>
#include<string>
using namespace std;
class base
{
public:
base(string nam,int ag)
{name=nam;age=ag;}
protected:
string name;
int age;
};
class leader:virtual public base
{public:
leader(string nam,int ag,string jo,string par):
base(nam,ag),job(jo),part(par){}
protected:
string job;
string part;
};
class engineer:virtual public base
{
public:
engineer(string nam,int ag,string titl,string majo):
base(nam,ag),title(titl),major(majo){}
protected:
string title;
string major;
};
class chairman:public leader,public engineer
{
public:
chairman(string nam,int ag,string jo,string par,string titl,string majo,int w)
:base(nam,ag),leader(nam,ag,jo,par),engineer(nam,ag,titl,majo),wage(w){}
void display()
{ cout<<"name:"<<name <<" age:"<<age <<" job:"<<job <<" part:"<<part <<endl<<"title:"<<title <<" major:"<<major <<" wages:"<<wage<<endl;
}
private:
int wage;
};
int main()
{
cout<<"信息1101班 王明辉" <<endl;
chairman c("Wang Minghui",'21',"Zhiwu1","Net part","Profession","Math and Computer",9000);
c.display();
return 0;
}
这个题的题目忘了{:3_50:} 明天补发一下。
#include<iostream>
using namespace std;
class teacher
{public:
teacher(int hou){
hour=hou;}
void print(){
cout<<"工作"<<hour<<"小时的工资是:"<<salary<<endl;
}
virtual void money()=0;
protected:
int hour;
int salary;
};
class profession:public teacher
{public:
profession(int hou):
teacher(hou){}
void money(){
salary=hour*50+5000;
}
};
class fuprofession:public teacher
{public:
fuprofession(int hou):
teacher(hou){}
void money(){
salary=hour*30+3000;
}
};
class jiang:public teacher
{public:
jiang(int hou):
teacher(hou){}
void money(){
salary=hour*20+2000;
}
};
int main()
{
profession pro(20);
teacher *t1;
t1=&pro;
t1->money();
cout<<" 教授:";
pro.print();
fuprofession fu(30);
teacher *t2;
t2=&fu;
t2->money();
cout<<"副教授:";
fu.print();
jiang jiang(40);
teacher *t3;
t3=&jiang;
t3->money();
cout<<" 讲师:";
jiang.print();
return 0;
}
#include<iostream.h>
#include<string.h>
class circle
{
double radius;
public:
circle(double r) { radius=r; }
double getarea() { return radius*radius*3.1415; }
};
class table
{
double height;
public:
table(double h) { height=h; }
double getheight() { return height; }
};
class roundtable : public table,public circle
{
char *color;
public:
roundtable(double h, double r, char c[]) : circle (r) , table (h)
{
color=new char;
strcpy (color, c);
}
char *getcolor() { return color; }
};
void main()
{
roundtable rt(0.9,1.5,"紫色");
cout << "圆桌属性数据:" << endl;
cout << "高度:" <<rt.getheight() << "米" << endl;
cout << "面积:" <<rt.getarea() << "平方米" << endl ;
cout << "颜色:" <<rt.getcolor() << endl;
}
建立一个分数类franction。分数类的数据成员包括分子和分母。运算符重载求约分、通分、减、乘、除、求倒。
#include<iostream.h>
#include<math.h>
class fraction{
public:
int zi; //分子
int mu; //分母
void yuefen(); //约分
void tongfen(fraction&); //通分
fraction(int a=0,int b=1){
zi=a;
mu=b;
try{ if(mu==0) throw mu; }
catch (int)
{cout<<"分母不可为零!"<<endl;}
}
fraction dao(); //求倒数
void display();
void input(); //输入分数
friend fraction operator+(fraction &f1,fraction &f2);
friend fraction operator-(fraction &f1,fraction &f2);
friend fraction operator*(fraction &f1,fraction &f2);
friend fraction operator/(fraction &f1,fraction &f2);
};
void fraction::yuefen(){ //约分
int a,b,t;
if(mu<0){
mu=-mu;
zi=-zi;
}
a=abs(zi);
b=abs(mu);
while(a%b){
t=a; a=b; b=t%b;
}
zi/=b;
mu/=b;
}
void fraction::tongfen(fraction& b){ //通分
int temp;
yuefen();
b.yuefen();
zi*=b.mu;
b.zi*=mu;
temp=mu*b.mu;
mu=b.mu=temp;
}
fraction operator + (fraction &f1,fraction &f2)
{
fraction f3(0,1);
f3.mu=f1.mu*f2.mu;
f3.zi=f1.zi*f2.mu+f1.mu*f2.zi;
f3.yuefen();
return f3;
};
fraction operator - (fraction &f1,fraction &f2)
{ fraction f3(0,1);
f3.mu=f1.mu*f2.mu;
f3.zi=f1.zi*f2.mu-f1.mu*f2.zi;
f3.yuefen();
return f3;
}
fraction operator * (fraction &f1,fraction &f2)
{ fraction f3(0,1);
f3.mu=f1.mu*f2.mu;
f3.zi=f1.zi*f2.zi;
f3.yuefen();
return f3;
}
fraction operator / (fraction &f1,fraction &f2)
{fraction f3(0,1);
f3.mu=f1.mu*f2.zi;
f3.zi=f1.zi*f2.mu;
f3.yuefen();
return f3;
}
fraction fraction::dao(){ //求倒数
fraction temp;
temp.zi=mu;
temp.mu=zi;
temp.yuefen();
return temp;
}
void fraction::display(){
yuefen();
cout<<"="<<zi<<"/"<<mu<<endl;
}
void fraction::input(){
cout<<"请输入分子、分母:"<<endl;
cin>>zi>>mu;
try{ if(mu==0) throw mu; }
catch (int)
{cout<<"分母不可为零!"<<endl;
}
yuefen();
}
void main(){
fraction f1(1,2),f2(3,6),f3(4,-6),f4(1,1),f5(1,1),f6(1,1);
cout<<"f1"; f1.display();
cout<<"f2"; f2.display();
cout<<"f3"; f3.display();
cout<<"f4"; f4.display();
cout<<"f5"; f5.display();
f4=f1+f3;cout<<"两分数相加:f1+f3="; f4.display();
f4=f1-f2;cout<<"两分数相减:f4=f1-f2="; f4.display();
f4=f1*f3;cout<<"两分数相乘:f4=f1*f3="; f4.display();
f4=f2/f3;cout<<"两分数相除:f4=f1/f3="; f4.display();
f4=f2.dao();cout<<"求分数的倒数:1/f2="; f4.display();
cout<<"以上为构造函数的数据,下面请输入2个分数:"<<endl;
f4.input();
f5.input();
cout<<"f4"; f4.display();
cout<<"f5"; f5.display();
cout<<"f6=f4+f5";f6=f4+f5;f6.display();
cout<<"f6=f4-f5";f6=f4-f5;f6.display();
cout<<"f6=f4*f5";f6=f4*f5;f6.display();
cout<<"f6=f4/f5";f6=f4/f5;f6.display();
cout<<"f6=1/f4";f6=f4.dao();f6.display();
}
定义一个Shape抽象类,建立一个point类,表示平面中的一个点;建立一个line类,表示平面中的一条线段,内含两个point类的对象;建立triangle类,表示一个三角形,内含三个line类的对象构成一个三角形,确定派生关系,编制相应程序输出相关信息,设计triangle类的成员函数完成三条边是否能构成三角形的检验和三角型面积计算,输出相关信息。
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
class Shape
{public:
};
class Point:public Shape
{public:
Point(float a,float b)
{x=a;y=b;}
void setPofloat(float a,float b)
{x=a;y=b;}
float x,y;
};
class Line
{public:
Line(float a1,float b1,float a2,float b2):
p1(a1,b1),p2(a2,b2){};
double length()
{ return (sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))); }
void setline(){
cin>>p1.x>>p1.y>>p2.x>>p2.y;
}
protected:
Point p1,p2;
};
class Triangle
{
public:
Triangle(float a1,float b1,float a2,float b2, float a3,float b3,float a4,float b4 ,float a5,float b5,float a6,float b6):
l1(a1,b1,a2,b2), l2(a3,b3,a4,b4) , l3(a5,b5,a6,b6){};
float delta(){
d1=l1.length();
d2=l2.length();
d3=l3.length();
if((d1+d2>d3)&&(d2+d3>d1)&&(d3+d1>d2))
return 1;
else
return 0;
}
void sett(){
cout<<"输入3条线的6个点的坐标(12个数字)"<<endl;
l1.setline();
l2.setline();
l3.setline();
cout<<"3条线的长度分别为:"<<endl;
cout<<l1.length()<<", "<<l2.length()<<", "<<l3.length()<<"."<<endl;
try
{
double p=(l1.length()+l2.length()+l3.length())/2;
if ( (l1.length()+l2.length()<=l3.length()) || (l2.length()+l3.length()<=l1.length()) || (l3.length()+l1.length()<=l2.length()) ) throw l1.length();
double s=sqrt( p*(p-l1.length()) * (p-l2.length()) *(p-l3.length()) );
cout<<"三角形的面积:"<<s<<endl;
}
catch (double)
{cout<<"经过异常处理,发现您所输入的坐标构成的3条线段长度不满足三角形";
}
}//sett()
protected:
Line l1,l2,l3;
float d1,d2,d3;
};
int main()
{
Triangle t1(0,0,1,1, 1,2,3,4 , 4,5,6,7);
try {
float whether=t1.delta();
if(!whether) throw whether;
t1.sett();
}
catch(float)
{cout<<"(0,0)(1,1),(1,2),(3,4),(4,5),(6,7)这几个点不可以构成三角形"<<endl;
}
return 0;
}
楼主好厉害,谢谢分享哈 哈哈哈哈哈哈哈
页:
[1]
2