重新定义,基本类型

发布于 2024-12-08 10:13:27 字数 6731 浏览 1 评论 0原文

我一直在从事模拟杂货店作业,我相信它相对完成,但在编译时出现分段错误。如果您能提供一些方向或解决方案,我将不胜感激。 谢谢, 泰勒

/*********************************************
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;
}

I have been working on a simulation grocery store assignment and i believe its relatively finished but i get a segmentation fault when i compile. If you could provide some direction or a solution I'd be most obliged.
Thanks,
Taylor

/*********************************************
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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-12-15 10:13:27

当指向 null、未分配内存或只读内存的指针、引用或迭代器被取消引用时,通常会发生分段错误。最有可能的是,您的 list::iterator i 在未初始化的情况下被取消引用。

使用 -g 选项进行编译并在调试器中运行程序(例如 gdb progname)应该可以找到发生无效取消引用的代码行。然后,您必须查看较早的执行,以找到应该将迭代器设置为指向有效内存的位置。

Segmentation faults generally occur when a pointer, reference, or iterator which is pointing to null, unallocated memory, or read-only memory is dereferenced. Most likely, your list<cust>::iterator i is being dereferenced without being initialized.

Compiling with the -g option and running the program in the debugger (e.g. gdb progname) should get you the line of your code in which the invalid dereference occurs. You must then look to earlier execution to find where the iterator should be set to point at valid memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文