| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 665 人关注过本帖, 1 人收藏
标题:C++日程安排管理,一些步骤的意思不太明白
取消只看楼主 加入收藏
scogt641235
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-7-10
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:1 
C++日程安排管理,一些步骤的意思不太明白
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include   <time.h>
#define LEN sizeof(struct Calender)
   
//定义时间日期结构体
struct Date_time
{
    int year;
    int month;
    int day;
    int hour;
    int min ;   
};
//定义日程结构体
struct Calender
{
    struct Date_time date;
    struct Date_time remind_date;
        
    char name[50] ;
    char detail[100]  ;
   
    struct Calender *next;
   
}*head=NULL,*p0=NULL; //head为头指针,p0总是指向最后一个结点
char set[6];

//显示菜单
char Menu()  
{   
    char choice[6];
    printf("\n");
    printf("\n◇◇◇◇◇◇◇◇◇◇◇欢迎使用日程安排系统◇◇◇◇◇◇◇◇◇◇\n");
    printf("◇==========================================================◇\n");
    printf("◇                  1)添加日程安排                    ◇\n");
    printf("◇              2)删除日程信息                    ◇\n");
    printf("◇                    3)查询日程信息                    ◇\n");
    printf("◇                    4)显示日程安排                    ◇\n");
    printf("◇                5)设置日程信息                    ◇\n");
    printf("◇              6)修改日程安排                    ◇\n");
    printf("◇              0)退出日程管理                    ◇\n");
    printf("◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇\n\n");   
    gets(choice);
    return choice[0];   
}
// 判断日程是否为空函数
int Empty_Calender()
{
    if(head == NULL)
    {
        printf("日程不存在,请返回");
        getchar();
        return 1;
    }
    else
        return 0;
}

//判断是否闰年
int is_leap(int year)        
{
    int leap=0;
   
    if( (year % 4 == 0  &&  year%100 != 0)  ||  ( year % 400 == 0 ) )
        leap=1;
    else
        leap=0;
        
    return leap;
}

//检查输入时间是否正确
int time_exam(struct Date_time time)  
{
    int right  =  1;
    int day_of_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
   
    if( is_leap(time.year) == 1)
    {
        day_of_month[1]=29;
    }
    if(time.year > 9999  ||  time.year <= 0)
        right=0;   
    if(time.month > 12  ||  time.month <= 0)
        right=0;
    if(time.day>day_of_month[time.month-1]  ||  time.day <= 0)
        right=0;
    if(time.hour > 24  ||  time.hour < 0 )
        right=0;
    if(time.min  > 60  ||  time.min < 0 )
        right=0;
        
    if(right == 0)
        printf("输入错误!请重新输入\n");
            
    return right;
}

//检查输入日期是否正确
int date_exam(int year,int month,int day)
{
    int right  =  1;
    int day_of_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
   
    if(is_leap(year)==1)
    {
        day_of_month[1]=29;
    }
    if(year > 9999  ||  year <= 1900)
        right=0;
    if(month > 12  ||  month <= 0)
        right=0;
    if(day>day_of_month[month-1]  || day <= 0)
        right=0;
        
    if(right == 0)
        printf("输入错误!\n");
            
    return right;
}

//显示指针指向的日程事件信息
void print_program(struct Calender *p)        
{
    struct tm *newtime;
    time_t long_time;
      time(&long_time);
    newtime=localtime(&long_time);//查找系统时间
   

    printf("------------------------------\n");
    printf("◇日程名称◇:%s\n",p->name);
    printf("◇日程时间◇:%-4d-%-2d-%-2d %2d:%2d\n",
                p->date.year,
                p->date.month,
                p->date.day,
                p->date.hour,
                p->date.min);
   
    printf("◇日程详情◇:%s\n",p->detail);
    printf("◇日程设置◇:\n");
    printf("◇提醒时间◇:%-4d-%-2d-%-2d %-2d:%-2d\n",
                    p->remind_date.year,
                    p->remind_date.month,
                    p->remind_date.day,
                    p->remind_date.hour,
                    p->remind_date.min    );

   
    if(        newtime->tm_year+1900  >   p->date.year
   
        ||    newtime->tm_year+1900  ==  p->date.year   
         &&    newtime->tm_mon+1      >   p->date.month
         
         ||  newtime->tm_year+1900  ==  p->date.year
        &&    newtime->tm_mon+1      ==  p->date.month
        &&  newtime->tm_mday       >   p->date.day
        
        ||  newtime->tm_year+1900  ==  p->date.year
        &&    newtime->tm_mon+1      ==  p->date.month
        &&  newtime->tm_mday       ==  p->date.day
        &&  newtime->tm_hour       >   p->date.hour
        
        ||  newtime->tm_year+1900  ==  p->date.year
        &&    newtime->tm_mon+1      ==  p->date.month
        &&  newtime->tm_mday       ==  p->date.day
        &&  newtime->tm_hour       ==  p->date.hour
        &&  newtime->tm_min        >=  p->date.min
        )
    {
        printf("◇该日程信息已生效发生◇\n");
    }
    else
    {
        printf("◇该日程安排未生效◇\n");
    }
    printf("\n");
}

