【实验5】类与对象3

1、实验内容1

#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
    
    // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) 
    // 补足代码 
    // 。。。 
    likes.push_back("ONER");
    likes.push_back("katto");
    likes.push_back("pinkray");
    likes.push_back("BC221");
    
    
    cout << "-----I like these-----" << endl;
    // 调用子函数输出vector<string>数组对象likes的元素值 
    // 补足代码
    // 。。。 
    output1(likes);
    
    
    // 为vector<string>数组对象dislikes添加元素值 
    // 补足代码 
    // 。。。 
    dislikes.push_back("english");
    dislikes.push_back("coriander");
    dislikes.push_back("chocolate");
    dislikes.push_back("sit-up");
    
    cout << "-----I dislike these-----" << endl;
    // 调用子函数输出vector<string>数组对象dislikes的元素值 
    // 补足代码
    // 。。。 
    output1(dislikes);
    
    // 交换vector<string>对象likes和dislikes的元素值 
    // 补足代码
    // 。。。 
    likes.swap(dislikes);
    
    cout << "-----I likes these-----" << endl;
    // 调用子函数输出vector<string>数组对象likes的元素值 
    // 补足代码
    // 。。。 
    output2(likes);
    
    cout << "-----I dislikes these-----" << endl;
    // 调用子函数输出vector<string>数组对象dislikes的元素值 
    // 补足代码
    // 。。。 
    output2(dislikes);
        
                        
    return 0;
}


// 函数实现 
// 以下标方式输出vector<string>数组对象v的元素值  
void output1(vector<string> &v) {
    // 补足程序
    // 。。。 
    for(int i=0;i<v.size();++i)
        cout << v[i] << " ";
    cout << endl;
}

// 函数实现
// 以迭代器方式输出vector<string>数组对象v的元素值 
void output2(vector<string> &v) {
    // 补足程序
    // 。。。 
    vector<string>::iterator it;
    for(it = v.begin();it != v.end();++it)
        cout << *it << " " ;
    cout << endl;
}

截图:

 2、实验内容2

6-17:

 

//指针p没有赋初值,这样的话会指向一个不确定地址,很危险。所以要为指针p赋初值,定义一个int型变量a初始化指针p。
//修改后的代码如下:
#include<iostream>
using namespace std;
int main(){
    int a; 
    int *p = &a;
    *p = 9;
    cout << "The value at p:" << *p;
    return 0; 
} 
//遇到的问题:第一次我想把指针p初始化为空指针int *p=NULL,发现还是无法运行。所以定义了另外一个变量。

 

 

6-18:

//代码中用new申请了堆空间用于存放一个int型数据,但是最后没有释放掉。我一开始加入了delete,但是发现报错,于是在定义a时就把a定义成指针a,int*型函数返回p,最后用delete释放内存。
//改正后:
#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;
    delete a;
    return 0; 
} 

3、实验内容3:

 

//main.cpp
#include <iostream>
#include "matrix.h"
using namespace std; 
int main() {
    int l,c;
    int i,j;
    int n;
    float M[l*c];
    cout << "输入矩阵行数和列数:" ;
    cin >> l >> c; 
    cout<<"输入矩阵:"<<endl; 
    for( i = 0 ; i < l*c ; i++ )
    cin >> M[i] ;
    Matrix m(l,c);
    m.setMatrix(M);
    cout << "输出矩阵:" << endl;
    m.printMatrix();
    cout << "输入i和j然后返回在第i行第j列的数:"  ;
    cin >> i >> j;
    cout << m.element(i,j) << endl;
    cout << "你希望重新设置这个数为:" ;
    cin >> n; 
    m.setElement (i,j,n);
    cout << "现在这个数为:"  ;
    cout << m.element(i,j) << endl;
    cout << "矩阵的行数与列数为:"  ;
    cout << m.getLines() << " " << m.getCols() << endl ;
    return 0;
}

 

//matrix.cpp
#include <iostream>
#include "matrix.h"
using namespace std;
int i,j;
//构造函数
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(i = 0;i < lines*cols;i++)
    p[i] = X.p[i];
}

//析构函数 释放空间 
Matrix::~Matrix(){
    delete[] p;
}

// 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值 
void Matrix::setMatrix(const float *pvalue){
    for(i = 0;i < lines*cols;i++)
    p[i] = pvalue[i];
}

// 显示矩阵
void Matrix::printMatrix() const{
    for(i = 0;i < lines;i++){
        for(j = 0;j < cols;j++)
            cout << p[i*lines+j] << " ";
        cout<<endl;
    }
}

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

运行截图:

输出函数有问题,我试着走了一遍的确有问题但是想不出解决方法……

过程中出现的问题:

1、函数中的p[(i-1)*lines + (j-1)]= value;我原本写了p+(i-1)*lines + (j-1)= value; 报错说赋值左边是变量不能是表达式。

