实验要求

推荐实验环境

实验内容

  1. 使用递归的方法编写Fibonacci数列(1 1 2 3 5 8 ……)程序。输入n,输出数列中第n个数的值
  2. 用结构体、函数实现复数操作(复数的加减乘除),使如下的主程序可以正确运行

       1 int main() {
       2     struct complex a, b, sum, diff, product;
       3     scanf("%f%f%f%f", &a.real, &a.imag, &b.real, &b.imag);
       4     sum = add(a, b);
       5     diff = substract(a, b);
       6     product = multiply(a, b);
       7     printf("sum:%f %f\n", sum.real, sum.imag);
       8     printf("diff:%f %f\n", diff.real, diff.imag);
       9     printf("product:%f %f\n", product.real, product.imag);
      10     return 0;
      11 }
    
  3. 使用标准C++输入输出实现在屏幕上用以下格式输出乘法表,要求排列整齐(不考虑屏幕宽度不够时造成的折行)。输入:乘法表的行数(1<=n<=99)输出:如下乘法表,总共n行

    1
    2    4
    3    6    9
    4    8   12   16
    5   10   15   20   25
    6   12   18   24   30   36
    ……
  4. Monty Hall游戏:该游戏来自电视节目Let's Make a Deal. 游戏中设有三扇门,其中一扇门后面是一辆汽车,另外两扇门后面各有一头山羊。玩家首先选择其中一扇门,然后节目主持人将另外两扇门中藏有山的那扇门打开,并给游戏参与者一个改选的机会。此时玩家可以维持原先的选择不变,也可以直接选择另一扇门,或者随机决定如何选择。最后玩家如果选中了那个藏有汽车的门,则获得胜利。请你写一个程序,计算游戏中玩家如何选择概率较高,不同的选择概率各是多少?
  5. 将前面实验中的复数程序用C++类重新实现
  6. 做一个存放整数的Stack栈类,包含如下成员函数:构造函数Stack(n):n指定栈中可以存放元素的最大个数;push(i):把元素i添加到栈尾部;pop():把尾部元素从栈中删除;top():取栈尾部的元素的值;size():取栈中间存放的元素个数;~Stack():析构函数(如果有必要的话);Stack(Stack & s ):拷贝构造函数(如果有必要)。使如下程序可以正常运行:

       1 int main() {
       2     Stack s1(5);
       3     s1.push(1);
       4     s1.push(2);
       5     s1.push(3);
       6     Stack s2(s1);
       7     while(s1.size() > 0) {
       8         cout << s1.top() << endl;
       9         s1.pop();
      10     }
      11     s1.push(4);
      12     s1.push(5);
      13     while(s2.size() > 0) {
      14         cout << s2.top() << endl;
      15         s2.pop();
      16     }
      17 }
    
  7. 改写前面的Stack类,使其可以统计Stack类对象的个数,GetStackNumber()函数返回对象的个数。

  8. 改写前面的Stack类,使Stack类在整个程序里面只能够存在一个对象。
  9. 用链表实现前面的stack类,实现相同的功能。
  10. 写Date类,实现如下功能:Date(year, month, day):构造函数,构造代表year年month月day日的Date对象;DaysFrom19900101():从1990年1月1日以来的天数,日期在1990年1月1日以后,返回正数,以前的返回负数;Add(days):计算出days天之后的新日期。days为负为之前;Delta(Date another):计算两天之间的日期差;Output():输出日期值,格式为“二〇〇五年十一月十三日”;WeekDay():返回这个日期是星期几,0表示星期天,1表示星期一,6表示星期六;静态成员函数today()返回今天的日期;所有的成员函数可以定义成const都定义成const。

  11. 实现Person, Student, GradStudent, Teacher类。类之间有正确的继承关系;每个类有对应的属性;每个类有构造函数,对属性初始化;每个类都没有默认构造函数;每个类都有一个print成员函数,输出属性的值。

       1 int main() {
       2     Person p( ... ); // 省略号处填上所需的初始化参数。
       3     Student s( ... );
       4     Teacher t( ... );
       5     GradStudent g(...);
       6     p.print();
       7     s.print();
       8     t.print();
       9     g.print(); 
      10 }
    
  12. 用虚函数实现前面实验中的print函数

       1 int main() {
       2     Person *p[4];
       3     p[0] = new Person(...); 
       4     p[1] = new Student(...);
       5     p[2] = new Teacher( ... );
       6     p[3] = new GradStudent(...);
       7     for( int i = 0; i < 4; i++)
       8         p[i]->print();
       9 }
    
  13. 实现计算各种图形面积的程序。包括:Shape类:抽象基类,定义GetArea()和GetPerimeter()纯虚函数。Rectangle类:Shape的派生类,高、宽;Circle类:Shape的派生类,半径;每个类定义的构造函数,对成员进行初始化;在基类中定义虚函数GetArea, GetPerimeter,在派生类中override;每个类一个文件。

       1 int main() {
       2     Shape *s[2];
       3     s[0] = new Rectangle(...);
       4     s[1] = new Circle(...);
       5     for ( int i = 0; i < 2; i++) 
       6         cout<<s[i]->GetArea()<< s[i]->GetPerimeter() <<endl;
       7 }
    
  14. 复数程序用运算符重载重新实现,使以下main函数可以运行

       1 int main() {
       2     complex c1, c2, c3(-1.5, 1.5);
       3     cin >> c1 >> c2;
       4     complex c4 = c1 * c2;
       5     c4 *= c3;
       6     complex c5 = c1 + c2;
       7     c5 += c3;
       8     complex c6 = c4 - c5;
       9     cout << c4 << c5 << c6;
      10     cout << "c1 and c2 are"  << ( c1 == c2 ? "equal" : "not equal") << endl;
      11 }
    
  15. 实现String类

