把握命运,追逐梦想

对自己所做的事要有兴趣,同时还要能够坚持不懈

统计

留言簿(1)

阅读排行榜

评论排行榜

C++练习5作业

//train.h
#pragma once
#include
"stdafx.h"

class Train
{
public:
    Train(
int seatNum);
    
virtual ~Train(void);

    
bool sellOneTicket(int p[]);
    
bool sellTwoTickets(int p[]);
    
bool sellThreeTickets(int p[]);
    
bool sellFourTickets(int p[]);
private:
    
int *m_pSeat;
    
int m_seatNum;
    
int m_leavingSeat;
}
;
//TicketSell.h
#pragma once
#include
"Train.h"
#include
"func.h"
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include 
"targetver.h"

#include 
<stdio.h>
#include 
<tchar.h>
#include
<memory.h>



// TODO: 在此处引用程序需要的其他头文件
#include <iostream>
#include 
<fstream>
#include 
<vector>
#include 
<string>
#include 
<algorithm>
using namespace std;
//func.h
#pragma once
#include
"stdafx.h"

bool GetRequest(int *p);
void OutputResult(int * SeatNum, int Count);
//func.cpp

#include
"func.h"

bool GetRequest(int *p)
{
    
static bool first = true;
    
static string from = "d:\\request.txt";
    
static ifstream is(from.c_str());
    
static istream_iterator <int> ii(is);
    
static istream_iterator <int> eos;
    
static vector<int> b(ii,eos);
    
static vector< int>::iterator it  = b.begin();
    
if(it == b.end()) return false;
    
*= *(it++);
return true;
}

void OutputResult(int * SeatNum, int Count)
{
    
string to = "d:\\result.txt";
    ofstream os(to.c_str(),ios_base::app);
    ostream_iterator 
<int> oi(os ,",");
    vector
< int> ov(SeatNum,SeatNum + Count);
    os 
<< endl<<"======================="<<endl;
    copy(ov.begin(),ov.end(),oi);
}

// TicketSell.cpp : 定义控制台应用程序的入口点。
//

#include
"TicketSell.h"
#include
"stdafx.h"

void funcmain();

int _tmain(int argc, _TCHAR* argv[])
{
    
    funcmain();
    
return 0;
}


void funcmain()
{
    Train train(
80);
    
int num = 0;

    
if(!GetRequest(&num))
    
{
        cout
<<"wrong"<<endl;
    }

}


//Train.cpp
#include "Train.h"

Train::Train(
int seatNum)
{
    seatNum 
= seatNum - seatNum%8;             //车厢座位自动向下调整为8的倍数
    this->m_seatNum = seatNum;
    
this->m_leavingSeat = seatNum;
    
this->m_pSeat = new int[seatNum];
    memset(
this->m_pSeat,0,this->m_seatNum*sizeof(int));
    
}


Train::
~Train(void)
{
    delete[] 
this->m_pSeat;
}



bool Train::sellOneTicket(int p[])
{
    
if(this->m_leavingSeat == 0)
        
return false;
    
for(int i = 0; i < this->m_seatNum; i++)
    
{
        
if(this->m_pSeat[i] == 0)
        
{
            
this->m_pSeat[i] = 1;
            
this->m_leavingSeat--;
            
*= i+1;
            
return true;
        }

    }

    
return true;
}


bool Train::sellTwoTickets(int p[])
{
    
if(this->m_leavingSeat <2)
        
return false;
    
for(int i = 0; i < this->m_seatNum; i+=2)
    
{
        
if(this->m_pSeat[i] == 0 && this->m_pSeat[i+1]==0)
        
{
            
this->m_pSeat[i] = 1;
            
this->m_pSeat[i+1= 1;
            
this->m_leavingSeat-=2;
            p[
0= i+1;
            p[
1= i+2;
            
return true;
        }

    }

    
for(int i = 0, j = 0; i < this->m_seatNum && j <2; i++)
    
{
        
if(this->m_pSeat[i] == 0 )
        
{
            
this->m_pSeat[i] = 1;
            
this->m_leavingSeat--;
            p[j] 
= i+1;
            j
++;
        }

    }

    
return true;
}


bool Train::sellThreeTickets(int p[])
{
    
if(this->m_leavingSeat < 3)
        
return false;
    
for(int i = 0; i < this->m_seatNum; i+=8)
    
{
        
if(this->m_pSeat[i] + this->m_pSeat[i+1+ this->m_pSeat[i+4+ this->m_pSeat[i+5< 2)
        
{
            
for(int j = 0, k = 0; j < 6 && k<3; )
            
{
                
if(this->m_pSeat[i+j] == 0)
                
{
                    
this->m_pSeat[i+j] = 1;
                    
this->m_leavingSeat--;
                    p[k]
= i+j+1;
                    k
++;
                }

                
if(j%2 == 0)
                
{
                    j
++;
                }

                
else
                
{
                    j
+=3;
                }


            }

            
        }

        
return true;
    }

    
for(int i = 0, j = 0; i < this->m_seatNum && j <3; i++)
    
{
        
if(this->m_pSeat[i] == 0 )
        
{
            
this->m_pSeat[i] = 1;
            
this->m_leavingSeat--;
            p[j] 
= i+1;
            j
++;
        }

    }

    
return true;

}

bool Train::sellFourTickets(int p[])
{
    
if(this->m_leavingSeat < 4)
        
return false;
    
for(int i = 0; i < this->m_seatNum; i+=8)
    
{
        
if(this->m_pSeat[i] == 0 && this->m_pSeat[i+1]==0 && this->m_pSeat[i+4]==0 && this->m_pSeat[i+5]==0)
        
{
            
this->m_pSeat[i] = 1;
            
this->m_pSeat[i+1= 1;
            
this->m_pSeat[i+4]= 1;
            
this->m_pSeat[i+5]= 1;
            
this->m_leavingSeat-=4;
            p[
0= i+1;
            p[
1= i+2;
            p[
2]= i+5;
            p[
3= i+6;
            
return true;
        }

        
if(this->m_pSeat[i+2== 0 && this->m_pSeat[i+3]==0 && this->m_pSeat[i+6]==0 && this->m_pSeat[i+7]==0)
        
{
            
this->m_pSeat[i+2= 1;
            
this->m_pSeat[i+3= 1;
            
this->m_pSeat[i+6]= 1;
            
this->m_pSeat[i+7]= 1;
            
this->m_leavingSeat-=4;
            p[
0= i+3;
            p[
1= i+4;
            p[
2]= i+7;
            p[
3= i+8;
            
return true;
        }

    }

    
for(int i = 0, j = 0; i < this->m_seatNum && j <4; i++)
    
{
        
if(this->m_pSeat[i] == 0 )
        
{
            
this->m_pSeat[i] = 1;
            
this->m_leavingSeat--;
            p[j] 
= i+1;
            j
++;
        }

    }

    
return true;

}

posted on 2009-08-20 12:05 把握命运 阅读(251) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理