关闭

火车站售票

标签: fpfile
629人阅读 评论(0) 收藏 举报

// huochepiao.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
int Ticket[81] = {1};
static int count = 0;

bool GetRequest(int *p) //读取request文件里的内容
{
 static FILE* fp = fopen("d:\\request.txt","r");
 static int nIndex = 0;
 static int inputData[80] = {0};
 static bool isfirst = true;
 int i;
 if(isfirst){
  for(i = 0; i < 80; i++){
   if(fscanf(fp,"%d,",&inputData[i]) == EOF)
    break;
  }
  isfirst = false;
  fclose(fp);
 }
 if(inputData[nIndex] == 0 || nIndex == 80)
  return false;
 *p = inputData[nIndex++];
 return true;
}

void OutputResult(int * SeatNum, int Count)//把获得的票信息写入到result文件中
{
 FILE* fp = fopen("d:\\result.txt","a");
 int i;
 fprintf(fp,"====================================\n");
 for(i = 0; i < Count; i++){
  fprintf(fp,"%d,",SeatNum[i]);
 }
 fprintf(fp,"\n");
 fclose(fp);
}

int SaleOne()
{
 int i = 0;
 for (i = 1; i != 81; ++i)
 {
  if (Ticket[i] == 0)
  {
   Ticket[i] = 1;
   cout << "票号为:" << i << endl;
   break;
  }
  else
  {
  }
 }
 count++;
 OutputResult(&i, 1);
 return 0;
}

bool IsCon()//两张票是否相连
{
 int num = 0;
 int a[2];
 bool flag;
 for (num = 1; num != 81; ++ num)
 {
  if (1 == num % 2 && Ticket[num] == 0 && Ticket[num+1] == 0)
  {
   flag = true;
   Ticket[num] = 1 ;
   a[0] = num;
   Ticket[num+1] = 1;
   a[1] = num + 1;
   cout << "票号为:" << num << "和" << num + 1 << endl;
   count += 2;
   OutputResult(a, 2);
   break;
  }
  else
  {
   flag = false;
  }
 }
 return flag;
}

int SaleTwo()
{
 if (!IsCon())
 {
  cout << "没有相连的票。";
  SaleOne();
  SaleOne();
 }
 return 0;
}
void OutputThree(int numa, int numb, int numc) //输出三张票的方法
{
 int a[3];
 Ticket[numa] = 1 ;
 Ticket[numb] = 1;
 Ticket[numc]= 1;
 a[0] = numa;
 a[1] = numb;
 a[2] = numc;
 OutputResult(a, 3);
 cout << "票号为:" << numa << "和" << numb << "和" << numc<< endl;
 count += 3;
}
bool IsRoom()//三张票是否在一个间隔内
{
 int num = 0;
 bool flag;
 for (num = 1; num != 81; ++ num)
 {
  if (1 == num % 2 &&Ticket[num] == 0 &&
   Ticket[num+1] == 0 && Ticket[num + 4] ==0)
  {
   flag = true;
   OutputThree(num, num+1, num+4);
   break;
  }
  if (1 == num % 2 &&Ticket[num] == 0 &&
   Ticket[num+4] == 0 && Ticket[num + 5] == 0)
  {
   flag = true;
   OutputThree(num, num+4, num+5);
   break;
  }
  if (0 == num % 2 &&Ticket[num] == 0 &&
   Ticket[num+3] == 0 && Ticket[num + 4] ==0)
  {
   flag = true;
   OutputThree(num, num+3, num+4);
   break;
  }
  if (0 == num % 2 &&Ticket[num-1] == 0 &&
   Ticket[num+3] == 0 && Ticket[num + 4] ==0)
  {
   flag = true;
   OutputThree(num+1, num+3, num+4);
   break;
  }
  else
  {
   flag = false;
  }  
 }
 return flag;
}
int SaleThree()
{
 if (!IsRoom())
 {
  cout << "没有相连的票。";
  SaleOne();
  SaleOne();
  SaleOne();
 }
 return 0;
}
bool IsSpace()//四张票是否在一个间隔内
{
 int num = 0;
 int a[4];
 bool flag;
 for (num = 1; num != 81; ++ num)
 {
  if (1 == num % 2 && Ticket[num] == 0 &&
   Ticket[num+1] == 0 && Ticket[num + 4] ==0 &&
   Ticket[num + 5] ==0)
  {
   flag = true;
   Ticket[num] = 1 ;
   a[0] = num;
   Ticket[num+1] = 1;
   a[1] = num + 1;
   Ticket[num+4]= 1;
   a[2] = num + 4;
   Ticket[num+5]= 1;
   a[3] = num + 5;
   cout << "票号为:" << num << "和" << num + 1 <<
    "和" << num + 4 << "和" << num + 5 << endl;
   count += 4;
   OutputResult(a, 4);
   break;
  }
  else
  {
   flag = false;
  }  
 }
 return flag;
}
int SaleFour()
{
 if (!IsSpace())
 {
  cout << "没有相连的票。";
  SaleOne();
  SaleOne();
  SaleOne();
  SaleOne();
 }
 return 0;
}
int _tmain(int argc, _TCHAR* argv[])

 int *p = Ticket;
 while(GetRequest(p))
 {
  if (*p > 4 || *p < 1)
  {
   cout << "此数字不符合买票标准!" << endl;
  }
  if (*p > 80 - count)
  {
   cout << "票不足或票已售完!" << endl;
   return 0;
  }
  else
  {
   switch (*p)
   {
   case 1:
    SaleOne();
    break;
   case 2:
    SaleTwo();
    break;
   case 3:
    SaleThree();
    break;
   case 4:
    SaleFour();
    break;
   default:
    break;
   }
  }
 }
 return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8064次
    • 积分:119
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:2篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论