//按名称选择日程
struct Calender * select_name_program()
{
    struct Calender *p;
    char select_event[50];
    int i=0;
   
    printf("\n◇请输入日程名称◇:");
    scanf("%s",select_event);
   
    p=head;
    printf("\n◇请确认日程信息◇:\n");
   
    while( p !=NULL  &&  strcmp(p->name,select_event) != 0)
        p=p->next;
   //如果没有找到指定日程
       if(p == NULL)
    {
        printf("\n----------没有找到日程,请重新选择,任意键返回主界面--------\n");
        getchar();
        return NULL;
    }   
    print_program(p);
    return p;
                                          
}

//按时间选择日程
struct Calender * select_date_program()
{
    struct Calender *p,*p_sel;
    struct Date_time time;
    int i=0;
    char select_event[50];
   
    p  = head;
    p_sel = NULL;
   
    do
    {        
        printf("\n◇请输入日程日期(年-月-日)◇:");
        scanf("%d-%d-%d",&time.year,&time.month,&time.day);
    }while(date_exam(time.year,time.month,time.day)==0);
   
    printf("\n◇请确认日程信息◇:\n");
   
    while(p != NULL)
    {
        if(        p != NULL      &&
             (   p->date.year  ==  time.year
             &&    p->date.month ==  time.month
             && p->date.day   ==  time.day))
        {
            p_sel = p;
            print_program(p);
            i++;   
        }
        p = p->next;
    }
    //如果没有找到指定日程
    if(i == 0)
    {
        printf("\n----------没有找到日程,请重新选择,任意键返回主界面--------\n");
        getchar();
    }
    //如果找到一件符合时间条件的日程
    else if(i == 1)
    {
        return p_sel;
    }
    //如果找到几件符合时间条件的日程,再按名称查找
    else
    {
        printf("\n◇请选择日程◇:\n");
        p_sel=select_name_program();     
    }
    return p_sel;
}

//选择日程
struct Calender * select_program()  
{
    struct Date_time time;
    struct Calender *p,*p1;
   
    char select_event[50];
    int  sel  =0;
   
    do
    {
        printf("\n◇通过以下方式选择◇\n");
        printf("1.◇输入日程名称◇\n");
        printf("2.◇输入日程日期◇\n");
        scanf("%d",&sel);
        getchar();
        if(sel == 1)
        {
            p=select_name_program();
        }
   
        else if(sel  == 2)
        {
            p=select_date_program();   
        }
   
        else
        {
            printf("\n选择错误!\n");
        }
    }while( sel !=1 && sel !=2);
   
    return(p);            //返回指向选中的日程的指针
}



//设置日程提醒日期
void set_remind_program(struct Calender *p)            
{
    do
    {        
        printf("请输入日程提醒时间(年-月-日 时:分):\n");
        scanf("%d-%d-%d %d:%d",
                &p->remind_date.year,&p->remind_date.month,&p->remind_date.day,
                &p->remind_date.hour,&p->remind_date.min);
        getchar();
    }while(time_exam(p->remind_date) == 0);
}


