【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/

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<ctime>
 4 using namespace std;
 5 class Dice{
 6     public:
 7     Dice(int n);
 8     ~Dice();
 9     int cast();
10     private:
11     int sides;
12 }; 
13 Dice::Dice(int n){
14     sides=n;
15 }
16 Dice::~Dice(){
17     
18 }
19  int   Dice::cast(){
20  
21      int a;
22     a=rand()%sides;
23     return a;
24 }
25 int main(){
26     int num,i;
27     cin>>num;
28     Dice d(num);
29     int b[num];
30     for(i=0;i<num;i++)
31     b[i]=0;
32     
33     int times,t;
34     cin>>times;
35     t=times;
36     while(times--){
37         srand(times) ;
38        i=d.cast();
39        b[i]++;
40     }
41     
42     for(i=0;i<num;i++){
43         cout<<i+1<<"  被点中的概率    "<<(double)b[i]/t<<endl; 
44     }
45     return 0;
46 }
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

 1 #ifndef User_H
 2 #define User_H
 3 #include <string>
 4 using std::string;
 5 class User{
 6     public:
 7         User(string y,string z="111111");
 8         void printmessage();
 9         void newpassword();
10         ~User();
11         void static show();
12           int static UserID;
13         private:
14             int id;
15             string name;
16             string password;
17           
18 };
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 #endif

User.cpp

 1 #include "User.h"
 2 #include <iostream> 
 3 #include <string>
 4 using std::string;
 5 using namespace std;
 6 int User::UserID=999;
 7 User::User(string y,string z){
 8     UserID++;
 9     id=UserID;
10     name=y;
11     password=z;
12 }
13 void User::show(){
14     cout<<UserID<<endl;
15 }
16 User::~User(){
17             UserID--;
18         }
19 void User::printmessage(){
20 
21     cout<<"编号:   "<<id<<"   姓名: "<<name<<"  密码:  "<<password<<endl; 
22 }
23 
24 void User::newpassword(){
25         string s,s2;
26     int n=3,j=0;
27      
28     while(n--){
29         cout<<"请输入现有密码:";
30         cin>>s;
31         cout<<endl;
32     if(s==password){
33         cout<<"请输入新密码:";
34         cin>> s2;
35         password=s2;
36         cout<<endl;
37         cout<<"修改成功!"<<endl;
38         cout<<endl;
39          break;
40     }
41     else
42     {
43         cout<<"您的密码有误,请重新输入密码!";
44         cout<<endl;
45         j++;
46         }    
47     }
48     
49         if(j==3){
50             cout<<"请稍后再进行修改!"<<endl; 
51         }
52     
53 }

main.cpp

 1 #include "User.h"
 2 #include <string>
 3 #include <iostream>
 4 using std::string;
 5 using namespace std;
 6 int main(){
 7     int *p1;
 8     User *u,*v;
 9     p1=&User::UserID;
10     cout<<*p1+1<<endl;
11     cout<<*p1+2<<endl;
12     cout<<*p1+3<<endl;
13     string h="xiao",l="123211";
14     User a(h,l),b("guaiguai"),c("lily");
15     u=&c; 
16     
17 
18     
19     cout<<u<<endl;
20     a.printmessage();
21     b.printmessage();
22     c.printmessage();
23      
24     b.newpassword();
25     c.newpassword(); 
26     
27     u->printmessage(); 
28     
29     return 0;
30 }

运行截图:

3. 图书入库 

main.cpp

 1 #include "book.h"
 2 #include <vector>
 3 #include<string>
 4 #include <iostream>
 5 using namespace std;
 6 using std::string;
 7 int main()
 8 {
 9      
10     
11      vector<Book>books;// 定义一个vector<Book>类对象
12     
13     string isbn, title;
14     float price;
15     int j=0;
16     
17     while(cin>>isbn>>title>>price){
18         Book book(isbn,title,price);
19         j++;
20         books.push_back(book);
21     }
22     // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
23     // 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) 
24     
25     for(int i=0;i<j;i++)
26     {
27         books[i].print();
28     }
29     
30     // 输出入库所有图书信息
31     // 补足程序
32     // ... 
33     
34     
35     return 0;
36 }

book.h

 1 #ifndef BOOK_H
 2 #define BOOK_H
 3 
 4 #include <string>
 5 using std::string;
 6 
 7 class Book {
 8     public:
 9         Book(string isbnX, string titleX, float priceX);  //构造函数  
10         void print(); // 打印图书信息 
11     private:
12         string isbn;
13         string title;
14         float price;
15 };
16 #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中,我添加了一个测试函数之后,编译报错。可是语法我并没有检查出什么错误。

原文地址:https://www.cnblogs.com/yitou13/p/9033527.html