Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.3 kB
0
Indexable
Never
#include <iostream>
#include <vector>
#include<unordered_map>
   
using namespace std;
   
typedef enum
{
    NAME,
    NUMBER,
    BIRTHDAY,
    EMAIL,
    MEMO
} FIELD;
   
typedef struct
{
    int count;
    char str[20];
} RESULT;
   
#define FIELDS 5
#define LENGTH 20
   
struct {
    string NAME;
    string NUMBER;
    string BIRTHDAY;
    string EMAIL;
    string MEMO;
    int isDeleted;
}contactdb[50000];
   
unordered_map<string, vector<int>> umap[FIELDS];
int record_count;
   
void InitDB()
{
    record_count = 0;
    for (int i = 0; i < 5; i++) {
        umap[i].clear();
    }
       
}
   
void Add(char* name, char* number, char* birthday, char* email, char* memo)
{
    contactdb[record_count].NAME = name;
    contactdb[record_count].NUMBER = number;
    contactdb[record_count].BIRTHDAY = birthday;
    contactdb[record_count].EMAIL = email;
    contactdb[record_count].MEMO = memo;
    contactdb[record_count].isDeleted = 1;
   
    umap[NAME][name].push_back(record_count);
    umap[NUMBER][number].push_back(record_count);
    umap[BIRTHDAY][birthday].push_back(record_count);
    umap[EMAIL][email].push_back(record_count);
    umap[MEMO][memo].push_back(record_count);
   
    record_count++; 
}
   
int Delete(FIELD field, char* str)
{
    vector<int> it = umap[field][str];
    int count = 0;
    string s;
       
    for (int i = 0; i < it.size(); i++) {
   
        switch (field) {
        case NAME:
            s = contactdb[it[i]].NAME;
            break;
        case NUMBER :
            s = contactdb[it[i]].NUMBER;
            break;
        case BIRTHDAY :
            s = contactdb[it[i]].BIRTHDAY;
            break;
        case EMAIL :
            s = contactdb[it[i]].EMAIL;
            break;
        case MEMO :
            s = contactdb[it[i]].MEMO;
            break;
        }
        if (contactdb[it[i]].isDeleted == 1 && s == str) {
            contactdb[it[i]].isDeleted = 0;
            count++;
        }
   
    }
    return count;
}
   
int Change(FIELD field, char* str, FIELD changefield, char* changestr)
{
    vector<int> it = umap[field][str];
    int count = 0;
    string s;
   
    for (int i = 0; i < it.size(); i++) {
   
        switch (field) {
        case NAME:
            s = contactdb[it[i]].NAME;
            break;
        case NUMBER:
            s = contactdb[it[i]].NUMBER;
            break;
        case BIRTHDAY:
            s = contactdb[it[i]].BIRTHDAY;
            break;
        case EMAIL:
            s = contactdb[it[i]].EMAIL;
            break;
        case MEMO:
            s = contactdb[it[i]].MEMO;
            break;
        }
        if (contactdb[it[i]].isDeleted == 1 && s == str) {
               
            umap[changefield][changestr].push_back(it[i]);
               
            switch (changefield)
            {
            case NAME:
                contactdb[it[i]].NAME = changestr;
                break;
            case NUMBER :
                contactdb[it[i]].NUMBER = changestr;
                break;
            case BIRTHDAY :
                contactdb[it[i]].BIRTHDAY = changestr;
                break;
            case EMAIL :
                contactdb[it[i]].EMAIL = changestr;
                break;
            case MEMO :
                contactdb[it[i]].MEMO = changestr;
                break;
            }
   
            count++;
        }
   
    }
    return count;
}
RESULT Search(FIELD field, char* str, FIELD returnfield)
{
   
    RESULT res;
    vector<int> it = umap[field][str];
    int count = 0;
    string s,final_str;
    int validindex =0;
   
    for (int i = 0; i < it.size(); i++) {
   
        switch (field) {
        case NAME:
            s = contactdb[it[i]].NAME;
            break;
        case NUMBER:
            s = contactdb[it[i]].NUMBER;
            break;
        case BIRTHDAY:
            s = contactdb[it[i]].BIRTHDAY;
            break;
        case EMAIL:
            s = contactdb[it[i]].EMAIL;
            break;
        case MEMO:
            s = contactdb[it[i]].MEMO;
            break;
        }
        if (contactdb[it[i]].isDeleted == 1 && s == str) {
            validindex = i;
            count++;
        }
    }
   
        res.count = count;
   
        if (count == 1) {
            switch (returnfield) {
            case NAME:
                final_str = contactdb[it[validindex]].NAME;
                break;
            case NUMBER:
                final_str = contactdb[it[validindex]].NUMBER;
                break;
            case BIRTHDAY:
                final_str = contactdb[it[validindex]].BIRTHDAY;
                break;
            case EMAIL:
                final_str = contactdb[it[validindex]].EMAIL;
                break;
            case MEMO:
                final_str = contactdb[it[validindex]].MEMO;
                break;
            }
   
            for (int i = 0; final_str[i] != '\0'; i++) {
                res.str[i] = final_str[i];
                }
                res.str[final_str.length()] = '\0';
        }
    return res;
}