//设置日程
void set_program(struct Calender *p)            
{
    int sel  = 0;
    int i=0;
    char filename[100];
   
    do{
        
        
        printf("\n请选择:\n");
        printf("1.设置提醒时间\n");
        printf("0.退出操作\n");
            
        scanf("%d",&sel);
        getchar();
        //第一次设置,把未设置的项目初始化
        if(i == 1  &&  strcmp(set,"new") == 0)        
        {
            if(sel == 0)
            {
                p->remind_date.year  = 0000;
                p->remind_date.month = 00;
                p->remind_date.day   = 00;
                p->remind_date.hour  = 00;
                p->remind_date.min   = 00;
               
            
            }
        
            
            else if(sel == 2)
            {
                p->remind_date.year  = 0000;
                p->remind_date.month = 00;
                p->remind_date.day   = 00;
                p->remind_date.hour  = 00;
                p->remind_date.min   = 00;
            }   
        }
            
        switch(sel)
        {
            case 0:
                break;
               
            case 1:
                set_remind_program(p);
                break;
               
        
               
            default:
                printf("\n选择错误!\n");        
        }
    }while(sel != 0);    //可多次设置直到选择0退出
}

//添加日程
void add_programe ()        
{
    struct Calender *p;
    struct Date_time time;

    char e_name[50];
    char e_detail[100];
        
    int sel = 0;
   
    strcpy(set,"new");

    do
    {        
        printf("请输入日程日期,时间(格式为;年-月-日 时:分):\n");
        scanf("%d-%d-%d %d:%d",
                &time.year,&time.month,&time.day,&time.hour,&time.min);
    }while(time_exam(time)==0);                      //输入日程时间

    printf("请输入日程名称 :\n");
    scanf("%s",e_name);                            //输入日程名称
   
    printf("请输入日程详情说明:\n");
    scanf("%s",e_detail);                       //输入日程详情
   
    p=(struct Calender * )malloc(LEN);
   
    strcpy(p->name, e_name);
    strcpy(p->detail, e_detail);
        
    p->date.year  = time.year;
    p->date.month = time.month;
    p->date.day   = time.day;
    p->date.hour  = time.hour;
    p->date.min   = time.min;
   
    printf("1.进入设置\n");
    printf("2.取消设置\n");
    getchar();
    scanf("%d",&sel);
   
    if(sel == 1)
    {
        set_program(p);
    }
    else
    {
        p->remind_date.year  = 0;
        p->remind_date.month = 0;
        p->remind_date.day   = 0;
        p->remind_date.hour  = 0;
        p->remind_date.min   = 0;
        

        
    }
        
    //寻找插入位置,新添加的日程插到后面
   
    if(head == NULL)
    {
        head = p0 = p;
        p0->next = NULL;
    }
   
    else
    {
        p0->next = p;
        p0 = p;   
        p0->next = NULL;                            //p0始终指向最后一个数据
    }         
}

//删除指定日程
void delet_event(struct Calender *p)
{
    struct Calender *p_sel;
    struct Calender *p_last;
   
    int sel = 0;
    int sure = 0;   

    p_sel = p_last = head;
        
    while(        p_sel != NULL
          &&   (
                  strcmp(p_sel->name,p->name) != 0
          ||    p_sel->date.year  != p->date.year
          ||    p_sel->date.month != p->date.month   
          ||    p_sel->date.day   != p->date.day
          ||    p_sel->date.hour  != p->date.hour
          ||    p_sel->date.min   != p->date.min
                  )
          )
    {
          p_last = p_sel;
        p_sel =p_sel->next;    //p_sel指向指定日程,p_last指向此日程的上一个
    }
   
    do
    {
        printf("1.删除此日程信息\n");
        printf("2.删除此日程设置\n");
        printf("0.退出操作\n");
                    
        scanf("%d",&sel);
        getchar();        
        switch(sel)
        {
            case 0:
                break;
                        
            case 1:                    //删除此日程事件
                printf("确认删除?:\n");
                printf("1.是\n");
                printf("2.否\n");
                scanf("%d",&sure);
                getchar();
                if(sure == 1)
                {
                    if(p_sel == head && p_sel != NULL)
                        head=p_sel->next;
                       else if(p_sel != NULL)
                        p_last->next = p_sel->next;
                }
                break;
                        
            case 2:                    //删除此日程设置
                printf("确认删除?:\n");
                printf("1.是\n2.否\n");
                scanf("%d",&sure);
                getchar();
                if(sure == 1)
                {
                    p_sel->remind_date.year  = 0000;
                    p_sel->remind_date.month = 00;
                    p_sel->remind_date.day   = 00;
                    p_sel->remind_date.hour  = 00;
                    p_sel->remind_date.min   = 00;
                           
        
                }
                break;
                        
            default:
                printf("\n选择错误!请重新选择!\n");                    
        }
    }while(sel != 1  &&  sel != 2  &&  sel != 0);
}

