实验五 类和对象-3

1.ex3.cpp

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5 
 6 // 函数声明 
 7 void output1(vector<string> &);  
 8 void output2(vector<string> &);  
 9 
10 int main()
11 {
12     vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes
13     
14     // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) 
15     // 补足代码 
16     // 。。。 
17      likes.push_back("favorite book");
18     likes.push_back("music");   
19     likes.push_back("film");
20     likes.push_back("paintings");
21  
22     
23     cout << "-----I like these-----" << endl;
24     // 调用子函数输出vector<string>数组对象likes的元素值 
25     // 补足代码
26     // 。。。 
27     output1(likes);
28     
29     // 为vector<string>数组对象dislikes添加元素值 
30     // 补足代码 
31     // 。。。 
32      dislikes.push_back("anime");
33       dislikes.push_back("sport");
34     dislikes.push_back("sportsman");
35     
36     cout << "-----I dislike these-----" << endl;
37     // 调用子函数输出vector<string>数组对象dislikes的元素值 
38     // 补足代码
39     // 。。。 
40     output2(dislikes);
41     
42     
43     // 交换vector<string>对象likes和dislikes的元素值 
44     // 补足代码
45     // 。。。 
46     likes.swap(dislikes);
47     
48     
49     cout << "-----I likes these-----" << endl;
50     // 调用子函数输出vector<string>数组对象likes的元素值 
51     // 补足代码
52     // 。。。 
53     output1(likes);
54     cout << "-----I dislikes these-----" << endl;
55     // 调用子函数输出vector<string>数组对象dislikes的元素值 
56     // 补足代码
57     // 。。。 
58     output2(dislikes);
59                         
60     return 0;
61 }
62 
63 
64 // 函数实现 
65 // 以下标方式输出vector<string>数组对象v的元素值  
66 void output1(vector<string> &v) {
67     // 补足程序
68     // 。。。 
69     for(int i=0;i<v.size();i++)
70     cout<<v[i]<<",";
71     cout<<endl;
72 }
73 
74 // 函数实现
75 // 以迭代器方式输出vector<string>数组对象v的元素值 
76 void output2(vector<string> &v) {
77     // 补足程序
78     // 。。。
79     vector<string>::iterator  iter;
80     for(iter=v.begin() ;iter!=v.end() ;iter++)
81     cout<<*iter<<",";
82     cout<<endl;
83 }

6-17

 1 #include <iostream>
 2 #include <stdlib.h>
 3 using namespace std;
 4 int main()
 5 { // int *p;定义了一个指针p,然而p并没有指向任何地址,所以当使用*p时是没有任何地址空间对应的,所以 *p=9 就会导致,不知道把这个1赋值给哪个地址空间了
 6     int *p=NULL;
 7     int a = 9;
 8     p =&a;
 9     cout << "The value at 不加上p: " << *p;
10     system("pause");
11     return 0;
12 }
13 /*不加上#include <stdlib.h>和system("pause"); 我vs2017的cmd框会闪退,求解惑.*/

6-18

 1 #include <iostream>
 2 #include "stdlib.h "
 3 using namespace std;
 4 int fn1() {
 5     int *p = new int(5);//new的值一定要delete
 6     return *p ;
 7     delete p;
 8 }
 9 int main()
10 {
11     int a = fn1();
12     cout << "the value of a is: " << a;
13     system("pause");
14     return 0;
15 }

期中考试题

第一题

 1 Dice.h
 2 
 3 class Dice {
 4 public:
 5     Dice(int n) :sides(n) {
 6 
 7     }
 8     int cast()
 9     {
10         return rand() % sides + 1;
11     }
12 
13 private:
14     int sides;
15 
16 };
17 
18 main.cpp
19 #include<iostream>
20 #include<cstdlib>
21 #include<ctime>
22 #include"Dice.h"
23 
24 using namespace std;
25 
26 int main() {
27 
28     srand(time(NULL));
29 
30     Dice A(40);
31 
32     int num = 0;
33 
34     for (int j = 0; j<500; ++j) {
35 
36         if (A.cast() == 19)num++;
37 
38     }
39 
40     double p = 0;
41 
42     p = (double)num / 500.0;
43 
44     cout << "学号 20178303019 被抽中的概率是:" << p << endl;
45 
46     return 0;
47 
48 }

第三题

book.h

扫描二维码关注公众号,回复: 997258 查看本文章
 1 #ifndef BOOK_H
 2 #define BOOK_H
 3 
 4 #include <string>
 5 using std::string;
 6 
 7 class Book {
 8     public:
 9         Book(string isbnX, string titleX, float priceX);  //构造函数  
10         void print(); // 打印图书信息 
11     private:
12         string isbn;
13         string title;
14         float price;
15 };
16 #endif

