我写了个石头剪子布,增加写入功能,可以读取数据

 1 //computer.cpp
 2 #include "Computer.h"
 3 void Computer::Input() {
 4     srand((unsigned)time(0));
 5     for (int i = 0; i < 10; i++) {
 6         radom = rand() % 3;
 7     }
 8     switch (radom) {
 9     case 0:setbase(0);
10         break;
11     case 1:setbase(1);
12         break;
13     case 2:setbase(2);
14         break;
15     }
16 }
17 void Computer::Show()const {
18     cout << "电脑出的是:" << getbase() << endl;
19 }
20 void Computer::Read(fstream& f) const {
21     f.read((char*)this, sizeof(Computer));
22 }
23 void Computer::Write(fstream& f)const {
24     f.write((char*)this, sizeof(Computer));
25 }
 1 //computer.h
 2 #pragma once
 3 #include"project.h"
 4 class Computer:virtual public project
 5 {
 6 public:
 7     Computer() {};
 8     virtual ~Computer() {};
 9     void Input();
10     void Show()const;
11     void Read(fstream& f)const;
12     void Write(fstream& f)const;
13 };

以上是COMPUTER类的声明和实现

//people.h
#pragma once
#include"project.h"
class people:virtual public project
{
public:
 people() {};
 virtual ~people() {};
 void Input();
 void Show()const;
 void Read(fstream& f)const;
 void Write(fstream& f)const;
};
//people.cpp 
1
#include "people.h" 2 void people::Input() { 3 cout << "请输入石头(1),剪刀(2),布(3)" << endl; 4 cin >> num; 5 switch (num) { 6 case 1:setbase(0); 7 break; 8 case 2:setbase(1); 9 break; 10 case 3:setbase(2); 11 break; 12 default: 13 cout << "输入失败!" << endl; 14 Input(); 15 } 16 } 17 void people::Show() const { 18 cout << "您出的是:" << getbase() << endl; 19 } 20 void people::Read(fstream&f) const { 21 f.read((char*)this, sizeof(people)); 22 } 23 void people::Write(fstream& f)const { 24 f.write((char*)this, sizeof(people)); 25 }
 1 //PROJECT.H
 2 #pragma once
 3 #include<iostream>
 4 #include<string>
 5 #include<time.h>
 6 #include<fstream>
 7 using namespace std;
 8 enum base { rock, scissor, cloth };
 9 class project
