程式實踐:電子備忘錄,實踐電子備忘錄

來源:互聯網
上載者:User

程式實踐:電子備忘錄,實踐電子備忘錄

電子備忘錄

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.