实验5

1

#include <iostream>
#include <vector>
#include <string>
using namespace std; 
void output1(vector<string> &);  
void output2(vector<string> &); 
int main()
{
vector<string>likes, dislikes;
string ch,ch1;
cout<<"为likes添加元素,输入-1结束"<<endl; 
while(cin>>ch,ch!="-1")
{
    likes.push_back(ch);
 } 
cout << "-----I like these-----" << endl;
output1(likes);
cout<<"为dislikes添加元素,输入-1结束"<<endl;
while(cin>>ch1,ch1!="-1"){
    dislikes.push_back(ch1);
} 
cout << "-----I dislike these-----" << endl;
output2(dislikes);
likes.swap(dislikes);
cout << "-----I likes these-----" << endl;
output1(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;
}
}
void output2(vector<string> &v) {
for(vector<string>::iterator it = v.begin();it!=v.end();++it)cout<<(*it)<<endl;
} 

2

 

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

 

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

3

matrix.h

#ifndef MATRIX_H
#define MATRIX_H
class Matrix {
public:
    Matrix(int n);
    Matrix(int n, int m);
    Matrix(const Matrix &X); 
    ~Matrix(); 
    void setMatrix(const float *pvalue);
    void printMatrix() const;
    inline float &element(int i, int j) {
        return p[i*lines + j];
    inline float element(int i, int j) const {
        return p[i*lines + j];
    }
    void setElement(int i, int j, int value);
    inline int getLines() const {
        return lines;
    }
    inline int  getCols() const {
        return cols;
    }
private:
    int lines;
    int cols;
    float *p; 
};
#endif

matrix.cpp

#include "stdafx.h"
#include "matrix.h"
#include<iostream>
using namespace std;

Matrix::Matrix(int n)
{
    lines = n;
    cols = n;
}
Matrix::Matrix(int n, int m)
{
    lines = n;
    cols = m;
}
Matrix::~Matrix()
{
    delete[]  p;
}
Matrix::Matrix(const Matrix &X)
{
    lines = X.lines;
    cols = X.cols;
    p = X.p;
}
void Matrix::setMatrix(const float *pvalue)
{
    for (int i = 0; i < lines*cols; i++)
        p[i] = pvalue[i];
}
void Matrix::printMatrix() const
{
    cout << "The Matrix is:" << endl;
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < cols; j++)
            cout << p[i*lines+j] << " ";
        cout << endl;
    }
}
void Matrix::setElement(int i, int j, int value)
{
    element(i, j) = value;
}

main.cpp

#include "stdafx.h"
#include <iostream>
#include "matrix.h"
using namespace std;

int main()
{
    Matrix m(4);
    const float *pvalue[20] = { 0 };
    m.setMatrix(*pvalue);
    m.setElement(2, 4, 6);
    m.printMatrix();
    return 0;
}

 4

第一题

#include "stdafx.h"
#include<iostream>
#include<time.h>
using namespace std;

class dice {
public:
    dice(int d);
    int cast();
private:
    int sides;
};
dice::dice(int d) {
    sides = d;
}
int dice::cast()
{
    
    int p = rand() % sides + 1;
    return p;
}
int main()
{
    srand((unsigned)(time(NULL)));
    dice d(40);
    int k = 0;
    
    for (int i = 1; i <= 500; i++)
    {
        if (d.cast() == 16)
            k++;
    }
    cout << k << endl;
    return 0;
}

 

posted @ 2018-05-23 16:45  群艺术啊  阅读(88)  评论(0编辑  收藏  举报