第一题
#include<iostream> #include<cstdlib> #include<ctime> using namespace std; class Dice { public: Dice(int n) :sides(n) {}; int cast(); private:int sides; }; int Dice::cast() { return(rand() % (sides - 1 + 1) + 1); } int main() { srand(time(NULL)); int n, m; cout << "请输入班级人数和你的学号:"; cin >> n >> m; Dice A(n); int num = 0; for (int i = 0; i<500; i++) { if (A.cast() == m) num++; } double p = 0; p = num / 500.0; cout << "你的学号被抽中的概率为:" << p << endl; return 0; }
第二题
//user.h #pragma once #include<iostream> #include<string> using namespace std; class user{ public: user(string s1); user(string s1, string s2); user(user &p); void show1(); static void show2(); void changepassword(); private: int id; string name,password; static int CurrentID; };
//user.cpp #include "user.h" #include<iostream> #include<string> using namespace std; int user::CurrentID = 999; user::user(string s1):name(s1) { password = "111111"; CurrentID++; id = CurrentID; } user::user(string s1, string s2) :name(s1), password(s2) { CurrentID++; id = CurrentID; } user::user(user &p) : name(p.name), password(p.password) { CurrentID++; id = CurrentID; } void user::show1() { cout << "id:" << id << "用户名:" << name << "密码:" << password << endl; } void user::show2() { cout << "CurrentID:" << CurrentID << endl; } void user::changepassword() { //改密码 int n = 3; while (n--) { string re; cout << "请输入密码:" << endl; cin >> re; if (password == re) { cout << "请输入新密码:" << endl; cin >> password; break; } else { cout << "密码错误,您还有" << n << "次输入机会。" << endl; } if (n == 0) cout << "密码错误次数过多,请稍后再试!" << endl; } }
//main.cpp #include"user.h" #include<iostream> #include<string> using namespace std; int main() { user u1("user1"); user u2("user2", "123456"); user u3("user3", "666999"); u1.show1(); u2.show1(); u3.show1(); user u(u3); u3.show2(); cout << "user2改密码" << endl; u2.changepassword(); u2.show1(); cout << "user3改密码" << endl; u3.changepassword(); return 0; }
//book.h #pragma once #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 << endl; cout << "书名:" << title << endl; cout << "价格:" << price << endl; }
//main.cpp #include "book.h" #include <vector> #include <iostream> using namespace std; int main() { vector<Book>A;// 定义一个vector<Book>类对象 string isbn, title; float price; while (1) { // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中 cout << "请输入图书信息,若想终止输入, 请输入Quit " << endl; cout << "出版编号:" ; cin >> isbn ; if (isbn != "Quit") { //判断是否输入 cout << "书名:"; cin >> title; cout << "价格:"; cin >> price; } if (isbn == "Quit") //输入Quit终止 break; Book a(isbn, title, price); A.push_back(a); } cout << endl; for (int i = 0; i<A.size(); i++) // 输出入库所有图书信息 A[i].print(); return 0; }