实验参考答案

  1.    1 #include <stdio.h>
       2 int fibonacci(int n) 
       3 {
       4     if(n==1 || n==2)
       5         return 1;
       6     else
       7         return fibonacci(n-1) + fibonacci(n-2);
       8 }
       9 int main() {
      10     int n;
      11     scanf("%d", &n);
      12     printf("%d", fibonacci(n));
      13 }
    
  2.    1 #include <stdio.h>
       2 
       3 struct complex {
       4     double imag;
       5     double real;    
       6 };
       7 
       8 struct complex add(struct complex a, struct complex b)
       9 {
      10     struct complex c;
      11     c.imag = a.imag + b.imag;
      12     c.real = a.real + b.real;
      13     return c;
      14 }
      15 
      16 struct complex substract(struct complex a, struct complex b)
      17 {
      18     struct complex c;
      19     c.imag = a.imag - b.imag;
      20     c.real = a.real - b.real;
      21     return c;
      22 }
      23 
      24 struct complex multiply(struct complex a, struct complex b)
      25 {
      26     struct complex c;
      27     c.imag = a.imag * b.real + a.real * b.imag;
      28     c.real = a.real * b.real - a.imag * b.imag;
      29     return c;
      30 }
    
  3.    1 #include <iostream>
       2 #include <iomanip>
       3 using namespace std;
       4 
       5 int main() 
       6 {
       7     int n;
       8     cin >> n;
       9     for(int i = 1; i <= n; i++) {
      10         for(int j = 1; j <= i; j++) {
      11             cout << setw(5) << i*j;
      12         }
      13         cout << endl;
      14     }
      15 }
    
  4.    1 #include <iostream>
       2 #include <stdlib.h>
       3 #include <time.h>
       4 
       5 using namespace std;
       6 int main() {
       7     int count;
       8     cin >> count;
       9     srand(time(NULL));
      10     int nochange = 0;
      11     int change = 0;
      12     int random = 0;
      13     for(int i = 0; i < count; i++) {
      14         int car = rand() % 3;
      15         int choice = rand() % 3;
      16         if(car == choice)
      17             nochange ++;
      18         else
      19             change ++;
      20         if(rand()%2)
      21             random++;
      22     }
      23     cout << (double)nochange / count << endl;
      24     cout << (double)change / count << endl;
      25     cout << (double)random / count << endl;
      26 }
    
  5.    1 #include <iostream>
       2 using namespace std;
       3 
       4 class complex {
       5 public:
       6     complex(double r = 0.0, double i = 0.0) 
       7     : real(r), imag(i) {
       8     }
       9     complex add(complex a)
      10     {
      11         complex c;
      12         c.real = real + a.real;
      13         c.imag = imag + a.imag;
      14         return c;
      15     }    
      16     void substract(complex a) {
      17         imag -= a.imag;
      18         real -= a.real;
      19     }
      20     void multiply(complex a) {
      21         double r = real * a.real - imag * a.imag;
      22         double i = real * a.imag + imag * a.real;
      23         real = r; imag = i;
      24     }
      25     void print() const{
      26         cout << real << '\t' << imag;
      27     }
      28 private:
      29     double imag;
      30     double real;    
      31 
      32 };
      33 
      34 int main() {
      35     double real, imag;
      36     cin >> real >> imag;
      37     complex a(real, imag);
      38     cin >> real >> imag;
      39     complex b(real, imag);
      40     complex sum = a.add(b);
      41     complex diff = a;
      42     diff.substract(b);
      43     complex product = a;
      44     product.multiply(b);
      45     sum.print();
      46     diff.print();
      47     product.print();
      48     return 0;
      49 }
    
  6.    1 #include <iostream>
       2 #include <stdexcept>
       3 using namespace std;
       4 
       5 class stack {
       6 public:
       7     stack(int capacity) 
       8     : capacity_ (capacity), size_(0), 
       9     data_ (new int[capacity]) {
      10     }
      11     void push(int i) {
      12         if(size_ < capacity_)
      13             data_[size_++] = i;
      14     }
      15     void pop() {
      16         if(size_ > 0)
      17             size_ --;
      18     }
      19     int top() const {
      20         if(size_ > 0)
      21             return data_[size_-1];
      22         else
      23             throw std::out_of_range("");
      24     }
      25     int size() const {
      26         return size_;
      27     }
      28     ~stack() {
      29         delete[] data_;
      30     }
      31     stack(stack& s) 
      32     : size_(s.size_), capacity_(s.capacity_), 
      33     data_(new int[s.capacity_]) {
      34         for(int i =0; i < size_; i++)
      35             data_[i] = s.data_[i];
      36     }
      37 private:
      38     int *data_;
      39     int size_;
      40     int capacity_;
      41 };
    
ch3n2k.com | Copyright (c) 2004-2020 czk.