2011年复试题目

1. 编写一个程序,利用如下的公式计算 e x e^x 的值,精确到 1 0 10 10^{-10}

其中 e x = 1 + x 1 ! + x 2 ! + x 3 ! + x 4 ! + e^x = 1 + \frac{x}{1!} + \frac{x}{2!} + \frac{x}{3!} + \frac{x}{4!} + ···

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    int x;
    double term = 1.0;
    double exp = 1.0;
    int i = 1;
    cout << "Enter a num: ";
    cin >> x;
    while(term > 1e-10)
    {
        term = term * x/i;
        exp += term;
        i++;
    }
    cout << fixed << setprecision(10) << exp << endl;
}

2. 编写一个程序,利用如下公司计算 π \pi 的值,要求小数点的位数可以达到计算机可以表示的最大范围。

π = 4 4 3 + 4 5 4 7 + 4 9 4 11 + \pi = 4 - \frac{4}{3} + \frac{4}{5} -\frac{4}{7} + \frac{4}{9} - \frac{4}{11} + ···

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double term = 1;
    double pi = 0.0;
    int size = sizeof(double) * 8;
    for(int i = 1; fabs(term) >= pow(2.0,-1*size);i++)
    {
        cout << i << "   value is  " << pi << endl;
        term = 4.0/(2*i - 1);
        if(i%2 == 0)
            term *= -1;
        pi += term;
    }
    cout << fixed << setprecision(10) << pi << endl;
}

3. 编写一个递归函数模板,从一个数组中找出最小值,并返回该值的数组元素下标。

template<class T>
int selectSmallIndex( const T str[] , int n )
{
    int small = 0;  //small作为返回值,可以不用静态变量
    if(n == 1)
        small = 0;
    else
    {
        small = selectSmallIndex(str,n - 1);
        if(str[small] > str[n - 1])
            small = n - 1;
    }
    return small;
}
template<typename T>
int recursionSmallIndex(const T a[], int low, int high)
{
    static int small = 0;
    if ( a[ low ] < a[small] )
       small =  low ;
    return low == high ?
       small : recursionSmallIndex(a, low + 1, high );
}

4. 编写两个函数 SortOne 和 SortTwo,分别对字符串数组实现插入排序和选择排序。

#include <iostream>
#include <string>
#include <cstring>
//编写两个函数 SortOne 和 SortTwo,分别对字符串数组实现插入排序和选择排序。

using namespace std;
void swapStr(char *,char *);
void Sortone(char [][20],int);
void Sorttwo(char [][20],int);


int main()
{
    char str[10][20];
    cout << ".输入数组:" << endl;
    for(int i = 0;i < 10; i++)
        cin >> &str[i][0];
    cout << ".输出数组:" << endl;
    for(int i = 0; i < 10;i ++)
    {
        cout << &str[i][0] << " ";
    }
    cout << endl;
    Sorttwo(str,10);
    cout << ".输出排序后的数组:" << endl;
    for(int i = 0; i < 10;i ++)
    {
        cout << &str[i][0] << " ";
    }
    return 0;

}

void Sortone(char str[][20],int size)
{
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < i; j++)
            if(strcmp(&str[j][0],&str[i][0])>0)
                swapStr(&str[j][0],&str[i][0]);
    }
}

void Sorttwo(char a[][20],int size)
{
    int smallIndex ;
    for(int i = 0; i < size; i++)
    {
        smallIndex = i;
        for(int j = i; j < size; j++)
        {
            if(strcmp(&a[smallIndex][0],&a[j][0]) > 0)
                smallIndex = j;
        }
        swapStr(&a[i][0],&a[smallIndex][0]);
    }
}

void swapStr(char *a, char *b)
{
    char temp[20];
    strcpy(temp,a);
    strcpy(a,b);
    strcpy(b,temp);
}

5. 对于一个数组 Array 类的 chess 对象,通过调用运算符重载函数(),可实现 chess[row][column],请完成:

  1. Array 类的基本定义,包括构造函数、析构函数、拷贝构造函数和基本数据成员;
  2. 运算符重载函数()的 定义。

Chess.h:

#ifndef CHESS_H
#define CHESS_H

#include <iostream>
using namespace std;

class Chess
{
   friend ostream &operator<<(ostream &, const Chess & );
   friend istream &operator>>( istream &, Chess & );

public:
   Chess( int = 3, int = 3 );
   Chess( const Chess & );
   ~Chess();
   int getRowSize() const;
   int getColumnSize() const;
   const Chess &operator=(const Chess & );
   bool operator==( const Chess & ) const;
   bool operator!=( const Chess &right ) const
   {
      return !( *this == right );
   }
   int &operator()( int, int );
   int operator()( int, int ) const;
private:
   int rowSize;
   int columnSize;
   int *ptr;
};
#endif

Chess.cpp:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "Chess.h"
using namespace std;

