重新再学习C++的练习



#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <iterator>
#include <string>
#define DEBUGVAR(x) cout << #x << " = " << x << endl

using namespace std;

struct Record
{
    string name;
    string addr;
    string phone;

    Record(string n, string a, string p) {
        name = n; addr = a; phone = p;
    }
};

class DataSet
{
public:
    DataSet();
    ~DataSet();

    void Insert(Record r);
    void Delete(string name);
    void Show(string filter);
    friend ostream & operator <<(ostream &os, const DataSet &ds);
private:
    vector<Record> datas;
    bool match_wildcard(const char *wild, const char *input);
};

DataSet::DataSet()
{
}

DataSet::~DataSet()
{
}

void DataSet::Insert(Record r)
{
    datas.push_back(r);
}

void DataSet::Delete(string name)
{
    for(vector<Record>::iterator pos = datas.begin(); pos < datas.end(); ++pos)
        if (name == (*pos).name)
        {
            cout << "deleting record: " << name << endl;
            datas.erase(pos);
        }
}

void DataSet::Show(string filter)
{
    for(vector<Record>::iterator pos = datas.begin(); pos < datas.end(); ++pos) {
        if (match_wildcard(filter.c_str(), (*pos).name.c_str()))
            copy(pos, pos+1, ostream_iterator<Record>(cout, ""));
    }
}

bool DataSet::match_wildcard(const char *wild, const char* input)
{
    const char *cp = 0L, *mp = 0;

    while ((*input) && (*wild != '*')) {
        if ((*wild != *input) && (*wild != '?')) {
            return 0;
        }
        wild++;
        input++;
    }

    while (*input) {
        if (*wild == '*') {
            if (!*++wild) {
                return 1;
            }
            mp = wild;
            cp = input+1;
        } else if ((*wild == *input) || (*wild == '?')) {
            wild++;
            input++;
        } else {
            wild = mp;
            input = cp++;    
        }  
    }   
    while (*wild == '*') {
            wild++;
    }
    return !*wild;
}
ostream & operator <<(ostream &os, const Record &r)
{
    os << r.name << '/t' << r.addr << '/t' << r.phone << endl;
    return os;
}
ostream & operator <<(ostream &os, const DataSet &ds)
{
    copy(ds.datas.begin(), ds.datas.end(),
        ostream_iterator<Record>(cout, ""));
    return os;
}

//
#include "DataSet.h"

class QuerySystem
{
public:
    QuerySystem();
    int GetUserChoice();
    void DisplayRecords();
    void InsertRecord();
    void DeleteRecord();
    void FindRecords();

    void Execute(int i);
private:
    void (QuerySystem::*fptr[4])();
    static char *menu[6];
    DataSet ds;

    void DisplayMenu();
    string GetInput(string prompt);
};

char * QuerySystem::menu[] = {
    "(1) Display table",
    "(2) Append record",
    "(3) Remove record",
    "(4) Find records",
    "(0) Exit Programme",
    "" }; 
   
QuerySystem::QuerySystem()
{
    fptr[0] = &QuerySystem::DisplayRecords;
    fptr[1] = &QuerySystem::InsertRecord;
    fptr[2] = &QuerySystem::DeleteRecord;
    fptr[3] = &QuerySystem::FindRecords;
}

int QuerySystem::GetUserChoice()
{
    DisplayMenu();
    string s("");
    while (s.size() != 1 || s[0] < '0' || s[0] > '4')
        s = GetInput("Input your choice [0-4]");
    return s[0] - '0';
}

void QuerySystem::DisplayMenu()
{
    cout << endl << "============ Query System ============" << endl;
    for (int i = 0; menu[i] != 0; i++) cout << '/t' << menu[i] << endl;
}

void QuerySystem::DisplayRecords()
{
    cout << ds;
    system("pause");
}

void QuerySystem::InsertRecord()
{
    string name = GetInput("Input Name:");
    string addr = GetInput("Input Address:");
    string phone = GetInput("Input phone:");

    ds.Insert(Record(name, addr, phone));
    system("pause");
}

void QuerySystem::DeleteRecord()
{
    string name = GetInput("Input name:");
    ds.Delete(name);
    system("pause");
}

void QuerySystem::FindRecords()
{
    string name = GetInput("Input name (support ? or * search):");
    ds.Show(name);
    system("pause");
}

string QuerySystem::GetInput(string prompt)
{
    string s;
    cout << prompt << endl;
    cin >> s;
    return s;
}

void QuerySystem::Execute(int i)
{
    if (i < 0 || i > 3) {
        cerr << "Error Operation!" << endl;
        exit(1);
    }
    (this->*fptr[i])();
}

//
#include "stdafx.h"
#include "QuerySystem.h"

int _tmain(int argc, _TCHAR* argv[])
{
    QuerySystem qs;
    int i;
    while((i = qs.GetUserChoice()) != 0)
        qs.Execute(i-1);
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值