10 {
11 protected:
12     int num;
13     int radom;
14 public:
15     virtual ~project() {};
16     virtual void Input() = 0;
17     void setbase(int b) {
18         num = (base)b;
19     }
20     int getnum() {
21         return num;
22     }
23     string getbase() const {
24         string c;
25         if (num == 0) {
26             c = "石头";
27         }
28         else if (num == 1) {
29             c = "剪刀";
30         }
31         else
32             c = "";
33         return c;
34     }
35     virtual void Show()const = 0;
36     virtual void Read(fstream& f)const = 0;
37     virtual void Write(fstream& f)const = 0;
38 };
 1 //systemm.h
 2 #pragma once
 3 #include"people.h"
 4 #include"Computer.h"
 5 #include"usertype.h"
 6 class systemm 
 7 {
 8 protected:
 9     fstream file;
10     usertype* usertable;
11     int maxSize;                                            
12     int count;        
13 public:
14     systemm();
15     ~systemm();
16     void Adduser(const usertype& e);
17     void adddate();
18     void run();
19     void show();
20 };
//systemm.cpp
#include "systemm.h"
static double wins = 0.0;
bool inline UserSaysYes()
{
    char flag = 'N';
    cin >> flag;
    if (flag == 'y' || flag == 'Y')
    {
        return true;
    }
    else
    {
        return false;
    }
}
systemm::systemm()
{
    ifstream userfile("user.idx", ios::binary);                
    if (!userfile.fail())
    {
        userfile.seekg(0, ios::end);                            
        count = userfile.tellg() / sizeof(systemm);            
        maxSize = count + 100;
        usertable = new usertype[maxSize];                    
        userfile.seekg(0, ios::beg);                            
        int i = 0;                                                
        userfile.read((char*)& usertable[i++], sizeof(usertype));
        while (!userfile.eof())
        {        
            userfile.read((char*)& usertable[i++], sizeof(usertype));
        }
        userfile.close();                                        
    }
    else
    {        
        count = 0;                                                
        maxSize = count + 100;                        
        usertable = new usertype[maxSize];                    
    }
    ifstream iFile("user.dat");                                
    if (iFile.fail())
    {                                                            //打开文件失败,表示文件不存在
        ofstream oFile("user.dat");                            //建立输出文件
        if (oFile.fail())throw("打开文件失败!");                //抛出异常
        oFile.close();                                            //关闭文件
    }
    else
    {    
        iFile.close();                                            //关闭文件
    }
    file.open("user.dat", ios::in | ios::out | ios::binary);        //以读写的方式打开文件
    if (file.fail())throw("打开文件失败!");                        //抛出异常
}
systemm::~systemm()
{
    ofstream userfile("user.idx", ios::binary);            
    for (int i = 0; i < count; i++)
    {
        userfile.write((char*)& usertable[i], sizeof(usertype));
    }
    userfile.close();                                    
    file.close();                                            
}
void systemm::Adduser(const usertype& e)
{
    if (count >= maxSize)        //扩展缓存区长度,主要是为了防止缓存区溢出
    {
        maxSize += 100;
        usertype* temusertable = new usertype[maxSize];
        for (int i = 0; i < count; i++)
        {
            temusertable[i] = usertable[i];
        }
        delete[]usertable;
        usertable = temusertable;
    }
    //添加元素
    usertable[count++] = e; //加入到索引表中
}
void systemm::adddate()
{
    Computer* computer = new Computer;
    people* peo = new people;
    usertype computeritem;    
    computeritem.usertype = '1';
    usertype peopleitem;
    peopleitem.usertype = '2';
    int ss = 3;
    int f = 0;
    file.seekg(0, ios::end);        //将dat文件的指针指向末尾,为下一步的增加数据做准备
    do                            //第一个循环,来确认是否继续输入
    {
        do {
            if (f > 0) {
                Computer* computer = new Computer;
                people* peo = new people;
            }
            computer->Input();
            peo->Input();
            if (computer->getnum() == 0 && peo->getnum() == 1) {
                computer->Show();
                peo->Show();
                ss = 0;
            }
            else if (computer->getnum() == 1 && peo->getnum() == 2) {
                computer->Show();
                peo->Show();
                ss = 0;
            }
            else if (computer->getnum() == 2 && peo->getnum() == 0) {
                computer->Show();
                peo->Show();
                ss = 0;
            }
            else if (computer->getnum() == 0 && peo->getnum() == 2) {
                computer->Show();
                peo->Show();
                ss = 1;
            }
            else if (computer->getnum() == 1 && peo->getnum() == 0) {
                computer->Show();
                peo->Show();
                ss = 1;
            }
            else if (computer->getnum() == 2 && peo->getnum() == 1) {
                computer->Show();
                peo->Show();
                ss = 1;
            }
            else if (computer->getnum() == 0 && peo->getnum() == 0) {
                computer->Show();
                peo->Show();
                ss = 2;
            }
            else if (computer->getnum() == 1 && peo->getnum() == 1) {
                computer->Show();
                peo->Show();
                ss = 2;
            }
            else if (computer->getnum() == 2 && peo->getnum() == 2) {
                computer->Show();
                peo->Show();
                ss = 2;
            }
            if (ss == 2) {
                cout << "平局,请继续";
                computeritem.num = computer->getnum();
                peopleitem.num = peo->getnum();
                computeritem.position = file.tellg();
                Adduser(computeritem);
                computer->Write(file);
                delete computer;
                file.seekg(0, ios::end);             
                peopleitem.position = file.tellg();
                Adduser(peopleitem);
                peo->Write(file);
                delete peo;
            }
            f++;
        } while (ss == 2);
        if (ss == 1) {
            cout << "你输了" << endl;
            computeritem.num = computer->getnum();
            peopleitem.num = peo->getnum();
            computeritem.position = file.tellg();
            Adduser(computeritem);
            computer->Write(file);
            delete computer;
            file.seekg(0, ios::end);
            peopleitem.position = file.tellg();
            Adduser(peopleitem);
        }
        else {
            cout << "你赢了" << endl;
            wins += 1;
            computeritem.num = computer->getnum();
            peopleitem.num = peo->getnum();
            computeritem.position = file.tellg();
            Adduser(computeritem);
            computer->Write(file);
            delete computer;
            file.seekg(0, ios::end);
            peopleitem.position = file.tellg();
            Adduser(peopleitem);
        }
        peo->Write(file);
        cout << "是否要继续添加?";
        delete peo;
    } while (UserSaysYes());
}
void systemm::show() {
    Computer* computer = new Computer;
    people* peo = new people;
    int pos;
    file.seekg(0, ios::beg);
    for (pos = 0; pos < count; pos++) {
        if (usertable[pos].usertype == '1') {
            cout << "" << pos + 1 << "" << endl;
            file.seekg(usertable[pos].position, ios::beg);
            computer->Read(file);
            computer->Show();
        }
        else {
            cout << "" << pos + 1 << "" << endl;
            file.seekg(usertable[pos].position, ios::beg);
            peo->Read(file);
            peo->Show();
        }
    }
    cout << "胜率为:" << (wins / (pos + 1)) * 100 << "%" << endl;
    if (pos == 0) {
        cout << "数据为空" << endl;
    }
}
void systemm::run() {
    int select;
    do
    {
        cout << "请选择" << endl;
        cout << "1.开始/继续游戏" << endl;
        cout << "2.显示游戏记录" << endl;
        cout << "3.退出游戏" << endl;
        cin >> select;
        while (cin.get() != '\n');

        switch (select)
        {
        case 1:
            adddate();
            break;
        case 2:
            show();
            break;
        case 3:
            break;
        default:
            cout << "输入失败!" << endl;
        }
    } while (select != 3);
}
 1 //undertype.h
 2 #pragma once
 3 #include <string>
 4 class usertype
 5 {
 6 public:
 7     int position;
 8     char usertype;
 9     int num;
10 };
 1 //main.cpp
 2 #include"systemm.h"
 3 #include"usertype.h"
 4 int main(void) {
 5     try
 6     {
 7         systemm obj;
 8         obj.run();
 9     }
10     catch (char* error)
11     {
12         cout << error;
13     }
14     system("PAUSE");
15     return 0;
16 }

    充分地分析和理解问题本身,弄清要求做什么。在确定解决方案框架过程中,考虑怎样使程序结构清晰、合理、简单和易于调试,并确定每个函数的简单功能,以及函数之间的调用关系。

综上 新手写的 欢迎指出问题~

posted @ 2019-06-18 17:54  杰尊  阅读(442)  评论(0编辑  收藏  举报