book.cpp

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

// 构造函数
// 补足程序 
// ...
Book::Book(string isbnX, string titleX, float priceX) {
    isbn = isbnX;
    title = titleX;
    price = priceX;
}


// 打印图书信息
// 补足程序 
// ...
void Book::print() {
    cout << "Isbn: " << isbn << " Title: " << title << " Price: " << price << endl;
}

main.cpp

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

int main()
{
    // 定义一个vector<Book>类对象
    // 补足程序
    // ... 
    vector<Book> BOOK;
    string isbn, title;
    float price;
    
    // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
    // 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) 
    // 补足程序
    // ... 
    cout << "请依次录入图书信息 出版编号(isbn), 书名(title), 定价(price)" << endl;
     do
     {
         cin >> isbn >> title >> price;
     } while (cin >> isbn);
    // 输出入库所有图书信息
    // 补足程序
    // ... 
    for (int i = 0; i<BOOK.size(); i++)
        BOOK[i].print();
    
    return 0;
}

//可能是我停止录入那边的代码写错了。。不会写,求指点

动态矩阵

matrix.h

 1 #ifndef MATRIX_H
 2 #define MATRIX_H
 3 class Matrix {
 4     public:
 5         Matrix(int n); // 构造函数,构造一个n*n的矩阵 
 6         Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 
 7         Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 
 8         ~Matrix(); //析构函数 
 9         void setMatrix(const float *pvalue); // 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值 
10         void printMatrix() const; // 显示矩阵
11         inline float &element(int i, int j); //返回矩阵第i行第j列元素的引用
12         inline float element(int i, int j) const;// 返回矩阵第i行第j列元素的值 
13         void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
14         inline int getLines() const; //返回矩阵行数 
15         inline int  getCols() const; //返回矩阵列数 
16     private:
17         int lines;    // 矩阵行数
18         int cols;      // 矩阵列数 
19         float *p;   // 指向存放矩阵数据的内存块的首地址 
20 };
21 #endif

matrix.cpp

#include<iostream>
#include"matrix.h"
using namespace std;
int i, j;
Matrix::Matrix(int n) :lines(n), cols(n) {
    p = new float[lines*cols];
}
Matrix::Matrix(int n, int m) : lines(n), cols(m) {
    p = new float[lines*cols];
}
Matrix::Matrix(const Matrix &x) : lines(x.lines), cols(x.cols) {
    p = new float[lines*cols];
    for (i = 0; i<lines*cols; i++)
        p[i] = x.p[i];
}
Matrix::~Matrix() {
    delete[] p;
}
void Matrix::setMatrix(const float *pvalue) {
    for (i = 0; i<lines*cols; i++)
        p[i] = pvalue[i];
}
void Matrix::printMatrix() const {
    for (i = 0; i<lines; i++) {
        for (j = 0; j<cols; j++)
            cout << p[i*lines + j] << " ";
        cout << endl;
    }
}
inline float Matrix::element(int i, int j) const {
    cout << p[(i - 1)*lines + j - 1] << endl;
}
void Matrix::setElement(int i, int j, int value) {
    p[(i - 1)*lines + j - 1] = value;
}
inline int Matrix::getLines() const {
    cout << lines << endl;
}
inline int Matrix::getCols() const {
    cout << cols << endl;
}

main.cpp

#include<iostream>
#include"matrix.h"
using namespace std;
int main()
{
    int l, c, i, j, n;
    cout << "输入行数和列数" << endl;
    cin >> l >> c;
    float a[l*c];
    cout << "初始化矩阵" << endl;
    for (i = 0; i<l*c; i++)
        cin >> a[i];
    Matrix A(l, c);
    A.setMatrix(a);
    cout << "输出矩阵" << endl;
    A.printMatrix();
    cout << "输入行数和列数输出该数" << endl;
    cin >> i >> j;
    A.element(i, j);
    cout << "改变第几行第几列的数" << endl;
    cin >> n;
    A.setElement(i, j, n);
    cout << "输出这个数" << endl;
    A.element(i, j);
    cout << "返回矩阵行数与列数" << endl;
    A.getLines();
    A.getCols();
    return 0;
}

真的 看了很多人很多人的代码,这行都是这么写的。

但我确实编不出来啊。。。

期中考试 第二条 还不会写

就不把代码拿出来丢人了。。

猜你喜欢

转载自www.cnblogs.com/wuyijie/p/9074139.html
今日推荐