全部知识点

第7581题

定义一个矩阵类(Matrix),设计相关的构造函数、析构函数等,采用运算符重载方式实现矩阵的加、减运算。编写main函数,对以上所有功能进行测试。注意:按照实际情况大小使用内存空间,矩阵的加、减运算不要造成内存浪费。

#include <iostream>
#include <vector>
#include <cstdlib>
using
namespace std;
class
Matrix {
public:
Matrix (int row, int col); //构造row x col大小矩阵
Matrix operator + (const Matrix &rhs) const;//相同大小矩阵相加
Matrix operator - (const Matrix &rhs) const;//相同大小矩阵相减
void Display () const;//显示矩阵
//矩阵元素,可作为右值
const int &at (int row, int col) const {
return _datas [row][col];
}
//矩阵元素,可作为左值
int& at (int row, int col) {
return _datas [row][col];
}
//矩阵行数
int GetRowCount () const {
return _datas.size ();
}
//矩阵列数
int GetColCount () const {
return _datas [0].size ();
}
private:
//存放矩阵元素,本类没有直接动态分配,无需重载拷贝构造和赋值
vector<vector<int> > _datas;
};
Matrix::Matrix (int row, int col)
{
for (int i = 0; i < row; i++) {
vector<int> dataRow (col);
_datas.push_back (dataRow);
}
}
Matrix Matrix::operator + (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) + rhs.at (i, j);
}
}
return result;
}
Matrix Matrix::operator - (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) - rhs.at (i, j);
}
}
return result;
}
void Matrix::Display () const
{
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
cout.width (5);
cout << at (i,j);
}
cout << endl;
}
cout << endl;
}
int main()
{
Matrix A (5, 6), B (5, 6);
int i, j;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
A.at(i, j) = rand () % 100 ;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
B.at(i, j ) = rand () % 100;
A.Display();
B.Display();
Matrix C = A + B;
C.Display();
Matrix D = A - B;
D.Display();
return 0;
}#include <iostream>
#include <vector>
#include <cstdlib>
using
namespace std;
class
Matrix {
public:
Matrix (int row, int col); //构造row x col大小矩阵
Matrix operator + (const Matrix &rhs) const;//相同大小矩阵相加
Matrix operator - (const Matrix &rhs) const;//相同大小矩阵相减
void Display () const;//显示矩阵
//矩阵元素,可作为右值
const int &at (int row, int col) const {
return _datas [row][col];
}
//矩阵元素,可作为左值
int& at (int row, int col) {
return _datas [row][col];
}
//矩阵行数
int GetRowCount () const {
return _datas.size ();
}
//矩阵列数
int GetColCount () const {
return _datas [0].size ();
}
private:
//存放矩阵元素,本类没有直接动态分配,无需重载拷贝构造和赋值
vector<vector<int> > _datas;
};
Matrix::Matrix (int row, int col)
{
for (int i = 0; i < row; i++) {
vector<int> dataRow (col);
_datas.push_back (dataRow);
}
}
Matrix Matrix::operator + (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) + rhs.at (i, j);
}
}
return result;
}
Matrix Matrix::operator - (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) - rhs.at (i, j);
}
}
return result;
}
void Matrix::Display () const
{
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
cout.width (5);
cout << at (i,j);
}
cout << endl;
}
cout << endl;
}
int main()
{
Matrix A (5, 6), B (5, 6);
int i, j;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
A.at(i, j) = rand () % 100 ;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
B.at(i, j ) = rand () % 100;
A.Display();
B.Display();
Matrix C = A + B;
C.Display();
Matrix D = A - B;
D.Display();
return 0;
}
第7582题

请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

2,1

4,3

#include<iostream.h>
class A
{
int a;
public:
A(int i=0){a=i;}
Int Geta(){return a;}
};
class B
{
A a;
int b;
public:
B(int i=0,int j=0):___________(1)____________{}
void display(){cout<<a.Geta()<<’,’<<b<<endl;}
};
void main()
{
B b[2]={B(1,2),B(3,4)};
for(int i=0;i<2;i++)
_____________(2)______________;
}
第7583题

下面程序中A是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

B1 called

B2 called

