| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 576 人关注过本帖
标题:这是一个预约实验室机位的系统,目前我知道在打开文件的地方有问题,还有一 ...
只看楼主 加入收藏
圈圈ing
Rank: 2
等 级:论坛游民
帖 子:12
专家分:45
注 册:2015-1-4
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
这是一个预约实验室机位的系统,目前我知道在打开文件的地方有问题,还有一个编译问题,在一个if的地方说有“)”错误。我不知道怎么改。希望大家帮忙看看,谢谢了!
应该还有其他错误,希望大家帮忙看看。多多指教。谢谢了!
程序代码:
/*3.14 试验机位预约管理(难度系数:4级)
(1)任务描述
    编写程序,模拟计算机实验室中试验机位的预约登记信息管理。
(2)功能要求
     >假设实验室有30台机器供学生预约上机使用。假定:可以预约的时间段是早8:00到晚上8点,每个预约时间段为2小时。
    周一到周六开放,每次至少提前1个小时或最多一周预约或取消预约。
     >每次预约前,显示时间
     >根据选择进行预约等级或取消预约登记。
     >如果是预约,则输入预约的日期,列出预约日可用机位信息。等待预约,成功,预约信息保留,给出提示。
    不预约则退出,提醒没有预约。
     >如果是取消预约,则输入预约信息,提示正确取消
     >程序可以查看全部已有的预约信息,或查看指定时间段的预约信息,信息列出时阿按约定日期先后排序,
    或按预约人身份编号排序,
(3)设计提示
预约等级信息管理至少需要的格式:预约人身份编号,预约时间,预约机器位置,用的时间段。
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>

struct app_info//预约信息
{
    int seat;//机位
    char date[11];//预约时间
    char No_[11];//学号
    char time[12];//预约时段
    struct app_info*next;//连接
};

//***************************************判断输入***********************************************

int judge_date(char*p)//判断日期是否正确输入
{
    int i;
    for(i=0;i<10;i++)//共有十位
    {
        if(i==4||i==7)//其中是‘。’的两位
        {
                if(*(p+i)=='.')
                    ;
                else
                     return 0;
        }
        else//其余都是数字
         {
               if(*(p+i)>='0'&&*(p+i)<='9')
                  ;
              else
                return 0;
         }
    }
         return 1;//全都验证正确,返回真
}


 int judge_time(char*p)//判断时间是否输入正确
{
    int i;
    for(i=0;i<11;i++)//一共十一个字符
    {
        if(i==5)//一个‘-’符号
        {
            if(*(p+i)=='-')
                ;
               else
                return 0;
        }
        else if(i==2||i==7)//一个‘:’符号
          {
              if(*(p+i)==':')
                  ;
              else
                return 0;
          }
        else//其余都为数字
         {
              if(*(p+i)>='0'&&*(p+i)<='9')
                  ;
              else
                 return 0;
         }
    }
     return 1;//全都验证正确,返回真
}


 int isInSevenDay(char*p)//是否在一周之内
 {
    int i,j,m,n,a[3];
    long d;
    time_t t;

    i=0;j=0;

    while(*(p+i)!='\0')
    {
        a[j]=atoi(p);//转换字符为整形
            j++;
            n=0;
        while(*(p+i)>='0'&&*(p+i)<='9')
        {
            n++;//行列位数
            i++;
        }
        i=0;
        while(*(p+i)!='\0')//将字符串前移n+1位
        {
            *(p+i)=*(p+i+n+1);
            i++;
        }
        i=0;
    }
    switch(a[1])//判断月份
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            m=31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            m=30;
            break;
        case 2:
            {
                if(a[0]%4==0&&a[0]%100!==0||a[0]%400==0)
                    m=29;
                else
                    m=28;
            }
    }
    t=time(NULL);
    d=((a[0]-1972)/4)*366*24*3600+(a[0]-1970-(a[0]-1972)/4)*365*24*3600+a[1]*m*3600+a[2]*3600;//计算输入的时间到1970的秒数
    if(d-t<7*24*3600)
        return d-t;
    else
        return 0;

 }

int isInOneHour(long c,char*p)//一小时之前
{
    if(c+atoi(p)*3600>3600)
        return 1;
    else
        return 0;
}

//***********************************各种输入、输出、插入*************************************

struct app_info* Input(char *p,long e)//输入结构体内容,初始
{
    int j;
    long dis;
    struct app_info*person=NULL;
    j=0;
    person=(struct app_info*)malloc(sizeof(    struct app_info));
    memset(person,0,sizeof(    struct app_info));
    printf("\n请输入您的学号(如:11403070105):");
        fflush(stdin);
            gets(person->No_);
    printf("\n请输入您要预约(取消)的机位(如:1):");
        scanf("%d",&person->seat);
    while(j==0)
    {
        printf("\n请输入你要预约(取消)的时间段(如:10:00-12:00):");
            gets(person->time);
            judge_time(person->time);
            if(j==0)
                printf("您输入的内容或格式错误!");
            if((dis=isInOneHour(e,person->time))==0)
            {
                printf("您要预约(取消)的不到一个小时,不能预约(取消),请重新输入:");
                    continue;   
            }
    }
        person->next=NULL;
        return person;
}

struct app_info*Insert(struct app_info*head,struct app_info*node)//插入链表
{
    struct app_info*p;
    if(head==NULL)//头为空的时候
        head=node;
    else
    {
        if(strcmp(node->date,head->date)>=0)//和头结点相等的时候
        {
            node->next=head;
                head=node;
        }
        for(p=head;p->next!=NULL;p=p->next)//和头以后的某个相等
            if(strcmp(node->date,p->next->date)>=0)
            {
                node->next=p->next;
                p->next=node;
            }
                if(p->next==NULL)//没有就插到最后
                    p->next=node;
    }
    return head;
}

struct app_info* openTheFile(struct app_info*head)//打开文件
{
    FILE*app;
    struct app_info*node;
        if((app=fopen("appointment.txt","rb"))==NULL)
            printf("目前还没有预约信息!");
        else
        {
            while(!feof(app))
            {
                node=(struct app_info*)malloc(sizeof(struct app_info));//申请空间
                memset(node,0,sizeof(struct app_info));
                fread(node,sizeof(struct app_info),1,app);//读出文件
                head=Insert(head,node);//插入链表
            }
        }
        fclose(app);//关闭文件
        return head;//返回头部
}

void writeTheFile(struct app_info*p)//写入文件
{
    FILE*app;
    struct app_info*p1;

    app=fopen("appointment.txt","wb");//打开文件
        while(p!=NULL)//逐个写入
        {
            fwrite(p,sizeof(struct app_info),1,app);
            p1=p;
            p=p->next;
            free(p1);//逐个清空链表
        }
        fclose(app);//关闭文件
}

//****************************************预约机位************************************************

int isSunday(char*p)//是否为星期天
{
    int i,j,n,a[3],d;

    i=0;j=0;

    while(*(p+i)!='\0')
    {
        a[j]=atoi(p);//转换字符为整形
            j++;
            n=0;
        while(*(p+i)>='0'&&*(p+i)<='9')
        {
            n++;//行列位数
            i++;
        }
        i=0;
        while(*(p+i)!='\0')//将字符串前移n+1位
        {
            *(p+i)=*(p+i+n+1);
            i++;
        }
        i=0;
    }
    if(a[1]==1)//计算需求
        a[1]=13;
    if(a[1]==2)
        a[1]=14;
        return d=(a[2]+1+2*a[1]+3*(a[1]+1)/5+a[0]+a[0]/4-a[0]/100+a[0]/400)%7-1;
}

void app_()//预约机位
{
    char a;
    int n,j;
    long dis;
    time_t t;//取当前时间
    struct app_info*person;
    struct app_info*head=NULL;
    struct app_info*p;
    p=head;
    n=0;j=0;
    person=(struct app_info*)malloc(sizeof(struct app_info));
    memset(person,0,sizeof(struct app_info));

    printf("您真的要预约吗?y/n");//再次确认
    fflush(stdin);
        a=getchar();
        if(a=='y'||a=='Y')
            time(&t);//获得当前时间
        printf("现在的时间为:%s",ctime(&t));
    while(j==0)//判断输入,直到输入正确
    {
        printf("\n请输入您要预约的日期(如2015.01.20):");
            scanf("%s",person->date);
                j=judge_date(person->date);
                if(j==0)
                    printf("您的输入格式或内容有误!");
          if(isSunday(person->date)==0)//判断输入,直到输入正确
          {
              printf("您输入的是星期天!请重新输入。");   
                continue;
          }
          if((dis=isInSevenDay(person->date))==0)
          {
              printf("您输入的时间超过一周,请重新输入!");
                continue;
          }
    }
           head=openTheFile(head);//打开文件
           for(p=head;p!=NULL;p=p->next)//寻找当日已有的预约
            if(person->date==p->date)
                printf("\n当日%d号机,在%s已被预约。",p->seat,p->time);
                printf("\n其余任选。");
                while(n==0)//输入想要预约的信息,并判断
                {
                    person=Input(person->date,dis);
                        for(p=head;p!=NULL;p=p->next)
                        {
                            if(person->date==p->date&&person->seat==p->seat&&person->time==p->time)
                                printf("已经被预约了,请重新预约!");
                            else
                            {
                                n=1;//改变记号,循环结束
                                head=Insert(head,person);//插入链表
                                writeTheFile(head);//写入文件
                                printf("预约成功!请选择其他操作或退出。");
                            }
                        }
                }
}

//****************************************取消预约****************************************************

struct app_info* Delete(struct app_info*head,struct app_info*d)//从链表中删除
{
    struct app_info*p;
    struct app_info*empty;
        if(d->date==head->date&&d->seat==head->seat&&d->time==head->time)//如果等于头结点
        {
            empty=head->next;
            head=head->next;
            free(empty);
        }
        for(p=head;p->next!=NULL;p=p->next)//等于头结点以外的
            if(d->date==p->next->date&&d->seat==p->next->seat&&d->time==p->next->time)
            {
                empty=p->next;
                p->next=p->next->next;
                free(empty);
            }
            return head;//返回头结点,以便写入文件
}

void Quit_app()//取消预约
{
    char ch;
    int flag,j;//记号
    long dis;
    struct app_info*person;
    struct app_info*head=NULL;
    struct app_info*p;
    flag=0;j=0;
    person=(struct app_info*)malloc(sizeof(struct app_info));
    memset(person,0,sizeof(struct app_info));

    head=openTheFile(head);//打开文件
            while(flag==0)
            {
                while(j==0)//判断输入,直到输入正确
                {
                    printf("\n请输入您要取消的日期(如2015.01.20):");
                        scanf("%s",person->date);
                            j=judge_date(person->date);
                            if(j==0)
                                printf("您的输入格式或内容有误!");
                      if(isSunday(person->date)==0)//判断输入,直到输入正确
                      {
                          printf("您输入的是星期天!请重新输入。");   
                            continue;
                      }
                      if((dis=isInSevenDay(person->date))==0)
                      {
                          printf("您输入的时间超过一周,请重新输入!");
                            continue;
                      }
                }
                        person=Input(person->date,dis);
                    for(p=head;p!=NULL;p=p->next)
                        if(person->date==p->date&&person->seat==p->seat&&person->time==p->time)//查询信息
                        {
                            printf("您要取消的预约是:\n");
                            printf("学号:%s\n日期:%s\n机位:%d\n时段:%s\n",p->No_,p->date,p->seat,p->time);
                                flag=1;
                        }
                        else
                            printf("未查询到,请检查您是否输入了错误的信息?请重新输入。");
            }
                        printf("您真的要取消吗?y/n");//确认是否取消
                        fflush(stdin);
                            ch=getchar();
                        if(ch=='y'||ch=='Y')
                        {
                            head=Delete(head,p);
                            writeTheFile(head);//写入文件
                            printf("取消预约成功!请选择其他操作或退出。");
                        }
}

//*************************************查看预约******************************************

void look_allAppoint()//查看所有预约信息
{
    char ch;
    struct app_info*head=NULL;
    struct app_info*p;
    printf("您真的要查看所有预约信息吗y/n");
    fflush(stdin);
    ch=getchar();
    if(ch=='y'||ch=='Y')
    {
        head=openTheFile(head);//打开文件
        if(head!=NULL)
        for(p=head;p!=NULL;p=p->next)//输出所有
            printf("%s%d%s",p->date,p->seat,p->time);
    }
}

void look_partAppint()
{   
    int j;
    char ch[12];
    struct app_info*head=NULL;
    struct app_info*p;
    head=openTheFile(head);
    while(1)//有错误便于再次输入
    {
        printf("请输入你想查看预约信息的日期或时段。");
        scanf("%s",ch);
        if(strlen(ch)==11)//按时间段查询时的判断和输出
        {
            j=judge_time(ch);
            if(j==0)
                printf("请检查您是否输入错误格式,如:08:00-10:00,01.01.并重新输入,如不想继续进行,请输入'O'");
            else
            {
                for(p=head;p!=NULL;p=p->next)
                    if(strcmp(p->time,ch)==0)//匹配
                        printf("日期:%s\n机位:%d\n时段:%s\n",p->date,p->seat,p->time);
                break;
            }
        }
        else if(strlen(ch)==10)//按日期查询时的判断和输出
        {
            j=judge_date(ch);
            if(j==0)
                printf("请检查您是否输入错误格式,如:08:00-10:00,01.01.并重新输入,如不想继续进行,请输入'O'");
            else
            {
                printf("\n已有的预约有:\n");
                for(p=head;p!=NULL;p=p->next)
                    if(strcmp(p->date,ch)==0)//匹配
                        printf("日期:%s\n机位:%d\n时段:%s\n",p->date,p->seat,p->time);
                break;
            }
        }
        else//既不是日期也不是时段,提示错误
        {   
            printf("请检查您是否输入错误格式,如:08:00-10:00,01.01.并重新输入,如不想继续进行,请输入'O'");
            fflush(stdin);
            if(getchar()=='O')
                break;
        }
    }
}


void look_appint()//查看预订信息的方式分配
{
    char c;
    printf("您想要查看全部预约信息还是指定日期或时间的预约信息?A/P");
    fflush(stdin);
    c=getchar();
    if(c=='A'||c=='a')
        look_allAppoint();
    else if(c=='P'||c=='p')
        look_partAppint();
    else
        printf("您输入了错误的字母!");
   
}

//****************************************界面*******************************************

void main()
{
    int chioce;
    while(1)
    {
        printf("        欢迎预约机房位置!");
        printf("\n声明:");
        printf("\n   1.验室有30台机器供学生预约上机使用。");
        printf("\n   2.可以预约的时间段是早8:00到晚上8:00,每个预约时间段为2小时。");
        printf("\n   3.时段分为:08:00-10:00,10:00-12:00,12:00-14:00,14:00-16:00,16:00-18:00,18:00-20:00");
        printf("\n   4.周一到周六开放,每次至少提前1个小时或最多一周预约或取消预约。");   
        printf("\n请选择您想要的功能:");
        printf("\n     ┏━━━━━━━━━━━━━━━━━━━━━┓");
        printf("\n     ┃           1.查看已有预约信息。           ┃ ");
        printf("\n     ┃******************************************┃");
        printf("\n     ┃               2.预约机位。               ┃");
        printf("\n     ┃******************************************┃");
        printf("\n     ┃               3.取消预约。               ┃ ");
        printf("\n     ┗━━━━━━━━━━━━━━━━━━━━━┛");
        printf("\n        您选择的是:");
        scanf("%d",&chioce);
        switch(chioce)
        {
            case 1:
                look_appint();
                break;
            case 2:
                app_();
                break;
            case 3:
                Quit_app();
                break;
        }
    }
}
      
搜索更多相关主题的帖子: 编写程序 实验室 计算机 试验机 时间段 
2015-01-08 16:20
圈圈ing
Rank: 2
等 级:论坛游民
帖 子:12
专家分:45
注 册:2015-1-4
收藏
得分:0 
大家可以复制下来试试……
2015-01-08 23:38
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
a[0]%100!==0
应该是 !=
2015-01-09 08:29
快速回复:这是一个预约实验室机位的系统,目前我知道在打开文件的地方有问题,还 ...
数据加载中...
 
   



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

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