【C++ mid-term exerises】
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】的更多相关文章
- 最全的MySQL基础【燕十八传世】
1.课前准备! 开启mysql服务:1).配置环境变量;2).net start mysql 将该sql文件导入到你的数据库中,以下所有操作都是基于该数据库表操作的!!! [此笔记是本人看着视频加上自 ...
- 【Reporting Services 报表开发】— 表达式
一.常用的SSRS原始函数可以打开文本框的表达式中看到,如图1 图1 如下为SSRS中设计报表时常用的运算函数: 运算符/函数 说明 + 前后位数字则为加法,前后为字符串则为链接符号 - 数值减法 * ...
- 【Open Search产品评测】- 来往,7天轻松定制属于自己的搜索引擎
[Open Search产品评测]-- 来往,7天轻松定制属于自己的搜索引擎 [使用背景] 相信很多人都遇到过要给网站或者app做一个搜索功能的需求,很久之前自己折腾过lucene,搞了很久, ...
- 【NOIP 2012 疫情控制】***
题目描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都, 也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散 ...
- 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...
- 【动态规划】【二分】【最长上升子序列】Vijos P1028 魔族密码
题目链接: https://vijos.org/p/1028 题目大意: 给N个字符串(N<=2000),求能组成词链的单词最多有几个. 如果在一个由一个词或多个词组成的表中,除了最后一个以外, ...
- 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...
- 【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1041 题目大意: 一个二维平面,上面n(n<=1 000 000)个点.问至少选 ...
- 【Lucene4.8教程之三】搜索
1.关键类 Lucene的搜索过程中涉及的主要类有以下几个: (1)IndexSearcher:执行search()方法的类 (2)IndexReader:对索引文件进行读操作,并为IndexSear ...
- 【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 ...
随机推荐
- SpringCloud无废话入门02:Ribbon负载均衡
1.白话负载均衡 在上一篇的介绍中,我们创建了两个一模一样的服务提供者:Provider1和Provider2,然后它们提供的服务也一模一样,都叫Hello-Service.为什么一样的服务我们要部署 ...
- 《分布式任务调度平台XXL-JOB》
一.简介 1.1 概述 XXL-JOB是一个轻量级分布式任务调度框架,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并接入多家公司线上产品线,开箱即用. 1.2 特性 1.简单:支 ...
- 通过配置JVM参数解决生成日志存在乱码问题
项目上生产环境时出现一个问题,生成的日志文件存在乱码,服务器编码为 LANG=zh_US.UTF-8,按理说中文不会存在乱码,这里存在一个问题,就是在SIT等其他环境我们使用的连接工具为Xshell, ...
- Swift 拷贝文件夹,实现文件夹内容整体复制
我们知道,在沙盒内,iOS要拷贝一个文件,可以使用 fileManager.copyItem(atPath: fullPath, toPath: fulltoPath) 方法简单实现,不过当我们要拷贝 ...
- Spring Framework 5.x 学习专栏
Spring Framework 5.0 入门篇 Spring构建REST Web Service 消费一个RESTful Web Service 事务管理 Spring使用JDBC访问关系数据 任务 ...
- Linux应该知道的技巧
https://coolshell.cn/articles/8883.html https://www.quora.com/Linux/What-are-some-time-saving-tips-t ...
- Super expression must either be null or a function, not undefined
按照之前买的用JavaScript开发移动应用的例子来编写的,然后报了这个错.我的头部声明是这样的 var React = require('react-native'); var { Text, V ...
- idea 配置 maven 项目
maven 项目 用模块引入进来 1.引入 pom.xml 2.如果不是web则要添加web支持 3.配置资源 类 和依赖 and 项目语言环境 5.配置 artifacts 部署 w ...
- 【交换机】交换机RLDP(环路检测&链路检测)功能介绍及配置说明
功能简介RLDP 全称是Rapid Link Detection Protocol,是锐捷网络自主开发的一个用于快速检测以太网链路故障的链路协议.一般的以太网链路检测机制都只是利用物理连接的状态,通过 ...
- fastDFS 命令笔记
端口开放 这是命令运行的前提 iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT iptables -I I ...