CPP改写的联系人

来源:互联网 发布:乐乎记录生活 编辑:程序博客网 时间:2024/05/16 09:59
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>


using namespace std;


class date
{
private:
    int year;
    int month;
    int day;


public:
    void input();
    void output();
#ifdef FAKE
    void fake();
#endif
};


void date::input()
{
    cout << "DATE [YYYY]: ";
    cin >> year;
    cout << "DATE [MM]: ";
    cin >> month;
    cout << "DATE [DD]: ";
    cin >> day;
}


void date::output()
{
    cout << setw(4) << setfill('0') << year << '-';
    cout << setw(2) << setfill('0') << month << '-';
    cout << setw(2) << setfill('0') << day << endl;
}


#ifdef FAKE
void date::fake()
{
    int d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    year = rand() % 111 + 1900;
    month = rand() % 12 + 1;
    d[1] += ((year % 100 != 0) && (year % 4 == 0) || (year % 400 == 0));
    day = rand() % d[month - 1] + 1;
}
#endif 


struct contact
{
private:
    string name;
    string phone;
    string mail;
    date birthday;
#ifdef FAKE
    static void fake_name(string &);
    static void fake_phone(string &);
    static void fake_mail(string &);
#endif


public:
#ifdef FAKE
    void fake();
    void input();
    void output();
#endif
    
};


#ifdef FAKE
void contact::fake_name(string & name)
{
    char buf[0x20];
    int i, r;
    r = rand() % 5 + 8;
    for(i = 0; i < r; i++)
    {
       buf[i] = 'a' + rand() % 26;
    }
    buf[i] = 0;
    name.append(buf);
}


void contact::fake_phone(string & phone)
{
    char buf[0x20];
    int s[30] = {130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189};
    snprintf(buf, 0x20, "%3d%08d", s[rand() % 30], rand() % 100000000);
    phone.append(buf);
}


void contact::fake_mail(string & mail)
{
    int r;
    string s[10] = {"@live.com", "@hotmail.com", "@msn.com", "@163.com", "@126.com", "@sina.com", "@qq.com", "@263.com", "@gmail.com", "@outlook.com"};
    r = rand() % 10;
    fake_name(mail);
    mail.append(s[r]);
}


void contact::fake()
{
    fake_name(name);
    fake_phone(phone);
    fake_mail(mail);
    birthday.fake();
}
#endif


void contact::input()
{
}


void contact::output()
{
    cout << setw(16) << setfill(' ') << left << name << ' ';
    cout << setw(16) << setfill(' ') << left << phone << ' ';
    cout << setw(16) << setfill(' ') << left << mail << ' ';
    birthday.output();
    cout << endl;
}


class array
{
public:
    contact data[0x100];
    int length;
    int selected;


public:
    array();
    void add(const contact &);
    void erase();
    void list();
    void select(int);
};


array::array()
{
    length = 0;
    selected = -1;
}


void array::add(const contact & c)
{
    data[length] = c;
    length++;
}


void array::erase()
{
    for(int i = selected; i < length - 1; i++)
    {
        data[i] = data[i + 1];
    }
    length--;
}


void array::list()
{
    for(int i = 0; i < length; i++)
    {
        cout << "[" << (i == selected ? '*' : ' ') << i << "] ";
        data[i].output();
    }
}


void array::select(int index)
{
    selected = index;
}


int main()
{
#ifdef FAKE
   srand(time(NULL));
#endif
   
   string cmd;
   array a;
   while(true)
   {
       cout << "> ";
       cin >> cmd;
       if(cmd == "exit")
       {
           return 0;
       }
       else if(cmd == "add")
       {
       }
       else if(cmd == "list")
       {
            a.list();
       }
       else if(cmd == "select")
       {
           int index;
           cin >> index;
           a.select(index);
       }
       else if(cmd == "delete")
       {
           a.erase();
       }
#ifdef FAKE
       else if(cmd == "fake")
       {
           int n;
           cin >> n;
           for(int i = a.length; i < a.length + n; i++)
           {
               a.data[i].fake();
           }
           a.length += n;
       }
#endif
   }
   return 0;
}
原创粉丝点击