#include<iostream.h>
class A
{
public:
______________(1)____________;
};
class B1:public A
{
public:
void display(){cout<”B1 called”<<endl;
};
class B2:public A
{
public:
void display(){cout<<”B2 called”<<endl;
};
void show(_________(2)________)
{
p->display();
}
void main()
{
B1 b1;
B2 b2;
A* p[2]={&b1,&b2};
for(int i=0;i<2;i++)
show(p[i]);
}
第7584题

请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

Name:王小明

Grade:90

#include<iostream.h>
#include<string.h>
class Person
{
char name[20]
public:
Person(char* s){strcpy(name,s);}
void display(){cout<<”Name:”<<name<<endl;}
};
class Student:public Person
{
int grade;
public:
Student(char* s,int g):________(1)_____{grade=g;}
void display(){
___________(2)___________;
cout<<”Grade:”<<grade<<endl;
}
};
void main()
{
Student s(“王小明”,90);
s.display();
}
第7585题

请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为5。

#include<iostream.h>
class Integer
{
int x;
public:
Integer(int a=0){x=a;}
void display(){cout<<x<<endl;}
_____________(1)_____________;
};
Integer Max(Integer a,Integer b)
{
if(__________(2)_________)
return a;
return b;
}
void main()
{
Integer a(3),b(5),c;
c=Max(a,b);
c.display();
}
第7586题

请在下面的横线处填上适当内容,以使类的定义完整。

class Array
{
Int* ptr;
Int size;
public:
Array(){size=0; ptr=NULL;}
Array(int n){size=n;ptr=new int[size];}
Array(______(1)_______)//复制初始化构造函数
{
size=a.size;
ptr=new int[size];
for(int i=0;i<size;i++)
________(2)________;//将源对象的动态数组内容复制到目标对象
}
};
第7587题

程序阅读题

1、

#include<iostream.h>
class Test
{
private:
int num;
public:
Test(int n=0){num=n;num++}
~Test(){cout<<″Destructor is active,number=″<<num<<endl;}
};
void main()
{
Test x[2];
cout<<″Exiting main″<<endl;
}

2、

#include <iostream.h>
class A
{
public:
virtual void fun (int data){cout<<”class A:”<<data<<endl;}
void fun(char *str){ cout<<”class A:”<<str<<endl; }
};
class B: public A
{
public:
void fun() {cout<<”class B”<<endl;}
void fun(int data) { cout<<”class B:”<<data<<endl;}
void fun(char *str){ cout<<”class B:”<<str<<endl;}
};
void main()
{
A *pA;
B b;
pA=&b;
pA->fun(1);
pA->fun(“Hello”);
}

3、

#include <iostream.h>
void main()
{
cout.fill('*');
cout.width(10);
cout<<"123.45"<<endl;
cout.width(8);
cout<<"123.45"<<endl;
cout.width(4);
cout<<"123.45"<<endl;
}

4、从键盘输入10,给出程序的输出结果。

#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
main()
{
vector<int> v;
int i,j,n;
cin>>n;
for(i=1;i<=n;i++)
{
if(i%2!=0)
v.push_back(x);
}
vector<int>::iterator p;
for(p=v.begin();p!=v.end();p++)
cout<<*p<<endl;
}

5、

#include<iostream>
using namespace std;
class Sample
{
private:
int i;
static int count;
public:
Sample();
void display();
};
Sample::Sample()
{
i=0;
count++;
}
void Sample::display()
{
cout<<"i="<<i++<<",count="<<count<<endl;
}
int Sample::count=0;
void main()
{
Sample a,b;
a.display();
b.display();
}
第7588题

按下列要求编程,实现类的定义,并在主函数中测试这个类。

定义一个描述日期的类Date,包括的数据成员有年(year)、月(month)和日(day),并实现如下功能函数;

(1)日期对象初始化;

(2)设置日期;

(3)以year/month/day形式输出日期;

(4)判断闰年。


第7589题

根据下列Vector类定义,编程完成Vector类的具体实现:

class Vector
{
friend ostream &operator<<(ostream &out, const Vector &v);
private:
int *data;
int size;
public:
Vector();
Vector(int a[],int n);
Vector(const Vector &s);
~Vector();
Vector &operator=(const Vector &v);
int &operator[](int index);
};

第7590题

请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:

The square’s area is 4

#include<iostream.h>
class Rectangle
{
    double W,H;
 public:
     Rectangle(double w=0,h=0){W=w;H=h;}
     void show(){cout<<W*H;}
 };
 class Square:public Rectangle
 {
 public:
     Square(double r):__________(1)_________{};
     void show()
     {
         cout<<”The square’s area is “;
         _______________(2)___________;
      }
  };
  void main()
  {
      Square s(2);
      s.show();
   }
第7591题

下面程序中A是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

B1 called

B2 called

#include<iostream.h>
class A
{
public:
______________(1)____________;
};
class B1:public A
{
public:
void display()
{cout<”B1 called”<<endl};
};
class B2:public A
{
public:
void display()
{cout<<”B2 called”<<endl};
};
void show(_________(2)________)
{
p->display();
}
void main()
{
B1 b1;
B2 b2;
A* p[2]={&b1,&b2};
for(int i=0;i<2;i++)
show(p[i]);
}
第7592题

请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为8。

#include<iostream.h>
template<class T>
void fun(___________(1)___________)
{
t=x[0];
for(int i=0;i<n;i++)
if(a[i]>t)
t=a[i];
}
void main()
{
int a[]={3,5,8,4,6};
int m;
fun(a,5,__(2)____);
cout<<m<<endl;
}
第7593题

下面程序通过把类Distance声明为类Point的友元类来实现计算两点之间的距离。请在下面程序的横线处填上适当字句,以使程序完整。

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
double X,Y;
public:
Point(double x,double y)
{X=x;Y=y;}
____________(1)____________;
};
class Distance
{
public:
double Dis(Point& p1,Point& p2);
{
double t;
____________________(2)___________________;
return t;
}
}
void main()
{
Point p(10,10),q(20,20);
Distance d;
cout<<d.Dis(p,q)<<endl;
}
第7594题

程序阅读题

1、

#include <iostream>
using namespace std;
int& element(int x[],int index)
{
return x[index+1];
}
void main()
{
int a[]={3,7,2,1,5};
element(a,3)=9;
for(int i=0;i<5;i++)
cout<<a[i]<<’’;
}

2、

#include<iostream>
using namespace std;
class A
{
int num;
public:
A()
{
num=0;cout<<"A default constructor"<<endl;}
A(int n)
{num=n;cout<<”A constructor,num=”<<num<<endl;}
~A()
{cout<<"A destructor,num="<<num<<endl;}
};
void main()
{
A a,*p;
p=new A(2);
a=*p;
delete p;
cout<<”Exiting main”<<endl;
}

3、

#include<iostream>
using namespace std;
class Format
{
public:
virtual void header()
{cout<<"This is a head"<<endl;
}virtual void footer()
{cout<<"This is a footer"<<endl;}
virtual void body()
{cout<<"This is a body"<<endl;}
void display()
{header();body();footer();}
};
class MyFormat:public Format
{
public:
void header()
{cout<<"This is my header"<<endl;}
void footer()
{cout<<"This is my footer"<<endl;}
};
void main()
{
Format * p;
p=new Format;
p->display();
delete p;
p=new MyFormat;
p->display();
delete p;
}

4、从键盘输入8 4 3 1 9 6 1 1 2 5,给出程序的输出结果。

#include<iostream>
#include<vector>
#include<stack>
#include<iterator>
using namespace std;
main()
{
vector<int> v;
stack<int> s;
int i,n,x;
cin>>n;
for(i=0;i<n;i++)
{
cin>>x;
v.push_back(x);
}
for(vector<int>::iterator p=v.begin();p!=v.end();p++)
if(*p%2==0)
s.push(*p);
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
}

5、

#include<iostream>
#include<exception>
using namespace std;
class Fract
{
int den,num;
public:
Fract(int d=0,int n=1)
{
if(n==0)
throw exception("divided by zero");
den = d;num=n;
}
int& operator[](int index)
{
if(index<0||index>1)
throw exception("index out of range");
if(index==0)
return den;
if(index==1)return num;
}
void show()
{ cout<<den<<"/"<<num<<endl;}
};
void main()
{
try
{
Fract f(0,2);
f.show();
cout<<f[1]<<"/"<<f[2]<<endl;
}
catch(exception e)
{
cout<<e.what()<<endl;
}
}
第7595题

请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:Hello

hELLO

#include <iostream.h>
#include <string.h>
class Words
{char *str;
public:
Words(char *s)
{str=new char[strlen(s)+1];
strcpy(str,s);
}
void disp(){cout<<str<<endl;}
char operator[](int i)
{if(str[i]>='A'&&str[i]<='Z')
return char(str[i]+32);
else if(str[i]>='a'&&str[i]<='z')
return char(str[i]-32);
else 
return str[i];
}
};
int main()
{int i;char *s="Hello";
Words word(s);
______________(1)____________;
for (i=0;i<strlen(s);i++)
______________(2)____________;
cout<<endl;
}
第7596题

利用函数模板,设计求一个数组元素之和的函数sum和两个数组元素之和的函数sum,请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

数组a之和:15

数组b之和:55

两数组之和:70

#include <iostream.h>
template <class T>
T sum (T a[],int n)
{int i;
T s=0;
for(i=0;i<n;i++)
s+=a[i];
return s;
}
template <class T>
T sum(______________(1)____________)
{
return ______________(2)____________;
}
int main()
{int a[5]={1,2,3,4,5};
int b[10]={1,2,3,4,5,6,7,8,9,10};
int s1=sum(a,5);
int s2=sum(b,10);
int s3=sum(a,5,b,10);
cout<<"数组a之和:"<<s1<<endl;
cout<<"数组b之和:"<<s2<<endl;
cout<<"两数组之和:"<<s3<<endl;
}
第7597题

请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为

(1,2)

5,6

(6,9)

#include <iostream.h>
class A
{
public:
A(int i,int j){a=i;b=j;}
void move(int x,int y){ ____(1)______;b+=y;}
void show(){cout<<"("<<a<<","<<b<<")"<<endl;}
private:
int a,b;
};
class B:private A
{
public:
B(int i,int j,int k,int l):A(i,j){x=k;y=l;}
void show(){cout<<x<<","<<y<<endl;}
void fun(){move(3,5);}
void fl(){____(2)______;}
private:int x,y;
};
int main()
{A e(1,2);
e.show();
B d(3,4,5,6);
d.fun();
d.show();
d.fl();
}
第7598题

请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:

10+10i

#include <iostream.h>
class complex
{
int x,y;
public:complex(){}
complex(int i,int j)
{x=i; y=j;}
complex operator+(complex &p)
{________(1)_______;}
void show()
{cout<<x<<"+"<<y<<"i";}
};
int main()
{complex p1(5,8),p2(5,2),p3;
________(2)_______;
p3.show();
第7599题

程序阅读题

1、

#include <iostream.h>
class B
{
public:
B(){}
B(int i,int j){a=i;b=j;}
void printb()
{cout<<"a="<<a<<",b="<<b<<endl;}
private:
int a,b;
};
class A
{
B c;
public:
A(){}
A(int i,int j):c(i,j){}
void printa(){c.printb();}
};
int main()
{A a(7,8);
a.printa();
}

2、

#include <iostream.h>
class CArray
{int *m_pArray;
int m_iSize;
public:
CArray(int iArray[],int iSize)
{m_pArray=iArray;
m_iSize=iSize;
}
int GetSize()
{return m_iSize;}
int &operator[](int i)
{return m_pArray[i];}
};
int main()
{int s[]={3,7,2,1,5};
CArray oArray(s,5);
oArray[0]=9;
for(int i=0;i<5;i++)
cout<<oArray[i]<<" ";
cout<<endl;
}

3、

#include <iostream.h>
#include <math.h>
template <class T>
class TAdd
{T x,y;
public:
TAdd(T a,T b){x=a,y=b;}
T add(){return x+y;}
};
int main()
{TAdd<int> A(5,6);
TAdd<double> B(2.4,5.8);
cout<<"s1="<<A.add()<<endl;
cout<<"s2="<<B.add()<<endl;
}

4、

#include <iostream.h>
class base
{
public:
void who(){cout<<"base class"<<endl;}
};
class derive1:public base
{
public:
void who(){cout<<"derive1 class"<<endl;}
};
class derive2:public base
{
public:
void who(){cout<<"derive2 class"<<endl;}
};
int main()
{base obj1,*p;
derive1 obj2;
derive2 obj3;
p=&obj1;
p->who();
p=&obj2;
p->who();
p=&obj3;
p->who();
obj2.who();
obj3.who();
}

5、

#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
double area(double a, double b, double c) {
if (a <= 0 || b <= 0 || c <= 0)
throw invalid_argument("the side length should be positive");
if (a + b <= c || b + c <= a || c + a <= b)
throw invalid_argument("the side length should fit the triangle inequality");
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main() {
double a, b, c;cin >> a >> b >> c; //输入1 2 4
try {
double s = area(a, b, c);
cout << "Area: " << s << endl;
} catch (exception &e) {
cout << "Error: " << e.what() << endl;
}
}
第7600题

请补充fun函数,该函数的功能是:计算N×N维矩阵元素的方差,结果由函数返回。维数在主函数中输入。
例如:

矩阵

的计算结果是14.414。
求方差的公式为:

公式

其中

公式

注意:
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横
线上填入所编写的若干表达式或语句。

/**********code.c**********/
#include <stdio.h>
#include <stdlib.h>
#define N 20
/**********found**********/
double fun(①______,int n)
{
int i,j;
double s=0.0;
double f=0.0;
double aver=0.0;
double sd=0.0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
s+=a[i][j];
/**********found**********/
aver=②______;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
f+=(a[i][j]-aver)*(a[i][j]-aver);
f/=(n*n);
/**********found**********/
sd=③______;
return sd;
}
main()
{
int a[N][N];
int n;
int i,j;
double s;
printf("*****Input the dimension of array n******\n");
scanf("%d",&n);
printf("*****The array*****\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=rand()%50;
while (a[i][j]==0)
a[i][j]=rand()%60;
printf("%4d",a[i][j]);
}
printf("\n\n");
}
s=fun(a,n);
printf("*****THE RESULT*****\n");
printf("%4.3f\n",s);
}