C + + experiment 5

Source: Internet
Author: User

Experimental conclusion

Experimental content 1
Source

#include <iostream> #include <vector> #include <string>using namespace std;//function declaration void output1 (vector  <string> &);  void Output2 (vector<string> &);    int main () {vector<string>likes, dislikes;//create vector<string> object likes and dislikes Likes.push_back ("book");       Likes.push_back ("Music");    Likes.push_back ("film");    Likes.push_back ("paintings");     add element values for vector<string> Array object likes (favorite book, Music, film, PAINTINGS,ANIME,SPORT,SPORTSMAN,ETC)//complement code             。。。    cout << "-----I like these-----" << Endl;    OUTPUT1 (likes);     Call child function output vector<string> Array object likes element value//complement code//...    Dislikes.push_back ("anime");    Dislikes.push_back ("Sport");    Dislikes.push_back ("Sportsman");         add element value for vector<string> Array object dislikes//complement code//...    cout << "-----I dislike these-----" << Endl;    Output2 (dislikes); Call the child function output vector<string> Array object dislikes element value//complementFull Code//...    Likes.swap (dislikes);             Exchange Vector<string> object likes and dislikes element values//complement code//...    cout << "-----I likes these-----" << Endl;    OUTPUT1 (likes);         Call child function output vector<string> Array object likes element value//complement code//...    cout << "-----I dislikes these-----" << Endl;    Output2 (dislikes);                                     Call child function output vector<string> Array object dislikes element value//complement code//... return 0;} The function implements//follows the output of the Vector<string> array object v element value void Output1 (Vector<string> &v) {for (int i=0;i<v.size ();    i++) cout<<v[i]<< "";    cout<<endl; Top-up program//... The}//function implements//outputs an iterator to the element value of vector<string> array object v void Output2 (vector<string> &v) {vector<string>::    Iterator it;    For (It=v.begin (); It!=v.end (); it++) cout<<*it<< "";    cout<<endl; Top-up program//... }



Experimental Content 2
SOURCE 1

#include <iostream>using namespace std;int main(){    //int a;     //int *p=&a;  可以先定义一个变量a,并将其地址赋给指针p     int *p;//指针再被使用前没有被赋初值     *p=9;    cout<<"The value at p: "<<*p;    return 0;}

SOURCE 2

#include <iostream>using namespace std;int fn1(){    int *p=new int(5);    return *p;//用new分配的内存没有用delete释放,会导致分配的内存无法回收     //delete p;}int main(){    int a=fn1();    cout<<"the value of a is: "<<a;    return 0;}

Experimental Content 3
Source
Main.cpp

#include <iostream>#include "matrix.h"using namespace std; int main() {    int l,c,i,j,n;    cout<<"输入矩阵行数和列数"<<endl;    cin>>l>>c;     float a[l*c];    cout<<"输入矩阵的各行各列的数"<<endl;     for(i=0;i<l*c;i++)    cin>>a[i];    Matrix A(l,c);    A.setMatrix(a);    cout<<"显示矩阵"<<endl;    A.printMatrix();    cout<<"输入i,j 返回第i行第j列的数"<<endl;    cin>>i>>j;    A.element(i,j);    cout<<"重新设置这个数"<<endl;    cin>>n;     A.setElement (i,j,n);    cout<<"重新返回这个数"<<endl;    A.element(i,j);    cout<<"返回矩阵行数与列数"<<endl;    A.getLines() ;    A.getCols() ;    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;} void Matrix::setmatrix (const float *pvalue) {for (i=0;i<lines*cols;i++) p[i]=pvalue[i];} void Matrix::p rintmatrix () const{for (i=0;i<lines;i++) {for (j=0;j<cols;j++) cout<<p[i*lines+j]<< ;"    ";    cout<<endl; }}inline float matrix::element (int i,int j) const{cout<<p[(i-1) *lines+j-1]<<endl;} void matrix::setelement (int i, int j, int value) {p[(i-1) *lines+j-1]=value;} inline int matrix::getlines () const{Cout<<lines<<endl;} inline int matrix::getcols () const{Cout<<cols<<endl;}  

Matrix.h

#ifndef MATRIX_H#define MATRIX_Hclass 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) const;// 返回矩阵第i行第j列元素的值         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



Experimental Content 4
The second question of midterm
Source
Main.cpp

#include <iostream>#include"user.h"#include <string>using namespace std;int main() {    string n,p;    cout<<"请输入用户名和密码"<<endl;    cin>>n>>p;    User user(n,p);    user.print();    user.change();    cout<<"当前ID:";    user.getCurrentID();     return 0;}

User.h

class User{    public:        User(string n,string p="111111");        void print();        void change();        void static getCurrentID();    private:        int id;        string name,password;        int static CurrentID;};

User.cpp

  #include <iostream> #include "user.h" #include <string>using namespace Std;int user::currentid= 999;    User::user (String n,string p= "111111"): Name (n), password (p) {Id=++currentid;     }void User::p rint () {cout<< "subscriber number:" <<id<<endl;    cout<< "User name:" <<name<<endl; cout<< "Password:" <<PASSWORD<<ENDL;}    void User::change () {string p;    cout<< "Please enter old password" <<endl;    while (true) {cin>>p; if (p!=password) cout<< "password wrong!    Please re-enter "<<endl;     else break;    } string Newp;    while (true) {cout<< "Please enter a new password" <<endl;    cin>>p; if (P==password) cout<< "The new password cannot be the same as the old password!"    "<<endl,continue;    cout<< "Please enter your new password again" <<endl;    cin>>newp;    if (newp!=p) cout<< "two times password is different, please re-enter" <<endl;     else cout<< "Modify Success" <<endl,break; } PASSWORD=NEWP;} void User::getcurrentid () {Cout<<currentid<<endl;}  



The third question of the midterm
Source
Main.cpp

#include "book.h"#include <vector>#include <iostream>using namespace std;int main(){    cout<<"请输入出版编号,书名及定价"<<endl;    vector<Book>mybooks;     string isbn, title;    float price;    int s=0;    while(cin>>isbn>>title>>price){        Book book(isbn,title,price);        mybooks.push_back(book);        s++;     }    for(int i=0;i<s;i++){        mybooks[i].print();    }    return 0;}

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<<endl;    cout<<"书名:"<<title<<endl;    cout<<"定价:"<<price<<endl;}


Experimental summary

Once again to see the topic of the midterm, feel not to do when it feels so difficult, maybe the time of the exam is tight, half can not have clear and concise ideas, so do not know, and for the function return reference this aspect in the experiment encountered some problems, check the function return value and return reference of the difference, When a function returns a value, a temporary variable is generated to indirectly assign the value, and the function return reference does not produce a temporary variable, but rather a direct assignment, which saves a bit of memory and improves some efficiency.

C + + experiment 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.