//删除日程
void delet_program ()
{
    struct Calender *p;
   
    p=select_program();
   
    if( p == NULL)
    {
        return;
    }
   
    getchar();
    delet_event(p);
}

//查询提醒事件
void search_remind_program(int year,int month,int day)        
{
    struct Calender *p,*p_event;   
    p=head;
    p_event = NULL;
   
    printf("\n------------显示查询的日程信息----------\n\n");
   
    while( p  !=  NULL)
    {   
        if( p->remind_date.year   == year     &&
            p->remind_date.month  == month    &&
            p->remind_date.day    == day )

            {
                p_event = p;
                print_program(p);
            }
    p = p->next;            
    }   
    //如果一直没有找到指定日程
    if(p_event == NULL)
    {
        printf("\n-------------无查询日程-------------\n");
    }
}


//查询某天事件
void search_day_program(int year,int month,int day)        
{
    struct Calender *p,*p_event;
    p=head;
    p_event = NULL;
   
    printf("\n-----------显示查询日程----------\n");

    while(p  !=  NULL)
    {
        if( p->date.year   == year     &&
            p->date.month  == month    &&
            p->date.day    == day )
        {
            p_event = p;
            print_program(p);
        }
        p  = p->next;
    }
    //如果一直没有找到指定日程
    if(p_event == NULL)
    {
        printf("\n*----------无查询日程----------\n");
    }
}  
 
//查询指定日程,按名称查询
void search_name_program(char name[50])   
{
    struct Calender *p,*p_event;
    p=head;
    p_event = NULL;

    printf("\n===========查询的日程信息如下==========\n\n");

    while(p != NULL)
     {
         if(strcmp(p->name, name) == 0)
         {
             p_event = p;
             print_program(p);
         }
         p = p->next;
    }
    //如果一直没有找到指定日程
    if(p_event == NULL)
    {
        printf("\n----------无查询日程----------\n");
    }
   
}

//查询日程
void search_program()
{
    struct Date_time time;
    char e_name[50];
    int sel =0;
        
    do
    {
        printf("\n请选择:\n");
        printf("1.查询提醒日程\n");   
        printf("2.查询当日日程\n");
        printf("3.查询指定日程\n");
        printf("0.退出操作:\n");
                    
        scanf("%d",&sel);
        getchar();
        switch(sel)
        {
            case 0:
                break;
            case 1:
                do
                {        
                    printf("请输入日程提醒日期(年-月-日):\n");
                    scanf("%d-%d-%d",
                            &time.year,&time.month,&time.day);
                    getchar();
                }while(date_exam(time.year,time.month,time.day)==0);   
                search_remind_program(time.year,time.month,time.day);
                break;
                    
            case 2:
                do
                {        
                    printf("请输入日程日期(年-月-日):\n");
                    scanf("%d-%d-%d",
                            &time.year,&time.month,&time.day);
                    getchar();
                }while(date_exam(time.year,time.month,time.day)==0);   
                search_day_program(time.year,time.month,time.day);
                break;
               
            case 3:
                printf("请输入事件名称:");            
                scanf("%s",e_name);
                getchar();
                search_name_program(e_name);
                break;
               
            default:
                printf("\n选择错误!\n\n");
        }
    }while(sel != 0);
}

//显示全部日程
void show_program()                     
{
    struct Calender *p;
    p=head;
   
    if( Empty_Calender() == 1 )
        return;        
        
    printf("\n--------------------全部日程信息如下----------------------\n");
   
    while(p  != NULL)
    {
        print_program(p);
        p=p->next;
    }
    printf("\n----------------------------------------------------------\n");
    printf("\n-----------------------按回车键返回-----------------------\n");
   
    getchar();
   
}

