zoukankan      html  css  js  c++  java
  • c++第五次实验报告

    #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 = { "favorite book, music, film, paintings,anime,sport,sportsman,etc" };
        cout << "-----I like these-----" << endl;
        output1(likes);
        dislikes = { "running, studying, etc" };
        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;
    }
    
    
    // 函数实现 
    // 以下标方式输出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 itr = v.begin();
        for (itr; itr != v.end(); itr++)
            cout << *itr << endl;
    }

    二.1.

    #include<iostream>
    using namespace std;
    int main() {
        int *p;//指针为初始化
        *p = 9;//未初始化后直接使用
        cout << *p << endl;
        return 0;
    }

    应该改为

    #include<iostream>
    using namespace std;
    int main() {
        int *p=new int(9);//指针初始化
        cout << *p << endl;
    delete p;
    return 0; }

    2.

    #include<iostream>
    using namespace std;
    int fn1() {
        int *p = new int(5);//为指针p申请内存,函数中并未释放指针p的内存
        return *p;//
    }
    int main() {
        int a = fn1();
        cout << "the value of a is: " << a;
        return 0;
    }
    #include<iostream>
    using namespace std;
    int fn1() {
        int *p = new int(5);//为指针p申请内存
        delete p;//若是在此处释放,则导致没法返回指针p指向的值,程序运行出错
        return *p;
    }
    int main() {
        int a = fn1();
        cout << "the value of a is: " << a;
        return 0;
    }

    所以应该改为

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

    三.

    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; // 显示矩阵
        float &element(int i, int j); //返回矩阵第i行第j列元素的引用
        float element(int i, int j) const;// 返回矩阵第i行第j列元素的值 
        void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
        int getLines() const; //返回矩阵行数 
        int  getCols() const; //返回矩阵列数 
    private:
        int lines;    // 矩阵行数
        int cols;      // 矩阵列数 
        float *p;   // 指向存放矩阵数据的内存块的首地址 
    };
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;
        float*pvalue = new float[n*n];
        for (int i = 0; i < n*n; i++)
            cin >> pvalue[i];
        Matrix M(n);
        M.setMatrix(pvalue);
        M.printMatrix();
        cout<<M.element(2, 2)<<endl;
        M.setElement(2, 2, 2);
        M.printMatrix();
        cout << M.getCols() << endl;
        cout << M.getLines() << endl;
        return 0;
    }

    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    ;
    Matrix::Matrix(int n) :lines(n), cols(n) {
    p = new float[n*n];
    }
    Matrix::Matrix(int n, int m) : lines(m), cols(n) {
    p = new float[m*n];
    }
    Matrix::Matrix(const Matrix &X) : lines(X.lines), cols(X.cols) {
    p = new float[X.lines*X.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 << p[i*cols + j] << " ";
    cout << endl;
    }
    }
    float &Matrix::element(int i, int j) {
    return p[(i-1)*cols + j-1];
    }
    float 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)*cols + j-1] = (float)value;
    }
    int Matrix::getLines() const {
    return lines;
    }
    int Matrix::getCols() const {
    return cols;
    }

    四.期中考试第二题

    #include<iostream>
    #include<string>
    using namespace std;
    class User {
    public:
        User(string nm,string pd="111111") {
            name = nm;
            password = pd;
            id = Currentid + 1;
            Currentid++;
        };
        User(User&u) {
            name = u.name;
            password = u.password;
            id = u.id;
            Currentid++;
        };
        ~User() {};
        void printUser() {
            cout <<"name: "<< name << endl;
            cout <<"passward: " <<password<<endl;
            cout <<"id: "<<id << endl;
        };
        void resetpassward() {
            int n = 3;
            string oldpassward;
            while (n)
            {
                cout<<"请输入原密码"<<endl;
                cin >> oldpassward;
                if (password.compare(oldpassward) == 0)
                {
                    cout << "请输入新密码" << endl;
                    cin >> password;
                    break;
                }
                else
                    cout<<"输入错误,";
                    n--;
            }
        };
        void printCurrentid() {
            cout << Currentid << endl;
            cout << "id: "<<id << endl;
            cout << "name: " << name << endl;
            cout << "passward: "<<password<<endl;
        
        };
    private:
        string name;
        string password;
        int id;
        static int Currentid;
    };
    int User::Currentid = 999;
    int main() {
        User u1("liurui", "111111");
        u1.printUser();
        u1.printCurrentid();
        u1.resetpassward();
        return 0;
    }

    期中考试第三题

    #include<string>
    using namespace std;
    class Book {
    public:
        Book(string isbnX, string titleX,float priceX);
        void print();
    private:
        string title;
        string isbn;
        float price;
    };
    #include"book.h"
    #include<iostream>
    using namespace std;
    Book::Book(string isbnX, string titleX, float priceX) :isbn(isbnX), title(titleX), price(priceX) {};
    void Book::print() {
        cout << "isbn: "<<isbn << endl;
        cout << "title: " << title<<endl;
        cout << "price: " << price << endl;
    };
    #include<iostream>
    #include"book.h"
    #include<vector>
    using namespace std;
    int main() {
        vector<Book>books;
        string isbn, title;
        int price;
        while (true) {
            cout << "输入书名" << endl;
            cin >> title;
            cout << "输入编号" << endl;
            cin>> isbn;
            cout << "输入价格" << endl;
            cin >> price;
            Book book1(isbn, title, price);
            books.push_back(book1);
            cout << "输入c继续,输入s停止" << endl;
            char a;
            cin >> a;
            if (a == 's')
                break;
            else;
        };
        for (int i = 0; i < books.size(); i++)
            books[i].print();
        return 0;
    }

  • 相关阅读:
    C语言与内存模型初探
    【编辑中】软件工程知识萃取
    【深入理解计算机系统01】不同层级程序指令间的转换
    【统计学中的普适智慧】假设检验
    windows 内部预览版与迅雷极速版不配合
    网络安全理论初涉
    Unix philosophy
    BOP 2016 复赛题目
    10.8做题——USACO1.2命名那个数字(Name That Number)
    10.6上课——problem1切割木板(USACO 2006 November Gold)
  • 原文地址:https://www.cnblogs.com/miaorui1314/p/9073635.html
Copyright © 2011-2022 走看看