码迷,mamicode.com
首页 > 其他好文 > 详细

程序实践:电子备忘录

时间:2015-06-10 06:36:46      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:程序实践   电子备忘录   

电子备忘录

1 系统的基本功能
利用电子备忘录记录未来某时间内的待办事宜。允许查询、添加。
2 要求
(1)备忘信息的保存方式自己设定,尽量保证操作灵活。一条备忘信息最多允许100个汉字。备忘信息的时间设置可以精确到具体的某个日期、或者某个日期中的某个时间段、或者某个日期中的某个具体时间。可以对事件的重复出现进行设置,如每月10号取工资,6月16号到28号每天上午9:10考试等。
(2)如果在添加时,该时段内已有安排则出现提示信息。
(3)查询时,如果当前日期有备忘信息,自动提醒。
(4)根据键盘输入的起始日期和终止日期,统计该时间段内的所有备忘信息并输出结果(包括时间和备忘信息);统计时按照时间的先后顺序进行排序。


好惨啊,写的太丑


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <ctime>
#include <fstream>
using namespace std;

int const MAXLEN = 10000;

struct E_Memorandum
{
    string begin_date;
    string end_date;
    string item;
    bool repeat;
}em[MAXLEN];

int item_num;

string get_to_year(string date)
{
    string ans = "";
    int len = date.length();
    for(int i = 0; i < len; i++)
    {
        if(date[i] != '/')
            ans += date[i];
        else
            break;
    }
    return ans;
}

string get_to_month(string date)
{
    string ans = "";
    int cnt = 0;
    int len = date.length();
    for(int i = 0; i < len; i++)
    {
        if(date[i] == '/')
            cnt ++;
        if(cnt == 2)
            break;
        ans += date[i];
    }
    return ans;
}

string get_to_day(string date)
{
    string ans = "";
    int cnt = 0;
    int len = date.length();
    for(int i = 0; i < len; i++)
    {
        if(date[i] == '/')
            cnt ++;
        if(cnt == 3)
            break;
        ans += date[i];
    }
    return ans;
}

string get_to_hour(string date)
{
    string ans = "";
    int cnt = 0;
    int len = date.length();
    for(int i = 0; i < len; i++)
    {
        if(date[i] == '/')
            cnt ++;
        if(cnt == 4)
            break;
        ans += date[i];
    }
    return ans;
}

string get_to_minute(string date)
{
    string ans = "";
    int cnt = 0;
    int len = date.length();
    for(int i = 0; i < len; i++)
    {
        if(date[i] == '/')
            cnt ++;
        if(cnt == 5)
            break;
        ans += date[i];
    }
    return ans;
}

bool is_leap(string y)
{
    int year = 0;
    int len = y.length();
    for(int i = 0; i < len; i++)
        year = year * 10 + (y[i] - '0');
    if((year % 400 == 0) || (year % 4 == 0 && year % 100))
        return true;
    return false;
}

int Convert(string date)
{
    int num = 0;
    int len = date.length();
    for(int i = 0; i < len; i++)
        num = num * 10 + (date[i] - '0');
    return num;
}

bool judge_input(string in)
{
    if(in == "notsure")
        return true;
    string year = get_to_year(in);
    int y = Convert(year);
    if(y != 0 && (y > 9999 || y < 1000))
    {
        cout << "error year (1000 - 9999)\n";
        return false;
    }
    string tmp = get_to_month(in), month = "", day = "", hour = "", minute = "";
    for(int i = 5; i < (int) tmp.length(); i++)
        month += tmp[i];
    int m = Convert(month);
    if(m != 0 && (m> 12 || m < 1))
    {
        cout << "error month (01 - 12)\n";
        return false;
    }
    tmp = get_to_day(in);
    for(int i = 8; i < (int) tmp.length(); i++)
        day += tmp[i];
    int d = Convert(day);
    if(d != 0)
    {
        if(m == 2)
        {
            if(is_leap(year) && d > 29)
            {
                cout << "error day (Leap yaer Feb only 29 days)\n";
                return false;
            }
            else if(!is_leap(year) && d > 28)
            {
                cout << "error day (no Leap yaer Feb only 28 days)\n";
                return false;
            }
        }
        else if((m == 1 || m == 3 || m == 5 || m == 7
            || m == 8 || m == 10 || m == 12) && d > 31)
        {
            cout << "error day (this month only 31 days)\n";
            return false;
        }
        else if((m == 4 || m == 6 || m == 9 || m == 11) && d > 30)
        {
            cout << "error day (this month only 30 days)\n";
            return false;
        }
        if(d < 1)
        {
            cout << "error day (must more than 01)\n";
            return false;
        }
    }
    tmp = get_to_hour(in);
    for(int i = 11; i < (int) tmp.length(); i++)
        hour += tmp[i];
    int h = Convert(hour);
    if(h != 0 && (h > 24 || h < 0))
    {
        cout << "error hour (00 - 24)\n";
        return false;
    }
    tmp = get_to_minute(in);
    for(int i = 16; i < (int) tmp.length(); i++)
        minute += tmp[i];
    int minu = Convert(minute);
    if(minu != 0 && (minu > 60 || minu < 1))
    {
        cout << "error minute (01 - 60)\n";
        return false;
    }
    return true;
}

