期中.....
第一题Dice类
- Dice.h
#ifndef DICE_H
#define DICE_H
class Dice{
public :
Dice(int num);
int rest();
private :
int sides;
};
#endif
- Dice.cpp
#include<iostream>
#include<cstdlib>
#include"Dice.h"
using namespace std;
Dice::Dice(int num):sides(num){
}
int Dice::rest(){
int num;
num=rand()%sides;
return num;
}
- main.cpp
#include<iostream>
#include"Dice.h"
#include<ctime>
#define student_number 35
using namespace std;
int main()
{
srand(time(NULL));
Dice my_Dice(40);
int count=0;
for(int i=0;i<500;i++)
{
if(my_Dice.rest()==student_number)
{
count++;
}
}
double times=count/500.0;
cout<<"学号为"<<student_number<<"的学生,在500次中,被抽中的概率为:"<<times<<endl;
}
User类
考试时候想不到要怎么在输出CurrentID的同时输出最后一名用户信息(想了好久),回来后突然想到可以和CurrentID一样,声明一个静态指针,用来存放最后一个对象的地址....... ,然后就是给静态成员赋值,在多文件结构下,一开始在User.h中进行定义(这个是错误的),后面网上了解过后才知道应该在User.cpp里面定义,不然就会出现重复定义的错误。
- User.h
#ifndef USER_H
#define USER_H
#include<string>
using namespace std;
class User {
public:
User(string n, string word = "111111");
void setpassword();
void showdata();
static void showCurrentID();
private:
string name;
string password;
int id;
static User* final;
static int CurrentID;
};
#endif
- User.cpp
#include<iostream>
#include<string>
#include"User.h"
using namespace std;
int User::CurrentID = 999;
User* User::final = NULL;
User::User(string n, string word) :name(n), password(word) {
CurrentID++;
id = CurrentID;
final = this;
}
void User::setpassword() {
string oldpassword;
int n = 3;
while (n--)
{
cout << "Please enter oldpassword:";
cin >> oldpassword;
if (oldpassword == password)
{
cout << "Please enter newpassword:";
cin >> password;
break;
}
else {
cout << "Password mistake..." << endl;
}
}
if (n == 0)
{
cout << "Too many errors,please re_operate..." << endl;
}
}
void User::showdata() {
cout << "id : " << id << " name : " << name << " password : " << password << endl;
}
void User::showCurrentID() {
cout << "CurrentID: " << CurrentID << endl;
cout << "The final user's data:" << endl;
final->showdata();
}
- main.cpp
#include<iostream>
#include"User.h"
using namespace std;
int main()
{
User user1("bird"), user2("pig"), user3("cat");
user1.showdata();
user2.showdata();
user3.showdata();
user1.setpassword();
user1.showdata();
User::showCurrentID();
return 0;
}
Book类
考试时候卡在第二题,这题没看,不过vector的一些函数还不会用,应该也是做不出来。主要是用到.push_back() 这个函数进行插入.
- 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 : " << isbn << " title : " << title << " price : " << price << endl;
}
- main.cpp
#include "book.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// 定义一个vector<Book>类对象
vector<Book>books;
string isbn, title;
float price;
// 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
// 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式)
while (cin >> isbn >> title >> price) {
Book book(isbn, title, price);
books.push_back(book);
}
// 输出入库所有图书信息
for (int i = 0; i < books.size(); i++)
{
books[i].print();
}
return 0;
}