1. 用掷骰子方式,模拟班级每个学号被随机抽点的概率。 (12分) 具体要求如下: (1)设计并实现一个骰子类Dice。 ① 数据成员sides表示骰子面数。构造时,指定骰子是6面,8面,还是其它数值。 ② 成员函数int cast()是掷骰子操作的抽象,返回一个位于1~sides之间的随机数。 例如,如果骰子是6面,返回1~6之间的随机数;如果骰子是40面,返回1~40之间的随机数。

图1 UML类图 Dice类

(2)在main中,定义一个骰子对象,以班级人数作为骰子的面数构造。 比如,软嵌(计嵌)班级 40人,输入40后,以40作为骰子面数构造一个骰子对象。 使用骰子对象,掷骰子500次,统计并输出自己学号(取学号最后两位)被抽点中的概率。

附:相关函数原型: ① int rand(void) 返回一个0~RAND_MAX之间的伪随机数; 头文件<cstdlib>(也有的在<cmath>) http://www.cplusplus.com/reference/cstdlib/rand/ ② void srand (unsigned int seed); 为rand()设置随机种子。通常以时间作为随机种子,即srand(time(NULL)) 头文件<cstdlib>(也有的在<cmath>), <ctime> http://www.cplusplus.com/reference/cstdlib/srand/

 #include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
class Dice{
public:
Dice(int n);
~Dice();
int cast();
private:
int sides;
};
Dice::Dice(int n){
sides=n;
}
Dice::~Dice(){ }
int Dice::cast(){ int a;
a=rand()%sides;
return a;
}
int main(){
int num,i;
cin>>num;
Dice d(num);
int b[num];
for(i=;i<num;i++)
b[i]=; int times,t;
cin>>times;
t=times;
while(times--){
srand(times) ;
i=d.cast();
b[i]++;
} for(i=;i<num;i++){
cout<<i+<<" 被点中的概率 "<<(double)b[i]/t<<endl;
}
return ;
}

Dice随机数

截图:

2. 用户管理(新用户添加、密码修改) (18 分) 具体要求如下: (1)基于以下场景表述设计并实现用户类User ① 每一个用户有用户编号(id), 用户名(name), 密码(password)三个属性。其中,用户编号(id)由系统自动 顺序编号,用户名和密码都是字母、数字、符号的组合,新用户密码,默认”111111”。Use 类所有对象还 有一个共有属性CurrentID,用来记录当前已经被使用的最大id号(初始值999)。 每当新增一个新用户时,CurrentID的值加1,同时将这个值作为新用户的id号。 例如: 新增第1个用户user1时,CurrentID为1000,同时,user1的id自动编号为1000 新增第2个用户user2时,CurrentID为1001,同时,user2的id自动编号为1001

② 除了构造函数外,还要求User类能实现以下要求: a) 打印用户信息,包括用户编号(id), 用户名(name), 密码(password) b) 修改密码。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。如果输入旧密码时,连续三 次输入错误,则提示用户稍后再试,暂时退出修改密码程序。 c) 打印 User类所有对象共有属性CurrentID,在打印该属性同时,打印最后一个新增用户的信息。

(2)在 main中创建 User类实例,测试User类的各项操作(新用户添加、密码修改、共有属性及最后一 个新增用户信息打印)

User.h

 #ifndef User_H
#define User_H
#include <string>
using std::string;
class User{
public:
User(string y,string z="");
void printmessage();
void newpassword();
~User();
void static show();
int static UserID;
private:
int id;
string name;
string password; }; #endif

User.cpp

 #include "User.h"
#include <iostream>
#include <string>
using std::string;
using namespace std;
int User::UserID=;
User::User(string y,string z){
UserID++;
id=UserID;
name=y;
password=z;
}
void User::show(){
cout<<UserID<<endl;
}
User::~User(){
UserID--;
}
void User::printmessage(){ cout<<"编号: "<<id<<" 姓名: "<<name<<" 密码: "<<password<<endl;
} void User::newpassword(){
string s,s2;
int n=,j=; while(n--){
cout<<"请输入现有密码:";
cin>>s;
cout<<endl;
if(s==password){
cout<<"请输入新密码:";
cin>> s2;
password=s2;
cout<<endl;
cout<<"修改成功!"<<endl;
cout<<endl;
break;
}
else
{
cout<<"您的密码有误,请重新输入密码!";
cout<<endl;
j++;
}
} if(j==){
cout<<"请稍后再进行修改!"<<endl;
} }

main.cpp

 #include "User.h"
#include <string>
#include <iostream>
using std::string;
using namespace std;
int main(){
int *p1;
User *u,*v;
p1=&User::UserID;
cout<<*p1+<<endl;
cout<<*p1+<<endl;
cout<<*p1+<<endl;
string h="xiao",l="";
User a(h,l),b("guaiguai"),c("lily");
u=&c; cout<<u<<endl;
a.printmessage();
b.printmessage();
c.printmessage(); b.newpassword();
c.newpassword(); u->printmessage(); return ;
}

运行截图:

3. 图书入库

main.cpp

 #include "book.h"
