老师布置银行排队系统,队列的元素老不走

daytimeis 2015-08-04 03:22:13


主队里的人越来越多,三个窗口却不动,而且明明主队列只出去一个人(主队列从第二号开始,说明1号客人进去了),却把三个窗口都排了,而且时间到了还不走,不知道是哪里出啦问题,求论坛里大神了
...全文
173 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-08-04
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。 将复杂数据结构的整个内容在处理它的每一步使用一小段代码按自己很容易理解的格式输出,非常有助于调试!或者可以说是“基础设施”: 仅供参考:
//带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
    int          data;
    struct NODE *next;
} H,*head,*p,*q,*s1,*s2,*s3,*s4,*s;
int i,j,k,n,t,m;
int main() {
    srand(time(NULL));

    //填写头节点数据
    H.data=-1;
    H.next=NULL;
    head=&H;

    //创建10个节点的单链表
    p=head;
    for (i=0;i<10;i++) {
        q=(struct NODE *)malloc(sizeof(struct NODE));
        if (NULL==q) return 1;
        q->data=rand()%100;//填写0..99的随机值
        q->next=NULL;
        p->next=q;
        p=q;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将值为5的结点插入到单链表的第k个结点前
    k=3;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=(struct NODE *)malloc(sizeof(struct NODE));
            if (NULL==q) return 1;
            q->data=5;
            q->next=p->next;
            p->next=q;
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //删除第k个节点
    k=5;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=p->next;
            if (q) {
                p->next=q->next;
                free(q);
            }
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //从小到大排序
    for (p=head;p!=NULL && p->next!=NULL;p=p->next) {
        for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {
            if (p->next->data > q->next->data) {

                //交换data
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
//              t=p->next->data;p->next->data=q->next->data;q->next->data=t;

                //或者

                //交换next
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
                s1=p->next;
                s2=p->next->next;
                s3=q->next;
                s4=q->next->next;

                if (s2!=s3) {
                     p->next=s3;
                    s3->next=s2;
                     q->next=s1;
                    s1->next=s4;
                } else {
                     p->next=s3;
                    s3->next=s1;
                           q=s3;
                    s1->next=s4;
                }

                //输出整个单链表
//              s=head->next;
//              while (1) {
//                  if (NULL==s) {
//                      printf("\n");
//                      break;
//                  }
//                  printf("%02d->",s->data);
//                  s=s->next;
//              }
//              getchar();
            }
        }
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10
    m=4;
    n=6;
    k=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        k++;
        if (m+1==k) {
            q=p;
        }
        s=p;
        p=p->next;
    }
    s1=head->next;
    head->next=q->next;
    s->next=s1;
    q->next=NULL;

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //释放所有节点
    p=head->next;
    while (1) {
        if (NULL==p) {
            break;
        }
        q=p->next;
        free(p);
        p=q;
    }

    return 0;
}
//18->94->58->17->27->20->43->57->75->78->
//18->94->05->58->17->27->20->43->57->75->78->
//18->94->05->58->27->20->43->57->75->78->
//05->18->20->27->43->57->58->75->78->94->
//43->57->58->75->78->94->05->18->20->27->
//
daytimeis 2015-08-04
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
float CLOCK;
应改为
int CLOCK;

for (CLOCK = 0.0; CLOCK <= CloseTime; CLOCK += 1) {
应改为
for (CLOCK = 0; CLOCK <= CloseTime; CLOCK += 1) {


我把Windows_Depature();这个从窗口删除客人的函数删了就变这样了,现在是三个窗口貌似只循环了一次,就没人进来了
daytimeis 2015-08-04
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
在0~CloseTime即50之间,怎么才能产生flow即100个互不相同的随机数?
CloseTime在主函数里用scanf_s给输入了新的值
赵4老师 2015-08-04
  • 打赏
  • 举报
回复
float CLOCK; 应改为 int CLOCK; for (CLOCK = 0.0; CLOCK <= CloseTime; CLOCK += 1) { 应改为 for (CLOCK = 0; CLOCK <= CloseTime; CLOCK += 1) {
赵4老师 2015-08-04
  • 打赏
  • 举报
回复
CLOCK += 1 和 CLOCK += 1.0 不是一回事!
赵4老师 2015-08-04
  • 打赏
  • 举报
回复
在0~CloseTime即50之间,怎么才能产生flow即100个互不相同的随机数?
daytimeis 2015-08-04
  • 打赏
  • 举报
回复
窗口那里的三个数分别是客人的ID号、到达时刻、办理业务的时间 主队里的数分别是客人的ID号、到达时刻。 有一个时间轴一直在加1,时刻一到就执行相应的出队进队的操作 改了一个下午都没改出来。
daytimeis 2015-08-04
  • 打赏
  • 举报
回复
#include <stdio.h> #include <time.h> #include <stdlib.h> #include<malloc.h> #include <math.h> #include <graphics.h> #include<conio.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef struct Customer { int ID; int arrivetime; int duringtime; struct Customer *next; }Customer; typedef struct { Customer *front;//头指针 Customer *rear;//尾指针 }LinkQueue; //排队主操作函数 void CustomerArrived();//顾客达到队列 //void CustomerDepature();//顾客离开队列 void Windows_Arrived();//顾客来到窗口 void Windows_Depature();//顾客离开窗口 void Bank_Simulation(); //银行排队模拟 //队列基本操作函数 int InitQueue(LinkQueue *Q); //初始化队列Q int EmptyQueue(LinkQueue *Q); //若队列Q为空,返回TRUE,否则返回FALSE int DelQueue(LinkQueue *Q, Customer *memrroy); //若队列Q不为空,首结点出队,用memrroy返回,并返回OK;否则返回ERROR int EnQueue(LinkQueue *Q, Customer e);//结点e入队Q int QueueLength(LinkQueue *Q);//返回队列Q的长度,即元素个数 //int GetHead(LinkQueue *Q, Customer *memorry);//若队列Q不为空,用memrroy返回其首结点,并返回OK,否则返回ERROR int QueueTraverse(LinkQueue *Q);//遍历队列Q int QueueTraverseMain(LinkQueue *Q);//遍历队列Q void PrintQueue();//显示当前窗口队列 /****************************全局变量*****************************************/ #define M 10000 #define MAXSIZE 20 //宏定义 #define R 5 LinkQueue q[MAXSIZE],MainQ;//队列指针结构体数组 Customer customer;//队列节点 int windows_num;//窗口个数 int CloseTime = 50;//关闭时间,即营业时间长度 int flow;//客流量 int a[M]; int b[20][M]; float CLOCK; /*******************************主函数*******************************************/ int main() { int n, j, i, t; srand((unsigned)time(NULL)); //控制在不同时间内生成不同的随机数 printf("请输入顾客流量:"); scanf_s("%d", &flow); printf("请输入银行关闭时间:"); scanf_s("%d", &CloseTime); printf("请输入窗口个数:"); scanf_s("%d", &windows_num); while (windows_num<1 || windows_num>MAXSIZE) { printf("请输入1到20之间的数:"); scanf_s("%d", &windows_num); } for (i = 0; i < windows_num; i++) InitQueue(&q[i]);//初始化windows_no个窗口队列 InitQueue(&MainQ);//初始化排队队列 //客流量的散布 for (int k = 0; k<flow;) //生成N个随机数 { n = rand(); //用随机函数给赋值 if (n <= CloseTime) //筛选在[0,480]之间的 { for (j = 0; j<k;) if (a[j] != n) //判数本次循环中,生成的随机数是否与数组中已有的相等 { j++; } else { break; } if (j >= k)//如果j=k则,本次生成的随机数,不与已存在的相等,放入到数组中 { a[k] = n; k++; } } } for (i = 0; i<flow; i++) { for (j = i + 1; j<flow; j++) { if (a[j]<a[i]) { t = a[j]; a[j] = a[i]; a[i] = t; } } } //srand((unsigned)time(NULL));//设置随机种子 /*for (i = 0; i < windows_num; i++) { for (j = 0; j < M; j++) b[i][j] = 32767; }*/ for (CLOCK = 0.0; CLOCK <= CloseTime; CLOCK += 1) { CustomerArrived(); Windows_Arrived(); Windows_Depature(); PrintQueue(); } return 0; } /*******************银行排队主操作函数****************************************/ void CustomerArrived() { int i; Customer e; for (i = 0; i < flow; i++) { if (a[i] == CLOCK) { e.arrivetime = a[i]; e.ID = i + 1; EnQueue(&MainQ, e); } //break; } } void Windows_Arrived() { Customer e; int i,j; for (i = 0; i < windows_num;i++) if (QueueLength(&q[i])==0) { DelQueue(&MainQ, &e); e.duringtime = rand() % (2+i) + 1; for (j = 0; b[i][j] < e.ID; j++); b[i][j] = e.ID; EnQueue(&q[i], e); } } void Windows_Depature() { Customer e; int i; for (i = 0; i < windows_num; i++) { if ((q[i].front->next->arrivetime)+(q[i].front->duringtime)==CLOCK) DelQueue(&q[i], &e); } } /**********************队列相关函数******************************************/ //初始化队列Q int InitQueue(LinkQueue *Q) { Q->front = Q->rear = (Customer *)malloc(sizeof(Customer)); if (!Q->front) { printf("内存分配失败!\n"); exit(-1); } Q->front->next = NULL; return OK; } //若队列Q为空,返回TRUE,否则返回FALSE int EmptyQueue(LinkQueue *Q) { if (Q->front == Q->rear) return TRUE; else return FALSE; } //若队列Q不为空,首结点出队,用memorry返回,并返回OK;否则返回ERROR int DelQueue(LinkQueue *Q, Customer *memorry) { Customer *p;//节点指针 if (EmptyQueue(Q)) { printf("队列为空,不能再进行出列操作!\n"); return ERROR; } else { p = Q->front->next; *memorry = *p; Q->front->next = p->next; if (Q->rear == p) { Q->rear = Q->front; } free(p); return OK; } } //结点e入队Q int EnQueue(LinkQueue *Q, Customer e) { Customer* p;//节点指针 p = (Customer *)malloc(sizeof(Customer)); if (NULL == p) { printf("内存分配失败!\n"); exit(-1); } else { *p = e; p->next = NULL; Q->rear->next = p; Q->rear = p; return OK; } } //返回队列Q的长度,即元素个数 int QueueLength(LinkQueue *Q) { Customer * p;//队列节点指针 int count; count = 0; p = Q->front->next;//得到队列第一个节点的地址 while (p) { count++; p = p->next; } return count; } //若队列Q不为空,用memorry返回其首结点,并返回OK,否则返回ERROR int GetHead(LinkQueue *Q, Customer *memorry) { if (EmptyQueue(Q)) return ERROR; *memorry = *(Q->front->next); return OK; } //遍历队列Q int QueueTraverse(LinkQueue *Q) { Customer *p = Q->front->next; if (!p) { printf("无人\n"); return ERROR; } while (p) { printf("(%d,%d,%d)",p->ID, p->arrivetime, p->duringtime); p = p->next; } printf("\n"); return OK; } int QueueTraverseMain(LinkQueue *Q) { Customer *p = Q->front->next; printf("主队列:"); if (!p) { printf("无人\n"); return ERROR; } while (p) { printf("(%d,%d)", p->ID, p->arrivetime); p = p->next; } printf("\n"); return OK; } void PrintQueue() { int i; printf("\n队列状态:\n"); for (i = 0; i < windows_num; i++) { printf("窗口 %d :", i + 1); QueueTraverse(&q[i]); } QueueTraverseMain(&MainQ); printf("\n"); } //这是完整程序

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