void Add()
{
    string b_date, e_date, item;
    cout << "please input your item (formate: begin_date-end_date content)\n";
    cout << "if no end_date (formate: begin_date-notsure content)\n";
    cout << "date formate(Y/M/D/H/MIN)\n";
    bool flag = false;
    while(!flag)
    {
        flag = true;
        cout << "Input start time: ";
        cin >> b_date;
        while(!judge_input(b_date))
        {
            cout << "Input start time: ";
            cin >> b_date;
        }
        cout << "Input end time: ";
        cin >> e_date;
        while(!judge_input(e_date))
        {
            cout << "Input end time: ";
            cin >> e_date;
        }
        if(e_date <= b_date)
        {
            cout << "the end time should not ahead of begin time\n";
            flag = false;
        }
        for(int i = 0; i < item_num; i++)
        {
            if(em[i].begin_date <= b_date && em[i].end_date != "notsure" && em[i].end_date >= b_date)
            {
                cout << "begin time conflict\n";
                flag = false;
                break;
            }
            else if(em[i].begin_date <= e_date && em[i].end_date != "notsure" && em[i].end_date >= e_date)
            {
                cout << "end time conflict\n";
                flag = false;
                break;
            }
        }
    }
    cout << "Input the item: ";
    cin >> item;
    cout << "Whether repeat ?(Y/N) ";
    char tmp2[2] = "";
    while(tmp2[0] != 'Y' && tmp2[0] != 'N')
    {
        cin >> tmp2;
        if(tmp2[0] == 'Y')
        {
            em[item_num].repeat = true;
            break;
        }
        else if(tmp2[0] == 'N')
        {
            em[item_num].repeat = false;
            break;
        }
        else
            cout << "error input" << endl;
    }
    if(em[item_num].repeat)
    {
        if(em[item_num].begin_date.length() == 7)
        {
            string curb, cure;
            for(int i = 5; i < 7; i++)
                curb += em[item_num].begin_date;
            for(int i = 5; i < 7; i++)
                cure += em[item_num].end_date;
            char tmp[100];
            for(int i = 1000; i <= 9999; i++)
            {
                sprintf(tmp, "%d", i);
                em[item_num].begin_date += tmp;
                em[item_num].begin_date += "/";
                em[item_num].begin_date += curb;
                if(em[item_num].end_date != "notsure")
                {
                    em[item_num].begin_date += tmp;
                    em[item_num].begin_date += "/";
                    em[item_num].begin_date += cure;
                }
                else
                    em[item_num].end_date = e_date;
                em[item_num].item = item;
                em[item_num++].repeat = true;
            }
        }
        else if(em[item_num].begin_date.length() == 9)
        {
            string curb, cure;
            for(int i = 8; i < 10; i++)
                curb += em[item_num].begin_date;
            for(int i = 8; i < 10; i++)
                cure += em[item_num].end_date;
            for(int i = 1000; i <= 9999; i++)
            {
                char tmp[100];
                for(int j = 1; j <= 12; j++)
                {
                    char tmpp[100];
                    sprintf(tmp, "%d", i);
                    sprintf(tmpp, "%d", j);
                    em[item_num].begin_date += tmp;
                    if((int)strlen(tmpp) < 2)
                    {
                        tmpp[1] = tmpp[0];
                        tmpp[0] = '0';
                    }
                    em[item_num].begin_date += "/";
                    em[item_num].begin_date += tmpp;
                    em[item_num].begin_date += "/";
                    em[item_num].begin_date += curb;
                    if(em[item_num].end_date != "notsure")
                    {
                        em[item_num].begin_date += tmp;
                        if((int)strlen(tmpp) < 2)
                        {
                            tmpp[1] = tmpp[0];
                            tmpp[0] = '0';
                        }
                        em[item_num].begin_date += "/";
                        em[item_num].begin_date += tmpp;
                        em[item_num].begin_date += "/";
                        em[item_num].begin_date += cure;
                    }
                    else
                        em[item_num].end_date = e_date;
                    em[item_num].item = item;
                    em[item_num++].repeat = true;
                }
            }
        }
    }
    else
    {
        em[item_num].begin_date = b_date;
        em[item_num].end_date = e_date;
        em[item_num].item = item;
        item_num ++;
    }
    ofstream file;
    file.open("file.txt");
    file << item_num << endl;
    for(int i = 0; i < item_num; i++)
        file << em[i].begin_date << " " << em[i].end_date << " " << em[i].item << " " << em[i].repeat << endl;
    cout << "Add sucessfully\n";
    return;
}

