第五次cplus实验

Posted manganese123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第五次cplus实验相关的知识,希望对你有一定的参考价值。

一。实验结论:

1.ex3:

代码:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
void output1(vector<string> &);  
void output2(vector<string> &);  
int main()
{
    vector<string>likes, dislikes; 
    likes.push_back("Music games");
    likes.push_back("Dead by daylight");
    likes.push_back("Japanese");
    likes.push_back("Cue ball");
    likes.push_back("Chemistry");
    likes.push_back("etc.");
    cout << "-----I like these-----" << endl;
    output1(likes); 
    dislikes.push_back("King of glory");
    dislikes.push_back("Basketball");
    dislikes.push_back("Eggplant");
    dislikes.push_back("Soap opera");
    dislikes.push_back("Ecomony");
    dislikes.push_back("etc.");
    cout << "-----I dislike these-----" << endl;
    output1(dislikes);
    likes.swap(dislikes);
    cout << "-----I likes these-----" << endl;
    output2(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]<<endl;
    }
}
void output2(vector<string> &v) {
    int i;
    for(vector<string>::iterator i=v.begin();i<v.end();++i)
    {
        cout<<*i<<endl;
    }
}

运行结果:

技术分享图片

2.习题6-17:

#include<iostream>
using namespace std;
int main()
{
    int *p;
    //*p=9;指针指向不明
    p=new int(9); 
    cout<<"The value at p:"<<*p;
    return 0;
}

习题6-18:

#include<iostream>
using namespace std;
int fn1()
{
    int *p=new int(5);
    delete p;//释放内存
    return *p;
}
int main()
{
    int a=fn1();
    cout<<"The value of a is "<<a;
    return 0;
}

3.Matrix类的定义

#ifndef MATRIX_H
#define MATRIX_H
class Matrix {
    public:
        Matrix(int n); // ???????????????*n??????
        Matrix(int n, int m); // ???????????????*m??????
        Matrix(const Matrix &X); // ??????????????????????????????
        ~Matrix(); //?????? 
        void setMatrix(const float *pvalue); // ??????,??pvalue???????豼鯡??????????? 
        void printMatrix() const; // ????????
        inline float &element(int i, int j) //????????i????j????????????
        {
            float &q=p[(i-1)*lines+j-1];
            return q;
        }
        inline float element(int i, int j) const// ????????i????j?????????? 
        {
            return p[(i-1)*lines+j-1];
        }
        void setElement(int i, int j, int value); //?靹?????i????j??????????value
        inline int getLines() const; //?????????? 
        inline int getCols() const; //?????????? 
    private:
        int lines;    // ??????
        int cols;      // ?????? 
        float *p;   // ???豺????????????豼魴??????? 
};
#endif

Matrix类的实现:

 1 #include"matrix.h"
 2 #include<iostream>
 3 using namespace std;
 4 Matrix::Matrix(int n)
 5 {
 6     lines=n;
 7     cols=n;    
 8     p=new float[lines*cols];
 9 }
10 Matrix::Matrix(int m,int n)
11 {
12     lines=n;
13     cols=m;
14     p=new float[lines*cols];
15 }
16 Matrix::Matrix(const Matrix &X)
17 {
18     int i;
19     lines=X.lines;
20     cols=X.cols;
21     p=new float[X.lines*X.cols];
22     for(i=0;i<X.lines*X.cols;i++)
23         p[i]=X.p[i];
24 }
25 Matrix::~Matrix()
26 {
27     delete []p;
28 }
29 void Matrix::setMatrix(const float *pvalue)
30 {
31     int i;
32     for(i=0;i<lines*cols;i++)
33     {
34         p[i]=pvalue[i];
35     }
36 }
37 void Matrix::printMatrix() const
38 {
39     int i;
40     for(i=0;i<lines*cols;i++)
41     {
42         if((i%lines!=0||i==0)&&i!=lines*cols-1)
43             cout<<p[i]<<" ";
44         else if(i==lines*cols-1)
45         {
46             cout<<p[i]<<endl;
47         }
48         else
49         {
50             cout<<endl;
51             cout<<p[i]<<" ";
52         }
53     }
54 }
55 void Matrix::setElement(int i,int j,int value)
56 {
57     element(i,j)=value;
58 }
59 inline int Matrix::getCols() const
60 {
61     return cols;
62 }
63 inline int Matrix::getLines() const
64 {
65     return lines;
66 }

主函数:

#include"matrix.h"
#include<iostream>
using namespace std;
int main()
{
    int m,n,i,j,v,x,y;
    while(1)
    {
        cout<<"Please input the lines and cols:";
        cin>>n>>m;    
        Matrix a(n,m),b(n);
        if(m<=1||n<=1)
        {
            cout<<"Error!"<<endl;
            continue;
        }
        float p[m*n],q[n*n];
        cout<<"Please input the elements of the matrix A:"<<endl;
        for(i=0;i<m*n;i++)
        {
            cin>>p[i];
        }
        a.setMatrix(p);
        cout<<"Please input the elements of the matrix B:"<<endl;
        for(i=0;i<n*n;i++)
        {
            cin>>q[i];
        }
        b.setMatrix(q);
        while(1)
        {
            cout<<"Press 1 to modify, press 2 to print, press 3 to change the matrix, press 4 to quit, please input a number:";
            cin>>x;
            if(x==1)
            {
                while(1)
                {
                    cout<<"Press 1 to modify the matrix A, press 2 to modify the matrix B, press 3 to back:";
                    cin>>y;
                    if(y==1)
                    {
                        while(1)
                        {
                            cout<<"Please input the line, col and the value after modify of the Matrix A:";
                            cin>>i>>j>>v;
                            if(i<=0||i>n||j<=0||j>m)
                            {
                                cout<<"Error!Please input again!"<<endl;    
                            }
                            else
                            {
                                a.setElement(i,j,v);
                                break;
                            }
                        }
                    }
                    else if(y==2)
                    {
                        while(1)
                        {
                            cout<<"Please input the line, col and the value after modify of the Matrix B:";
                            cin>>i>>j>>v;
                            if(i<=0||i>n||j<=0||j>n)
                            {
                                cout<<"Error!Please input again!"<<endl;    
                            }
                                else
                            {
                                b.setElement(i,j,v);
                                break;
                            }
                        }
                    }
                    else if(y==3)
                    {
                        break;
                    }
                    else
                        continue;
                }
            }
            else if(x==2)
            {
                cout<<"The Matrix A is:"<<endl;
                a.printMatrix();
                cout<<"The Matrix B is:"<<endl;
                b.printMatrix();
            }
            else if(x==3||x==4)
            {
                break;
            }
            else
                continue;
        }
        if(x==4)
        {
            break;
        }
    }
    return 0;
}

