实验五

实验五

1.

vector3.cpp 完整程序及运行截图

#include <vector>
#include <string>
#include <iomanip>
using namespace std;
// 函数声明 
void output1(vector<string> &); 
void output2(vector<string> &); 
int main()
{
vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes

// 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) 
// 补足代码 
// 。。。 
likes.push_back("favorite book");
likes.push_back("music");
likes.push_back("film");
likes.push_back("paintings");
likes.push_back("anime");
likes.push_back("sport");
likes.push_back("sportsman");
likes.push_back("etc"); 

cout << "-----I like these-----" << endl;
// 调用子函数输出vector<string>数组对象likes的元素值 
// 补足代码
// 。。。 
output1(likes);
// 为vector<string>数组对象dislikes添加元素值 
// 补足代码 
// 。。。 
dislikes.push_back("favorite book");
dislikes.push_back("music");
dislikes.push_back("sport");
dislikes.push_back("anime");
dislikes.push_back("etc");

cout << "-----I dislike these-----" << endl;
// 调用子函数输出vector<string>数组对象dislikes的元素值 
// 补足代码
// 。。。 
output1(dislikes);
// 交换vector<string>对象likes和dislikes的元素值 
// 补足代码
// 。。。 
swap(dislikes,likes);

cout << "-----I likes these-----" << endl;
// 调用子函数输出vector<string>数组对象likes的元素值 
// 补足代码
// 。。。 
output1(likes);

cout << "-----I dislikes these-----" << endl;
// 调用子函数输出vector<string>数组对象dislikes的元素值 
// 补足代码
// 。。。 
output1(dislikes);

return 0;
}
// 函数实现 
// 以下标方式输出vector<string>数组对象v的元素值 
void output1(vector<string> &v) {
// 补足程序
// 。。。 
for(int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}

// 函数实现
// 以迭代器方式输出vector<string>数组对象v的元素值 
void output2(vector<string> &v) {
// 补足程序
// 。。。 
}


 

6-17

#include<bits/stdc++.h>
using namespace std;
int main(){
int *p;
p=new int (1); 
*p=9;
cout<<"The value at p:"<<*p;
    delete p;
return 0;
} 

  

 

6-18

#include<iostream>
using namespace std;
int p1;
int *p = &p1;  
int fnl() {
    p = new int(5);
    return *p;
}

int main() {
    int a = fnl();
    cout << "the value of a is: " << a << endl;
    delete p; 
    system("pause");
    return 0;
}

 

  

 

期中考试

1.

#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() == 28)
            s++;
    }
    double p;
    p = (double)s / 500.0;
    cout << "The posibility of num.28 is " << p << endl;
    system("pause");
    return 0;
}

  

2.

#include<iostream>
#include<string>
using namespace std;
static int n=999;
class User {
private:
    int id; string name; string password; static int CurrentID;
public:
    User(string s1,string s2="111111") {
        n++; id = n;
        name = s1; password = s2; CurrentID++;
    }
    User(User &u1) {
        id = u1.id; name = u1.name; password = u1.password; CurrentID = u1.CurrentID;
    }
    void show1() {
        cout << "该用户信息为 id:" << id << " name:" << name << " password:" << password << endl;
    }
    static void show2(User &u1) {      //输出CurrentID
        cout << "CurrentID:" << CurrentID << endl; cout << "最后一个新增用户为:";
        u1.show1();
    }
    void change() {                         //修改密码
        string s3, s4; int j; j = 3;
        while (j) {
            cout << "请输入原有密码:"; cin >> s3;
            if (s3 == password) {
                cout << "请输入新密码:"; cin >> s4;
                password = s4; break;
            }
            if (s3 != password) {
                j--; cout << "输入原密码错误!" << endl;
            }
        }
        if (j == 0)cout << "错误次数过多,请稍后再试" << endl;
    }
}; int User::CurrentID = 999;//给CurrentID赋初值
int main() {
    User u1("user1", "123456");
    User u2("user2");
    User u3("user3", "234567");
    u1.show1(); u2.show1(); u3.show1(); User u(u3); User::show2(u3);
    u1.change(); u1.show1();
    u2.change();
    return 0;
}

  

 

总结:

这次实验其实还有很多不会的。。。还需要继续深入学习。

 

posted @ 2018-05-23 23:24  Anx听风吟  阅读(167)  评论(0)    收藏  举报