zoukankan      html  css  js  c++  java
  • 期中考试

    虽然这个试题在考试时没能做出来qaq,不过我还是觉得有必要搞清楚知识点,所以今天结合上网查的一些知识点和书本还是把它做出来了 

    题一 Dice类

      这个类很简单,考试时就做出来了,就不多说了,上代码和截图

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    class Dice {
    private:
        int sides;
    public:
        Dice(int n);
        int cast() { return rand()%sides+1; };
    };
    
    Dice::Dice(int n) :sides(n) {}
    
    int main() {
        int num, s=0;
        cout << "Num:";
        cin >> num;
        Dice d(num);
        for (int i = 1; i <= 500; i++) {
            if (d.cast() == 31)
                s++;
        }
        double p;
        p = (double)s / 500.0;
        cout << "The posibility of num.31 is " << p << endl;
        system("pause");
        return 0;
    }

    题二 User类

    考试时就是死在这个类上了,一直在纠结那个怎么获得最后一个对象的地址,那个静态变量的初始化也忘了,翻书看了资料才想起来。最后,我也是在翻网上的资料,翻到了反射可以获得这个类的类名,还要通过函数指针,突然就想到了可以通过静态指针来锁定最后一个对象,指针平常真的还是很少用和练习,虽然懂,但关键时候都想不起来,难受owo,要多看看练练指针了。

    其他就考考C风格字符串,没什么难点了,上代码!

    User.h

    #pragma once
    #include<string>
    using namespace std;
    
    class User {
    private:
        int id;
        string name;
        string password;
        static int CurrentID;
        static User *last;          //静态指针变量
    public:
        User(string n, string p="111111");
        void changepassword();
        void printData();    
        static void showCurrentID();     //static不要忘
    };

    User.cpp

    #include<iostream>
    #include<string>
    #include"User.h"
    using namespace std;
    
    int User::CurrentID = 999;     //静态变量在外面单独初始化
    User* User::last = NULL;
    User::User(string n, string p):id(++CurrentID),name(n),password(p){
        last = this;
    }
    void User::changepassword() {
        string oldpassword, newpassword = "0";
        int i = 3;
        while (i--) {
            cout << "Print your oldpassword:";
            cin >> oldpassword;
            if (password == oldpassword) {
                cout << "Right! Print your newpassword:";
                cin >> newpassword;
                password = newpassword;
                break;
            }
            else {
                if (i != 0)
                    cout << "The password is wrong! Print again!" << endl;
                else
                    cout << "The password is wrong!" << endl;
            }
        }
        if (newpassword != password)
            cout << "Too many faults! Try again later..." << endl;
    }
    
    void User::printData() {
        cout << "ID: " << id << ", Name: " << name << ", Password: " << password << endl;
    }
    
    void User::showCurrentID() {
        cout << "CurrentID: " << CurrentID << endl;
        cout << "The data of last one: ";
        last->printData();    //通过指针锁定
    }

    main.cpp

    #include<iostream>
    #include<string>
    #include"User.h"
    using namespace std;
    int main() {
        User user1("David");
        User user2("Nancy", "222222");
        User user3("Daniel", "123456");
        user1.printData();
        user2.printData();
        user3.printData();
    
        user1.changepassword();
        user1.printData();
        user2.changepassword();
        user2.printData();
    
        User::showCurrentID();
    
        system("pause");
        return 0;
    }

     

    题三 Book类

    这个类考试就最后结束瞄了一眼,当时觉得好像很简单,后悔没有做,可是做的时候又卡住了,发现考试看了也做不出来,虽然知道vector数组的定义和size的用法,可是这种未知成员个数的真的不会,后来还是上网查的要利用push_back来添加成员。

    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 << ", title: " << title << ", price: " << price << endl;
    }

    main.cpp

    #include "book.h"
    #include <vector>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        // 定义一个vector<Book>类对象
        vector <Book>books;               //先定义一个vector数组,不加个数和初值
        // 补足程序
        // ... 
         
        string isbn, title;
        float price;
        while (cin >> isbn >> title >> price) {
            Book book(isbn, title, price);
            books.push_back(book);              //利用push_back添加成员
        }
        // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
        // 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) 
        // 补足程序
        // ... 
        for (int i = 0; i < books.size(); i++) {
            books[i].print();
        }
        // 输出入库所有图书信息
        // 补足程序
        // ... 
        
        system("pause");
        return 0;
    }

    这次考试可能也是我大学第一次挂科,考完情绪当时十分低落,但至少现在好了很多,我知道了我的cplus学习中还有许多的不足,我要做的是练习,复习来弥补,这也是一种挫折和成长吧,不多说了,希望期末能够不再像现在这样重蹈覆辙了

  • 相关阅读:
    查询数据库锁的SQL
    注解学习实例(模拟hibernate,table,column注解,拼装SQL)
    mongoDB学习笔记
    拼装SQL.例子
    MySQL实现类似Oracle序列的函数
    面试总结
    linux下常用命令
    PHP 中 flush() 与 ob_flush() 的区别
    PHP 使用共享内存的资料
    移动设备的web站开发和将web封转成移动端应用的一些资料
  • 原文地址:https://www.cnblogs.com/zhaoluolong/p/9015694.html
Copyright © 2011-2022 走看看