C++期末练习1答案

Posted 自动秃头化

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++期末练习1答案相关的知识,希望对你有一定的参考价值。

1.

#include<iostream>
using namespace std;
const int m=0;
class Complex{
public:
int real;
int imag;
Complex(){
real=imag=0.0;
}
Complex(int r,int i){
real=r;
imag=i;
}
virtual void Print(){
if (imag>0)
cout<<real<<"+"<<imag<<"j"<<endl;
if (imag<0)
cout<<real<<imag<<"j"<<endl;
}
Complex operator = (const Complex &c){
real=c.real;
imag=c.imag;
return *this;
}
friend Complex operator - (const Complex &c1,Complex c2)
{
return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
friend Complex operator ++ (Complex &c){
c.real++;
c.imag++;
return c;
}
friend Complex operator ++(Complex &c,int){
Complex temp(c);
c.real++;
c.imag++;
return temp;
}
friend bool operator == ( Complex &c1,Complex &c2){
if(c1.real==c2.real&&c1.imag==c2.imag)
{
return 1;
}
else
return 0;
}
};
int main()
{
Complex c1(1,2),c2;
c2=c1;
c2.Print();
Complex c3(2,3),c4(1,2);
Complex cc;
(c3-c4).Print();
Complex c6(1,2);
(c6++).Print();
(++c6).Print();
Complex c7(1,2),c8(2,3),c9(1,2);
cout<<(c7==c8)<<endl;
cout<<(c7==c9)<<endl;
return 0;
}

2.

friend Matrix operator + (const Matrix &a1,Matrix &a2)
{
Matrix m;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
m.M[i][j]=a1.M[i][j]+a2.M[i][j];}}
return m;
}
friend Matrix operator - (const Matrix &a1,Matrix &a2)
{
Matrix m;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
m.M[i][j]=a1.M[i][j]-a2.M[i][j];}}
return m;
}
void Print(){
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<M[i][j]<<" ";
}
cout<<endl;
}
}

3.

private:
double x,y;
double m;
public:
Point(){
x=y=0.0;
}
Point(double i,double j)
{
x=i;
y=j;
}
operator double(){
return x-y;
}
friend Point operator - (const Point &a,Point &b)
{

double da=sqrt(a.x*a.x+a.y*a.y);
double db=sqrt(b.x*b.x+b.y*b.y);
return Point(da,db);
}

4.

operator int(){
int z=0;
for(int i=0;i<10;i++)
z=v[i]+z;
return z;
}
friend Vector operator * (Vector &a1,Vector &a2)
{
Vector m;
for(int i=0;i<10;i++)
{
m.v[i]=a1.v[i]*a2.v[i];
}
return m;
}

5.

class Complex{
public:
int real;
int imag;
Complex(){
real=imag=0.0;
}
Complex(int r,int i){
real=r;
imag=i;
}
virtual void Print(){
if (imag>0)
cout<<real<<"+"<<imag<<"j"<<endl;
if (imag<0)
cout<<real<<imag<<"j"<<endl;
}
Complex operator = (const Complex &c){
real=c.real;
imag=c.imag;
return *this;
}
friend Complex operator - (const Complex &c1,Complex c2)
{
return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
friend Complex operator ++ (Complex &c){
c.real++;
c.imag++;
return c;
}
friend Complex operator ++(Complex &c,int){
Complex temp(c);
c.real++;
c.imag++;
return temp;
}
friend bool operator == ( Complex &c1,Complex &c2){
if(c1.real==c2.real&&c1.imag==c2.imag)
{
return 1;
}
else
return 0;
}
};

6.

class Animal{
public:
static int number;
Animal(){
number++;
}
virtual ~Animal(){
number--;
}
};
class Dog:public Animal{
public:
static int number;
Dog(){
number++;
}
~Dog(){
number--;
}
};
class Cat:public Animal{
public:
static int number;
Cat(){
number++;
}
~Cat(){
number--;
}
};
int Animal::number=0;
int Dog::number=0;
int Cat::number=0;

7.

#include<iostream>
using namespace std;
const double pi=3.14;
class Circle{
public:
int r;
virtual void area(){
}
virtual void volume(){
}
};
class Sphere:public Circle{
public:
int r;
double s;
double v;
Sphere(int a)
{
r=a;
}
void area(){
s=4*pi*r*r;
cout<<"s.area = "<<s<<endl;
}
void volume(){
v=pi*r*r*r*4/3;
cout<<"s.volume= "<<v<<endl;
}
};
class Column:public Circle{
public:
int r;
int h;
double s;
double v;
Column(int a,int b)
{
r=a;
h=b;
}
void area(){
s=2*pi*r*r+2*pi*r*h;
cout<<"c.area = "<<s<<endl;
}
void volume(){
v=pi*r*r*h;
cout<<"c.volume= "<<v<<endl;
}
};
int main(){
int a,b,c;
cin>>a;
cin>>b>>c;
Sphere s(a);
Column co(b,c);
s.area();
s.volume();
co.area();
co.volume();
}

8.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Animal
{
string name;
int weight;
public:
void set(string a,int b)
{name = a; weight = b;}
string getn()
{return name;}
int getw()
{return weight;}
virtual string who(){
stringstream ss;
ss<<getw();
string s1 = ss.str();
return string("My name is ")+getn()+string(". My weight is ")+s1+string(" Kgs. ");
}
virtual string sound()=0;
virtual void js()
{cout<<"zl";}
};
class sheep:public Animal
{
public:
void sets(string a,int b)
{set(a,b);}
string who()
{
stringstream ss;
ss<<getw();
string s1 = ss.str();
return string("My name is ")+getn()+string(". My weight is ")+s1+string(" Kgs. ");
}
string sound()
{return "Baaaa!!";}
void js()
{cout << "I'm a sheep. ";}

};
class dog:public Animal
{
public:
void setd(string a,int b)
{set(a,b);}
string who()
{
stringstream ss;
ss<<getw();
string s1 = ss.str();
return string("My name is ")+getn()+string(". My weight is ")+s1+string(" Kgs. ");
}
string sound()
{return "woof woof!!";}
void js()
{cout << "I'm a dog. ";}
};
class cow:public Animal
{
public:
void setc(string a,int b)
{set(a,b);}
string who()
{
stringstream ss;
ss<<getw();
string s1 = ss.str();
return string("My name is ")+getn()+string(". My weight is ")+s1+string(" Kgs. ");
}
string sound()
{return "Mooooo!!!";}
void js()
{cout << "I'm a cow. ";}

};
class Zoo
{
public:
Zoo(){};
Animal *p[3];
sheep s;
dog d;
cow c;
void setz()
{
p[0] = &s;
p[1] = &d;
p[2] = &c;
}
};
int main()
{
int x;
string y;
int z;
Zoo T;
for(int i = 1; i <= 3; i++)
{
cin >> x >> y >> z;
if(x==0)
T.s.sets(y,z);
if(x==1)
T.d.setd(y,z);
if(x==2)
T.c.setc(y,z);
}
T.setz();
for(int i = 0; i <= 2 ;i++)
{
T.p[i]->js();
cout << (T.p[i]->who());
cout << (T.p[i]->sound());
cout << endl;
}
return 0;
}


以上是关于C++期末练习1答案的主要内容,如果未能解决你的问题,请参考以下文章

《计算机网络》期末模拟考试练习+答案

大学C语言期末考试练习题(带详解答案)

仁爱版七年级下册英语期末试卷及答案

期末复习数据结构与算法练习题

2016级算法期末模拟练习赛-B.AlvinZH的青春记忆I

2016级算法期末模拟练习赛-C.AlvinZH的青春记忆II