2、中途有一次getLinsgetCols函数报错说不能有cv限定,百度了一下也没怎么懂什么是cv限定……

 

4、实验内容4

冷静下来写一下感觉其实很简单……但是考试的时候就很慌脑子一片空白……

第一题:

用时间做种子来实现随机数,一开始忘了加时间有关的头文件后来加上了。在类定义中加入了your_number实现输入自己的学号。循环500次随机,每抽到一次就把计数用的count1,最后用count除以次数500得到一个概率值,输出。

//1
#include<iostream>
#include<cstdlib> 
#include<ctime>
using namespace std;

class Dice{          //Dice类定义 
    public:
        Dice(int n,int a);
        int cast();
    private:
        int sides;
        int your_number; 
};


//构造函数 
Dice::Dice(int n,int a):sides(n),your_number(a){ }
int Dice::cast(){
    srand(time(NULL));
    int ra; //计数 
    for(int i = 0; i<500 ;i++){
        if(rand()%sides+1==your_number)
        ++ra;
    }
    return ra;
}

int main(){
    int nu,your_number;
    cout << "Please enter the number of the class:" ;
    cin >> nu;
    cout <<"Please enter your number in the class:";
    cin >> your_number; 
    Dice p1(nu,your_number);
    cout << "result : " << (float)p1.cast()/500 <<endl;
    return 0;
} 

 运行截图:

 

第二题:

 

//User.h

#ifndef USER_H
#define USER_H
#include<string>

using namespace std;

class User{
    public:
        User(string na,string pa = "111111");
        void printUser();
        void printCurrentID();
        void changePassword();
        int static getCurrentID();
    private:
        int id;
        string name;
        string password;
        static int CurrentID;
        
}; 

#endif

 

//User.cpp

#include"User.h"
#include<iostream>
#include<string>

using namespace std;

int User::CurrentID=999;

User::User(string na,string pa):name(na),password(pa){
     CurrentID++;
     id = CurrentID;
}

void User::printUser(){
    cout << "ID:" << id << endl; 
    cout << "Name:" << name << endl;
    cout << "Password:" << password << endl;
}

void User::printCurrentID(){
    cout << CurrentID << endl;
    cout << "ID:" << id << endl; 
    cout << "Name:" << name << endl;
    cout << "Password:" << password << endl;
}

void User::changePassword(){
    string OldPassword,NewPassword;
    bool n = true;
    cout << "请输入原密码:" ;
    while(n){
        cin >> OldPassword;
        if (OldPassword != password)
            cout << "密码错误!请重新输入:";
        else
            n = false; 
    }
    n = true;
    cout << "请输入新密码:";
    while(n){
        cin >> NewPassword;
        if(NewPassword == OldPassword)
            cout << "新密码不能和原密码相同!请重新输入新密码:" ;
        else
            n = false; 
    }
    cout << "修改成功!" ; 
    password = NewPassword; 
}

int User::getCurrentID(){
    return CurrentID;
}
//main.cpp

#include <iostream>
#include"User.h"
#include <string>
using namespace std;
int main() {
    string na,pa;
    cout << "请输入用户名:" ;
    cin >> na;
    cout << "请输入密码:" ;
    cin >> pa;
    User u(na,pa);
    u.printUser();
    u.printCurrentID();
    u.changePassword();
    cout << "当前ID:" << u.getCurrentID() << endl;
    return 0;
}

运行截图:

出现的问题:

1、定义构造函数时出现了两次不同的初始化导致报错。之后我在类的定义密码初始化成111111,在构造函数的定义中把密码再次初始化就ok了。

2、在重新设置密码那边,输入原密码判断是否需要重新输入时,我把输入写在了while循环外面导致疯狂输出“密码错误!请重新输入:”,然后把输入写到while循环里面就好了。

 

 第三题:

 

//book.h

#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 <iostream> 
#include <string>
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;
}
//main.cpp

#include "book.h"
#include <vector>
#include <iostream>
using namespace std;

int main()
{
    // 定义一个vector<Book>类对象
    // 补足程序
    // ... 
    vector<Book>books;
     
    string isbn, title;
    float price;
    
    // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
    // 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) 
    // 补足程序
    // ... 
    cout << "请录入图书信息(按下Ctrl+Z停止录入):" << endl; 
    int number;
    while(cin >> isbn >> title >> price){
        Book book(isbn,title,price);
        books.push_back(book);
        number++;
    }
    
    
    // 输出入库所有图书信息
    // 补足程序
    // ... 
    cout << "当前所有图书信息:" << endl;
    for (int n = 0; n < number ; n++)
        books[n].print();
    
    return 0;
}

运行截图:

出现的问题:

这道题就是不太懂怎么ctrl+Z结束,后来百度了发现原来本来就有这个功能……

猜你喜欢

转载自www.cnblogs.com/Werol714/p/9076387.html