运行结果:

技术分享图片

技术分享图片

PS:(1).一张图装不下运行结果,所以运行结果分两张图表示(第一张图的最后两行与第二张图的前两行重合)

(2).不知道怎么回事,注释中的汉字拷贝粘贴过来就会变成乱码(第二题的那个注释是我自己改成汉字的)。。。为了防止乱码影响其他同学的阅读,所以我删除了第一题(ex3)以及第四题第三小题(books类)中的注释。

4.期中考试习题:

(1)第一题代码:

#include<iostream>
#include<cstdlib>
using namespace std;
class Dice
{
    public:
        Dice(int n);
        int cast();
    private:
        int sides;
};
Dice::Dice(int n)
{
    sides=n;
}
int Dice::cast()
{
    int a=rand()%sides+1;
    return a;
}
int main()
{
    int side,number,i,m,t=0;
    float p;
    cin>>side;
    Dice a(side);
    cin>>number;
    for(i=0;i<500;i++)
    {
        m=a.cast();
        if(m==number)
        {
            t++;
        }
    }
    p=float(t)/500;
    cout<<p;
    return 0;    
}

第一题运行结果:

技术分享图片

(2)第二题:

Users类的定义:

#include<iostream>
#include<string>
using namespace std;
class Users
{
    public:
        Users(string Name,string Password="111111");
        void change();
        void print();
        void show();
    private:
        int id;
        string name;
        string password;
        int static CurrentID;
};

Users类的实现:

#include"users.h"
#include<iostream>
#include<string>
using namespace std;
int id;
int Users::CurrentID=999;
Users::Users(string Name,string Password)
{
    CurrentID++;
    id=CurrentID;
    name=Name;
    password=Password;
}
void Users::change()
{
    int count=0;
    string s;
    while(1)
    {
        cout<<"Please input the original password:";
        cin>>s;
        if(s==password)
        {
            cout<<"You are correct!"<<endl;
            cout<<"Please input the new password:";
            cin>>password;
            break;
        }
        else 
        {
            if(count!=2)
                cout<<"You are wrong! Please try again:";
            count++;
        }
        if(count==3)
        {
            cout<<"Please try again later."<<endl;
            break;
        }
    }
}
void Users::print()
{
    cout<<"Current ID:"<<id<<" Name:"<<name<<" Password:"<<password<<endl;
}
void Users::show()
{
    cout<<CurrentID<<endl;
}

主函数:

#include"Users.h"
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    int i,id,j;
    string Name,Password;
    vector<Users>users;
    while(1)
    {
        cout<<"Press 1 to insert; press 2 to modify; press 3 to print; press 4 to show; press 5 to end; Please input number:";
        cin>>i;
        if(i==1)
        {
            cout<<"Name:";
            cin>>Name;
            cout<<"Password:";
            cin>>Password;
            Users u(Name,Password);
            users.push_back(u);
        }
        else if(i==2)
        {
            cout<<"Please input the id:";
            cin>>id;
            users[id-1000].change();
        }
        else if(i==3)
        {
            for(j=0;j<users.size();j++)
            {
                users[j].print();
            }
        }
        else if(i==4)
        {
            users[i].show();
        }
        else if(i==5)
        {
            break;
        }
        else
            continue;
    }
    return 0;
}

运行结果:

技术分享图片

(3)第三题:

Books类的定义:

#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

Books类的实现:

#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<<" Title:"<<title<<" Price:"<<price<<endl;
}

主函数:

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

int main()
{
    vector<Book>book;
    string isbn, title;
    float price;    
    
    while(cin>>isbn>>title>>price)
    {
        Book b(isbn,title,price);
        book.push_back(b);
    }
    int i;
    for(i=0;i<book.size();i++)
    {    
        book[i].print();
    }
    return 0;
}

运行结果:

技术分享图片

二。实验总结与讨论:

       通过这次实验,我在类的声明与实现以及构造函数这两个方面上实现了突破。但还是存在着一些不足:首先,编程速度太慢,反复调试次数太多。仅仅做期中考试那3道题就用了2个半小时,而考试时要求1小时完成,远远超出了考试要求的时间。其次,还有一些知识掌握的不太熟练,比如说深复制的应用以及vector类的应用。所以在这门学科上,以后还有很长的路要走。

以上是关于第五次cplus实验的主要内容,如果未能解决你的问题,请参考以下文章

第五次实验

20165210 Java第五次实验报告

《Java程序设计》第五次实验实验报告

第五次作业

第五次实验(数组实验)

JLU数据结构荣誉课第五次上机实验