zoukankan      html  css  js  c++  java
  • 实验5

    1.补全ex3

     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 using namespace std;
     5 
     6 // 函数声明 
     7 void output1(vector<string> &);
     8 void output2(vector<string> &);
     9 
    10 int main()
    11 {
    12     vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes
    13     likes.push_back("《他改变了中国》");
    14     likes.push_back("native faith");
    15     likes.push_back("火星救援");
    16     likes.push_back("pid:19766212");
    17     likes.push_back("幸运☆星");
    18     likes.push_back("慢跑");
    19     likes.push_back("暂无");
    20     // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) 
    21     // 补足代码 
    22     // 。。。 
    23 
    24 
    25     cout << "-----I like these-----" << endl;
    26     output1(likes);
    27     // 调用子函数输出vector<string>数组对象likes的元素值 
    28 
    29     dislikes.push_back("矫情的");
    30     dislikes.push_back("节奏强过头的");
    31     dislikes.push_back("强行改编毁原著的");
    32     dislikes.push_back("超现实主义");
    33     dislikes.push_back("某抗中奇侠");
    34     dislikes.push_back("短跑");
    35     dislikes.push_back("暂无");
    36     // 为vector<string>数组对象dislikes添加元素值 
    37 
    38     cout << "-----I dislike these-----" << endl;
    39     output1(dislikes);
    40     // 调用子函数输出vector<string>数组对象dislikes的元素值 
    41 
    42     likes.swap(dislikes);
    43     // 交换vector<string>对象likes和dislikes的元素值 
    44 
    45 
    46     cout << "-----I likes these-----" << endl;
    47     output2(likes);
    48     // 调用子函数输出vector<string>数组对象likes的元素值 
    49 
    50     cout << "-----I dislikes these-----" << endl;
    51     output2(dislikes);
    52     // 调用子函数输出vector<string>数组对象dislikes的元素值 
    53 
    54 
    55     return 0;
    56 }
    57 
    58 
    59 // 函数实现 
    60 // 以下标方式输出vector<string>数组对象v的元素值  
    61 void output1(vector<string> &v) {
    62     int i;
    63     for (i = 0; i < v.size(); ++i) {
    64         cout << v[i] << " ";
    65     }
    66     cout << endl;
    67 }
    68 
    69 // 函数实现
    70 // 以迭代器方式输出vector<string>数组对象v的元素值 
    71 void output2(vector<string> &v) {
    72     vector<string>::iterator it;
    73     cout << "(迭代器)" << "  ";
    74     for (it = v.begin(); it != v.end(); ++it) {
    75         cout << *it << " ";
    76     }cout << endl;
    77 }

    6.17

    1 #include<iostream>
    2 using namespace std;
    3 
    4 int main() {
    5     int *p;
    6     *p = 9;
    7     cout << "The value at p:" << *p;      //vs错误代码 c4700 指针p没有被初始化,没有指向地址
    8     return 0;
    9 }

    修改

     1 #include<iostream>
     2 using namespace std;
     3 
     4 int main() {
     5     int *p;
     6     int i;
     7     p = &i;
     8     *p = 9;
     9     cout << "The value at p:" << *p; 
    10     return 0;
    11 }

    6.18

     1 include<iostream>
     2 using namespace std;
     3 int fn1() {
     4     int *p = new int(5);    //只有new 没有delete
     5     return *p;
     6 }
     7 int main() {
     8     int a = fn1();
     9     cout << "the value of a is:" << a;
    10     return 0;
    11 }

    修改

     1 #include<iostream>
     2 using namespace std;
     3 int fn1() {
     4     int *p = new int(5);
     5     return *p;
     6     delete p;
     7 }
     8 int main() {
     9     int a = fn1();
    10     cout << "the value of a is:" << a;
    11     return 0;
    12 }

    3. 实现一个动态矩阵类 Matrix

    matrix.h

     1 class Matrix {
     2 public:
     3     Matrix(int n); // 构造函数,构造一个n*n的矩阵 
     4     Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 
     5     Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 
     6     ~Matrix(); //析构函数 
     7     void setMatrix(const float *pvalue); // 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值 
     8     void printMatrix() const; // 显示矩阵
     9     inline float &element(int i, int j); //返回矩阵第i行第j列元素的引用
    10     inline float element(int i, int j) const;// 返回矩阵第i行第j列元素的值 
    11     void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
    12     inline int getLines() const; //返回矩阵行数 
    13     inline int  getCols() const; //返回矩阵列数 
    14 private:
    15     int lines;    // 矩阵行数
    16     int cols;      // 矩阵列数 
    17     float *p;   // 指向存放矩阵数据的内存块的首地址 
    18 };

    matrix.cpp

     1 #include <iostream>
     2 #include "matrix.h"
     3 using namespace std;
     4 Matrix::Matrix(int n) :lines(n), cols(n) {    //构造函数的实现
     5     p = new float[lines*cols];
     6 }
     7 Matrix::Matrix(int n,int m) : lines(n), cols(m) {    //重载
     8     p = new float[lines*cols];
     9 }
    10 Matrix::~Matrix() {        //析构函数
    11     delete p;
    12 }
    13 Matrix::Matrix(const Matrix &X) :lines(X.lines),cols(X.cols){  //复制构造函数的实现
    14     p = new float[lines*cols];
    15     for (int i= 0;i < lines*cols; i++)
    16         p[i] = X.p[i];
    17 }
    18 void Matrix::setMatrix(const float *pvalue) {    //给矩阵赋值
    19     for (int i = 0; i < lines*cols; i++)
    20         p[i] = pvalue[i];
    21 }
    22 void Matrix::printMatrix() const {         //输出矩阵
    23     for (int i = 0; i < lines; i++) {
    24         for (int j = 0; j < cols; j++)
    25             cout << p[i*cols + j] << " ";
    26         cout << endl;
    27     }
    28 }
    29 inline int Matrix::getLines() const {return lines;}//行数
    30 inline int Matrix::getCols() const {return cols;}  //列数
    31 
    32 inline float &Matrix::element(int i, int j) {        //以下三个不知道对不对
    33     return p[(i - 1)*cols + j - 1];
    34 }
    35 inline float Matrix::element(int i, int j) const { return p[(i-1)*cols + j-1]; }
    36 void Matrix::setElement(int i, int j, int value) { element(i,j) = (float)value;}

    main.cpp

     1 #include<iostream>
     2 #include"matrix.h"
     3 using namespace std;
     4 int main()
     5 {
     6     Matrix M1(3); Matrix M2(3, 4); 
     7     const float a[9] = { 1,2,3,4,5,6,7,8,9 };
     8     const float b[12] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
     9     M1.setMatrix(a);
    10     M2.setMatrix(b);
    11     Matrix M3 = M2;
    12     cout << "M1为:" << endl;
    13     M1.printMatrix();
    14     cout << "M2为:" << endl;
    15     M2.printMatrix();
    16     cout << "M3为:" << endl;
    17     M3.printMatrix();
    18     cout << "将M1矩阵第2行第2列换为100" << endl; 
    19     //system("pause");
    20     M1.setElement(2, 2, 100);
    21     cout << "M1为:" << endl;
    22     M1.printMatrix();
    23     cout << "M2矩阵第2行第2列值为:" << M2.element(2, 2) << endl;
    24     return 0;
    25 }

    期中考试

    1.已提交

    2.用户管理

    User.h

     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 class User {
     5 public:
     6     User(string s1);
     7     User(string s1, string s2);
     8     User(User &X);
     9     void show();
    10     static void showCID();
    11     void changepassword();
    12 private:
    13     string name;
    14     int id;
    15     static int CurrentID;
    16     string password;
    17 };

    User.cpp

     1 #include<iostream>
     2 #include"user.h" 
     3 #include<string>
     4 using namespace std;
     5 int User::CurrentID = 999;    
     6 User::User(string s1){
     7     name = s1;
     8     password = "111111";
     9     CurrentID++;
    10     id = CurrentID;
    11 }
    12 User::User(string s1, string s2) :name(s1), password(s2) {
    13     CurrentID++;
    14     id = CurrentID;
    15 }
    16 User::User(User &X) : name(X.name), password(X.password) {
    17     CurrentID++;
    18     id = CurrentID;
    19 }
    20 void User::show() {
    21     cout << "id:" << id << endl;
    22     cout << "用户名:" << name << endl;
    23     cout << "密码:" << password << endl;
    24 }
    25 void User::showCID() {
    26     cout << "CurrentID:" << CurrentID << endl;
    27 }
    28 void User::changepassword() {
    29     string p, np; 
    30     int j= 0;
    31     while (1) {
    32         cout << "请输入原有密码:"<<endl; 
    33         cin >> p;
    34         if (p == password) {
    35             cout << "请输入新密码:"; cin >> np;
    36             password = np; break;
    37         }
    38         if (p != password) {
    39             j++; cout << "输入原密码错误!" << endl;
    40         }
    41         if (j == 3) {
    42         cout << "错误次数过多,请稍后再试" << endl;
    43         break;
    44         }
    45     
    46             }
    47 }

    main.cpp

     1 #include<iostream>
     2 #include"user.h" 
     3 #include<string>
     4 using namespace std;
     5 
     6 int main() {
     7     User u1("user1", "123456");
     8     User u2("user2");
     9     User u3("user3", "234567");
    10     u1.show(); u2.show(); u3.show(); User u(u3); u3.showCID();
    11     cout << "用户1换密码" << endl;
    12     u1.changepassword(); 
    13     u1.show();
    14     cout << "用户2换密码" << endl;
    15     u2.changepassword();
    16     return 0;
    17 }

    3.图书入库

    book.h

     1 #include <string>
     2 using std::string;
     3 
     4 class Book {
     5     public:
     6         Book(string isbnX, string titleX, float priceX);  //构造函数  
     7         void print(); // 打印图书信息 
     8     private:
     9         string isbn;
    10         string title;
    11         float price;
    12 };

    book.cpp

     1 #include "book.h"
     2 #include <iostream> 
     3 #include <string>
     4 using namespace std;
     5 
     6 // 构造函数
     7 Book::Book(string isbnX, string titleX, float priceX) :isbn(isbnX), title(titleX), price(priceX) {
     8 }
     9 
    10 
    11 // 打印图书信息
    12 void Book::print() {
    13     cout << "出版编号:" << isbn << endl;
    14     cout << "书名:" << title << endl;
    15     cout << "价格:" << price << endl;
    16 }

    main.cpp

     1 #include "book.h"
     2 #include <vector>
     3 #include <iostream>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     // 定义一个vector<Book>类对象
     9     vector<Book> b1;
    10      
    11     string isbn, title;
    12     float price;
    13     char quit;
    14     // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
    15     while (1) {
    16         cout << "输入信息:" << endl;
    17         cin >> isbn >> title >> price;
    18         Book a(isbn, title, price);
    19         b1.push_back(a);
    20         cout << "是否退出,退出输入“Z”" << endl;
    21         cin >> quit;
    22         if (quit == 'Z')
    23         { 
    24             int i;
    25             for (i = 0; i < b1.size(); i++)
    26             {
    27                 b1[i].print();
    28             }
    29             return 0;
    30         }
    31     }
    32     
    33     
    34     // 输出入库所有图书信息
    35     
    36 }

    体会

    会的太少,太丢人了

  • 相关阅读:
    BZOJ1527 : [POI2005]Pun-point
    2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016)
    2016-2017 ACM-ICPC Northwestern European Regional Programming Contest (NWERC 2016)
    NAIPC-2016
    BZOJ2498 : Xavier is Learning to Count
    ACM ICPC Vietnam National Second Round
    XVI Open Cup named after E.V. Pankratiev. GP of Ukraine
    XVI Open Cup named after E.V. Pankratiev. GP of Peterhof
    HDU5509 : Pattern String
    BZOJ4583 : 购物
  • 原文地址:https://www.cnblogs.com/zhibifenli/p/9080167.html
Copyright © 2011-2022 走看看