排队等待问题(队列) 蹦了直接~求搭救 谢谢

CS番茄 2013-04-10 11:22:53
银行有一个客户办理业务站,在一天内随机地有客户到达,设每位客户的业务办理时间是某个范围内的值。设只有一个窗口,一位业务人员,要求程序模拟统计在一天时间内,所有客户的平均等待时间。模拟数据按客户到达的先后顺序依次由键盘输入,对应每位客户有两个数据,到达时刻和需要办理业务的时间。


输入格式

第一行:一天内的客户总人数n
第二行:第一个客户的到达时刻和需要办理业务的时间
第三行:第二个客户的到达时刻和需要办理业务的时间
……
第n行:第n - 1个客户的到达时刻和需要办理业务的时间
第n + 1行:第n 个客户的到达时刻和需要办理业务的时间


输出格式

第一行:所有客户的平均等待时间(精确到小数点后2位)


输入样例

3 1 3 2 1 3 5

输出样例

1.33




源代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef struct QNode{ //修改结构体的定义,结构体含有两个数据 intertime和occurtime
double intertime;
double occurtime;
struct QNode *next;
}QNode,*Queueptr;

typedef struct{ //单向链式队列

Queueptr front; //队头指针 队头指针,没有数据域
Queueptr rear; //队尾指针
}LinkQueue;