Chess::Chess(int row, int cloumn )
{
   rowSize = ( row > 0 ? row : 3 );
   columnSize = ( cloumn > 0 ? cloumn : 3 );
   ptr = new int[ rowSize * columnSize ];
   for ( int i = 0; i < rowSize * columnSize; i++ )
      ptr[ i ] = 0;
}

Chess::Chess(const Chess &arrayToCopy )
:rowSize( arrayToCopy.rowSize ), columnSize( arrayToCopy.columnSize )
{
   ptr = new int[ rowSize * columnSize ];
   for ( int i = 0; i < rowSize * columnSize; i++ )
      ptr[ i ] = arrayToCopy.ptr[ i ];
}

Chess::~Chess()
{
   delete [] ptr;
}

int Chess::getRowSize() const
{
   return rowSize;
}

int Chess::getColumnSize() const
{
   return columnSize;
}

const Chess &Chess::operator=(const Chess &right )
{
   if ( &right != this )
   {
      if ( rowSize * columnSize != right.rowSize * right.columnSize )
      {
         delete [] ptr;
         rowSize = right.rowSize;
         columnSize = right.columnSize;
         ptr = new int[ rowSize * columnSize ];
      }

      for ( int i = 0; i < rowSize * columnSize; i++ )
         ptr[ i ] = right.ptr[ i ];
   }
   return *this;
}

bool Chess::operator==(const Chess &right ) const
{
   if ( rowSize * columnSize != right.rowSize * right.columnSize )
      return false;
   for ( int i = 0; i < rowSize * columnSize; i++ )
      if ( ptr[ i ] != right.ptr[ i ] )
         return false;
   return true;
}

int &Chess::operator()(
   int row, int column )
{
   if ( ( row < 0 || row >= rowSize ) ||
      ( column < 0 || column >= columnSize ) )
   {
      cerr << "\nError: One or both subscripts out of range" << endl;
      exit( 1 );
   }
   return ptr[ ( row * columnSize ) + column ];
}

int Chess::operator()(
   int row, int column ) const
{
   if ( ( row < 0 || row >= rowSize ) ||
      ( column < 0 || column >= columnSize ) )
   {
      cerr << "\nError: One or both subscripts out of range" << endl;
      exit( 1 );
   }
   return ptr[ ( row * columnSize ) + column ];
}

istream &operator>>( istream &input, Chess &a )
{
   for ( int i = 0; i < a.rowSize * a.columnSize; i++ )
      input >> a.ptr[ i ];

   return input;
}

ostream &operator<<( ostream &output, const Chess &a )
{
   for ( int i = 0; i < a.rowSize; i++ )
   {
      for ( int i2 = 0; i2 < a.columnSize; i2++ )
         output << a.ptr[ ( i * a.columnSize ) + i2 ] << ' ';

      output << endl;
   }

   return output;
}

test.cpp

#include <iostream>
#include "Chess.h"
using namespace std;

int main()
{
   Chess chess1( 2, 4 );
   Chess chess2;
   cout << "Size of Chess chess1 is "
      << chess1.getRowSize() << " X " << chess1.getColumnSize()
      << "\nChess after initialization:\n" << chess1;
      
   cout << "\nSize of Chess chess2 is "
      << chess2.getRowSize() << " X " << chess2.getColumnSize()
      << "\nChess after initialization:\n" << chess2;

   cout << "\nEnter 17 integers:" << endl;
   cin >> chess1 >> chess2;

   cout << "\nAfter input, the Chesss contain:\n"
      << "chess1:\n" << chess1
      << "\nchess2:\n" << chess2 << endl;

   cout << "Evaluating: chess1 != chess2" << endl;

   if ( chess1 != chess2 )
      cout << "chess1 and chess2 are not equal" << endl;

   Chess chess3( chess1 );

   cout << "\nSize of Chess chess3 is "
      << chess3.getRowSize() << " X " << chess3.getColumnSize()
      << "\nChess after initialization:\n" << chess3;

   cout << "\nAssigning chess2 to chess1:" << endl;
   chess1 = chess2; 

   cout << "chess1:\n" << chess1
      << "\nchess2:\n" << chess2;

   cout << "\nEvaluating: chess1 == chess2" << endl;

   if ( chess1 == chess2 )
      cout << "chess1 and chess2 are equal" << endl;


   cout << "\nchess1( 1, 2 ) is " << chess1( 1, 2 );

   cout << "\n\nAssigning 1000 to chess1( 1, 2 )" << endl;
   chess1( 1, 2 ) = 1000;
   cout << "chess1:\n" << chess1;

   cout << "\nAttempt to assign 1000 to chess1( 15, 2 )" << endl;
   chess1( 15, 2 ) = 1000; 
} 

6. 定义一个具有多态性的基类 Shape,派生出三个类:

