1. 程式人生 > >c++第五次實驗報告

c++第五次實驗報告

mes ret 實驗 iterator 第三題 叠代器 sta AC RR

#include <iostream>
#include <vector>
#include <string>
using namespace std;

// 函數聲明 
void output1(vector<string> &);
void output2(vector<string> &);

int main()
{
    vector<string>likes, dislikes; // 創建vector<string>對象likes和dislikes
    likes = { "favorite book, music, film, paintings,anime,sport,sportsman,etc
" }; cout << "-----I like these-----" << endl; output1(likes); dislikes = { "running, studying, etc" }; cout << "-----I dislike these-----" << endl; output1(dislikes); swap(likes, dislikes); cout << "-----I likes these-----" << endl; output2(likes); cout
<< "-----I dislikes these-----" << endl; output2(dislikes); return 0; } // 函數實現 // 以下標方式輸出vector<string>數組對象v的元素值 void output1(vector<string> &v) { for (int i = 0; i!=v.size(); i++) cout << v[i] << endl; } // 函數實現 // 以叠代器方式輸出vector<string>數組對象v的元素值
void output2(vector<string> &v) { vector<string>::iterator itr = v.begin(); for (itr; itr != v.end(); itr++) cout << *itr << endl; }

技術分享圖片

二.1.

#include<iostream>
using namespace std;
int main() {
    int *p;//指針為初始化
    *p = 9;//未初始化後直接使用
    cout << *p << endl;
    return 0;
}

應該改為

#include<iostream>
using namespace std;
int main() {
    int *p=new int(9);//指針初始化
    cout << *p << endl;
delete p;
return 0; }

2.

#include<iostream>
using namespace std;
int fn1() {
    int *p = new int(5);//為指針p申請內存,函數中並未釋放指針p的內存
    return *p;//
}
int main() {
    int a = fn1();
    cout << "the value of a is: " << a;
    return 0;
}
#include<iostream>
using namespace std;
int fn1() {
    int *p = new int(5);//為指針p申請內存
    delete p;//若是在此處釋放,則導致沒法返回指針p指向的值,程序運行出錯
    return *p;
}
int main() {
    int a = fn1();
    cout << "the value of a is: " << a;
    return 0;
}

所以應該改為

#include<iostream>
using namespace std;
int fn1() {
int t = 5;
int *p = &t;
return *p;
}
int main() {
int a;
a=fn1();
cout << "the value of a is: " << a;
return 0;
}

三.

暫未完成,仍在調試

四.期中考試第二題

#include<iostream>
#include<string>
using namespace std;
class User {
public:
    User(string nm,string pd="111111") {
        name = nm;
        password = pd;
        id = Currentid + 1;
        Currentid++;
    };
    User(User&u) {
        name = u.name;
        password = u.password;
        id = u.id;
        Currentid++;
    };
    ~User() {};
    void printUser() {
        cout <<"name: "<< name << endl;
        cout <<"passward: " <<password<<endl;
        cout <<"id: "<<id << endl;
    };
    void resetpassward() {
        int n = 3;
        string oldpassward;
        while (n)
        {
            cout<<"請輸入原密碼"<<endl;
            cin >> oldpassward;
            if (password.compare(oldpassward) == 0)
            {
                cout << "請輸入新密碼" << endl;
                cin >> password;
                break;
            }
            else
                cout<<"輸入錯誤,";
                n--;
        }
    };
    void printCurrentid() {
        cout << Currentid << endl;
        cout << "id: "<<id << endl;
        cout << "name: " << name << endl;
        cout << "passward: "<<password<<endl;
    
    };
private:
    string name;
    string password;
    int id;
    static int Currentid;
};
int User::Currentid = 999;
int main() {
    User u1("liurui", "111111");
    u1.printUser();
    u1.printCurrentid();
    u1.resetpassward();
    return 0;
}

技術分享圖片

期中考試第三題

#include<string>
using namespace std;
class Book {
public:
    Book(string isbnX, string titleX,float priceX);
    void print();
private:
    string title;
    string isbn;
    float price;
};
#include"book.h"
#include<iostream>
using namespace std;
Book::Book(string isbnX, string titleX, float priceX) :isbn(isbnX), title(titleX), price(priceX) {};
void Book::print() {
    cout << "isbn: "<<isbn << endl;
    cout << "title: " << title<<endl;
    cout << "price: " << price << endl;
};
#include<iostream>
#include"book.h"
#include<vector>
using namespace std;
int main() {
    vector<Book>books;
    string isbn, title;
    int price;
    while (true) {
        cout << "輸入書名" << endl;
        cin >> title;
        cout << "輸入編號" << endl;
        cin>> isbn;
        cout << "輸入價格" << endl;
        cin >> price;
        Book book1(isbn, title, price);
        books.push_back(book1);
        cout << "輸入c繼續,輸入s停止" << endl;
        char a;
        cin >> a;
        if (a == s)
            break;
        else;
    };
    for (int i = 0; i < books.size(); i++)
        books[i].print();
    return 0;
}

技術分享圖片

c++第五次實驗報告