sprmrkt.c

来自「一本已经绝版的好书」· C语言 代码 · 共 555 行 · 第 1/2 页

C
555
字号
/***********************************************************
Module name: SprMrkt.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
***********************************************************/


#include "..\CmnHdr.H"                  /* See Appendix C. */
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>            // For random number stuff
#include <string.h>
#include <stdarg.h>
#include <process.h>           // For _beginthreadex
#include "Resource.H"


///////////////////////////////////////////////////////////


// This is the correction to a bug in windowsx.h:
#undef FORWARD_WM_HSCROLL
#define FORWARD_WM_HSCROLL(hwnd, hwndCtl, code, pos, fn) \
    (void)(fn)((hwnd), WM_HSCROLL, \
       MAKEWPARAM((UINT)(code),(UINT)(pos)), \
       (LPARAM)(UINT)(hwndCtl))


///////////////////////////////////////////////////////////


// Forward references to the supermarket
// and shopper thread functions.
DWORD WINAPI ThreadSuperMarket (LPVOID lpvParam);
DWORD WINAPI ThreadShopper (LPVOID lpvParam);


//////////////////////////////////////////////////////////////


// Global variables
HWND g_hwndLB = NULL;   // List box for shopper events

// User-settable simulation parameters.
int g_nMaxOccupancy;
int g_nTimeOpen;
int g_nCheckoutCounters;
int g_nMaxDelayBetweenShopperCreation;
int g_nMaxWaitToGetInMarket;
int g_nMaxTimeShopping;
int g_nMaxWaitForDeliCntr;
int g_nMaxTimeSpentAtDeli;
int g_nMaxTimeAtCheckout;

// Synchronization objects used to control the simulation
HANDLE g_hSemEntrance;
HANDLE g_hMtxDeliCntr;
HANDLE g_hSemCheckout;


//////////////////////////////////////////////////////////////


// This function constructs a string using the format string
// passed and the variable number of arguments and adds the
// string to the shopper events list box identified by the
// global g_hwndLB variable.
void AddStr (LPCTSTR szFmt, ...) {
   TCHAR szBuf[150];
   int nIndex;
   va_list va_params;

   // Make va_params point to the first argument after szFmt.
   va_start(va_params, szFmt);

   // Build the string to be displayed.
   _vstprintf(szBuf, szFmt, va_params);
   do {
      // Add the string to the end of the list box.
      nIndex = ListBox_AddString(g_hwndLB, szBuf);

      // If the list box is full, delete the first item in it.
      if (nIndex == LB_ERR)
         ListBox_DeleteString(g_hwndLB, 0);

   } while (nIndex == LB_ERR);

   // Select the newly added item.
   ListBox_SetCurSel(g_hwndLB, nIndex);

   // Indicate that we are done referencing
   // the variable arguments.
   va_end(va_params);
}


//////////////////////////////////////////////////////////////


// This function returns a random number
// between 0 and nMaxValue, inclusive.
int Random (int nMaxValue) {
   return(rand() / (RAND_MAX / (nMaxValue + 1)));
}


//////////////////////////////////////////////////////////////


BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus,
   LPARAM lParam) {

   HWND hwndSB;

   // Associate an icon with the dialog box.
   chSETDLGICONS(hwnd, IDI_SPRMRKT, IDI_SPRMRKT);

   // Save the window handle to the shopper events list box
   // in a global variable so that the AddStr function has
   // access to it.
   g_hwndLB = GetDlgItem(hwnd, IDC_SHOPPEREVENTS);

   // Set the scroll bar range and default positions for all of
   // the simulation parameters.
   hwndSB = GetDlgItem(hwnd, IDC_MAXOCCUPANCY);
   ScrollBar_SetRange(hwndSB, 0, 500, TRUE);

   // Set the initial value of the scroll bar.
   FORWARD_WM_HSCROLL(hwnd, hwndSB, SB_THUMBTRACK,
      30, SendMessage);


   hwndSB = GetDlgItem(hwnd, IDC_TIMEOPEN);
   ScrollBar_SetRange(hwndSB, 0, 5000, TRUE);
   FORWARD_WM_HSCROLL(hwnd, hwndSB, SB_THUMBTRACK,
      5000, SendMessage);

   hwndSB = GetDlgItem(hwnd, IDC_NUMCOUNTERS);
   ScrollBar_SetRange(hwndSB, 0, 30, TRUE);
   FORWARD_WM_HSCROLL(hwnd, hwndSB, SB_THUMBTRACK,
      5, SendMessage);

   hwndSB = GetDlgItem(hwnd, IDC_SHOPPERCREATIONDELAY);
   ScrollBar_SetRange(hwndSB, 0, 1000, TRUE);
   FORWARD_WM_HSCROLL(hwnd, hwndSB, SB_THUMBTRACK,
      300, SendMessage);

   hwndSB = GetDlgItem(hwnd, IDC_DELAYTOGETIN);
   ScrollBar_SetRange(hwndSB, 0, 100, TRUE);
   FORWARD_WM_HSCROLL(hwnd, hwndSB, SB_THUMBTRACK,
      20, SendMessage);

   hwndSB = GetDlgItem(hwnd, IDC_TIMETOSHOP);
   ScrollBar_SetRange(hwndSB, 0, 100, TRUE);
   FORWARD_WM_HSCROLL(hwnd, hwndSB, SB_THUMBTRACK,
      80, SendMessage);

   hwndSB = GetDlgItem(hwnd, IDC_WAITDELICNTR);
   ScrollBar_SetRange(hwndSB, 0, 100, TRUE);
   FORWARD_WM_HSCROLL(hwnd, hwndSB, SB_THUMBTRACK,
      20, SendMessage);

   hwndSB = GetDlgItem(hwnd, IDC_TIMEATDELICNTR);
   ScrollBar_SetRange(hwndSB, 0, 100, TRUE);
   FORWARD_WM_HSCROLL(hwnd, hwndSB, SB_THUMBTRACK,
      70, SendMessage);

   hwndSB = GetDlgItem(hwnd, IDC_TIMEATCHECKOUT);
   ScrollBar_SetRange(hwndSB, 0, 100, TRUE);
   FORWARD_WM_HSCROLL(hwnd, hwndSB, SB_THUMBTRACK,
      60, SendMessage);

   return(TRUE);
}