圆 Circle(坐标点和半径),矩形 Rec 类(两点不同坐标),三角形 Tri 类(三个不同坐标),每个类中至少有一个计算面 积的函数。编写程序,从文件 file.txt 中读取数据来创建各类的对象,并放在 Shape 指针向量中,最后循环处理每个对象并输出面积。
【假设 file.txt 中的数据如下: C:123,5,40;T:1,2,32,50,60,3;R:6,8,8,100】
shape.h

#ifndef SHAPE_H
#define SHAPE_H


class Shape
{
    public:
        Shape();
        virtual ~Shape();
        virtual double getArea()const  //虚函数,不是纯虚函数
        {
            return 0.0;
        }
};

#endif // SHAPE_H

shape.cpp

#include "Shape.h"

Shape::Shape()
{
    //ctor
}

Shape::~Shape()
{
    //dtor
}

Circle.h

#include "Shape.h"
#ifndef CIRCLE_H
#define CIRCLE_H


class Circle:public Shape
{
    public:
        Circle(int,int,int);
        virtual ~Circle();
        double getArea() const;

    private:
        int pX;
        int pY;
        int radius;
};

#endif // CIRCLE_H

Circle.cpp

扫描二维码关注公众号,回复: 5806151 查看本文章
#include "Circle.h"

Circle::Circle(int x,int y ,int r)
:pX(x),pY(y),radius(r)
{

}

double Circle::getArea() const
{
    return 3.14*radius*radius;
}
Circle::~Circle()
{
    //dtor
}

Rectanle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "Shape.h"


class Rectangle:public Shape
{
    public:
        Rectangle(int ,int,int,int);
        virtual ~Rectangle();
        double getArea() const;

    private:
        int p1x,p2x,p1y,p2y;
};

#endif // RECTANGLE_H

Recttangle.cpp

#include "Rectangle.h"
#include <cmath>

Rectangle::Rectangle(int x1,int y1,int x2,int y2)
:p1x(x1),p1y(y1),p2x(x2),p2y(y2)
{
    //ctor
}

double Rectangle::getArea() const
{
    return fabs((p1x - p2x) * (p1y - p2y));
}

Rectangle::~Rectangle()
{
    //dtor
}

Triangle.h

#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "Shape.h"


class Triangle:public Shape
{
    public:
        Triangle(int ,int, int, int, int, int);
        virtual ~Triangle();
        double getArea() const;

    private:
        int p1x,p1y;
        int p2x,p2y;
        int p3x,p3y;
};

#endif // TRIANGLE_H

Triangle.cpp

#include "Triangle.h"
#include <cmath>

Triangle::Triangle(int x1,int y1,int x2,int y2,int x3,int y3):
    p1x(x1),p1y(y1),p2x(x2),p2y(y2),p3x(x3),p3y(y3)
{
    //ctor
}

double Triangle::getArea() const
{
    double a = sqrt((double)((p1x-p2x) * (p1x-p2x) + (p1y - p2y) * (p1y - p2y)));
    double b = sqrt((double)((p1x-p3x) * (p1x-p3x) + (p1y - p3y) * (p1y - p3y)));
    double c = sqrt((double)((p2x-p3x) * (p2x-p3x) + (p2y - p3y) * (p2y - p3y)));
    double p = (a+b+c)/2;
    return sqrt(p * (p - a) * (p - b) * (p - c));
}

Triangle::~Triangle()
{
    //dtor
}

test.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Triangle.h"

using namespace std;

int main()
{
    Shape *shape;
    char temp[20];
    vector<Shape*> shapes;
    ifstream ifs("file.txt",ios::in);
    if(!ifs)
    {
        cerr << "The file can not opened!\n";
    }
    while(ifs&&!ifs.eof())
    {
        ifs.getline(temp,50,';');
        stringstream iss(temp);
        char ch;
        switch(temp[0])
        {
        case 'C':
            int x,y,z;
            iss >> ch >> ch;
            iss >> x >> ch >> y >> ch >> z;
            shape = new Circle(x,y,z);
            shapes.push_back(shape);
            break;
        case 'T':
            int x1,y1,x2,y2,x3,y3;
            iss >> ch >> ch;
            iss >> x1 >> ch >> y1 >> ch >> x2 >> ch >> y2 >> ch >> x3 >> ch >> y3;
            shape = new Triangle(x1,y1,x2,y2,x3,y3);
            shapes.push_back(shape);
            break;
        case 'R':
            iss >> ch >> ch;
            iss >> x1 >> ch >> y1 >> ch >> x2 >> ch >> y2;
            shape = new Rectangle(x1,y1,x2,y2);
            shapes.push_back(shape);
            break;
        }
    }
    vector<Shape*>::iterator i;
    for(i = shapes.begin(); i < shapes.end();i++)
        cout << (*i)->getArea() << endl;
}

猜你喜欢

转载自blog.csdn.net/Dcwjh/article/details/88648260
今日推荐