版本5和6间的区别
于2005-12-18 01:53:34修订的的版本5
大小: 7813
编辑: czk
备注:
于2005-12-18 01:58:32修订的的版本6
大小: 13264
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 318: 行号 318:
 1. {{{
13. 实现字符串类string,声明如下
 1. {{{#!cplusplus
实现字符串类string,声明如下
行号 343: 行号 343:
 1. {{{#!cplusplus
实现完整的复数运算,复数类的声明如下:
class complex {
public:
    complex(double r = 0.0, double i= 0.0);
    complex operator+();
    complex operator-();
    complex operator+=(complex);
    complex operator-=(complex);
    complex operator*=(complex);
    bool operator!(); //非0返回假,0返回真
    double abs(); //取模
    double arg(); //取角度
    complex conj(); //共轭
    double &real(); //实部
    double &imag(); //虚部
private:
    double real_, imag_;
};
ostream &operator<<(ostream &os, complex);
istream &operator>>(istream &is, complex &);
complex operator+(complex a, complex b);
complex operator*(complex a, complex b);
complex operator-(complex a, complex b);
complex operator==(complex a, complex b);
complex operator!=(complex a, complex b);
}}}
 1. {{{#!cplusplus
实现Stack类,提供push(把一个元素放入栈中),pop(把栈中最后一个元素删去),top(取栈中最后一个元素)。请实现Stack类,使其能够得到正确的运行结果。
#include <iostream>
using namespace std;
int main() {
    Stack s1(5);
    for(int i = 0; i < 3; i++) s1.push(i);
    Stack s2(s1);
    while(s1.size() > 0) {
        cout << s1.top() << endl;
        s1.pop();
    }
    s1 = s2;
    for(int i = 3; i < 5; i++) s1.push(i);
    while(s1.size() > 0) {
        cout << s1.top() << endl;
        s1.pop();
    }
}
}}}
 1. {{{#!cplusplus
实现队列Queue类,提供enqueue(把一个元素放在队列的末尾中),dequeue(把队列中第一个元素删去),front(取队列中第一个元素的值),back(取队列最后一个元素的值)。请实现Queue类,使其能够得到正确的运行结果。
#include <iostream>
using namespace std;
int main() {
    Queue s1(5);
    for(int i = 0; i < 3; i++) s1.enqueue(i);
    Queue s2(s1);
    while(s1.size() > 0) {
        cout << s1.front() << endl;
        s1.dequeue();
    }
    s1 = s2;
    for(int i = 3; i < 5; i++) s1.enqueue(i);
    while(s1.size() > 0) {
        cout << s1.front() << endl;
        s1.dequeue();
    }
}
}}}
 1. {{{#!cplusplus
17. 财务软件
Account为账户,AccountList为账户的集合,两个类的声明如下,请给出这两个类的实现。
class Account {
public:
    Account(string name, int type, double amount);
    string &name();
    int &type();
    double &amount();
private:
    string name_;
    int type_;
    double amount_;
};
class AccountList {
public:
    AccountList(int capacity);
    void AddAccount(Account account);
    void RemoveAccount(string name);
    int Find(string name);
    Account &At(int i);
private:
    int capacity_;
    int size_;
    Account *accounts;
};
}}}
 1. {{{#!cplusplus
学校的人事关系
学校里面有四类人:人(Person),学生(Student),教师(Teacher),助教(Assistant)。其中助教是一类特殊的人,他们是教师,同时也是学生。请完成以下程序,使之能得到正确运行结果。
#include <iostream>
#include <string>
using namespace std;
int main() {
    Person *p[4];
    p[0] = new Person("jack");
    p[1] = new Student("rose", "12345");
    p[2] = new Teacher("czk", "lecturer");
    p[3] = new Assistant("oldbig", "23456", "assistant");
    for(int i= 0; i < 4; i++) {
        p[i] -> print();
        cout << endl;
    }
}
}}}
 1. {{{#!cplusplus
编写Clock类
编写一个时钟类Clock,并实现运算符++(时间增加1秒),--(时间减1秒),-(计算两个时间差多少秒),+(计算过一定秒数以后的新时间,秒数一定大于0),Clock类的声明写在clock.h中,Clock类成员函数的定义写在clock.cpp中。
main.cpp:
#include <iostream>
using namespace std;
#include "clock.h"
int main() {
    Clock c1 ( 8, 8, 8 ); //8点8分8秒
    Clock c2 ( 8, 8 ); //8点8分0秒
    Clock c3 ( 8 ); // 8点整
    cout << c3++ << endl;
    cout << ++c3 << endl;
    cout << c1-- << endl;
    cout << --c1 << endl;
    cout << c1 - c2 << endl;
    cout << c2 + 8 << endl;
    return 0;
}
}}}
 1. {{{#!cplusplus
编写一个计算各种图形面积的程序。
下列shape类是一个表示图形的抽象类,area( )为求图形面积的函数,total( )则是一个通用的用以求不同形状的图形面积总和的函数。请从shape类派生三角形类(triangle)、矩形类(rectangle)、圆形类(circle),并给出具体的求面积函数,使以下程序能够运行并得到正确结果。每个类都写在不同的文件里面,类的成员函数的定义写在类的声明里面。
main.cpp的内容:
  #include <iostream>
  using namespace std;
  #include "shape.h"
  #include "triangle.h"
  #include "circle.h"
  #include "rectangle.h"
  
  double total(shape *s[ ],int n) {
      double sum = 0.0;
   for(int i=0;i<n;i++)
       sum += s[i]->getarea();
      return sum;
  }
  int main() {
      shape *shapes[3];
      shapes[0] = new circle(3.0); //半径3.0
      shapes[1] = new rectangle(2.0, 3.0); //长和宽分别为2.0和3.0
      shapes[2] = new triangle(2.0, 3.0, 3.14159/2.0);
      //三角形两边长分别为2.0和3.0,这两边的夹角的弧度为3.14159/2.0
      cout << total(shapes, 3) << endl; //输出这三个图形的面积总和
      return 0;
  }
}}}

找出以下程序中的语法错误,并作出解释。

  1.    1 #include <stdlib.h>
       2 int main() {
       3     const int size;
       4     size = 10;
       5     int a[size];
       6     int *p1 = new int[size];
       7     int *p2 = malloc(sizeof(int)* size);
       8     int *p3 = new int(size);
       9     for(int i = 0; i < size; i++) {
      10         a[i] = i;
      11         p1[i] = a[i] * i;
      12         p2[i] = p1[i] * i;
      13         p3[i] = p2[i] * i;
      14     }
      15     int sum;
      16     for(int i = 0; i < size; i++)
      17         sum += a[i] + p1[i] + p2[i] + p3[i];
      18     cout << sum;
      19     delete p1;
      20     delete [] p2;
      21     delete p3;
      22 }
    
  2.    1 #include <iostream>
       2 using namespace std;
       3 class N {
       4     static int n = 0;
       5     int i;
       6 public:
       7     void N() : i(0) { }
       8     void N(int a = 0) : i (a) { }
       9     void N(int a = 0, int b) : i(a), n(b) { }
      10     int get() const { return i++; }
      11     void set(int a) { i = a;}
      12     void print() const { cout << get() << endl;}
      13     static int getn() const { return n; }
      14     static void setn(int a) { n = a; i++; }  
      15 }
      16 int main() {
      17     cout << N::n << endl;
      18     cout << N::getn() << endl;
      19     cout << get() << endl;
      20     cout << N::get() << endl;
      21     cout << N::setn(10) << endl;
      22     N *p = new N(5);
      23     N n();
      24     n.set(10);
      25     n.setn(11);
      26     p.set(5);
      27     delete p;
      28 }
    
  3.    1 #include <string>
       2 #include <iostream>
       3 using namespace std;
       4 class S {
       5 public:
       6     S(int a0) { cout << a0 << endl; }
       7     S(double a0, int b0) : d(a0), b(b0){ cout << a0 << b0 << endl; }
       8     S(char *p0) : d(a), b(2) { cout << p << endl;} 
       9     S(int &a0) : a(0), b(1), c("abc"), d(r), p(new int) { }
      10 private:
      11     int a;
      12     const int b;
      13     string c;
      14     int &d;
      15     int *p;
      16 };
      17 int main() {
      18     S a(1);
      19     S b;
      20     S c(0.0, 1);
      21     S d("hello");
      22     S e(true);
      23     S f(a);
      24     e = a;
      25 }
    
  4.    1 #include <string>
       2 #include <iostream>
       3 using namespace std;
       4 class Person{
       5 public:
       6     Person(string name) : name_ (name) { }
       7     virtual void print() const = 0 { cout << name_; }
       8     virtual bool compare(Person p) const { return p.name_ == name_; }
       9 protected:
      10     string name_;
      11 };
      12 class Student : public Person {
      13     Student(string name, string id) { name_ = name; id_ = id; }
      14     Student(Person p, string id) { name_ = p.name_; id_ = id;}
      15     bool compare(Student s) const { return s.name_==name_ && s.id_ == id_; }
      16     void print() { cout << name_ << id_ << endl; }
      17 private:
      18     string id_;
      19 };
      20 
      21 int main() {
      22     Person p("Mike");
      23     Student s("jack", "12345");
      24     Student t(p, "23424");
      25     cout << s.compare(t);
      26     cout << p.compare(t);
      27     cout << s.compare(p);
      28     s.print();
      29     s.Person::print();
      30 }
    
  5.    1 class Integer{
       2 public:
       3     Integer(int a = 0) : i(a) {}
       4     Integer operator+(int a, int b) { return a + b;}
       5     Integer operator-() { return -i; }
       6     Integer operator+() { return i; }
       7     Integer operator++() { return ++i; }
       8     Integer operator++(int) { return i++; }
       9     bool operator<>(int a) { return i != a; }
      10 private:
      11     int i;
      12 };
      13 void operator>>(ostream &os, Integer i) {
      14     os << i;
      15 }
      16 istream & operator<<(istream &is, Integer i) {
      17     is >> i.i;
      18     return is;
      19 } 
      20 Integer operator-(Integer a, Integer b) {
      21     return a.i - b.i;
      22 }
      23 int main() {
      24     Integer i(-1);
      25     Integer j;
      26     cin >> j;
      27     i = -i;
      28     i = i - j;
      29     i = i + 10;
      30     cout << i;
      31 }
    

读程序,写出程序运行的结果

  1.    1 #include <iostream>
       2 using namespace std;
       3 int inc( int &a) {
       4     return ++a;
       5 }
       6 int dec( int a) {
       7     return --a;
       8 }
       9 int main() {
      10     int b = 10;
      11     cout << inc(b) << endl;
      12     cout << dec(b) << endl;
      13     cout << inc(b) << endl;
      14 }
    
  2.    1 #include <iostream>
       2 #include <string>
       3 using namespace std;
       4 int f( string a );
       5 int f( int b , int c = 1);
       6 int f( bool b);
       7 int main() {
       8     string s = "";
       9     cout << f(s) << endl;
      10     cout << f('\060') << endl;
      11     cout << f(1, 2) << endl;
      12     cout << f(false) << endl;
      13     cout << f(0) << endl;
      14 }
      15 int f( string a) {
      16     return a.length();
      17 }
      18 int f( int b, int c) {
      19     return b * c + b + c + 1;
      20 }
      21 int f(bool b) {
      22     return b?1:-1;
      23 }
    
  3.    1 #include <iostream>
       2 using namespace std;
       3 class Z {
       4 public:
       5     Z(int i) : id(i) { cout << id << " created" << endl; }
       6     ~Z() { cout << id << " destroyed" << endl; }
       7     Z(Z &z) : id(z.id) { cout << id << " created a copy" << endl; }
       8 private:
       9     int id;
      10 };
      11 Z z(0);
      12 int main() {
      13     cout << "main start" << endl;
      14     Z z1(1);
      15     Z z2(2);
      16     Z *z3 = new Z(3);
      17     Z z4(z1); 
      18 }
    
  4.    1 #include <iostream>
       2 using namespace std;
       3 class X {
       4 public: 
       5     X(int a) : x(a) { cout << "create X:" << x << endl; }
       6     ~X() { cout << "destroy X:"<< x << endl; }
       7 private:
       8     int x;
       9 };
      10 class Y : public X{
      11 public:
      12     Y() : x(1), X(2) { cout << "create Y" << endl; }
      13     ~Y() { cout << "destroy Y" << endl; }
      14 private:
      15     X x;
      16 };
      17 
      18 int main() {
      19     X x(5);
      20     Y  y;
      21 }
    
  5.    1 #include <iostream>
       2 using namespace std;
       3 class B{
       4 public:
       5     virtual void f() { cout << "B::f() "; }
       6     void g() { cout << "B::g() "; }
       7     virtual void h() { f(); }    
       8 };
       9 class D : public B {
      10 public:
      11     void f() { cout << "D::f() "; }
      12     void g() { cout << "D::g() "; }
      13 };
      14 int main() {
      15     B b;
      16     b.f(); b.g(); b.h();
      17     cout << endl;
      18     B *p = &b;
      19     p->f(); p->g(); p->h();
      20     cout << endl;
      21     B *p = new D;
      22     p->f(); p->g(); p->h();
      23     cout << endl;
      24     B &r = *p;
      25     r.f(); r.g(); r.h();
      26     cout << endl;
      27     delete p;
      28 }
    
  6.    1 #include <iostream>
       2 using namespace std;
       3 class B{
       4 public:
       5     B () { cout << "create B" << endl; }
       6     virtual ~B() { cout << "destroy B" << endl; }
       7     virtual void f() { cout << "B::f()" << endl; }
       8     virtual void f(int a) { cout << "B::f(int)" << endl; }
       9 };
      10 class D : public B {
      11 public:
      12     D() { cout << "create D" << endl; }
      13     virtual ~D() { cout << "destroy  D" << endl; }
      14     virtual void f() { cout << "D::f()" << endl; }
      15     virtual void f(bool a) { cout << "D::f(bool)" << endl; }
      16 };
      17 int main() {
      18     B *p = new B;
      19     p->f();
      20     p->f(0);
      21     delete p;
      22     p = new D;
      23     p->f();
      24     p->f(0);
      25     delete p;
      26     D *d = new D;
      27     d->f();
      28     d->f(0);
      29     delete d;
      30 }
    
  7.    1 #include <iostream>
       2 using namespace std;
       3 class D {
       4 public:
       5     D() { cout << "D created" << endl; }
       6     D(D&) { cout << "D created a copy" << endl; }
       7     ~D() { cout << "D destroyed" << endl; }
       8     D& operator=(D&) { cout << "D assigned" << endl; return *this; }
       9     void operator++() { cout << "operator++" << endl; }
      10     void operator++(int) { cout << "operator++ with int" << endl; }
      11 };
      12 int main() {
      13     D d1;
      14     D d2(d1);
      15     d1 = d2;
      16     ++d1;
      17     d2++;
      18 }
    

程序填空题

编程题,按要求编写程序

  1.    1 实现字符串类string声明如下
       2 class string {
       3 public:
       4         string(const char *str = "");           // 普通构造函数
       5         string(const string &other);            // 拷贝构造函数
       6         ~string();                                          // 析构函数
       7         string & operator = (const string &other);      // 赋值函数
       8     bool operator==(const string &s);
       9     bool operator!=(const string &s);
      10     bool operator<(const string &s);
      11     bool operator>(const string &s);
      12     bool operator<=(const string &s);
      13     bool operator>=(const string &s);
      14     string operator +(const string &s); //连接
      15     string operator +=(const string &s);
      16     int length() const;
      17     bool empty() const;
      18         char* c_str()   { return m_data; }
      19 private:
      20         char *m_data;                               // 用于保存字符串
      21 };
      22 ostream &operator<<(ostream &os, const string &s);
      23 istream &operator>>(istream &is, string &s);
    
  2.    1 实现完整的复数运算复数类的声明如下
       2 class complex {
       3 public:
       4     complex(double r = 0.0, double i= 0.0);
       5     complex operator+();
       6     complex operator-();
       7     complex operator+=(complex);
       8     complex operator-=(complex);
       9     complex operator*=(complex);
      10     bool operator!(); //非0返回假,0返回真
      11     double abs(); //取模
      12     double arg(); //取角度
      13     complex conj(); //共轭
      14     double &real(); //实部
      15     double &imag(); //虚部
      16 private:
      17     double real_, imag_;
      18 };
      19 ostream &operator<<(ostream &os, complex);
      20 istream &operator>>(istream &is, complex &);
      21 complex operator+(complex a, complex b);
      22 complex operator*(complex a, complex b);
      23 complex operator-(complex a, complex b);
      24 complex operator==(complex a, complex b);
      25 complex operator!=(complex a, complex b);
    
  3.    1 实现Stack类提供push把一个元素放入栈中),pop把栈中最后一个元素删去),top取栈中最后一个元素)。请实现Stack类使其能够得到正确的运行结果
       2 #include <iostream>
       3 using namespace std;
       4 int main() {
       5     Stack s1(5);
       6     for(int i = 0; i < 3; i++) s1.push(i);
       7     Stack s2(s1);
       8     while(s1.size() > 0) {
       9         cout << s1.top() << endl;
      10         s1.pop();
      11     }
      12     s1 = s2;
      13     for(int i = 3; i < 5; i++) s1.push(i);
      14     while(s1.size() > 0) {
      15         cout << s1.top() << endl;
      16         s1.pop();
      17     }
      18 }
    
  4.    1 实现队列Queue类提供enqueue把一个元素放在队列的末尾中),dequeue把队列中第一个元素删去),front取队列中第一个元素的值),back取队列最后一个元素的值)。请实现Queue类使其能够得到正确的运行结果
       2 #include <iostream>
       3 using namespace std;
       4 int main() {
       5     Queue s1(5);
       6     for(int i = 0; i < 3; i++) s1.enqueue(i);
       7     Queue s2(s1);
       8     while(s1.size() > 0) {
       9         cout << s1.front() << endl;
      10         s1.dequeue();
      11     }
      12     s1 = s2;
      13     for(int i = 3; i < 5; i++) s1.enqueue(i);
      14     while(s1.size() > 0) {
      15         cout << s1.front() << endl;
      16         s1.dequeue();
      17     }
      18 }
    
  5.    1 17.     财务软件
       2 Account为账户AccountList为账户的集合两个类的声明如下请给出这两个类的实现
       3 class Account {
       4 public:
       5     Account(string name, int type, double amount);
       6     string &name();
       7     int &type();
       8     double &amount();
       9 private:
      10     string name_;
      11     int type_;
      12     double amount_;
      13 };
      14 class AccountList {
      15 public:
      16     AccountList(int capacity);
      17     void AddAccount(Account account);
      18     void RemoveAccount(string name);
      19     int Find(string name);
      20     Account &At(int i);
      21 private:
      22     int capacity_;
      23     int size_;
      24     Account *accounts;
      25 };
    
  6.    1 学校的人事关系
       2 学校里面有四类人Person),学生Student),教师Teacher),助教Assistant)。其中助教是一类特殊的人他们是教师同时也是学生请完成以下程序使之能得到正确运行结果
       3 #include <iostream>
       4 #include <string>
       5 using namespace std;
       6 int main() {
       7     Person *p[4];
       8     p[0] = new Person("jack");
       9     p[1] = new Student("rose", "12345");
      10     p[2] = new Teacher("czk", "lecturer");
      11     p[3] = new Assistant("oldbig", "23456", "assistant");
      12     for(int i= 0; i < 4; i++) {
      13         p[i] -> print();
      14         cout << endl;
      15     }
      16 }
    
  7.    1 编写Clock类
       2 编写一个时钟类Clock并实现运算符++(时间增加1秒),--(时间减1秒),-(计算两个时间差多少秒),+(计算过一定秒数以后的新时间秒数一定大于0),Clock类的声明写在clock.h中Clock类成员函数的定义写在clock.cpp中
       3 main.cpp:
       4 #include <iostream>
       5 using namespace std;
       6 #include "clock.h"
       7 int main() {
       8     Clock c1 ( 8, 8, 8 ); //8点8分8秒
       9     Clock c2 ( 8, 8 ); //8点8分0秒
      10     Clock c3 ( 8 );  // 8点整
      11     cout  << c3++ << endl;
      12     cout <<  ++c3 << endl;
      13     cout << c1-- << endl;
      14     cout << --c1 << endl;
      15     cout << c1 - c2 << endl;
      16     cout << c2 + 8 << endl;
      17     return 0;
      18 }
    
  8.    1 编写一个计算各种图形面积的程序
       2 下列shape类是一个表示图形的抽象类area( )为求图形面积的函数total( )则是一个通用的用以求不同形状的图形面积总和的函数请从shape类派生三角形类(triangle)、矩形类rectangle)、圆形类circle),并给出具体的求面积函数使以下程序能够运行并得到正确结果每个类都写在不同的文件里面类的成员函数的定义写在类的声明里面
       3 main.cpp的内容:
       4   #include <iostream>
       5   using namespace std;
       6   #include "shape.h"
       7   #include "triangle.h"
       8   #include "circle.h"
       9   #include "rectangle.h"
      10   
      11   double total(shape *s[ ],int n) {
      12       double sum = 0.0;
      13           for(int i=0;i<n;i++)
      14               sum += s[i]->getarea();
      15       return sum;
      16   }
      17   int main() {
      18       shape *shapes[3];
      19       shapes[0] = new circle(3.0); //半径3.0
      20       shapes[1] = new rectangle(2.0, 3.0); //长和宽分别为2.0和3.0
      21       shapes[2] = new triangle(2.0, 3.0, 3.14159/2.0); 
      22       //三角形两边长分别为2.0和3.0,这两边的夹角的弧度为3.14159/2.0
      23       cout << total(shapes, 3) << endl; //输出这三个图形的面积总和
      24       return 0;
      25   }
    

C++练习 (2008-06-27 10:53:51由218编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.