#include <vector>
#include<string>
#include <iostream>
using namespace std;
using std::string;
int main()
{ vector<Book>books;// 定义一个vector<Book>类对象 string isbn, title;
float price;
int j=; while(cin>>isbn>>title>>price){
Book book(isbn,title,price);
j++;
books.push_back(book);
}
// 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
// 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) for(int i=;i<j;i++)
{
books[i].print();
} // 输出入库所有图书信息
// 补足程序
// ... return ;
}

book.h

 #ifndef BOOK_H
#define BOOK_H #include <string>
using std::string; class Book {
public:
Book(string isbnX, string titleX, float priceX); //构造函数
void print(); // 打印图书信息
private:
string isbn;
string title;
float price;
};
#endif

book.cpp

#include "book.h"
#include <iostream>
#include <string>
using namespace std;
Book::Book(string isbnX, string titleX, float priceX){
isbn=isbnX;
title=titleX;
price=priceX;
}
void Book::print(){
cout<<"编号 "<<isbn<<" 标题 "<<title<<" 价格 "<<price<<endl;
}

问题:在使用编译器devc++5.10,修改类和函数任何一处,然后程序就别想编译了。但是函数是对的呀,不明白为啥。

比如程序2中,我添加了一个测试函数之后,编译报错。可是语法我并没有检查出什么错误。

【C++ mid-term exerises】的更多相关文章

  1. 最全的MySQL基础【燕十八传世】

    1.课前准备! 开启mysql服务:1).配置环境变量;2).net start mysql 将该sql文件导入到你的数据库中,以下所有操作都是基于该数据库表操作的!!! [此笔记是本人看着视频加上自 ...

  2. 【Reporting Services 报表开发】— 表达式

    一.常用的SSRS原始函数可以打开文本框的表达式中看到,如图1 图1 如下为SSRS中设计报表时常用的运算函数: 运算符/函数 说明 + 前后位数字则为加法,前后为字符串则为链接符号 - 数值减法 * ...

  3. 【Open Search产品评测】- 来往,7天轻松定制属于自己的搜索引擎

    [Open Search产品评测]--   来往,7天轻松定制属于自己的搜索引擎   [使用背景] 相信很多人都遇到过要给网站或者app做一个搜索功能的需求,很久之前自己折腾过lucene,搞了很久, ...

  4. 【NOIP 2012 疫情控制】***

    题目描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都, 也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散 ...

  5. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  6. 【动态规划】【二分】【最长上升子序列】Vijos P1028 魔族密码

    题目链接: https://vijos.org/p/1028 题目大意: 给N个字符串(N<=2000),求能组成词链的单词最多有几个. 如果在一个由一个词或多个词组成的表中,除了最后一个以外, ...

  7. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

  8. 【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1041 题目大意: 一个二维平面,上面n(n<=1 000 000)个点.问至少选 ...

  9. 【Lucene4.8教程之三】搜索

    1.关键类 Lucene的搜索过程中涉及的主要类有以下几个: (1)IndexSearcher:执行search()方法的类 (2)IndexReader:对索引文件进行读操作,并为IndexSear ...

  10. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

随机推荐

  1. mac 下apache服务的根目录

    根据文章的介绍 http://jingyan.baidu.com/article/67508eb434539f9cca1ce4da.html apache服务的根目录是在 /Library/WebSe ...

  2. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

  3. Eclipse和MyEclipse使用技巧--解决MyEclipse中的js报错的小方法

    今天,下了个模版,但是导进去的时候发现js会报错.看了下其他都没有错误.而有一个js报错误,请原谅我有点红色强迫症,不能留一点红色 . 错误如下:Syntax error on token " ...

  4. 单片机成长之路(51基础篇) - 021 STC89C51系列单片机 内部EEPROM 驱动

    最近又看了一下关于stc单片机的知识,感觉在使用中EEPROM是个经常用到的东西,特地学习了一下,给大家分享一下心得,如有不足,多多包涵,废话不多说,一图解千言,先上图: /*------------ ...

  5. linux 设备驱动概述

    linux 设备驱动概述 目前,Linux软件工程师大致可分为两个层次: (1)Linux应用软件工程师(Application Software Engineer):       主要利用C库函数和 ...

  6. laravel中及其常用的一些函数方法(自己看)和技巧(不断添加中)

    手册:https://laravelacademy.org/ 1.中间件的定义Middleware 2.路由的定义和写法 3.控制器Controller之Request 4.控制器Controller ...

  7. CountDownLatch、CyclicBarrier和Semaphore 使用示例及原理

    备注:博客园的markDown格式支持的特别不友好.也欢迎查看我的csdn的此篇文章链接:CountDownLatch.CyclicBarrier和Semaphore 使用示例及原理 CountDow ...

  8. 物联网架构成长之路(26)-Docker构建项目用到的镜像2

    0. 前言 前面介绍的都是一些标准的第三方中间件,基本都是有现成的Dockerfile或者Image,不需要我过多的关心,这一篇要介绍一些自己构建的Docker Image了.刚开始学,Dockerf ...

  9. Spring Boot项目的接口防刷

    说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点:springboot的基本知识,redis基本操作, 首先是写一个注解类: import java.lang.an ...

  10. C语言 · 求先序遍历

    算法训练 求先序排列   时间限制:1.0s   内存限制:256.0MB        锦囊1 后序的最后一个字母为根结点.   问题描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树 ...