1. 程式人生 > 其它 >實驗報告2

實驗報告2

實驗結果:

實驗4程式碼如下:info.hpp

#include <iostream>
#include <string>

using namespace std;

class info
{
private:
    string nickname;
    string contact;
    string city;
    int n;
    static int mebers_reserved;

public:
    info(){ n = 0; cout << "initialized without any parameter!\n"
; } info(string a, string b, string c, int d) : nickname(a), contact(b), city(c), n(d) { mebers_reserved += d;} info(const info& a) : nickname(a.nickname), contact(a.contact), city(a.city), n(a.n) { mebers_reserved += a.n;} void print() const; int current_audience_number() { return
mebers_reserved; } }; int info::mebers_reserved = 0; void info::print() const { cout << "the name of the member is: " << nickname << endl; cout << "contact :" << contact << endl; cout << "city:" << city << endl; cout << "
the number of is group:" << n << endl; }

main.cpp

#include <iostream>
#include <string>
#include "info.hpp"
#include <vector>

using namespace std;
int main()
{
    vector<info> audience_info_list;
    const int max_audience = 100;
    cout << "please print the data of audience on the screen! and type end to end this programme!\n";
    string nickname;
    string contact;
    string city;
    int n;
    while (cin >> nickname)
    {
        cin >> contact;
        cin >> city;
        cin >> n;
        audience_info_list.push_back(info(nickname, contact, city, n));
        if (audience_info_list[0].current_audience_number() > max_audience)
        {
            cout << "oop! too more audience ! error , the programme is finished!\n";
            audience_info_list.pop_back();
            break;
        }
    }

    cout << "untile now , there are " << audience_info_list[0].current_audience_number()  << " members in this group!\n";
    for (auto i = audience_info_list.begin(); i != audience_info_list.end(); ++i)
        (*i).print();

    return 0;
}

測試1:輸入control + z 自動結束,列印

測試2:當人數輸入超過最大上線的人數時,程式結束,列印當前資訊

實驗5程式碼如下:

#include <iostream>
#include <string>

using namespace std;
class TextCoder
{
private:
    string text;
public:
    TextCoder() { cout << "initialized without any input!\n"; }
    TextCoder(string a) : text(a) {}
    TextCoder(TextCoder& a) :text(a.text) {}

    string encoder();
    string decoder();
};

string TextCoder::encoder()
{
    string temp = text;
    for (auto &ch : temp)
    {
        if (ch >= 'A' && ch <= 'U')
            ch += 5;
        else if (ch >= 'V' && ch <= 'Z')
            ch = 'A' + ch - 'V';
        else if (ch >= 'a' && ch <= 'u')
            ch += 5;
        else if (ch >= 'v' && ch < 'z')
            ch = 'a' + ch - 'v';
    }
    return temp;
}
string TextCoder::decoder()
{
    string temp = text;
    for (auto &ch : temp)
    {
        if (ch >= 'f' && ch <= 'z')
            ch -= 5;
        else if (ch >= 'a' && ch <= 'e')
            ch = 'z' + ch - 'e';
        if (ch >= 'F' && ch <= 'Z')
            ch -= 5;
        else if (ch >= 'A' && ch <= 'E')
            ch = 'Z' + ch - 'E' ;
    }
    return temp;
}

實驗5測試:

大小寫轉化正常,輸入contr z退出

實驗總結:

1、語法層面:

(1)類陣列的模板類構造使用是預設建構函式以及複製建構函式,所以,在處理人數的時候,如果在構造的時候已經將總人數自增了,那麼在傳引數給模板類,讓模板類壓棧的時候,就不應該再一次增加人數,也就是複製建構函式不應該有member += d 這一句,因為這樣人數會變成原來的兩倍。

(2)auto for迴圈的語法,for(auto xx : temp)自動選取temp裡面的序列,有點像是python裡面的for i in {sequence},但是這個sequence是由auto自動取得的,注意,如果想要修改xx,則應該為&xx,表示xx為temp序列的一個引用,如果是xx,那麼諸如x += 5,就是相當於將這個指標往後跳五個序列長度,這裡要注意。

2、開發層面:

(1)要學會用新版本的新特性,這樣可以節省生命,讓程式設計更輕鬆一點。