//修改日程事件
void alter_program()               
{
    struct Calender *p;
    struct Date_time time;
    struct Date_time remind_time;

    char e_name[50];
    char e_detail[100];
        
    int sel = 0;
   
    p=select_program();                //选中日程事件
    if( p == NULL)
    {
        printf("\n----------无查询信息----------\n");
        getchar();
        return;
    }
   
    do
    {
        printf("\n**请选择要修改的项目**\n");
        printf("1.修改日程安排时间\n");
        printf("2.修改日程安排名称\n");
        printf("3.修改日程详情说明\n");
        printf("4.更改日程提醒时间\n");
        printf("0.退出修改\n");
        
        scanf("%d",&sel);
        getchar();
        switch(sel)
        {
            case 0:
                break;
               
            case 1:                //输入日程时间
                do
                {        
                    printf("请输入日程日期,时间,格式为:(年-月-日 时-分):\n");
                    scanf("%d-%d-%d %d-%d",
                            &time.year,&time.month,&time.day,&time.hour,&time.min);
                }while(time_exam(time)==0);
               
                p->date.year  = time.year;
                p->date.month = time.month;
                p->date.day   = time.day;
                p->date.hour  = time.hour;
                p->date.min   = time.min;   
                break;
               
            case 2:                //输入日程名称     
                printf("请输入日程事件名称:\n");
                scanf("%s",e_name);     
               
                strcpy(p->name, e_name);
                break;
               
            case 3:                //输入日程详情
                printf("请输入日程详情说明:\n");
                scanf("%s",e_detail);   
               
                strcpy(p->detail, e_detail);
                break;
               
            case 4:                //输入提醒时间
                do
                {        
                    printf("请输入提醒日程日期,时间(年-月-日 时:分):\n");
                    scanf("%d-%d-%d %d:%d",
                            &remind_time.year,
                            &remind_time.month,
                            &remind_time.day,
                            &remind_time.hour,
                            &remind_time.min);
                }while(time_exam(remind_time)    ==    0);  
               
                p->remind_date.year  = remind_time.year;
                p->remind_date.month = remind_time.month;
                p->remind_date.day   = remind_time.day;
                p->remind_date.hour  = remind_time.hour;
                p->remind_date.min   = remind_time.min;   
                break;
               
            default:
               
                    printf("\n菜单选择错误!请重新选择!(0~6)\n");
                        
        }
        
    }while( sel != 0);

}


int main()
{
    struct Calender *p;
   
    char choice;
    char filename[100];
         
    struct tm *newtime;
    time_t long_time;
      time(&long_time);
    newtime=localtime(&long_time);
   

     
    printf("现在时间:%d-%d-%d %d:%d\n",    //从系统读取时间
            newtime->tm_year+1900,
            newtime->tm_mon+1,
            newtime->tm_mday,
            newtime->tm_hour,
            newtime->tm_min);   
    do
    {
        choice=Menu();

        switch(choice)
        {
            case'0':
                printf("\n---------谢谢使用---------\n\n");
                break;
            case'1':
                add_programe();
                getchar();
                break;
            case'2':
                delet_program();
                break;
            case'3':
                search_program();
                break;
            case'4':
                show_program();
                break;
            case'5':
                strcpy(set,"reset");
                p=select_program();
               
                if( p == NULL)
                {
                    printf("\n*****无查找信息*****\n");
                    getchar();
                    break;
                }
                set_program(p);   
                break;
            case'6':
                alter_program();
                break;
            default:
                printf("菜单选择错误!请重新选择!");   
        }   
    }
    while(choice != '0');
        
    return 0;
}

程序可运行,从教材辅导书上看到这些结构,但不是很明白 p->xxx 的用法是什么意思。还有为何tm_year后面+1900
红字标注的地方求解释,好让我能明白后面相同结构的作用
搜索更多相关主题的帖子: include detail 结构体 
2013-07-10 10:38
scogt641235
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-7-10
收藏
得分:0 
回复 2楼 love云彩
这程序按照正确的输入可以执行,但是在添加日程第一步我直接不按照格式输入,直接输入几个数字,他就会报错退出。或者按照格式用错误的数字输入,他能够提示输入错误,但是重新输入的时候,输入错误的格式,比如5之类的,它却能进行到下一步。我不知道哪步骤有问题能帮我看下么
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
2013-07-10 12:26
快速回复:C++日程安排管理,一些步骤的意思不太明白
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033858 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved