c++ 实验5

//6-17
#include<iostream>
using namespace std;
int main()
{
	int m=9;
	int *p=&m;
	cout << "The value at p:" << *p;
	return 0;	
} 

  

#include<iostream>
using namespace std;
int fn1(){
    int n;
    int *p=new int (5);
    n=*p;
    delete p;
    return n;
}
int main()
{
    int a=fn1();
    cout<<"the va;ue of a is: "<<a<<endl;    
    return 0;
} 

#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("favorite book");
    likes.push_back("music");
    likes.push_back("film");
    likes.push_back("sport");
    likes.push_back("sportsman");
    cout << "-----I like these-----" << endl;
    output1(likes);
    
    dislikes.push_back("music");
    cout << "-----I dislike these-----" << endl;
    output1(dislikes);

    swap(likes,dislikes);//即可完成交换 
    
    cout << "-----I likes these-----" << endl;
    output2(likes);
    cout << "-----I dislikes these-----" << endl;
    output2(dislikes);                    
    return 0;
}

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) {
    for(vector<string>::iterator it=v.begin();it!=v.end();++it)
    cout<<*it<<endl;
}

//main.cpp 
#include <iostream>
#include "matrix.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std; 
int main ( )
{
    int n,m,p,q;
    Matrix a1(3),a2(3,4),a3(a1);
    cout<<"the matrix a1's line and col is "<<a1.getCols()<<" "<<a1.getLines()<<endl;
    cout<<"the instant matrix a1's and a2's and a3's content is----"<<endl;
    a1.printMatrix();
    a2.printMatrix();
    a3.printMatrix();
    n=a1.element(2,2);
       m=a2.element(3,4);
       cout<<"the matrix a1's line 2, col 2 is: "<<n<<endl;
    cout<<"the matrix a2's line 3, col 4 is: "<<m<<endl;
    cout<<"-----------------------------------------"<<endl;
    a1.setElement(2,3,50);
    cout<<"the changed a1 is: "<<endl;
    a1.printMatrix();
    cout<<"now,decide your matrix's lines and cols: "<<endl;
    cin>>p>>q;
    float x[p*q];
    cout<<"decide your numbers:"<<endl;
    for(int i=0;i<p*q;i++)
    cin>>x[i];
    Matrix a4(p,q);
    a4.setMatrix(x);
    a4.printMatrix();
    return 0;
}
//matrix.cpp 
#include<iostream>
#include<vector>
#include"matrix.h"
using namespace std;
Matrix::Matrix(int n){
    lines=n;
    cols=n;
    p=new float [lines*cols];
    for(int i=0;i<lines;i++){
        for(int j=0;j<cols;j++)
        p[i*cols+j]=0;
    } 
}

Matrix::Matrix(int n,int m){
    lines=n;
    cols=m;
    p=new float [lines*cols];
    for(int i=0;i<lines;i++){
        for(int j=0;j<cols;j++)
        p[i*cols+j]=0;
    } 
}

Matrix::Matrix(const Matrix &X){
    lines=X.lines;
    cols=X.cols;
    p=new float [lines*cols];
    for(int i=0;i<lines*cols;i++){
        p[i]=X.p[i];
    }
}

Matrix::~Matrix(){
    cout << "destructor is called----"<<endl;
    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 << p[i*cols+j]<<" ";
        cout << endl;     
    } 
}

//inline int Matrix::element(int i, int j){
//    return p[(i-1)*cols+j-1];
//}
//
//inline int Matrix::element(int i, int j) const{
//    return p[(i-1)*cols+j-1];
//} 

void Matrix::setElement(int i, int j, int value){
    p[(i-1)*lines+j-1]=value;
}
//inline int Matrix::getLines() const{
//    return lines;
//}
//inline int Matrix::getCols() const{
//    return cols;
//}
//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){
            return p[(i-1)*cols+j-1];
        } //返回矩阵第i行第j列元素的引用   // ???
        inline float element(int i, int j) const{
            return p[(i-1)*cols+j-1];
        }// 返回矩阵第i行第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

posted @ 2018-05-23 23:52 tacore 阅读(...) 评论(...) 编辑 收藏