C++ 重新定义,基本类型
我一直在做一个模拟杂货店的任务,我相信它相对来说已经完成了,但我在编译时遇到了一个分段错误。如果您能提供一些指导或解决方案,我将不胜感激。 谢谢 泰勒C++ 重新定义,基本类型,c++,list,queue,segmentation-fault,C++,List,Queue,Segmentation Fault,我一直在做一个模拟杂货店的任务,我相信它相对来说已经完成了,但我在编译时遇到了一个分段错误。如果您能提供一些指导或解决方案,我将不胜感激。 谢谢 泰勒 /********************************************* 程序员:泰勒·杜西特 作业:5 文件:Prog5.cc 截止日期:2011年5月10日 用法:包含Prog5.h中定义的方法 *********************************************/ //#包括“/home/onyuk
/*********************************************
程序员:泰勒·杜西特
作业:5
文件:Prog5.cc
截止日期:2011年5月10日
用法:包含Prog5.h中定义的方法
*********************************************/
//#包括“/home/onyuksel/courses/340/progs/11f/p5/prog5.h”
#包括“prog5.h”
int main()
{
//初始化到达和离开时间
int custArrival,custLeave=0;
//把时钟调零
int时钟=0;
//将计数设置为第二个客户
整数计数=2;
//用于跟踪最早下一次到达和最早下一次离开的变量
int-earlyAr=0;
int-earlyDept=0;
//跟踪下一个事件的变量
int earlyEvent=earlyAr;
//创建客户列表
清单l;
//为列表创建迭代器
列表::迭代器i;
//为签出行创建队列
队列q;
//创建stat对象以存储最终统计信息
统计数据s;
//创建客户对象
客户c1;
//使用第一个客户数据初始化
c1.atime=0;
c1.wtime=0;
c1.s_checkout=开始_checkout(0);
c1.id=1;
//添加到列表中
l、 推回(c1);
//确定最早的下一个事件
CustArrival=到达时间(时钟);
earlyEvent=CustArrival;
//将时钟更新为下一个最早事件的时间
时钟=到达时间;
//开始模拟*********************
//当模拟还在运行时
同时(时钟<模拟时间)
{
//如果客户到达
如果(时钟==到达)
{
//为客户创建对象
客户c;
//填充数据字段
c、 id=计数;
c、 时间=到达时间;
c、 s_checkout=开始_checkout(时钟);
c、 wtime=0;
l、 推回(c);//放入列表
CustArrival=到达时间(时钟);//计算下次到达时间
i=min_元素(l.begin(),l.end(),cmp);//首先查找以进入签出
count++;//增加客户id号
//设定最早的下次到达时间
earlyAr=到达时间;
}
//如果客户已完成拣选项目
else if(时钟==i->s_签出)
{
//创建对象以将客户从列表移动到队列
客户温度;
//将值从列表项复制到队列项
temp.id=i->id;
temp.atime=i->atime;
temp.wtime=i->wtime;
临时s_checkout=i->s_checkout;
//将元素插入队列
q、 推动(温度);
//如果队列为空(这是队列中唯一的元素)
如果(q.size()==1)
{
//将等待时间设置为零
q、 front().wtime=0;
//使用部门时间获取出发时间
假期=部门时间(时钟);
}
//将出发时间设置为下一个最早出发时间
早退=离开;
}
//如果客户离开
其他的
{
//从队列中删除客户
客户c=q.前端();
q、 pop();
//使用部门时间并更新队列中下一个客户的wtime
q、 front().wtime=时钟-q.front().s_签出;
时钟+=部门时间(时钟);
//更新统计结构
更新财务统计(s、c、时钟);
}
//确定下一个最早的事件
//如果列表不是空的
如果(l.size()>0)
{
i=min_元素(l.begin(),l.end(),cmp);//查找最早签出时间
}
其他的
{
i->s_checkout=(1000000);//将值设置为高于sim时间
}
//如果到达时间少于结帐时间
如果(早期s_签出)
{
earlyEvent=earlyAr;//最早的事件是到达
}
//否则结帐时间比到达时间短
其他的
{
earlyEvent=i->s_checkout;//最早的事件是checkout
}
//如果列表不为空且出发时间少于最早事件
如果(l.size()>0&&earlyDept cout分段错误通常发生在取消引用指向空、未分配内存或只读内存的指针、引用或迭代器时。最有可能的情况是,您的list::iterator i
未经初始化就被取消引用
编译
/*********************************************
PROGRAMMER: TAYLOR DOUTHITT
ASSIGNMENT: 5
FILE: Prog5.cc
DUE DATE: 10-5-11
USE: Contains methods defined in Prog5.h
*********************************************/
//#include "/home/onyuksel/courses/340/progs/11f/p5/prog5.h"
#include "prog5.h"
int main()
{
//initialize arrival and departure time
int custArrive, custLeave = 0;
//set clock to zero
int clock = 0;
// set count to second customer
int count = 2;
// variables to track earliest next arrival and earlist next departure
int earlyAr = 0;
int earlyDept = 0;
//variable to track next event
int earlyEvent = earlyAr;
//create list of customers
list <cust> l;
//create iterator for list
list<cust>::iterator i;
//create queue for checkout line
queue <cust> q;
//create stat object to store final statistics
stat s;
//create a customer object
cust c1;
//initilize with 1st cust data
c1.atime = 0;
c1.wtime = 0;
c1.s_checkout = start_checkout(0);
c1.id = 1;
//add to list
l.push_back(c1);
//determine earliest next event
custArrive = arr_time(clock);
earlyEvent = custArrive;
//update clock to time of next earliest event
clock = custArrive;
//Start of Simulation*********************
// while simulation is still running
while(clock < SIM_TIME)
{
// if customer is arriving
if(clock == custArrive)
{
// create object for customer
cust c;
//fill data fields
c.id = count;
c.atime = custArrive;
c.s_checkout = start_checkout( clock );
c.wtime = 0;
l.push_back( c ); // put into list
custArrive = arr_time( clock ); // calc next arrival
i = min_element(l.begin(), l.end(), cmp); // find first to enter checkout
count++; //increment customer id number
//set earliest next arrival
earlyAr = custArrive;
}
// if customer has finished picking items
else if(clock == i -> s_checkout)
{
//create object to move cust from list to queue
cust temp;
//copy values from list item to queue item
temp.id = i -> id;
temp.atime = i -> atime;
temp.wtime = i -> wtime;
temp.s_checkout = i -> s_checkout;
//insert element into queue
q.push(temp);
//if queue is empty(this is the only element about to be in queue)
if(q.size() == 1)
{
//set wait time to zero
q.front().wtime = 0;
//use dept_time to obtain departure time
custLeave = dept_time(clock);
}
//set departure as next earliest departure
earlyDept = custLeave;
}
// if customer departed
else
{
//remove customer from queue
cust c = q.front();
q.pop();
//use dept_time and update wtime of next cust in queue
q.front().wtime = clock - q.front().s_checkout;
clock += dept_time(clock);
//update stat structure
update_fin_stat( s, c, clock );
}
//determine next earliest event
// if list isnt empty
if(l.size() > 0)
{
i = min_element(l.begin(),l.end(), cmp); //find earliest checkout time
}
else
{
i -> s_checkout = (1000000); //set value to above sim-time
}
//if arrival is less than checkout time
if(earlyAr < i -> s_checkout)
{
earlyEvent = earlyAr; //earliest event is arrival
}
//else the checkout time is less than arrival
else
{
earlyEvent = i -> s_checkout; //earliest event is checkout
}
//if list not empty and departure less than earliest event
if (l.size() > 0 && earlyDept < earlyEvent)
{
earlyEvent = earlyDept; //earliest event is departure
}
// Set sim clock to next earliest event time
clock = earlyEvent;
}
print_fin_stat(s);
return 0;
}
// returns next arrival time
int arr_time (const int& clock)
{
int temp = ( rand()% ( MAX_INT_ARR - MIN_INT_ARR + 1 ) + MIN_INT_ARR );
temp += clock;
return temp;
}
// returns next departure time
int dept_time (const int& clock)
{
int temp = ( rand()% ( MAX_SERV - MIN_SERV + 1 ) + MIN_SERV );
temp += clock;
return temp;
}
// returns time to enter checkout line
int start_checkout (const int& clock)
{
int temp = ( rand()% ( MAX_PICK - MIN_PICK + 1 ) + MIN_PICK );
temp += clock;
return temp;
}
void update_fin_stat(stat& s, const cust& c, const int& clock)
{
//initialize variables for shop, wait, and service time
int shop = 0;
int wait = 0;
int serv = 0;
//calculate shop time, wait time, and service time
//calculate shop time
shop = c.s_checkout - c.atime; //checkout time - arrival time
//calculate wait time
wait = c.wtime;
//calculate serv time
serv = dept_time(clock) - c.wtime;
//update values in struct s
s.num_dept++;
s.tot_shop += shop;
s.tot_wait += wait;
s.tot_serv += serv;
//if number departed is multiple of sample int
if(s.num_dept % SAMPLE_INT == 0)
{
cout << "num = " << s.num_dept << " ";
cout << "id = " << c.id << " ";
cout << "shop = " << shop << " ";
cout << "wait = " << wait << " ";
cout << "serv = " << serv << " ";
cout << endl;
}
}
bool cmp (const cust& c1, const cust& c2)
{
return c1.s_checkout < c2.s_checkout;
}
void print_fin_stat(stat& s)
{
//print final stats
cout << "Number of Customers" << s.num_dept << endl;
cout << "Total Sim Time " << SIM_TIME << endl;
//compute averages
s.avg_shop = s.tot_shop / s.num_dept;
s.avg_wait = s.tot_wait / s.num_dept;
s.avg_serv = s.tot_serv / s.num_dept;
//print averages
cout << "Average Shop Time" << s.avg_shop << endl;
cout << "Average Wait Time" << s.avg_wait << endl;
cout << "Average Serv Time" << s.avg_serv << endl;
}