重新再学习C++的练习

    技术2022-05-11  133

    #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; }

    最新回复(0)