//////////////////////////////////////////////////////////////


void Dlg_OnHScroll(HWND hwnd, HWND hwndCtl,
   UINT code, int pos) {

   TCHAR szBuf[10];
   int nPosCrnt, nPosMin, nPosMax;

   // Get the current position and the legal range for the
   // scroll bar that the user is changing.
   nPosCrnt = ScrollBar_GetPos(hwndCtl);
   ScrollBar_GetRange(hwndCtl, &nPosMin, &nPosMax);

   switch (code) {
      case SB_LINELEFT:
         nPosCrnt--;
         break;

      case SB_LINERIGHT:
         nPosCrnt++;
         break;

      case SB_PAGELEFT:
         nPosCrnt -= (nPosMax - nPosMin + 1) / 10;
         break;

      case SB_PAGERIGHT:
         nPosCrnt += (nPosMax - nPosMin + 1) / 10;
         break;

      case SB_THUMBTRACK:
         nPosCrnt = pos;
         break;

      case SB_LEFT:
         nPosCrnt = nPosMin;
         break;

      case SB_RIGHT:
         nPosCrnt = nPosMax;
         break;
   }

   // Make sure that the new scroll bar position
   // is within the legal range.
   if (nPosCrnt < nPosMin)
      nPosCrnt = nPosMin;

   if (nPosCrnt > nPosMax)
      nPosCrnt = nPosMax;

   // Set the new scroll bar position.
   ScrollBar_SetPos(hwndCtl, nPosCrnt, TRUE);

   // Change the value displayed in the text box to
   // reflect the value in the scroll bar.
   _stprintf(szBuf, __TEXT("%d"), nPosCrnt);
   SetWindowText(GetPrevSibling(hwndCtl), szBuf);
}


//////////////////////////////////////////////////////////////


void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl,
   UINT codeNotify) {

   DWORD dwThreadId;
   HANDLE hThread;

   switch (id) {
      case IDOK:
         // Load the scroll bar settings into the global
         // variables so that they can be used
         // by the simulation.
         g_nMaxOccupancy = ScrollBar_GetPos(
            GetDlgItem(hwnd, IDC_MAXOCCUPANCY));

         g_nTimeOpen = ScrollBar_GetPos(
            GetDlgItem(hwnd, IDC_TIMEOPEN));

         g_nCheckoutCounters = ScrollBar_GetPos(
            GetDlgItem(hwnd, IDC_NUMCOUNTERS));

         g_nMaxDelayBetweenShopperCreation = ScrollBar_GetPos(
            GetDlgItem(hwnd, IDC_SHOPPERCREATIONDELAY));

         g_nMaxWaitToGetInMarket = ScrollBar_GetPos(
            GetDlgItem(hwnd, IDC_DELAYTOGETIN));

         g_nMaxTimeShopping = ScrollBar_GetPos(
            GetDlgItem(hwnd, IDC_TIMETOSHOP));

         g_nMaxWaitForDeliCntr = ScrollBar_GetPos(
            GetDlgItem(hwnd, IDC_WAITDELICNTR));

         g_nMaxTimeSpentAtDeli = ScrollBar_GetPos(
            GetDlgItem(hwnd, IDC_TIMEATDELICNTR));

         g_nMaxTimeAtCheckout = ScrollBar_GetPos(

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?