示例#1
0
文件: proj5.c 项目: Rokuren/OldCode
int main(int argc, char *argv[])
{
   NODEPTR headBone = NULL, tailBone = NULL;
   NODEPTR headUser = NULL, tailUser = NULL;
   NODEPTR headSystem = NULL, tailSystem = NULL;
   NODEPTR headTrain = NULL, tailTrain = NULL; 
   NODEPTR curr = NULL, choice = NULL;
   LIST *boneyard, *user, *system, *train;
   FILE *ifp;
   int numDom, menu, turn = CONTINUE, status;
   int player = CONTINUE;
   
   /* check for correct number of arguments */
   if(argc != 3)
   {
      /* print message to standard error */
      fprintf(stderr, "incorrect number of arguments.\n");
      fprintf(stderr, "enter a.out, name of file, and number of starting\n");
      fprintf(stderr, "dominoes.\n");

      exit(-1);
   }

   int handSize = atoi(argv[2]);
   char *fileName = argv[1];
   
   ifp = fopen(fileName, "r");

   /* check if the file opened correctly */
   if(ifp  == NULL)
   {
      fprintf(stderr, "%s is incorrect\n", fileName);

      exit (-2);
   }

   /* makes sure num dominoes is positive */
   if(atoi(argv[2]) <= 0)
   {
      /* print message to standard error */
      fprintf(stderr, "number of dominoes must be positive\n");

      exit(-3);
   }

   /* gather necessary memory for the LIST structs */
   boneyard = (LIST *) malloc(sizeof(LIST));

   if(boneyard == NULL)
   {
      fprintf(stderr, "Out of Memory - boneyard\n");

      exit(-6);
   }

   user = (LIST *) malloc(sizeof(LIST));

   if(user == NULL)
   {
      fprintf(stderr, "Out of Memory - user\n");

      exit(-7);
   }

   system   = (LIST *) malloc(sizeof(LIST));

   if(system == NULL)
   {
      fprintf(stderr, "Out of Memory - system\n");
   
      exit(-8);
   }

   train    = (LIST *) malloc(sizeof(LIST));

   if(train == NULL)
   {
      fprintf(stderr, "Out of Memory - train\n");
      
      exit(-9);
   }

   /* point the head and tail to the list struct */
   boneyard->head = headBone;
   boneyard->tail = tailBone;
   boneyard->nrNodes = 0; 
   
   user->head = headUser;
   user->tail = tailUser;
   headUser = user->head;
   tailUser = user->head;
   user->nrNodes = 0;

   system->head = headSystem;
   system->tail = tailSystem;
   system->nrNodes = 0;

   train->head = headTrain;
   train->tail = tailTrain;
   train->nrNodes = 0;
      
   /* Creates the boneyard which holds all dominoes in the beginning */
   numDom = CreateBoneyard(boneyard, ifp);
  
   /* calculates max hand size for game of dominoes */
   if(handSize >= (numDom - 1) / 2)
   {
      fprintf(stderr, "%d is too large for number of dominoes\n", handSize);
      fprintf(stderr, "largest number of dominoes is %d\n", (numDom - 1) / 2);

      exit(-4);
   }

   /* prints greeting to the user */
   PrintGreeting();
   
   /* prints program instructions */
   PrintInstructions();
   
   /* gives the user a linked list of dominoes */
   DealUser(boneyard, user, handSize);
   
   /* gives the system a linked list of dominoes */
   DealSystem(boneyard, system, handSize);
   /* takes the first node from the boneyard to be used as the train */
   curr = boneyard->head;
   boneyard->head = boneyard->head->next;
   InsertLeft(train, curr);
  
   /* prints the menu for user interface */
   PrintMenu();

 
   /* Game Loop */  
   while(player == CONTINUE)
   {
      /* player turn loop */
      while(turn != OVER)
      {
	 printf("The train (%d): ", train->nrNodes);
	 PrintList(train->head);
	 printf("\n\n");
	 
	 printf("Your turn...(enter six for menu)\n");	 
	 menu = GetValidInt(); /* int used for menu nav */
	 
	 switch(menu)
	 {
	    case PRINTDOMINO:
	       printf("Your Dominoes: ");
	       PrintList(user->head);
	       printf("\n");
	       turn = CONTINUE; /* print domino doesnt use up your turn */
	       break;
	    case INSERTLEFT:
	       /* selects a domino to play */
	       choice = SelectDominoLeft(train, user);
	       if(choice != NULL)
	       {
		  InsertLeft(train, choice);
		  turn = OVER;
	       } else {
		  turn = CONTINUE; /* if can't play, turn isnt lost */
	       }
	       break;
	    case INSERTRIGHT:
	       /* selects a domino to play */
	       choice = SelectDominoRight(train, user);
	       if(choice != NULL)
	       {
		  choice->next = NULL;
		  InsertRight(train, choice);
		 
		  turn = OVER;
	       } else {
		  turn = CONTINUE;
	       }
	       break;
	    case DRAWDOMINO:
	       /* checks if boneyard is empty */
	       if(IsEmpty(boneyard->head) == FALSE)
	       {
		  status = CheckDomino(train, user, USER);
		  if(status == TRUE)
		  {
		     DrawDomino(user, boneyard);
		     turn = OVER;
		  } else {
		     printf("\nYou had a domino to play\n\n");
		     turn = CONTINUE;
		  }
		  
	       } else {
		  printf("Boneyard is empty\n");
	       }
	       break;
	    case PASSTURN:
	       /* checks if you can play a domino and for empty boneyard */
	       if(CheckDomino(train, user, USER) == TRUE && 
		  IsEmpty(boneyard->head) == TRUE)
	       {
		  printf("\nuser passes his turn\n");
		  turn = OVER;
	       } else 
		  if(IsEmpty(boneyard->head) == FALSE)
		  {
		     printf("\nCant pass, you can draw from the boneyard\n\n");
		     turn = CONTINUE;
		  } else {
		     printf("Can't pass you still have a domino to play");
		  }
	       break;
	    case PRINTMENU:
	       PrintMenu();
	       turn = CONTINUE; 
	       break;
	    case QUIT:
	       turn = OVER;
	       player = QUIT;
	       break;
	    default:
	       printf("Dios Mios!!, esta numero no existan!!!\n");
	       break;
	 }
	 /* checks if player ran out of dominoes */
	 if(IsEmpty(user->head) == TRUE)
	 {
	    player = WIN;
	 } 
      }
      
      turn = CONTINUE;
      
      printf("The Train: ");
      PrintList(train->head);
      printf("\n");

      /* computers turn */
      if(player == CONTINUE)
      {
	 printf("System's Hand: ");
	 PrintList(system->head);
	 printf("\n");
	 printf("\nSystem's turn...");

	 /* tries to play a domino for the system */
	 if(CheckDomino(train, system, SYSTEM) == FALSE)
	 {
	    system->nrNodes--;
	    printf("System has %d dominoes left\n\n", system->nrNodes);
	 } else 
	    if(IsEmpty(boneyard->head) == FALSE) /* tries to draw domino */
	    {
	       DrawDomino(system, boneyard);
	       printf("System drew a domino from the boneyard\n");
	       printf("System has %d dominoes left\n\n", system->nrNodes);
	    } else {
	       /* last passes if can do neither of the above */
	       printf("System has passed this turn\n");
	       printf("System has %d dominoes left\n\n", system->nrNodes);
	    }
	 
	 /* checks for empty hand */
	 if(IsEmpty(system->head) == TRUE)
	 {
	    player = LOSE;
	 }
	 
      }
   }

   if(player == WIN)
   {
      printf("Player Wins!\n");
      /* destroys all linked lists */
      DestroyList(user);
      DestroyList(boneyard);
      DestroyList(system);
      DestroyList(train);
   } else 
      if(player == LOSE)
      {
	 /* destroys all linked lists */
	 printf("System Wins! You Lose!");
	 DestroyList(user);
	 DestroyList(boneyard);
	 DestroyList(system);
	 DestroyList(train);
      } else {
	 /* destroys all linked lists */
	 printf("Hope you had a good game, Play again soon!\n");
	 DestroyList(user);
         DestroyList(boneyard);
         DestroyList(system);
         DestroyList(train);
      }

   return 0;
}
示例#2
0
/**
* @brief pos结算
* @param in cFlag 结算标识0正常结算    非0结算中断后的再次结算
* @return 
* @li APP_SUCC
* @li APP_FAIL
* @li APP_QUIT
*/
int Settle(char cFlag)
{	

	char *pszTitle="结算";
	STSYSTEM stSystem;
	STAMT_NUM_SETTLE stAmtNumSettle;
	STAMT_NUM_INFO_SETTLE stAmtNumInfo;

	int nRet = 0;
	char sPackBuf[MAX_PACK_SIZE]; 
	int nPackLen = 0;
	int nFieldLen = 0;
	char szSettleDateTime[5+1];
	char cProcStep = 0;
	int nTagSum=0;

	memset(&stSystem, 0, sizeof(STSYSTEM));

	/*
	* 检查POS限制,电量是否充足
	*/
	ASSERT_QUIT(DealPrintLimit()); 
	
	if (cFlag)
	{
		/*
		* 取原来结算中断的步骤
		*/
		if (YES == GetVarBatchHaltFlag())
		{
			cProcStep = 2;
		}
		else if (YES == GetVarPrintSettleHalt())
		{
			cProcStep = 3;
		}
		else if (YES == GetVarPrintDetialHalt())
		{
			cProcStep = 4;
		}
		else if (YES == GetVarClrSettleDataFlag())
		{
			cProcStep = 5;
		}
	}
	else
	{
		cProcStep = 0;
		/*
		* 将批上送中断标识的数置为1,从第一笔开始送
		*/
		SetVarBatchMagOfflineHaltFlag(1);
		SetVarFinanceHaltFlag(1);
		SetVarMessageHaltFlag(1);		
		SetVarEmvOfflineUpNum(1);
		SetVarEmvOnlineUpNum(1);
		SetVarEMVOfflineFailHaltFlag(1);
		SetVarEMVOnlineARPCErrHaltFlag(1);
		/*
		* 将批上送的笔数置为0,重新统计
		*/
		SetVarBatchSum(0);
	}	

	/**
	* 联机结算
	*/
	if (cProcStep <= 1)
	{
		/**
		* 检查是否签到
		*/
		ASSERT_QUIT(ChkLoginStatus());

		/**
		* 执行冲正
		*/
		ASSERT_HANGUP_QUIT(DoReversal());
		PubDisplayTitle(pszTitle);		
		/**
		* 再次判断拨号
		*/
		ASSERT_HANGUP_QUIT(CommConnect());
	
		/**
		* 采集数据
		*/
		DealSystem(&stSystem);
		CustodyTlvInit();

		DealSettle(&stAmtNumInfo);
		memset((char *)&stAmtNumSettle, 0, sizeof(stAmtNumSettle));
		PubHexToAsc(stAmtNumInfo.nDebitAmount_N.sSettleAmt, 12, 0,(uchar*)stAmtNumSettle.sDebitAmt_N);
		sprintf(stAmtNumSettle.sDebitNum_N, "%03ld", stAmtNumInfo.nDebitNum_N);		
		PubHexToAsc(stAmtNumInfo.nCreditAmount_N.sSettleAmt, 12, 0, (uchar*)stAmtNumSettle.sCreditAmt_N);
		sprintf(stAmtNumSettle.sCreditNum_N, "%03ld", stAmtNumInfo.nCreditNum_N);

#if 0
		stAmtNumSettle.cSettleCode_N = '0';
		PubHexToAsc(stAmtNumInfo.nDebitAmount_W.sSettleAmt, 12, 0, (uchar*)stAmtNumSettle.sDebitAmt_W);
		sprintf(stAmtNumSettle.sDebitNum_W, "%03ld", stAmtNumInfo.nDebitNum_W);	
		PubHexToAsc(stAmtNumInfo.nCreditAmount_W.sSettleAmt, 12, 0, (uchar*)stAmtNumSettle.sCreditAmt_W);
		sprintf(stAmtNumSettle.sCreditNum_W, "%03ld", stAmtNumInfo.nCreditNum_W);
		stAmtNumSettle.cSettleCode_W= '0';
#endif

		memcpy(stSystem.szTransCode, "200001", 6);
		memset(sPackBuf, 0, sizeof(sPackBuf));
		nPackLen =0;
		ASSERT_FAIL(CustodyTlvAddValue(1, stSystem.szTransCode, sPackBuf, &nPackLen, &nTagSum));
		ASSERT_FAIL(CustodyTlvAddValue(2, stSystem.szDate, sPackBuf, &nPackLen, &nTagSum));
		ASSERT_FAIL(CustodyTlvAddValue(3, stSystem.szTime, sPackBuf, &nPackLen, &nTagSum));
		ASSERT_FAIL(CustodyTlvAddValue(4, stSystem.szTrace, sPackBuf, &nPackLen, &nTagSum));
		ASSERT_FAIL(CustodyTlvAddValue(5, stSystem.szBatchNo, sPackBuf, &nPackLen, &nTagSum));
		ASSERT_FAIL(CustodyTlvAddValue(7,  stSystem.szPosID, sPackBuf, &nPackLen, &nTagSum));
		ASSERT_FAIL(CustodyTlvAddValue(8,  stSystem.szShopID, sPackBuf, &nPackLen, &nTagSum));
		ASSERT_FAIL(CustodyTlvAddValueLen(34, (char *)&stAmtNumSettle,36, sPackBuf, &nPackLen, &nTagSum));
		

		/**
		*拨号
		*/
		ASSERT_HANGUP_QUIT(CommConnect());
		ASSERT_HANGUP_FAIL(CustodyPack(sPackBuf, &nPackLen, nTagSum));

		IncVarTraceNo();
		ASSERT_HANGUP_FAIL(CommSend(sPackBuf, nPackLen));
		ASSERT_HANGUP_FAIL(CommRecv(sPackBuf, &nPackLen));
		
		ASSERT_HANGUP_FAIL(CustodyUpPack(sPackBuf, &nPackLen, &nTagSum));
		ASSERT_HANGUP_FAIL(CustodyChkRespon(&stSystem, sPackBuf, nPackLen));
		if (memcmp(stSystem.szResponse, "00", 2) != 0)
		{
			CommHangUp();
			CustoyDispResp(stSystem.szResponse, stSystem.szRespInfo);
			return APP_QUIT;
		}
	
		ASSERT_FAIL(CustodyGetField(34,  (char *)&stAmtNumSettle.cSettleCode_N, &nFieldLen));
		if(stAmtNumSettle.cSettleCode_N == '1' || stAmtNumSettle.cSettleCode_N == '2' || stAmtNumSettle.cSettleCode_N == '3')
		{
			SetVarCnCardFlag(stAmtNumSettle.cSettleCode_N);/*<内卡应答码*/
		}
		else
		{
			PubMsgDlg("结算", "内卡\n对帐应答代码错误", 3, 10);
			CommHangUp();
			return APP_FAIL;
		}
		/**
		* 保存结算日期时间
		*/
		PubAscToHex((uchar *)stSystem.szDate+4, 4, 0, (uchar *)szSettleDateTime);
		PubAscToHex((uchar *)stSystem.szTime, 6, 0, (uchar *)szSettleDateTime + 2);
		SetVarSettleDateTime(szSettleDateTime);
	}

	/**
	* 批上送
	*/
	if (cProcStep <= 2)
	{
		SetVarBatchHaltFlag(YES);
		/*银商要求除了通讯\打包问题,
		批上送应答只显示但是不失败,
		都强制成功*/
		
		//现在没有需要离线上送的交易
		//ASSERT_HANGUP_FAIL(DoBatchUp());
		SetVarBatchHaltFlag(NO);
	}

	CommHangUp();

	/**
	* 打印结算单
	*/
	if (cProcStep <= 3)
	{
		SetVarPrintSettleHalt(YES);
		PubClearAll();
		PubDisplayTitle("正在打印结算单");
		PubDisplayGen(3, "请稍候");
		PubUpdateWindow();
		nRet = PrintSettle(FIRSTPRINT);
		if (nRet == APP_FAIL)
		{
			return APP_FAIL;
		}
 		SetVarPrintSettleHalt(NO);
 	}
	
	/**
	* 打印明细单
	*/
	if (cProcStep <= 4)
	{
		SetVarPrintDetialHalt(YES);
		if (YES == GetVarIsPrintWaterRec())
		{
			int nSelect = 0;
			
			nRet = ProSelectList("0.否||1.是", "是否打印明细", 0xFF, &nSelect);
			if (nRet == APP_SUCC && nSelect == 1)
			{
				PubClear2To4();
				PubDisplayGen(3, "打印明细单");
				PubUpdateWindow();
				nRet = PrintAllWater();
				if (nRet == APP_FAIL)
				{
					return APP_FAIL;
				}
			}
		}

		SetVarPrintDetialHalt(YES);

		#if 0
		if(GetFailSendNum(0xFF) != 0)
		{
			PubClearAll();
			PubDisplayGen(3, "打印上送不成功的");
			PubDisplayGen(4, "交易明细");
			PubUpdateWindow();
	  		nRet = PrintFailWater(0xFF);
			if (nRet != APP_SUCC)
			{
				return APP_FAIL;
			}
		}
		if(GetFailSendNum(0xFE) != 0)
		{
			PubClearAll();
			PubDisplayGen(3, "打印上送被拒的");
			PubDisplayGen(4, "交易明细");
			PubUpdateWindow();
			nRet = PrintFailWater(0xFE);
			if (nRet != APP_SUCC)
			{
				return APP_FAIL;
			}
		}
		#endif
		SetVarPrintDetialHalt(NO);
	}

	/*
	* 清除结算数据流水等
	*/
	if (cProcStep <= 5)
	{
		SetVarClrSettleDataFlag(YES);
		PubClearAll();
		PubDisplayTitle(pszTitle);
		PubDisplayGen(2, "正在处理结算");
		PubUpdateWindow();
		nRet = InitWaterFile();/**<清除流水*/
		ASSERT(nRet);
		if (nRet != APP_SUCC)
		{
			InitWaterFile();/**<清除流水*/
		}
		ClearSettle();/**<清除结算数据*/
		IncVarBatchNo();/**<批次号加一*/
		SetVarClrSettleDataFlag(NO);
		SetVarOfflineUnSendNum(0);
	}
	PubClearAll();
	PubDisplayTitle(pszTitle);
	PubDisplayStrInline(DISPLAY_MODE_CENTER, 2, "结 算 成 功");
	PubUpdateWindow();
	return APP_SUCC;
}