int InitQueue(LinkQueue &Q)//队列初始化
{
Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
int EnQueue(LinkQueue &Q,double a,double b)//插入队尾元素a,b
{
Queueptr p;
p=(Queueptr)malloc(sizeof(QNode));
if(!p)
exit(OVERFLOW);
p->intertime=a;
p->occurtime=b;

p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}

int DeQueue(LinkQueue &Q)//删除队头元素
{
Queueptr p;
if(Q.front==Q.rear)
return ERROR;
p=Q.front->next;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return OK;
}
double waittime_Queue(LinkQueue &Q) //客户等待时间计算
{
double waittime;
Queueptr q;
double clock=0,num=0;
while(Q.front!=Q.rear)
{

q=Q.front->next;

if((q->intertime<clock)||(num==0))
{
num++; //num只是为了设置第一个数做特殊处理,因为第一个数据是不存在等待时间的
if(num==1)
{
clock=q->intertime+q->occurtime;
}
else
{
waittime=waittime+clock-(q->intertime);
clock=clock+q->occurtime;
}
}
DeQueue(Q);
}
return waittime;
}
int chudui(LinkQueue &Q)
{

Queueptr q;
printf("The Queue is:");
q=Q.front->next;
while((Q.rear-q)>=0)
{
printf("%lf %lf\n",q->intertime,q->occurtime);
q++;
}
return OK;
}

int main()
{
LinkQueue Q;
InitQueue(Q); //一定要初始化,否则会蹦的
int n,m;
double intertime,occurtime,aver;
scanf("%d",&n);
m=n;
while(n--)
{
scanf("%lf%lf",&intertime,&occurtime);
// printf("%lf %lf\n",intertime,occurtime); //检验输入是否有误
// EnQueue(Q,intertime,occurtime); //检验入队列的正确性
// printf("%lf\n",waittime_Queue(Q));
// chudui(Q); //检验队列的正确性
}
aver=waittime_Queue(Q)/m;
printf("%.2lf\n",aver);
return 0;
}




waittimeQueue那个函数应该有问题,还有chudui函数是用来测试的 好像也挂了,一些测试的语句被我注释掉了
思路:waitetime=(1+3-2)+(1+3+1-3)
aver=4/3
谢谢
...全文
477 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-05-05
  • 打赏
  • 举报
回复
Setting a Breakpoint When a Variable Changes Value To set a breakpoint when a variable changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the name of the variable. 4. Click OK to set the breakpoint. Setting a Breakpoint When an Expression Changes Value To set a breakpoint when an expression changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type an expression such as x+y. 4. Click OK to set the breakpoint. Setting a Breakpoint When an Expression Is True To set a breakpoint when an expression is true 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type an expression, such as x==3, that evaluates to true or false. 4. Click OK to set the breakpoint. Setting a Breakpoint on a Variable Outside the Current Scope To break on a variable outside the current scope 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the variable name. 4. Select the drop-down arrow to the right of the text box. 5. From the menu that appears, click Advanced. The Advanced Breakpoint dialog box appears. 6. In the Expression text box, type the function name and (if necessary) the filename of the variable. 7. Click OK to close the Advanced Breakpoint dialog box. The information that you specified appears in the Expression text box in the Breakpoints dialog box. 8. In the Breakpoints dialog box, click OK to set the breakpoint. Note You can enter context information directly into the Expression field, using the advanced breakpoints syntax. For details, see Using Advanced Breakpoint Syntax. Setting a Breakpoint When the Initial Element of an Array Changes Value To break when the initial element of an array changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the first element of the array (myArray[0], for example). 4. In the Number Of Elements text box on the Data tab, type 1. 5. Click OK to set the breakpoint on myArray [0]. Setting a Breakpoint When the Initial Element of an Array Has a Specific Value To break when the initial element of an array has a specific value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type an expression containing the initial element of the array (myArray[0]==1, for example). 4. In the Number Of Elements text box, type 1. 5. Click OK to set the breakpoint on myArray [0]. Setting a Breakpoint When a Particular Element of an Array Changes Value To break when a particular element of an array changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the element of the array (myArray[12], for example). 4. In the Number Of Elements text box, type 1. 5. Click OK to set the breakpoint on myArray [12]. Setting a Breakpoint When Any Element of an Array Changes Value To break when any element of an array changes value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the first element of the array (myArray[0]). 4. In the Number Of Elements text box, type the number of elements in the array. 5. Click OK to set the breakpoint on myArray. Setting a Breakpoint When Any of the First n Elements of an Array Change Value To break when any of the first n elements of an array change value 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the first element of the array (myArray[0], for example). 4. In the Number Of Elements text box, type n (for example, 10). 5. Click OK to set the breakpoint on myArray[0] through myArray[9]. Setting a Breakpoint When the Location Value of a Pointer Changes To break when the location value of a pointer changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the pointer variable name (p, for example). 4. Click OK to set the breakpoint. Setting a Breakpoint When the Value at a Location Pointed to Changes To break when the value at a location pointed to changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the dereferenced pointer variable name (*p or p->next, for example). 4. Click OK to set the breakpoint. Setting a Breakpoint When an Array Pointed to by a Pointer Changes To break when an array pointed to by a pointer changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the dereferenced pointer variable name (*p, for example). 4. In the Number Of Elements text box, type the length of the array in elements. For example, if the pointer is a pointer to double, and the array pointed to contains 100 values of type double, type 100. 5. Click OK to set the breakpoint. Setting a Breakpoint When the Value at a Specified Memory Address Changes To break when the value at a specified memory address changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the memory address for the byte. For a word or doubleword memory address, enclose the address in parentheses, and precede it with a cast operator. For example, WO(21406036) for the word at memory location 21406036. Use the cast operator BY for a byte (optional), WO for a word, or DW for a doubleword. (The debugger interprets all integer constants as decimal unless they begin with zero (0) for octal or zero and x (0x) for hexadecimal.) 4. In the Number Of Elements text box, type the number of bytes, words, or doublewords to monitor. If you used the BY operator in the Expression field, specify the number of bytes. If you used WO, specify the number of words. If you used DW, specify the number of doublewords. 5. Click OK to set the breakpoint. Setting a Breakpoint When a Register Changes To break when a register changes 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type a register mnemonic, such as CS. 4. In the Number Of Elements text box, type the number of bytes to monitor. 5. Click OK to set the breakpoint. Setting a Breakpoint When a Register Expression Is True To break when a register expression is true 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type an expression that contains a boolean comparison operator, such as CS==0. 4. In the Number Of Elements text box, type the number of bytes to monitor. 5. Click OK to set the breakpoint. Note When you set a data breakpoint, the debugger places the variable or variables used into a special debug register, if possible. The number of debug registers is limited. (Intel 80386 and later CPUs provide four debug registers. Motorola 680X0 and PowerPC chips have no debug registers.) Furthermore, stacked-based variables (parameters) cannot be placed into debug registers. If a breakpoint variable cannot be placed into a debug register, the debugger must examine the variable’s memory location after every instruction to determine whether the contents have changed. These extra memory accesses reduce execution speed of the program with the debugger. In some cases, the program may appear to hang. Performance may be especially slow if you are debugging a remote application.
赵4老师 2013-04-15
  • 打赏
  • 举报
回复
这个世界上最大的差别和最远的距离都存在于“说”和“做”之间。
CS番茄 2013-04-15
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
单步调试和设断点调试是程序员必须掌握的技能之一。 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。 判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一……
数据结构用断点不会比较难操作吗?还有vc6.0 不知怎么用 call stack 能说具体点吗?
pangxin2222 2013-04-15
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
单步调试和设断点调试是程序员必须掌握的技能之一。 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。 判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一……
赵老师 设置数据断点时候,被设置的数据必须是全局变量吗
hugett 2013-04-15
  • 打赏
  • 举报
回复
chudui那函数错了。。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define    OVERFLOW -2
typedef struct QNode{                                    //修改结构体的定义,结构体含有两个数据  intertime和occurtime
    double intertime;
    double occurtime;
    struct QNode *next;
}QNode,*Queueptr;

typedef struct{            //单向链式队列
    Queueptr front;        //队头指针  队头指针,没有数据域
    Queueptr rear;        //队尾指针                    
}LinkQueue;

int InitQueue(LinkQueue &Q)//队列初始化
{
    Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));
    if(!Q.front) exit(OVERFLOW);
    Q.front->next=NULL;
    return OK;
}
int EnQueue(LinkQueue &Q,double a,double b)//插入队尾元素a,b
{
    Queueptr p;
    p=(Queueptr)malloc(sizeof(QNode));
    if(!p)
        exit(OVERFLOW);
    p->intertime=a;
    p->occurtime=b;

    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    return OK;
}

