zoukankan      html  css  js  c++  java
  • 实验五

    ex3:

    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    // 函数声明 
    void output1(vector<string> &);  
    void output2(vector<string> &);  
    
    int main()
    {
        vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes
        likes.push_back("sanguoyanyi");
        likes.push_back("shijianzhuyu");
        likes.push_back("badminton");
        likes.push_back("wuwenxidong");
        likes.push_back("James");
    
        // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) 
        // 补足代码 
        // 。。。 
        
        
        cout << "-----I like these-----" << endl;
        output1(likes);
        // 调用子函数输出vector<string>数组对象likes的元素值 
        // 补足代码
        // 。。。 
        
        dislikes.push_back("table tennis");
        dislikes.push_back("hongloumeng");
        dislikes.push_back("Green");
    
        // 为vector<string>数组对象dislikes添加元素值 
        // 补足代码 
        // 。。。 
        
        cout << "-----I dislike these-----" << endl;
        output1(dislikes);
        // 调用子函数输出vector<string>数组对象dislikes的元素值 
        // 补足代码
        // 。。。 
        
        swap(likes, dislikes);
        // 交换vector<string>对象likes和dislikes的元素值 
        // 补足代码
        // 。。。 
        
        
        cout << "-----I likes these-----" << endl;
        output2(likes);
        // 调用子函数输出vector<string>数组对象likes的元素值 
        // 补足代码
        // 。。。 
        
        cout << "-----I dislikes these-----" << endl;
        output2(dislikes);
        // 调用子函数输出vector<string>数组对象dislikes的元素值 
        // 补足代码
        // 。。。 
            
        system("pause");
        return 0;
    }
    
    
    // 函数实现 
    // 以下标方式输出vector<string>数组对象v的元素值  
    void output1(vector<string> &v) {
        for (int i = 0; i < v.size(); i++)
            cout << v[i] << endl;
        // 补足程序
        // 。。。 
    }
    
    // 函数实现
    // 以迭代器方式输出vector<string>数组对象v的元素值 
    void output2(vector<string> &v) {
        vector <string>::iterator it;
        for (auto e : v)
            cout << e << endl;
        // 补足程序
        // 。。。 
    }

     

    6-17:

    #include<iostream>
    using namespace std;
    int main() {
        int *p;   //未给指针p初始化,使得p指向未知区域,编译不成功
        *p = 9;
        cout << "The value at p:" << *p;
        system("pause");
        return 0;
    }

    修正:

    #include<iostream>
    using namespace std;
    int main() {
        int p1;
        int *p = &p1;  //初始化p指向p1
        *p = 9;
        cout << "The value at p:" << *p << endl; //可以不换行的
        system("pause");
        return 0;
    }

    6-18:

    #include<iostream>
    using namespace std;
    int fnl() {
        int *p = new int(5);
        return *p;   //未delete动态申请的空间
    }
    
    int main() {
        int a = fnl();
        cout << "the value of a is: " << a;
        system("pause");
        return 0;
    }

    修正:

    #include<iostream>
    using namespace std;
    int p1;
    int *p = &p1;  //利用全局变量,以便delete
    int fnl() {
        p = new int(5);
        return *p;
    }
    
    int main() {
        int a = fnl();
        cout << "the value of a is: " << a << endl;
    delete p; //delete动态申请的空间 system(
    "pause"); return 0; }

    matrix:

    matrix.h

    #pragma once
    #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列元素的引用
        {
            float& n = p[i*cols + j];
            return n;
        }
        inline float element(int i, int j) const // 返回矩阵第i行第j列元素的值           inline函数在类内定义
        {
            return p[i*cols + j];
        }
        void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
        inline int getLines() const  //返回矩阵行数 
        {
            return lines;
        }
        inline int  getCols() const  //返回矩阵列数
        {
            return cols;
        }
    private:
        int lines;    // 矩阵行数
        int cols;      // 矩阵列数 
        float *p;   // 指向存放矩阵数据的内存块的首地址 
    };
    #endif

    matrix.cpp

    #include<iostream>
    #include<iomanip>
    #include"matrix.h"
    using namespace std;
    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 (int i = 0; i < X.lines*X.cols; i++) {
            *(p + i) = *(X.p + i);
        }
    }
    Matrix::~Matrix() {
        delete[] p;
    }
    
    void Matrix::setMatrix(const float *pvalue) {
        for (int i = 0; i < lines*cols; i++)
            *(p + i) = *(pvalue + i);
    }
    
    void Matrix::printMatrix() const {
        for (int i = 0; i < lines; i++) {
            for (int j = 0; j < cols; j++)
                cout << setiosflags(ios::fixed) << setprecision(1) << p[i*cols + j] << ' ';
            cout << endl;
        }
    }
    
    void Matrix::setElement(int i, int j, int value) {
        p[i*cols + j] = (float)value;
    }

    main.cpp

    #include<iostream>
    #include<iomanip>
    #include"matrix.h"
    using namespace std;
    
    int main() {
        Matrix a1(4);
        Matrix a2(3, 4);
        float b1[16], b2[12];
        for (int i = 1; i <= 16; i++)
            b1[i-1] = (float)i / (float)10;
        for (int i = 0; i < 12; i++)
            b2[i] = (float)(12 - i) / (float)10;
        float *p1 = b1;
        float *p2 = b2;
        a1.setMatrix(p1);
        cout << "a1是一个" << a1.getLines() << "" << a1.getCols() << "列的矩阵" << endl;
        a1.printMatrix();
        a2.setMatrix(p2);
        cout << "a2是一个" << a2.getLines() << "" << a2.getCols() << "列的矩阵" << endl;
        a2.printMatrix();
    
        Matrix a3(a1);
        cout << "a3是一个" << a3.getLines() << "" << a3.getCols() << "列的矩阵" << endl;
        a3.printMatrix();
        cout << "------------------" << endl;
        a3.setElement(2, 1, 5);
        a3.printMatrix();
        cout << "改变的元素是" << a3.element(2, 1) << endl;
    
        system("pause");
        return 0;
    }

    期中考试

    http://www.cnblogs.com/zhaoluolong/p/9015694.html

  • 相关阅读:
    get函数理解
    get函数理解
    Absolute C++ 第9章字符串 编程练习4
    Absolute C++ 第9章字符串 编程练习4
    Absolute C++ 第9章字符串 编程练习4
    Absolute C++ 第9章字符串 编程练习4
    C字符串使用陷阱 “=”和“==” 学习笔记
    [Android]ListView中分割线的设置
    如何在adapter 中调用activity的方法
    Android开发中Handler的经典总结
  • 原文地址:https://www.cnblogs.com/zhaoluolong/p/9077430.html
Copyright © 2011-2022 走看看