实验5

vector 基本用法;

#include <iostream>  
#include <vector>  
#include <string>  
using namespace std;  
  
// 函数声明   
void output1(vector<string> &);    
void output2(vector<string> &);    
  
int main()  
{  
    vector<string>likes(99), dislikes(99); 
    // 创建vector<string>对象likes和dislikes  
      
    likes[99]="favorite book,music,film,paintings,anime,sport,sportsman";
    // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc)   
    // 补足代码   
    // 。。。   
      
      
    cout << "-----I like these-----" << endl;  
    output1(likes);
    // 调用子函数输出vector<string>数组对象likes的元素值   
    // 补足代码  
    // 。。。   
      
      
    dislikes[99]="favorite book, music, film, paintings,anime,sport,sportsman";  
    // 为vector<string>数组对象dislikes添加元素值   
    // 补足代码   
    // 。。。   
      
    cout << "-----I dislike these-----" << endl;  
    output2(dislikes);
    // 调用子函数输出vector<string>数组对象dislikes的元素值   
    // 补足代码  
    // 。。。   
      
      
    swap(likes,dislikes);
    // 交换vector<string>对象likes和dislikes的元素值   
    // 补足代码  
    // 。。。   
      
      
    cout << "-----I likes these-----" << endl;  
    output1(likes);
    // 调用子函数输出vector<string>数组对象likes的元素值   
    // 补足代码  
    // 。。。   
      
    cout << "-----I dislikes these-----" << endl;  
    output2(dislikes);
    // 调用子函数输出vector<string>数组对象dislikes的元素值   
    // 补足代码  
    // 。。。   
          
                          
    return 0;  
}  
  
  
    // 函数实现   
    // 以下标方式输出vector<string>数组对象v的元素值    
void output1(vector<string> &v) {  
  
    
    // 补足程序  
    // 。。。   
}  
  
// 函数实现  
// 以迭代器方式输出vector<string>数组对象v的元素值   
void output2(vector<string> &v) {  
 
 
   
    // 补足程序   
    // 。。。   
} 

截图错误

 

6-17;

#include<iostream>  
using namespace std;  
int main()  
{  
    int *p;  
    //*p=9;  此处错误:指针不能直接指向数值 
    int a=9;p=&a;
    cout<<"The value at p:"<<*p;  
    return 0;  
} 

截图;

 

6-18;

#include<iostream>  
using namespace std;  
int fn1()  
{  
    int *p=new int(5);  
    return *p; 
    //用new动态分配后,应释放  
    delete p; 
}  
int main()  
{  
    int a=fn1();  
    cout<<"the value of a is:"<<a; 
    return 0;  
} 

截图;

 

Matrix动态矩阵类;

matrix.h;

#ifndef MATRIX_H
#define MATRIX_H
class Matrix {
public:
    Matrix(int n); // 构造函数,构造一个n*n的矩阵 
    Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 
    Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 
    ~Matrix(); //析构函数 
    void setMatrix(const float *pvalue); // 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值 
    void printMatrix() const; // 显示矩阵
    inline float &element(int i, int j); //返回矩阵第i行第j列元素的引用
    inline float element(int i, int j) const;// 返回矩阵第i行第j列元素的值 
    void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
    inline int getLines() const; //返回矩阵行数 
    inline int  getCols() const; //返回矩阵列数 
private:
    int lines;    // 矩阵行数
    int cols;      // 矩阵列数 
    float *p;   // 指向存放矩阵数据的内存块的首地址 
    };
 #endif

 matrix.cpp;

Matrix::Matrix(int n) :lines(n), cols(n) {
    p = new double[lines*cols];}

Matrix::Matrix(int n,int m) :lines(n), cols(m) {
    p = new double[lines*cols];}
    
Matrix::Matrix(const Matrix &x) : lines(x.lines), cols(x.cols) {
    p = new double[lines*cols];}
    
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;}}

 

1.

#include<iostream>
#include<cstdlib>
#include<ctime>
 class Dice {
  public:
    Dice(int n);
    int cast();
  private:
    int sides;
 };
 Dice::Dice(int n){sides=n;
 }
 int Dice::cast(){
 }
using namespace std;
int main() {
Dice d(40);
int j=0;
for (int i=0;i<500;i++) {
if (d.cast()==8) j++;}
double p;
p = (double)j / 500;
cout << "学号20171398008被抽中的概率是:" << p << endl;
return 0;
}

1

 

3.book.h

class Book {   
public:
    Book(string isbnX, string titleX, float priceX);
    void print();     
private:
    string isbn;
    string title;
    float price;
 };

book.cpp

Book::Book(string isbnX, string titleX, float priceX){
    isbn = isbnX;
    title = titleX;
    price = priceX;
}
void Book::print() {
    cout<< isbn << " " << title << " " << price << endl;
}

main.cpp

int main()
{
    vector<Book>BOOK;
    string isbn, title;
    float price;
    cout << "输入出版编号,书名,定价" << endl;
    for (int i = 0; i<=9; i++){
        cin>>Book[i].isbn>>Book[i].title>>Book[i].price;
    }
    for (int i = 0; i<=9; i++)
    BOOK[i].print();
    return 0;
}

 

posted @ 2018-05-23 23:37  hhxxr  阅读(88)  评论(0)    收藏  举报