bool cmp(E_Memorandum a, E_Memorandum b)
{
    int len1 = a.begin_date.length();
    int len2 = b.begin_date.length();
    if(len1 > len2)
        return a.begin_date < b.begin_date;
    if(len1 < len2)
        return a.begin_date > b.begin_date;
    if(len1 == len2)
    {
        if(a.begin_date != b.begin_date)
            return a.begin_date < b.begin_date;
        else
            return a.end_date < b.end_date;
    }
    return true;
}

void Output(int idx, bool flag)
{
    if(flag)
        cout << "Your have some item at now\n";
    if(em[idx].end_date != "notsure")
        cout << "date: " << em[idx].begin_date << " - " << em[idx].end_date << endl;
    else if(em[idx].end_date == "notsure")
        cout << "date: " << em[idx].begin_date << endl;
    cout << "item: " << em[idx].item << endl;
    return;
}

void Judge_no_end(int idx, string date, string now, bool &flag)
{
    if(date == now)
    {
        Output(idx, flag);
        flag = false;
    }
    return;
}

void Judge_has_end(int idx, string b_date, string e_date, string now, bool &flag)
{
    if(b_date <= now && now <= e_date)
    {
        Output(idx, flag);
        flag = false;
    }
    return;
}

void Query()
{
    bool find = false;
    time_t t = time(0);
    char tmp[100], get[100];
    memset(tmp, 0, sizeof(tmp));
    memset(get, 0, sizeof(get));
    strftime(tmp, sizeof(tmp), "%Y/%m/%d/%X",localtime(&t));
    int cnt = 0;
    for(int i = 0; i < (int) strlen(tmp); i++)
    {
        if(tmp[i] == ':')
            cnt++;
        if(cnt == 2)
            break;
        get[i] = tmp[i];
        if(tmp[i] == ':')
            get[i] = '/';
    }
    string now = get;
    string year = get_to_year(now);
    string month = get_to_month(now);
    string day =  get_to_day(now);
    string hour = get_to_hour(now);
    string minute = get_to_minute(now);
    bool flag = true;
    for(int i = 0; i < item_num; i++)
    {
        if(em[i].end_date == "notsure")
        {
            Judge_no_end(i, em[i].begin_date, minute, flag);
        }
        else
        {
            Judge_has_end(i, em[i].begin_date, em[i].end_date, minute, flag);
        }
    }
    if(!flag)
        find = true;

    sort(em, em + item_num, cmp);
    string b_date, e_date;
    cout << "Input the date that you want to query: (formate: Y/M/D/H/MIN)\n";
    cout << "Input start time: ";
    cin >> b_date;

    while(!judge_input(b_date))
    {
        cout << "Input start time: ";
        cin >> b_date;
    }
    cout << "Input end time: ";
    cin >> e_date;
    while(!judge_input(e_date))
    {
        cout << "Input end time: ";
        cin >> e_date;
    }
    while(e_date <= b_date)
    {
        cout << "the end time should not ahead of begin time\n";
        cout << "Input end time: ";
        cin >> e_date;
    }
    for(int i = 0; i < item_num; i++)
    {
        if(em[i].end_date == "notsure")
        {
            if(b_date <= em[i].begin_date && em[i].begin_date <= e_date)
            {
                Output(i, false);
                find = true;
            }
        }
        else
        {
            if(b_date <= em[i].begin_date && em[i].end_date <= e_date)
            {
                Output(i, false);
                find = true;
            }
        }
    }
    if(!find)
        cout << "There is no item in your query time\n";
}

int main()
{
    cout << "welcome to E_memorandum\n";
    char type[2];
    ifstream file;
    file.open("file.txt");
    file >> item_num;
    for(int i = 0; i < item_num; i++)
        file >> em[i].begin_date >> em[i].end_date >> em[i].item >> em[i].repeat;
    file.close();
    while(true)
    {
        cout << "please input your operation\n";
        cout << "press 1 to add\n";
        cout << "press 2 to query\n";
        cout << "press q to quit\n";
        cin >> type;
        if(type[0] != '1' && type[0] != '2' && type[0] != 'q')
        {
            cout << "Wrong input\n";
        }
        else if(type[0] == '1')
        {
            Add();
        }
        else if(type[0] == '2')
        {
            Query();
        }
        else
        {
            cout << "Thank for using\n";
            return 0;
        }
    }
}


程序实践:电子备忘录

标签:程序实践   电子备忘录   

原文地址:http://blog.csdn.net/tc_to_top/article/details/46434135

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!