码迷,mamicode.com
首页 > 编程语言 > 详细

c++第五次实验报告

时间:2018-05-23 23:37:13      阅读:33      评论:0      收藏:0      [点我收藏+]

标签:turn   code   cin   ima   isbn   running   iterator   iostream   name   

#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++第五次实验报告

标签:turn   code   cin   ima   isbn   running   iterator   iostream   name   

原文地址:https://www.cnblogs.com/miaorui1314/p/9073635.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!