int DeQueue(LinkQueue &Q)//删除队头元素
{
    Queueptr p;
    if(Q.front==Q.rear)
        return ERROR;
    p=Q.front->next;
    Q.front->next=p->next;
    if(Q.rear==p) Q.rear=Q.front;
    free(p);
    return OK;
}
double waittime_Queue(LinkQueue &Q)                //客户等待时间计算
{
    double waittime = 0;//这里要初始化。。
    Queueptr q;
    double clock=0,num=0;
    while(Q.front!=Q.rear)
    {
        q=Q.front->next;
    
        if((q->intertime<clock)||(num==0))
        {
            num++;                                //num只是为了设置第一个数做特殊处理,因为第一个数据是不存在等待时间的
            if(num==1)
            {
				clock=q->intertime+q->occurtime;
            }
            else
            {
				waittime=waittime+clock-(q->intertime);
				clock=clock+q->occurtime;
            }
        }
        DeQueue(Q);
    }
    return waittime;
}
int chudui(LinkQueue &Q)
{
    Queueptr q;
    printf("The Queue is:\n");
    q=Q.front->next;
    //while((Q.rear-q)>=0) 遍历链表不能这样的。。这里会挂掉。。
	while(q)
    {
        printf("%lf %lf\n",q->intertime,q->occurtime);
		q = q->next;//
    }
    return OK;
}

int main()
{
    LinkQueue Q;
    InitQueue(Q);
    int n,m;
    double intertime,occurtime,aver;
    scanf("%d",&n);
    m=n;
    while(n--)
    {
        scanf("%lf%lf",&intertime,&occurtime);
        //printf("%lf %lf\n",intertime,occurtime);     //检验输入是否有误
        EnQueue(Q,intertime,occurtime);                //检验入队列的正确性
        //printf("%lf\n",waittime_Queue(Q));
        chudui(Q);                                    //检验队列的正确性
    }
    aver=waittime_Queue(Q)/m;
    printf("%.2lf\n",aver);
    return 0;
}
CS番茄 2013-04-14
  • 打赏
  • 举报
回复
引用 2 楼 adlay 的回复:
waittimeQueue 里的 waittime 没有初始化就在使用. 要改成 double waittime = 0.0; chudui 里加一个指针判断. while(q != NULL && (Q.rear-q)>=0)
其实waitqueue 那里不应该初始化的 你加的判断条件也是不正确的 此处是链式队列 不是线性队列
www_adintr_com 2013-04-11
  • 打赏
  • 举报
回复
waittimeQueue 里的 waittime 没有初始化就在使用. 要改成 double waittime = 0.0; chudui 里加一个指针判断. while(q != NULL && (Q.rear-q)>=0)
赵4老师 2013-04-11
  • 打赏
  • 举报
回复
单步调试和设断点调试是程序员必须掌握的技能之一。 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。 判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。

69,369

社区成员

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

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