博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实验5
阅读量:4638 次
发布时间:2019-06-09

本文共 2946 字,大约阅读时间需要 9 分钟。

 实验内容1:vector3.cpp 完整程序及运行截图

#include <iostream>

#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("sport");
    dislikes.push_back("sportsman");
    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<iostream>

using namespace std;

int main() {
    int p1;
    int *p = &p1; 

  *p = 9;

    cout << "The value at p:" << *p << endl; 
    system("pause");
    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;
}

 

 

 

第3题不会

 

 

期中考试:

第一题:

源代码:

 

#include<iostream>
#include<cstdlib>
using namespace std;
class Dice{
    public:
        Dice (int n);
        int cast();
    private:
        int sides;
   
};
Dice::Dice(int n):sides(n) {}

 

int Dice::cast(){
    int a=rand()%sides+1;
    return a;
}
int main(){
    Dice p(40);
    int m;
    for(int i=0;i<500;i++)
    {
        m=p.cast();
        if(m==27)
        m++;
    }
    float n=m/500.0;
    cout<<n<<endl;
    return 0;
}
运行结果:

 

 

 

第二题:

源代码:

#include<iostream>

#include<cstdlib>
#include<ctime>
using namespace std;
class Dice {
private:int sides;
public:
    Dice(int n) {
        sides = n;
    }
    int cast() {
        int m;
        m = (rand() % (sides - 1 + 1) + 1);
        return m;
    }
};
int main() {
    srand((unsigned)time(NULL));//切记:srand别放在循环体里,否则效果感人……
    int n,x; cout << "请输入班级人数和你的学号:";
    cin >> n >> x;
    Dice A(n);
    int i; int m;double y = 0.00;
    for (i = 0; i < 500; i++) {
        if (A.cast() == x)y++;
    }
    double p = (double)y / 500.00;
    cout << "你的学号被抽中的概率为:" << p << endl;
    return 0;
}

 

运行结果:

 

 

第三题不会...

 

转载于:https://www.cnblogs.com/chuan12138/p/9079459.html

你可能感兴趣的文章
转--Android如何在java代码中设置margin
查看>>
反射的所有api
查看>>
上传文件
查看>>
css 定位及遮罩层小技巧
查看>>
用java向mysql数据库中插入数据为空
查看>>
项目中非常有用并且常见的ES6语法
查看>>
mac 端口转发方案
查看>>
[2017.02.23] Java8 函数式编程
查看>>
Knowledge Point 20180305 数据在计算机中的表示
查看>>
谈谈对web标准的理解
查看>>
求二进制中1的个数(编程之美2.1)
查看>>
58前端内推笔试2017(含答案)
查看>>
Java学习笔记
查看>>
sprintf 和strcpy 的差别
查看>>
打表打表何谓打表?
查看>>
MPEG4与.mp4
查看>>
实验5
查看>>
git 下载 安装
查看>>
录制终端信息并回放
查看>>
JS中window.event事件使用详解
查看>>