A. C#銀行排隊叫號系統
// 寫了一個比較簡陋的排隊系統,可以參考下:
classProgram{
staticvoidMain(string[]args){
ServiceWindow[]serviceWindows=newServiceWindow[Bank.MAX];
for(inti=0;i<Bank.MAX;i++)
serviceWindows[i]=newServiceWindow(){No=(i+1).ToString()};
Bankbank=newBank(serviceWindows);
Randomrand=newRandom();
//多線程模擬顧客取號
newThread(()=>{
intk=0;
while(true){
Bank.Enqueue();
if(++k>=100)break;
Thread.Sleep(1000);
}
}).Start();
//多線程模擬服務窗口叫號
newThread(()=>{
while(true){
bank.ServiceWindows[rand.Next(0,Bank.MAX)].Call();
Thread.Sleep(3000);
}
}).Start();
Console.ReadKey(true);
}
}
internalclassBank{
publicconstintMAX=3;
publicstaticConcurrentQueue<uint>queue{get;set;}
publicstaticvolatileuintcurNo=0;
publicBank(){
Init();
}
publicvoidInit()
{
queue=newConcurrentQueue<uint>();
}
publicBank(ServiceWindow[]serviceWindows)
{
Init();
ServiceWindows=serviceWindows;
}
publicServiceWindow[]ServiceWindows{get;set;}
publicstaticvoidEnqueue()
{
intk=0;
k=queue.Count();
curNo++;
queue.Enqueue(curNo);
Console.WriteLine("您的號碼是:"+(curNo)+",前面還有"+k+"人在等待!");
}
}
internalclassServiceWindow{
publicStringNo{get;set;}
publicboolCall()
{
uintresult=0;
boolsuccess=false;
success=Bank.queue.TryDequeue(outresult);
if(success)Console.WriteLine(result+"號,請到"+No+"號窗口辦理業務!");
returnsuccess;
}
}
B. c語言銀行排隊系統
你所指的隊列是線程嗎?
線程操作,大概思路如下:
1. 創建4個線程,表示四個窗口,用semaphore來等待客戶進入(WaitForSingleObject).
2. 創建n個線程表示客戶,每創建一個客戶增加一個ReleaseSemaphore,以讓窗口處理(記錄相關信息,隨機時間可以用srand獲取),窗口要設定為while循環,可以多次處理,條件可以設置為處理多少個客戶就可以退出.
3. 共用資源記得設置臨界區CriticalSection.
C. 想做一個c語言程序,,類似銀行排隊叫號,關於定時器的問題
你應該把自己的需求分解一下
1.如果你的每個窗口都是一個單獨線程的話;
除非你用了底層中斷,當成窗口從忙碌切到閑置時,發送中斷信號。保存現場和函數切換,一般由系統來處理。
2. 如果你不是多線程並行;
那麼執行本身在任務應用級別是串列的,你需要做的是,把你操作分解到原子操作,每個原子操作後又一定的狀態檢測;但是務必高效,比如64個子窗口進程,用一個longlong 64個bit,用位操作比較。切任務保存現場就需要你自己來做了。
D. 銀行排隊系統叫號,怎麼樣使各個窗口同時工作,c語言
設置一個標志,工作時置為1 然後while循環檢測時間, 到時間了就置為0,然後跳出while循環
調用窗口的時候先檢測該窗口的這個狀態,如果為1就接著檢測下個窗口,直到該窗口狀態為0,就為其服務
E. 模擬實現一個銀行叫號程序 (c語言)
#include<stdio.h>
main()
{
int n,i;
n=0;
while(1)
{
printf("請輸入指令:");
scanf("%d",&i);
if(i==0)
break;
if(i==1)
{
if(n==0)
printf("您前面無人排隊,請直接去櫃台辦理業務\n");
else
printf("您前面還有%d位,請等待\n",n);
n++;
}
if(i==2)
{
if(n>0)
n--;
}
}
}
F. 用C語言編銀行叫號系統
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <conio.h>
#define MAX_SIZE 500
struct tagCustom
{
int m_nID; // 客戶的編號
int m_nProcessTime; // 需要處理業務的時間
int m_nArrivalTime; // 到達時間
int m_nDepartTime; // 離開時間
};
struct tagTeller
{
int m_nCustomNum; // 處理的客戶數
int m_nAllServiceTime; // 所有服務時間
int m_nFinishServiceTime; // 櫃台針對一個客戶服務的結束時間
bool m_bBusy; // 櫃台是否忙
int m_nCustomID; // 正在服務的客戶的ID號
};
tagTeller g_aryTellers[MAX_SIZE];
int g_nTellerNum; // 櫃台數量
// 用戶到達數組(隨機的,無序的)
tagCustom g_aryAllCustoms[MAX_SIZE];
int g_nCustomsNum;
int g_nWorkTime; // 經營時間
int g_nArrivalRange,g_nServiceRange;
// 用戶隊列
tagCustom g_CustomQueue[MAX_SIZE];
int g_nHeader,g_nTail;
// 隊列的操作
void InitQueue()
{
g_nHeader = g_nTail = -1;
}
void AddToTail(tagCustom aCustom)
{
g_nTail++;
g_CustomQueue[g_nTail] = aCustom;
}
bool RemoveFromHeader(tagCustom* pCustom)
{
if(g_nHeader < g_nTail)
{
g_nHeader++;
(*pCustom) = g_CustomQueue[g_nHeader];
return true;
}
return false;
}
// 功能:模擬用戶的隨機到來
void GenerateCustom()
{
tagCustom aCustom;
for(int nCounter = 0; nCounter < g_nCustomsNum; nCounter++)
{
aCustom.m_nID = nCounter;
aCustom.m_nArrivalTime = rand() % g_nWorkTime ;
aCustom.m_nProcessTime = (rand() % g_nServiceRange + 1) * 6;
aCustom.m_nDepartTime = -1;
g_aryAllCustoms[nCounter] = aCustom;
}
}
void Init()
{
g_nTellerNum = 3;
g_nWorkTime = 200;
g_nCustomsNum = 30;
g_nArrivalRange = 18 ;
g_nServiceRange = 5 ;
srand((unsigned)time(NULL));
InitQueue();
GenerateCustom();
for(int nC = 0; nC < g_nTellerNum; nC++)
{
g_aryTellers[nC].m_nAllServiceTime = 0;
g_aryTellers[nC].m_nCustomID = -1;
g_aryTellers[nC].m_bBusy = false;
g_aryTellers[nC].m_nFinishServiceTime = -1;
}
}
G. 用C語言編銀行叫號系統
#include
"stdafx.h"
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
#include
<string.h>
#include
<conio.h>
#define
MAX_SIZE
500
struct
tagCustom
{
int
m_nID;
//
客戶的編號
int
m_nProcessTime;
//
需要處理業務的時間
int
m_nArrivalTime;
//
到達時間
int
m_nDepartTime;
//
離開時間
};
struct
tagTeller
{
int
m_nCustomNum;
//
處理的客戶數
int
m_nAllServiceTime;
//
所有服務時間
int
m_nFinishServiceTime;
//
櫃台針對一個客戶服務的結束時間
bool
m_bBusy;
//
櫃台是否忙
int
m_nCustomID;
//
正在服務的客戶的ID號
};
tagTeller
g_aryTellers[MAX_SIZE];
int
g_nTellerNum;
//
櫃台數量
//
用戶到達數組(隨機的,無序的)
tagCustom
g_aryAllCustoms[MAX_SIZE];
int
g_nCustomsNum;
int
g_nWorkTime;
//
經營時間
int
g_nArrivalRange,g_nServiceRange;
//
用戶隊列
tagCustom
g_CustomQueue[MAX_SIZE];
int
g_nHeader,g_nTail;
//
隊列的操作
void
InitQueue()
{
g_nHeader
=
g_nTail
=
-1;
}
void
AddToTail(tagCustom
aCustom)
{
g_nTail++;
g_CustomQueue[g_nTail]
=
aCustom;
}
bool
RemoveFromHeader(tagCustom*
pCustom)
{
if(g_nHeader
<
g_nTail)
{
g_nHeader++;
(*pCustom)
=
g_CustomQueue[g_nHeader];
return
true;
}
return
false;
}
//
功能:模擬用戶的隨機到來
void
GenerateCustom()
{
tagCustom
aCustom;
for(int
nCounter
=
0;
nCounter
<
g_nCustomsNum;
nCounter++)
{
aCustom.m_nID
=
nCounter;
aCustom.m_nArrivalTime
=
rand()
%
g_nWorkTime
;
aCustom.m_nProcessTime
=
(rand()
%
g_nServiceRange
+
1)
*
6;
aCustom.m_nDepartTime
=
-1;
g_aryAllCustoms[nCounter]
=
aCustom;
}
}
void
Init()
{
g_nTellerNum
=
3;
g_nWorkTime
=
200;
g_nCustomsNum
=
30;
g_nArrivalRange
=
18
;
g_nServiceRange
=
5
;
srand((unsigned)time(NULL));
InitQueue();
GenerateCustom();
for(int
nC
=
0;
nC
<
g_nTellerNum;
nC++)
{
g_aryTellers[nC].m_nAllServiceTime
=
0;
g_aryTellers[nC].m_nCustomID
=
-1;
g_aryTellers[nC].m_bBusy
=
false;
g_aryTellers[nC].m_nFinishServiceTime
=
-1;
}
}
H. 數據結構(C語言版):利用隊列結構實現銀行叫號系統(要求見圖)
這樣的一個數據收集系統很簡單:
銀行的叫號系統可以定義開始時間和結束時間.
開始時間就是你拿號的時間.
結束時間就是你接受到服務的時間.
具體的如何分析和改善控制,需要等你的數據收集完了後來分進行分析,不然白說.
I. C語言問題 急
沒考慮輸入是否合法(例如客戶類型只能為Ordinary和VIP,退出用Quit),
時間關系湊合著用,按正確輸入的話能實現
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#define null 0
struct List
{
char guesttype[10];
struct List *next;
}List;
struct List * InitList()
{
struct List *head,*p,*q;
head=(struct List *)malloc(sizeof(struct List));
head->next=null;
q=head;
int i;
bool star=true;
char c,InQuit[10],guest[10];
for (i=0;i<10&&(c=getchar())!='\n';i++)
{
if (c==' ')
{
star=false;
i=-1;
continue;
}
if (star)InQuit[i]=c;
else guest[i]=c;
}
guest[i]='\0';
while(InQuit[0]=='I'&&InQuit[1]=='N')
{
p=(struct List *)malloc(sizeof(struct List));
p->next=null;
strcpy(p->guesttype,guest);
q->next=p;
q=p;
star=true;
for (i=0;i<10&&(c=getchar())!='\n';i++)
{
if (c==' ')
{
star=false;
i=-1;
continue;
}
if (star)InQuit[i]=c;
else guest[i]=c;
}
}
return head;
}
void print(struct List *head)
{
struct List *p;
int num=0;
if(head)
{
p=head->next;
while(p)
{
printf("IN:\n");
printf("%d %s %d\n",num+1,p->guesttype,num);
p=p->next;
num++;
}
printf("GOOD BYE!\n");
}
}
void main()
{
struct List *head;
head=InitList();
print(head);
}
J. C語言考試中 求求各位幫幫忙 謝謝
#include <stdio.h>
#include <stdlib.h>
#define ASK_OP(response) puts("INPUT O = IN Ordinary V = IN VIP Q = QUIT");response = getch();
typedef struct _cus{
int no;
char type;
int type_num;
struct _cus *next;
}customer;
int main(int argc, char *argv[])
{
customer * phead = NULL, *pcur = NULL, *ptail = NULL;
int i,j;
char ch;
ASK_OP(ch);
while(('Q' != ch) && ('q' != ch))
{
if('o' == ch || 'O' == ch)
{
if(NULL == phead)
{
if(NULL == (phead = (customer *)malloc(sizeof(customer))))return ;
phead->no = 1;
phead->type = 'o';
phead->type_num = 0;
phead->next = NULL;
pcur = phead;
}
else
{
i=0; //記錄類型為 Ordingary 的個數
ptail = phead;
while(!(NULL == ptail->next))
{
if(ptail->type == 'o') i = ptail->type_num;
ptail = ptail->next;
}
if(ptail->type == 'o') i = ptail->type_num;
if(NULL == (pcur = (customer *)malloc(sizeof(customer)))) return ;
ptail->next = pcur;
pcur->no = ptail->no+1;
pcur->type = 'o';
pcur->type_num = i+1;
pcur->next = NULL;
}
printf("\n%d Ordinary %d 前面有%d人等待\n",pcur->no,pcur->type_num,pcur->no-phead->no);
}
if('v' == ch || 'V' == ch)
{
if(NULL == phead)
{
if(NULL == (phead = (customer *)malloc(sizeof(customer)))) return ;
phead->no = 1;
phead->type = 'v';
phead->type_num = 0;
phead->next = NULL;
pcur = phead;
}
else
{
i=0; //記錄類型為 Vip 的個數
ptail = phead;
while(!(NULL == ptail->next))
{
if(ptail->type == 'v') i = ptail->type_num;
ptail = ptail->next;
}
if(ptail->type == 'v') i = ptail->type_num;
if(NULL == (pcur = (customer *)malloc(sizeof(customer)))) return ;
ptail->next = pcur;
pcur->no = ptail->no+1;
pcur->type = 'v';
pcur->type_num = i+1;
pcur->next = NULL;
}
printf("\n%d VIP %d 前面有%d人等待\n",pcur->no,pcur->type_num,pcur->no-phead->no);
}
ASK_OP(ch);
}
printf("\n GOOD bye");
system("PAUSE");
return 0;
}