实验五+期中考试

实验五

(1)

#include <iostream>
#include <vector>
#include <string>
using namespace std;
void output1(vector<string> &);  
void output2(vector<string> &);  
int main()
{
    vector<string>likes(3), dislikes(3); // 创建vector<string>对象likes和dislikes
    likes.push_back("《The Moon And Six Pence》");
    likes.push_back("《Wake》");
    likes.push_back("《StarSky》");
    cout << "-----I like these-----" << endl;
    output1(likes);
    dislikes.push_back("BooksWNG");
    dislikes.push_back("SongsWNG");
    dislikes.push_back("PaintingsWNG");
    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) {
    int i;
    for (i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    cout << endl;
}
void output2(vector<string> &v) {
    vector<string>::iterator it;
    for (it = v.begin(); it != v.end(); ++it) {
        cout << *it << " ";
    }cout << endl;
}

(2)

#include<iostream>
using namespace std;
int fn1() {
    int *p = new int(5);
    return *p;
}
int main() {
    int a = fn1();
    cout << "the value of a is:" << a;
    return 0;//new与delete未配对使用。
}
//修改后
#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;
}
#include<iostream>
using namespace std;
int main() {
    int *p;
    *p = 9;//上没有将地址赋给p,由于p没有被初始化。
    cout << "The value at p:" << *p;
    return 0;
}
//修改后
#include<iostream>
using namespace std;
int main() {
    int *p; int n=9;
    p = &n;                
    cout << "The value at p:" << *p;
    return 0;
}

(3)

//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列元素的引用
    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 { return lines; };//返回矩阵行数
    inline int  getCols() const { return cols; }; //返回矩阵列数 
private:
    int lines;    // 矩阵行数
    int cols;      // 矩阵列数 
    float *p;   // 指向存放矩阵数据的内存块的首地址 
};
#endif
//matrix.cpp
#include"matrix.h"
#include<iostream>
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];
}
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 {        
    cout << "The Matrix is:" << endl;
    for (int i = 0; i < lines; i++) {
        for (int j = 0; j < cols; j++)
            cout << p[i*cols + j] << " ";
        cout << endl;
    }
}
inline float &Matrix::element(int i, int j) {
    return p[i*cols + j];
}
inline float Matrix::element(int i, int j) const {
    return p[i*cols + j];
}
void Matrix::setElement(int i, int j, int value) {
    p[i*cols + j] = value;
}
//main.cpp
#include"matrix.h"
#include<iostream>
using namespace std;
int main() {
    Matrix A(2); Matrix B(2, 3);
    const float a[4] = { 
        1,2,3,4,
     };
    const float b[6] = { 
        1,2,3,4,5,6,
    };
    const float c[12] = { 
        1,2,3,4,5,6,
     };
    A.setMatrix(a); 
    B.setMatrix(b);
    Matrix C(B);
    C.setMatrix(c);
    A.printMatrix();
    B.printMatrix();
    C.printMatrix();
    A.setElement(1, 1, 10);
    B.setElement(1, 1, 10);
    C.setElement(1, 1, 10);
    A.printMatrix();
    B.printMatrix();
    C.printMatrix();
    cout << A.getLines() << endl;
    cout << A.getCols()  << endl;
    return 0;
}

期中

(1)

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
class Dice {
public:
    Dice(int n) {
        sides = n;
    }
    int cast() {
        int m;
        m = 1+rand()%40;
        return m;
    }
private:
    int sides;
};
int main() {
    srand((unsigned)time(NULL));
    int n, x;//输入班级人数和学号。
    cin >> n >> x;
    Dice A(n);
    int i; 
    double y = 0.00;
    for (i = 0; i < 500; i++) {
        if (A.cast() == x)y++;
    }
    double p = y / 500.00;
    cout  << p << endl;
    return 0;
}

#include<iostream>
#include<string>
using namespace std;
static int n = 999;
class User {
public:
    User(string s1, string s2 = "111111") {
        n++;
        id = n;
        name = s1; 
        password = s2;
        CurrentID++;
    }
    User(User &u1) {
        id = u1.id;
        name = u1.name; 
        password = u1.password; 
        CurrentID = u1.CurrentID;
    }
    void show1() {
        cout << "ID:" << id << " Name:" << name << " Password:" << password << endl;
    }
    static void show2(User &u1) {     
        cout << "CurrentID:" << CurrentID << endl; cout << "The latest User:";
        u1.show1();
    }
    void change() {                    
        string s3, s4; int j; j = 3;
        while (j) {
            cout << "Old Password:"; 
            cin >> s3;
            if (s3 == password) {
                cout << "New Password:"; cin >> s4;
                password = s4; break;
            }
            if (s3 != password) {
                j--; cout << "Wrong Password!" << endl;
            }
        }
        if (j == 0)cout << "Please Try Later!" << endl;
    }
private:
    int id;
    string name;
    string password;
    static int CurrentID;
}; int User::CurrentID = 999;//给CurrentID赋初值
int main() {
    User u1("user1", "123456");
    User u2("user2");
    User u3("user3", "234567");
    u1.show1();
    u2.show1();
    u3.show1(); 
    User u(u3);
    User::show2(u3);
    u1.change();
    u1.show1();
    u2.change();
    return 0;
}

//book.h
#pragma once
#ifndef BOOK_H
#define BOOK_H

#include <string>
using std::string;

class Book {
public:
    Book(string isbnX, string titleX, float priceX);    
    void print(); 
private:
    string isbn;
    string title;
    float price;
};
#endif
//book.cpp
#include "book.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
    vector<Book>books;
    string isbn, title;
    float price;
    while (1) {
        cout << "Input 'Quit' to Quit;" << endl;
        cin >> isbn;
        if (isbn != "Quit") {
            cin >> title >> price;
        }
        if (isbn == "Quit")break;
        Book a(isbn, title, price);
        books.push_back(a);
    }
    int i;
    for (i = 0; i < books.size(); i++) {
        books[i].print();
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/nuist20178303037/p/9080444.html
今日推荐